基于kubernetes部署MySQL主从环境

部署方式

通过部署mysql主从容器,配置主从pod之间数据同步。

配置数据库访问的密码

bash 复制代码
创建 Mysql 密码的 Secret
[root@k8s-master1 master]# kubectl create secret generic mysql-password --namespace=app --from-literal=mysql_root_password=root
secret/mysql-passowrd created
You have new mail in /var/spool/mail/root
[root@k8s-master1 master]# kubectl get secret -n app
NAME                                  TYPE                                  DATA   AGE
default-token-w6q98                   kubernetes.io/service-account-token   3      48d
ingress-nginx-admission               Opaque                                3      35d
ingress-nginx-admission-token-sqbvf   kubernetes.io/service-account-token   3      35d
ingress-nginx-token-bt7g2             kubernetes.io/service-account-token   3      35d
mysql-passowrd                        Opaque                                1      6s

先配置MySQL-Master服务

配置数据卷

bash 复制代码
[root@k8s-master1 master]# cat mysql-master-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-master-pvc
  namespace: app
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi
  storageClassName: nfs-client              #这里要换成你环境的storageClassName
  volumeMode: Filesystem
[root@k8s-master1 master]# kubectl apply -f mysql-master-pvc.yaml
[root@k8s-master1 master]# kubectl get pvc -n app
NAME                         STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
mysql-master-pvc             Bound    pvc-f2b5598c-4fd8-48f2-bc93-0e8c27e225ec   2Gi        RWO            nfs-client     10s

通过ConfiigMap配置MySQL的配置文件my.cnf

bash 复制代码
[root@k8s-master1 master]# cat my.cnf
[mysqld]
skip-host-cache
skip-name-resolve
datadir                = /var/lib/mysql
socket                 = /var/run/mysqld/mysqld.sock
secure-file-priv       = /var/lib/mysql-files
pid-file               = /var/run/mysqld/mysqld.pid
user                   = mysql
secure-file-priv       = NULL
server-id              = 1                              #master节点的server-id=1
log-bin                = master-bin
log_bin_index          = master-bin.index
binlog_do_db           = deploy_test
binlog_ignore_db       = information_sechema
binlog_ignore_db       = mysql
binlog_ignore_db       = performance_schema
binlog_ignore_db       = sys
binlog-format          = row

[client]
socket                 = /var/run/mysqld/mysqld.sock

!includedir  /etc/mysql/conf.d/
[root@k8s-master1 master]# kubectl create cm mysql-master-cm --from-file=my.cnf -n app
configmap/mysql-master-cm created
[root@k8s-master1 master]# kubectl get cm -n app |grep mysql
mysql-master-cm            1      19s

配置Service服务

bash 复制代码
[root@k8s-master1 master]# cat mysql-master-svc.yaml
apiVersion: v1
kind: Service
metadata:
  name: mysql-master-svc
  namespace: app
  labels:
    app: mysql-master
spec:
  ports:
  - port: 3306
    name: mysql
    targetPort: 3306
    nodePort: 30306
  selector:
    app: mysql-master
  type: NodePort
  sessionAffinity: ClientIP
[root@k8s-master1 master]# kubectl apply -f mysql-master-svc.yaml
service/mysql-master-svc created

配置主节点pod启动的StatefulSet配置文件

bash 复制代码
[root@k8s-master1 master]# cat mysql-master-StatefulSet.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql-master
  namespace: app
