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,进行数据库链接了

到此这篇关于使用kubernetes集群环境部署mysql数据库的文章就介绍到这了,更多相关kubernetes部署mysql内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!