【深入分析】K8s部署Mysql主从复制+读写分离

一、Mysql StatefulSet应用文件预览
已用中文进行解释,请看绿色字体

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql
spec:
  selector:
    matchLabels:
      app: mysql
  serviceName: mysql
  replicas: 3
  template:
    metadata:
      labels:
        app: mysql
    spec:
      initContainers: 
      - name: init-mysql
        image: mysql:5.7
        imagePullPolicy: IfNotPresent
        command:
        - bash
        - "-c"
        - |
          set -ex
          # 从hostname中获取索引,比如(mysql-1)会获取(1)
          [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
          ordinal=${BASH_REMATCH[1]}
          echo [mysqld] > /mnt/conf.d/server-id.cnf
          # 为了不让server-id=0而增加偏移量
          echo server-id=$((100 + $ordinal)) >> /mnt/conf.d/server-id.cnf
          # 拷贝对应的文件到/mnt/conf.d/文件夹中
          if [[ $ordinal -eq 0 ]]; then
            cp /mnt/config-map/master.cnf /mnt/conf.d/
          else
            cp /mnt/config-map/slave.cnf /mnt/conf.d/
          fi
        volumeMounts:
        - name: conf
          mountPath: /mnt/conf.d
        - name: config-map
          mountPath: /mnt/config-map
      - name: clone-mysql
        image: twoeo/gcr.io-google-samples-xtrabackup:latest
        imagePullPolicy: IfNotPresent
        command:
        - bash
        - "-c"
        - |
          set -ex
          # 整体意思:
          # 1.如果是主mysql中的xtrabackup,就不需要克隆自己了,直接退出
          # 2.如果是从mysql中的xtrabackup,先判断是否是第一次创建,因为第二次重启本地就有数据库,无需克隆。若是第一次创建(通过/var/lib/mysql/mysql文件是否存在判断),就需要克隆数据库到本地。
          # 如果有数据不必克隆数据,直接退出()
          [[ -d /var/lib/mysql/mysql ]] && exit 0
          # 如果是master数据也不必克隆
          [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
          ordinal=${BASH_REMATCH[1]}
          [[ $ordinal -eq 0 ]] && exit 0
          # 从序列号比自己小一的数据库克隆数据,比如mysql-2会从mysql-1处克隆数据
          ncat --recv-only mysql-$(($ordinal-1)).mysql 3307 | xbstream -x -C /var/lib/mysql
          # 比较数据
          xtrabackup --prepare --target-dir=/var/lib/mysql
        volumeMounts:
        - name: data
          mountPath: /var/lib/mysql
          subPath: mysql
        - name: conf
          mountPath: /etc/mysql/conf.d
      containers:
      - name: mysql
        image: mysql:5.7
        imagePullPolicy: IfNotPresent
        env:
        - name: MYSQL_ALLOW_EMPTY_PASSWORD
          value: "1"
        ports:
        - name: mysql
          containerPort: 3306
        volumeMounts:
        - name: data
          mountPath: /var/lib/mysql
          subPath: mysql
        - name: conf
          mountPath: /etc/mysql/conf.d
        resources:
          requests:
            cpu: 50m
            memory: 50Mi
        livenessProbe:
          exec:
            command: ["mysqladmin", "ping"]
          initialDelaySeconds: 30
          periodSeconds: 10
          timeoutSeconds: 5
        readinessProbe:
          exec:
            # Check we can execute queries over TCP (skip-networking is off).
            command: ["mysql", "-h", "127.0.0.1", "-e", "SELECT 1"]
          initialDelaySeconds: 5
          periodSeconds: 2
          timeoutSeconds: 1
      - name: xtrabackup
        image: twoeo/gcr.io-google-samples-xtrabackup:latest
        imagePullPolicy: IfNotPresent
        ports:
        - name: xtrabackup
          containerPort: 3307
        command:
        - bash
        - "-c"
        - |
          set -ex
          cd /var/lib/mysql
          # 确定binlog 克隆数据位置(如果binlog存在的话).
          if [[ -f xtrabackup_slave_info ]]; then
            # 如果存在该文件,则该xrabackup是从现有的从节点克隆出来的。
            mv xtrabackup_slave_info change_master_to.sql.in
            # 在这种情况下,忽略xtrabackup_binlog_info(它是无用的)。
            rm -f xtrabackup_binlog_info
          elif [[ -f xtrabackup_binlog_info ]]; then
            # 我们直接从主人那里克隆。分析binlog位置。
            [[ `cat xtrabackup_binlog_info` =~ ^(.*?)[[:space:]]+(.*?)$ ]] || exit 1
            rm xtrabackup_binlog_info
            echo "CHANGE MASTER TO MASTER_LOG_FILE='${BASH_REMATCH[1]}',\
                  MASTER_LOG_POS=${BASH_REMATCH[2]}" > change_master_to.sql.in
          fi
          # 检查是否需要通过启动复制来完成克隆.
          if [[ -f change_master_to.sql.in ]]; then
            echo "Waiting for mysqld to be ready (accepting connections)"
            until mysql -h 127.0.0.1 -e "SELECT 1"; do sleep 1; done
            echo "Initializing replication from clone position"
            # 如果容器重新启动,最多尝试一次。
            mv change_master_to.sql.in change_master_to.sql.orig
            mysql -h 127.0.0.1 <
        volumeMounts:
        - name: data
          mountPath: /var/lib/mysql
          subPath: mysql
        - name: conf
          mountPath: /etc/mysql/conf.d
        resources:
          requests:
            cpu: 10m
            memory: 10Mi
      volumes:
      - name: conf
        emptyDir: {}
      - name: config-map
        configMap:
          name: mysql
  volumeClaimTemplates:
  - metadata:
      name: data
    spec:
      accessModes: ["ReadWriteOnce"]
      resources:
        requests:
          storage: 0.1Gi

1.initContainers作用:
(1).主数据库无需同步自己数据,从数据库需要被同步数据
(2).判断当前启动的容器是主还是slave,并向pod的conf卷写入如下数据

cat /mnt/conf.d/server-id.cnf
[mysqld]
server-id=10?
ls /mnt/conf.d/  
master[?salve].cnf # 从configmap处拷贝而来

(3)当执行完command命令后生命终止,但是pod的conf卷的数据依然存在,因为初始容器和接下来的container容器使用共同的volumes卷,不同的是conf被initContainer挂载到/mnt/conf.d,而在container被挂载在/etc/mysql/conf.d/
2.Container作用:
(1)提供mysql服务
(2)运行xtrabakup应用容器提供数据同步

二、数据结构分析

{
    "apiVersion":"apps/v1",
    "kind":"StatefulSet",
    "metadata":Object{...},
    "spec":{
        "selector":Object{...},
        "serviceName":"mysql",
        "replicas":3,
        "template":{
            "metadata":Object{...},
            "spec":{
                "initContainers":Array[2],
                "containers":Array[2],
                "volumes":Array[2]
            }
        },
        "volumeClaimTemplates":Array[1]
    }
}

【深入分析】K8s部署Mysql主从复制+读写分离_第1张图片
从上图可知知道satefulSet的pvc模板由pv提供存储,voluems作用范围在Pod内所有容器,volumes卷中conf的数据被所有容器共享,/var/lib/mysql映射的data是要被持久化的数据…

了解彼此资源、对象的关系后,看这篇文章实现K8s部署Mysql主从复制+读写分离,利用静态存储卷
【全网最全最详细】Kubernetes部署Mysql主从复制+读写分离

你可能感兴趣的:(Kubernetes)