spec:
  selector:
    matchLabels:
      app: mysql-master
  serviceName: "mysql-master-svc"
  replicas: 1
  template:
    metadata:
      labels:
        app: mysql-master
    spec:
      terminationGracePeriodSeconds: 10
      containers:
      - args:
        - --character-set-server=utf8mb4
        - --collation-server=utf8mb4_unicode_ci
        - --lower_case_table_names=1
        - --default-time_zone=+8:00
        name: mysql
        # image: docker.io/library/mysql:8.0.34
        image: registry.cn-shenzhen.aliyuncs.com/xiaohh-docker/mysql:8.0.34
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-data
          mountPath: /var/lib/mysql
        - name: mysql-conf
          mountPath: /etc/my.cnf
          readOnly: true
          subPath: my.cnf
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              key: mysql_root_password
              name: mysql-password
      volumes:
      - name: mysql-data
        persistentVolumeClaim:
          claimName: mysql-master-pvc
      - name: mysql-conf
        configMap:
          name: mysql-master-cm
          items:
          - key: my.cnf
            mode: 0644
            path: my.cnf
[root@k8s-master1 master]# kubectl apply -f mysql-master-StatefulSet.yaml
statefulset.apps/mysql-master created

检查服务的启动情况

bash 复制代码
[root@k8s-master1 master]# kubectl get pods -n app -o wide |grep mysql
mysql-master-0                              1/1     Running   0              104s    10.10.36.73      k8s-node1     <none>           <none>

配置mysql-salve

配置PVC

bash 复制代码
[root@k8s-master1 slave]# cat mysql-slave-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-slave-pvc
  namespace: app
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi
  storageClassName: nfs-client
  volumeMode: Filesystem
[root@k8s-master1 slave]# kubectl apply -f mysql-slave-pvc.yaml
persistentvolumeclaim/mysql-slave-pvc created

配置ConfigMap

bash 复制代码
[root@k8s-master1 slave]# cat my.cnf
[mysqld]
skip-host-cache
skip-name-resolve
datadir                = /var/lib/mysql
socket                 = /var/run/mysqld/mysqld.sock
secure-file-priv       = /var/lib/mysql-files
pid-file               = /var/run/mysqld/mysqld.pid
user                   = mysql
secure-file-priv       = NULL
server-id              = 2                                #这个要修改
log-bin                = master-bin
log_bin_index          = master-bin.index
binlog_do_db           = deploy_test
binlog_ignore_db       = information_sechema
binlog_ignore_db       = mysql
binlog_ignore_db       = performance_schema
binlog_ignore_db       = sys
binlog-format          = row

[client]
socket                 = /var/run/mysqld/mysqld.sock

!includedir  /etc/mysql/conf.d/
[root@k8s-master1 slave]# kubectl create cm mysql-slave-cm --from-file=my.cnf -n app
configmap/mysql-slave-cm created
You have new mail in /var/spool/mail/root
[root@k8s-master1 slave]# kubectl get cm -n app |grep mysql-slave-cm
mysql-slave-cm             1      13s

配置Service

bash 复制代码
[root@k8s-master1 slave]# cat mysql-slave-svc.yaml
apiVersion: v1
kind: Service
metadata:
  name: mysql-slave-svc
  namespace: app
  labels:
    app: mysql-slave
spec:
  ports:
  - port: 3306
    name: mysql
    targetPort: 3306
    nodePort: 30307
  selector:
    app: mysql-slave
  type: NodePort
  sessionAffinity: ClientIP
[root@k8s-master1 slave]# kubectl apply -f mysql-slave-svc.yaml
service/mysql-slave-svc created

创建MySQL的Pod

bash 复制代码
[root@k8s-master1 slave]# cat mysql-slave-StatefulSet.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql-slave
  namespace: app
spec:
  selector:
    matchLabels:
      app: mysql-slave
  serviceName: "mysql-master-svc"
  replicas: 1
  template:
    metadata:
      labels:
        app: mysql-slave
    spec:
      terminationGracePeriodSeconds: 10
      containers:
      - args:
        - --character-set-server=utf8mb4
        - --collation-server=utf8mb4_unicode_ci
        - --lower_case_table_names=1
        - --default-time_zone=+8:00
        name: mysql
        # image: docker.io/library/mysql:8.0.34
        image: registry.cn-shenzhen.aliyuncs.com/xiaohh-docker/mysql:8.0.34
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-data
          mountPath: /var/lib/mysql
        - name: mysql-conf
          mountPath: /etc/my.cnf
          readOnly: true
          subPath: my.cnf
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              key: mysql_root_password
              name: mysql-password
      volumes:
      - name: mysql-data
        persistentVolumeClaim:
          claimName: mysql-slave-pvc
      - name: mysql-conf
        configMap:
          name: mysql-slave-cm
          items:
          - key: my.cnf
            mode: 0644
            path: my.cnf
