美文网首页MySQLK8sk8s
k8s部署mysql主从

k8s部署mysql主从

作者: 渡边Hok | 来源:发表于2021-07-27 21:39 被阅读0次

    1、环境

    主机列表:
    node1 10.10.10.25
    node2 10.10.10.26
    node3 10.10.10.27

    node1 k8s的管理节点 nfs服务器
    node2 k8s的工作节点
    node3 k8s的工作节点

    1、创建数据目录

    在node1基于nfs创建mysql数据目录:

    mkdir /k8s/
    chmod 777 k8s
    cd k8s
    mkdir mysql
    cd mysql 
    mkdir master
    mkdir slave
    vi /etc/exports
    /k8s/ *(insecure,rw,sync,no_root_squash)
    systemctl restart nfs
    

    2、检查nfs目录是否创建成功

    登陆node2 或者node3执行以下命令

    showmount -e 10.10.10.25
    

    可以看到以下结果


    image.png

    3、配置mysql密码

    配置root的mysql密码123

    echo -n '123' | base64
    
    apiVersion: v1
    data:
      MYSQL_ROOT_PASSWORD: MTIz #密码123 采用base64编码后结果
    kind: Secret
    metadata:
      name: mysql-secret
      namespace: default
    type: Opaque
    

    4、编写my.cnf配置文件

    master my.cnf 配置

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: mysql-master-cnf
      namespace: default
    data:
      my.cnf: |-
        [client]
        default-character-set=utf8
        [mysql]
        default-character-set=utf8
        [mysqld]
        init_connect='SET collation_connection = utf8_unicode_ci'
        init_connect='SET NAMES utf8'
        character-set-server=utf8
        collation-server=utf8_unicode_ci
        skip-character-set-client-handshake
        skip-name-resolve
        #master-slaver repication
        server_id=1
        log-bin=mysql-bin
        read-only=0
        #binlog-do-db=admin 需要主从复制的表
        replicate-ignore-db=mysql
        replicate-ignore-db=sys
        replicate-ignore-db=information_schema
        replicate-ignore-db=performance_schema
    

    slave my.cnf配置

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: mysql-slave-cnf
      namespace: default
    data:
      my.cnf: |-
        [client]
        default-character-set=utf8
        [mysql]
        default-character-set=utf8
        [mysqld]
        init_connect='SET collation_connection = utf8_unicode_ci'
        init_connect='SET NAMES utf8'
        character-set-server=utf8
        collation-server=utf8_unicode_ci
        skip-character-set-client-handshake
        skip-name-resolve
        #master-slaver repication
        server_id=2
        log-bin=mysql-bin
        read-only=1
    #binlog-do-db=admin 需要主从复制的表
    
        replicate-ignore-db=mysql
        replicate-ignore-db=sys
        replicate-ignore-db=information_schema
        replicate-ignore-db=performance_schema
    

    5、配置pv

    master挂载卷配置

    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: mysql-pv-master
    spec:
      accessModes:
        - ReadWriteOnce
      capacity:
        storage: 5Gi
      nfs:
        path: /k8s/mysql/master
        readOnly: false
        server: 10.10.10.25
    

    slave挂载卷配置

    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: mysql-pv-slave
    spec:
      accessModes:
        - ReadWriteOnce
      capacity:
        storage: 5Gi
      nfs:
        path: /k8s/mysql/slave
        readOnly: false
        server: 10.10.10.25
    

    6、配置 pvc

    master pvc:

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: mysql-pvc-master
      namespace: default
    spec:
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
          storage: 5Gi
      volumeName: mysql-pv-master
    

    slave pvc:

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: mysql-pvc-slave
      namespace: default
    spec:
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
          storage: 5Gi
      volumeName: mysql-pv-slave
    

    7、创建有状态副本集StatefulSet

    master有状态副本集

    apiVersion: apps/v1
    kind: StatefulSet
    metadata:
      namespace: default
      labels:
        app: mysql-master
      name: mysql-master
      annotations:
        kubesphere.io/alias-name: mysql主节点
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: mysql-master
      template:
        metadata:
          labels:
            app: mysql-master
          annotations:
            kubesphere.io/containerSecrets: null
            logging.kubesphere.io/logsidecar-config: '{}'
        spec:
          containers:
            - name: master-container
              type: worker
              imagePullPolicy: IfNotPresent
              resources:
                requests:
                  cpu: '0.01'
                  memory: 10Mi
                limits:
                  cpu: '0.98'
                  memory: 1700Mi
              image: 'mysql:5.7'
              ports:
                - name: tcp-3306
                  protocol: TCP
                  containerPort: 3306
                  servicePort: 3306
                - name: tcp-33060
                  protocol: TCP
                  containerPort: 33060
                  servicePort: 33060
              env:
                - name: MYSQL_ROOT_PASSWORD
                  valueFrom:
                    secretKeyRef:
                      name: mysql-secret
                      key: MYSQL_ROOT_PASSWORD
              volumeMounts:
                - name: master-cnf-volume
                  readOnly: false
                  mountPath: /etc/mysql
                - name: master-data-volume
                  readOnly: false
                  mountPath: /var/lib/mysql
          serviceAccount: default
          affinity:
            podAntiAffinity:
              preferredDuringSchedulingIgnoredDuringExecution:
                - weight: 100
                  podAffinityTerm:
                    labelSelector:
                      matchLabels:
                        app: mysql-master
                    topologyKey: kubernetes.io/hostname
          initContainers: []
          imagePullSecrets: null
          volumes:
            - name: master-cnf-volume     #映射configMap信息
              configMap:
                name: mysql-master-cnf
                items:
                  - key: my.cnf
                    path: my.cnf
            - name: master-data-volume    #映射pvc信息
              persistentVolumeClaim:
                claimName: mysql-pvc-master
      updateStrategy:
        type: RollingUpdate
        rollingUpdate:
          partition: 0
      serviceName: mysql-master
    

    slave有状态副本集

    apiVersion: apps/v1
    kind: StatefulSet
    metadata:
      namespace: default
      labels:
        app: mysql-slave
      name: mysql-slave
      annotations:
        kubesphere.io/alias-name: mysql主节点
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: mysql-slave
      template:
        metadata:
          labels:
            app: mysql-slave
          annotations:
            kubesphere.io/containerSecrets: null
            logging.kubesphere.io/logsidecar-config: '{}'
        spec:
          containers:
            - name: slave-container
              type: worker
              imagePullPolicy: IfNotPresent
              resources:
                requests:
                  cpu: '0.01'
                  memory: 10Mi
                limits:
                  cpu: '0.98'
                  memory: 1700Mi
              image: 'mysql:5.7'
              ports:
                - name: tcp-3306
                  protocol: TCP
                  containerPort: 3306
                  servicePort: 3306
                - name: tcp-33060
                  protocol: TCP
                  containerPort: 33060
                  servicePort: 33060
              env:
                - name: MYSQL_ROOT_PASSWORD
                  valueFrom:
                    secretKeyRef:
                      name: mysql-secret
                      key: MYSQL_ROOT_PASSWORD
              volumeMounts:
                - name: slave-cnf-volume
                  readOnly: false
                  mountPath: /etc/mysql
                - name: slave-data-volume
                  readOnly: false
                  mountPath: /var/lib/mysql
          serviceAccount: default
          affinity:
            podAntiAffinity:
              preferredDuringSchedulingIgnoredDuringExecution:
                - weight: 100
                  podAffinityTerm:
                    labelSelector:
                      matchLabels:
                        app: mysql-slave
                    topologyKey: kubernetes.io/hostname
          initContainers: []
          imagePullSecrets: null
          volumes:
            - name: slave-cnf-volume     #映射configMap信息
              configMap:
                name: mysql-slave-cnf
                items:
                  - key: my.cnf
                    path: my.cnf
            - name: slave-data-volume    #映射pvc信息
              persistentVolumeClaim:
                claimName: mysql-pvc-slave
      updateStrategy:
        type: RollingUpdate
        rollingUpdate:
          partition: 0
      serviceName: mysql-slave
    
    

    8、创建无头服务HeadlessService

    master无头服务

    apiVersion: v1
    kind: Service
    metadata:
      namespace: default
      labels:
        app: mysql-master
      annotations:
        kubesphere.io/serviceType: statefulservice
        kubesphere.io/alias-name: mysql主节点
      name: mysql-master
    spec:
      sessionAffinity: ClientIP
      selector:
        app: mysql-master
      ports:
        - name: tcp-3306
          protocol: TCP
          port: 3306
          targetPort: 3306
        - name: tcp-33060
          protocol: TCP
          port: 33060
          targetPort: 33060
      clusterIP: None
      sessionAffinityConfig:
        clientIP:
          timeoutSeconds: 10800
    

    slave无头服务

    apiVersion: v1
    kind: Service
    metadata:
      namespace: default
      labels:
        app: mysql-slave
      annotations:
        kubesphere.io/serviceType: statefulservice
        kubesphere.io/alias-name: mysql主节点
      name: mysql-slave
    spec:
      sessionAffinity: ClientIP
      selector:
        app: mysql-slave
      ports:
        - name: tcp-3306
          protocol: TCP
          port: 3306
          targetPort: 3306
        - name: tcp-33060
          protocol: TCP
          port: 33060
          targetPort: 33060
      clusterIP: None
      sessionAffinityConfig:
        clientIP:
          timeoutSeconds: 10800
    

    9、创建外部访问的NodePort Service

    master NodePort Service

    apiVersion: v1
    kind: Service
    metadata:
      name: mysql-master-front
      labels:
        app: mysql-master
      namespace: default
    spec:
      selector:
        app: mysql-master
      type: NodePort
      ports:
        - name: ''
          port: 3306
          protocol: TCP
          targetPort: 3306
          nodePort: 30001  #指定主机任意端口30000-32767
      sessionAffinity: None
    

    slave NodePort Service

    apiVersion: v1
    kind: Service
    metadata:
      name: mysql-slave-front
      labels:
        app: mysql-slave
      namespace: default
    spec:
      selector:
        app: mysql-slave
      type: NodePort
      ports:
        - name: ''
          port: 3306
          protocol: TCP
          targetPort: 3306
          nodePort: 30002    #指定主机任意端口30000-32767
      sessionAffinity: None
    

    此时,mysql主机的部署已经完成,可以使用外部客户端访问了。

    10、主从同步

    进入mysql-master容器内部,

    
    # 1.进入mysql内部
    >  mysql -uroot -p123
    #切换到 mysql DB
    mysql> USE mysql;   
    # 查看root用户是否具备远程访问权限
    mysql> select Host,User,authentication_string,password_expired,password_last_changed from user; 
    
    # 2.授权 root可以远程访问(主从无关,如root没有访问权限,执行以下命令,方便我们远程连接MySQL)
    mysql> grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option;
    Query OK, 0 rows affected, 1 warning (0.00 sec)
    
    mysql> flush privileges;
    Query OK, 0 rows affected (0.00 sec)
    
    # 3.添加用来同步的用户
    mysql> GRANT REPLICATION SLAVE ON *.* to 'backup'@'%' identified by '123456';
    Query OK, 0 rows affected, 1 warning (0.01 sec)
    
    # 4.查看master状态
    mysql> show master status\G;
    *************************** 1. row ***************************
                 File: mysql-bin.000003
             Position: 889
         Binlog_Do_DB: ums,pms,oms,sms,wms,admin
     Binlog_Ignore_DB:
    Executed_Gtid_Set:
    1 row in set (0.00 sec)
    

    创建一个busybox,执行以下命令查看主库dns

    apiVersion: v1
    kind: Pod
    metadata:
      name: busybox
      namespace: default
    spec:
      containers:
      - name: busybox
        image: busybox:1.32
        command:
          - sleep
          - "3600"
        imagePullPolicy: IfNotPresent
      restartPolicy: Always
    

    进入busybox容器内,执行以下命令

    nslookup mysql-master
    
    image.png

    然后进入到mysql-slave内部

    # 进入mysql内部
    mysql -uroot -p123
    
    # 设置主库连接  主库 dns: mysql-master.default.svc.cluster.local
    change master to master_host='mysql-master.default.svc.cluster.local',master_user='backup',master_password='123456',master_log_file='mysql_bin.000003',master_log_pos=0,master_port=3306;
    
    # 启动从库同步
    start slave;
    
    # 查看从从库状态
    show slave status\G;
    

    只有当以下两项都是yes,才意味着同步成功。


    image.png

    如果同步不成功,尝试执行以下命令

    stop slave;
    reset slave;
    start slave;
    

    相关文章

      网友评论

        本文标题:k8s部署mysql主从

        本文链接:https://www.haomeiwen.com/subject/dpbomltx.html