MySQL----使用Kubernetes集群环境部署MySQL数据库

【原文链接】

1 编写 mysql.yaml文件

编写yaml如下

apiVersion: v1
kind: Namespace
metadata:
  name: devops   # Namespace 的名称

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: devops-mysql   # deployment控制器名称
  namespace: devops
spec:
  replicas: 1
  revisionHistoryLimit: 5
  strategy:
    type: RollingUpdate
  selector:
    matchLabels:
      app: devops-mysql
  template:
    metadata:
      labels:
        app: devops-mysql
    spec:
      volumes:
        - name: devops-mysql
          nfs:
            server: xx.xx.xx.xx  # 修改为挂载存储的服务器ip
            path: /root/data/nfs/mysql/devops   # 修改为存储服务器的存储挂载路径
      containers:
        - name: devops-mysql
          image: mysql:5.7
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: xxxxxxxx     # 设置MySQL数据库登录密码
          imagePullPolicy: Always
          ports:
            - containerPort: 3306
          volumeMounts:
            - name: devops-mysql
              mountPath: /var/lib/mysql

---

apiVersion: v1
kind: Service
metadata:
  name: devops-mysql    # 数据库服务的名称
  namespace: devops
spec:
  ports:
    - port: 3306
      protocol: TCP
      targetPort: 3306
      nodePort: 30001    # 对外访问的端口
  selector:
    app: devops-mysql
  type: NodePort
  sessionAffinity: ClientIP   

2 执行如下命令创建

kubectl apply -f mysql.yaml

3 通过如下命令查看创建结果

使用如下命令查看

kubectl get pod -n devops | grep mysql

如:

[root@master ~]# kubectl get pod -n devops | grep mysql
devops-mysql-59b68c47d4-ttbng               1/1     Running   0          23h
[root@master ~]#

4 命令行进入Pod并登录mysql

如下;

[root@master ~]# kubectl exec -it devops-mysql-59b68c47d4-ttbng bash -n devops
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@devops-mysql-59b68c47d4-ttbng:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.7.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.01 sec)

mysql>

5 至此,数据库已经安装完成,然后即可通过ip+端口,这里是30001,进行数据库链接了

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

redrose2100

您的鼓励是我最大的创作动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值