[root@k8s-master1 slave]# kubectl apply -f mysql-slave-StatefulSet.yaml
statefulset.apps/mysql-slave created
[root@k8s-master1 slave]# kubectl get pods -n app -o wide |grep mysql
mysql-master-0                              1/1     Running   0               14m     10.10.36.73      k8s-node1     <none>           <none>
mysql-slave-0                               1/1     Running   0               38s     10.10.36.74      k8s-node1     <none>           <none>

主从POD都创建完成了,咱们登录验证一下,并配置一下主从同步

bash 复制代码
[root@k8s-master1 slave]# kubectl exec -it mysql-master-0 -n app -- mysql -uroot -proot
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.34 MySQL Community Server - GPL

Copyright (c) 2000, 2023, 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>

在从节点执行,配置主从关系

bash 复制代码
mysql> change master to master_host='mysql-master-0.mysql-master-svc.app.svc.cluster.local',
    -> master_port=3306, master_user='root', master_password='root', master_log_file='master-bin.000003',
    -> master_log_pos=157,master_connect_retry=30,get_master_public_key=1;
Query OK, 0 rows affected, 11 warnings (0.03 sec)
master_host:这个参数是master的地址,kubernetes 提供的解析规则是 pod 名称.service名称.命名空间.svc.cluster.local,所以 master 的 mysql 地址是 deploy-mysql-master-0.deploy-mysql-master-svc.deploy-test.svc.cluster.local
master_port:主节点的 MySQL 端口,没改默认 3306
master_user:登录到主节点的 mysql 用户
master_password:登录到主节点的用户密码
master_log_file:之前查看 mysql 主节点状态时的 file 字段
master_log_pos:之前查看 mysql 主节点状态时的 Position 字段
master_connect-retry:主节点重连时间
get_master_public_key:连接主 mysql 的公钥获取方式

启动 slave

bash 复制代码
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)

查看slave的状态

bash 复制代码
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for source to send event
                  Master_Host: mysql-master-0.mysql-master-svc.app.svc.cluster.local
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 30
              Master_Log_File: master-bin.000003
          Read_Master_Log_Pos: 157
               Relay_Log_File: mysql-slave-0-relay-bin.000002
                Relay_Log_Pos: 327
        Relay_Master_Log_File: master-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

在主节点创建一个数据库验证一下主从同步情况

相关推荐
CQU_JIAKE1 小时前
【miniob】JOIN TABLE DEBUG
数据库·sql·mysql
让生命变得有价值2 小时前
k8s 启用 ValidatingAdmissionPolicy 特性
java·容器·kubernetes·kubelet
Allocator3 小时前
docker 容器访问宿主机器服务的最简单方法
运维·docker·容器
AlenTech3 小时前
如何设置docker的定时关闭和启动
docker·容器·eureka
我从不骗人4 小时前
Docker使用相关记录
运维·docker·容器
valkyrja1107 小时前
小白docker入门简介
docker·容器·小白·初学
十一落10 小时前
Sql(MySQL事务四大特性、事务隔离级别)
数据库·sql·mysql
秋秋秋秋秋雨11 小时前
linux强制修改mysql的root账号密码
linux·mysql·adb
君败红颜13 小时前
Docker 常用命令详解(详细版)
运维·docker·容器
想学习java初学者14 小时前
Docker Compose部署Rabbitmq(Dockerfile安装延迟队列)
docker·容器·rabbitmq