在TKE集群中创建mysql(主从复制+读写分离)

本次教程目的是创建mysql 并挂载数据卷(StatefulSet + service + configmap + pvc)

基于kubernetes官方文档run-replicated-stateful-application、Kubernetes-部署高可用的MySQL

需要注意的是,本案例只是一个示范,不可实践于生产环境,仅用于理解StatefulSet以及tke操作实践

在开始之前需要确保:

  1. 已创建TKE集群
  2. 集群所在地域CBS(云硬盘)充足

部署MySQL

示例MySQL部署包括ConfigMap,PersistentVolumeClaim,两个Services和StatefulSet。

ConfigMap

通过kubectl 创建 configmap

$ cat mysql-configmap.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql
  labels:
    app: mysql
data:
  master.cnf: |
    [mysqld]
    log-bin
    log_bin_trust_function_creators=1
    lower_case_table_names=1
  slave.cnf: |
    [mysqld]
    super-read-only
    log_bin_trust_function_creators=1

此ConfigMap提供了my.cnf覆盖,允许您独立控制MySQL主服务器和从服务器上的配置。在这种情况下,您希望主服务器能够将复制日志提供给从服务器,并且您希望从服务器拒绝任何不通过复制进行的写入。

ConfigMap本身并没有什么特别之处,它导致不同的部分应用于不同的Pod。每个Pod根据StatefulSet控制器提供的信息决定在初始化时查看哪个部分。

Service

$ cat mysql-service.yaml 
apiVersion: v1
kind: Service
metadata:
  name: mysql
  labels:
    app: mysql
spec:
  ports:
  - name: mysql
    port: 3306
  clusterIP: None
  selector:
    app: mysql
---
apiVersion: v1
kind: Service
metadata:
  name: mysql-read
  labels:
    app: mysql
spec:
  ports:
  - name: mysql
    port: 3306
  selector:
    app: mysql

StatefulSet控制器为Pod创建了一个DNS条目,而Headless服务为DNS条目提供一个主机。因为Headless服务的名称为mysql,其他Pod通过<pod-name>.mysql访问此Pod。客户端访问被称为mysql-read,客户端服务通过访问mysql-read读取数据。通过连接myql执行写入数据的操作。

StatefulSet

$ cat mysql-statefulset.yaml
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
        command:
        - bash
        - "-c"
        - |
          set -ex
          [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
          ordinal=${BASH_REMATCH[1]}
          echo [mysqld] > /mnt/conf.d/server-id.cnf
          echo server-id=$((100 + $ordinal)) >> /mnt/conf.d/server-id.cnf
          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: ist0ne/xtrabackup
        command:
        - bash
        - "-c"
        - |
          set -ex
          [[ -d /var/lib/mysql/mysql ]] && exit 0
          [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
          ordinal=${BASH_REMATCH[1]}
          [[ $ordinal -eq 0 ]] && exit 0
          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
        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: 500m
            memory: 1Gi
        livenessProbe:
          exec:
            command: ["mysqladmin", "ping"]
          initialDelaySeconds: 30
          periodSeconds: 10
          timeoutSeconds: 5
        readinessProbe:
          exec:
            command: ["mysql", "-h", "127.0.0.1", "-e", "SELECT 1"]
          initialDelaySeconds: 5
          periodSeconds: 2
          timeoutSeconds: 1
      - name: xtrabackup
        image: ist0ne/xtrabackup
        ports:
        - name: xtrabackup
          containerPort: 3307
        command:
        - bash
        - "-c"
        - |
          set -ex
          cd /var/lib/mysql
          if [[ -f xtrabackup_slave_info ]]; then
            mv xtrabackup_slave_info change_master_to.sql.in
            rm -f xtrabackup_binlog_info
          elif [[ -f xtrabackup_binlog_info ]]; then
            [[ `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 <<EOF
          $(<change_master_to.sql.orig),
            MASTER_HOST='mysql-0.mysql',
            MASTER_USER='root',
            MASTER_PASSWORD='',
            MASTER_CONNECT_RETRY=10;
          START SLAVE;
          EOF
          fi
          exec ncat --listen --keep-open --send-only --max-conns=1 3307 -c \
            "xtrabackup --backup --slave-info --stream=xbstream --host=127.0.0.1 --user=root"
        volumeMounts:
        - name: data
          mountPath: /var/lib/mysql
          subPath: mysql
        - name: conf
          mountPath: /etc/mysql/conf.d
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
      volumes:
      - name: conf
        emptyDir: {}
      - name: config-map
        configMap:
          name: mysql
  volumeClaimTemplates:
  - metadata:
      name: data
    spec:
      accessModes: ["ReadWriteOnce"]
      resources:
        requests:
          storage: 10Gi

通过kubectl create 创建所有

$ ll
total 16
-rw-r--r-- 1 root root  280 Apr 12 11:34 mysql-configmap.yaml
-rw-r--r-- 1 root root  329 Apr 12 11:34 mysql-service.yaml
-rw-r--r-- 1 root root 4380 Apr 12 11:35 mysql-statefulset.yaml
$ kubectl create -f .
configmap "mysql" created
service "mysql" created
service "mysql-read" created
statefulset.apps "mysql" created

可以通过运行来查看启动进度:

$ kubectl get pods -l app=mysql --watch
NAME      READY     STATUS            RESTARTS   AGE
mysql-0   2/2       Running           0          1m
mysql-1   0/2       PodInitializing   0          26s
mysql-1   1/2       Running   0         27s
mysql-1   2/2       Running   0         29s
mysql-2   0/2       Pending   0         0s
mysql-2   0/2       Pending   0         0s
mysql-2   0/2       Pending   0         15s
mysql-2   0/2       Init:0/2   0         15s
mysql-2   0/2       Init:1/2   0         26s
mysql-2   0/2       Init:1/2   0         29s
mysql-2   0/2       PodInitializing   0         36s
mysql-2   1/2       Running   0         39s
mysql-2   2/2       Running   0         41s

了解有状态Pod初始化

可以看到status有 PodInitializingInit:0/2 ,这是sts中声明的initContainers作用,这里直接引用官方文档的解释understanding-stateful-pod-initialization

生成配置文件

通俗来说:在这里initContainers的作用是根据序数索引生成特殊的MySQL配置文件。确保在启动mysql容器前先通过init-mysql初始化配置文件。

脚本从Pod名称的结尾处获取并确定它的顺序索引,顺序索引通过hostname命令获取。然后,它会按照顺序保存在conf.d目录下的server-id.cnf文件中。此行为将StatefulSet控制器提供的唯一和稳定的身份标识转为mysql服务Id的域。在init-mysql容器中,脚本使用来自于ConfigMap中master.cnf或slave.cnf。

在此例子的拓扑关系中,存在一个MySQL master节点和多个MySQL slave节点,脚本简单的指派顺序0给主节点。这能够保证MySQL主节点在创建从节点之前就已经准备就绪。

为什么pod的名字是 mysql-0 mysql-1 mysql-2?

StatefulSet控制器按顺序索引按顺序启动一个Pod。它会等待,直到每个Pod报告准备就绪,然后开始启动下一个。 此外,控制器为每个Pod分配一个唯一,稳定的pod名称 <statefulset-name>-<ordinal-index>。在这种情况下,导致pod命名mysql-0mysql-1mysql-2

克隆现有数据

一般来说,当一个新的Pod加入进来作为从节点时,必须假设MySQL master已经有关于它的数据。也假设slave副本的日志必须重新开始的。这些假设对于StatefulSet的扩缩容是很关键。

第二个初始化容器是clone-mysql,它在空的PersistentVolume上执行克隆从节点Pod的行为。这意味着它将从已在运行的Pod中拷贝数据,因此,它的当前状态能够与从master开始的副本节点一致。

MySQL自身并没有提供能够做到上述能力的机制,因此,此例子使用开源的Percona XtraBackup工具来实现。在克隆的过程中,为了对MySQL主节点影响的最小化,脚本会要求每一个新的Pod从顺序索引值小的Pod中进行克隆。这样做的原因是,StatefulSet控制器需要一直保证Pod N需要在Pod N+1之前准备就绪。

启动副本

在初始化容器完成后,容器将正常运行。MySQL Pod由运行实际mysqld服务的myqsl容器组成,xtrabacekup容器只是作为备份的工具。xtrabackup负责监控克隆数据文件,并确定是否在从节点初始化MySQL副本。如果是的话,它将等待mysqld就绪,然后执行 CHANGE MASTER TOSTART SLAVEXtraBackup克隆文件中提取复制参数命令。

一旦一个从节点开始复制,它将记住MySQL master,并自动进行重新连接,因为从节点寻找主节点作为稳定DNS名称(mysql-0.mysql),它们自动的发现主节点。最后,在启动副本后,xtrabackup容器也监听来自于其它Pod对数据克隆的请求。

验证mysql

通过运行一个容器(mysql:5.7镜像),使用MySQL 客户端发送测试请求给MySQL master节点(主机名为mysql-0.mysql)

kubectl run mysql-client --image=mysql:5.7 -i --rm --restart=Never --\
  mysql -h mysql-0.mysql <<EOF
CREATE DATABASE test;
CREATE TABLE test.messages (message VARCHAR(250));
INSERT INTO test.messages VALUES ('hello');
EOF

$ kubectl run mysql-client --image=mysql:5.7 -i --rm --restart=Never --\
>   mysql -h mysql-0.mysql <<EOF
> CREATE DATABASE test;
> CREATE TABLE test.messages (message VARCHAR(250));
> INSERT INTO test.messages VALUES ('hello');
> EOF
If you don't see a command prompt, try pressing enter.

在master节点上创建demo数据库,并创建一个只有message字段的demo.messages的表,并为message字段插入hello值。

使用主机mysql-read名将测试查询发送到报告为Ready的任何服务器:

kubectl run mysql-client --image=mysql:5.7 -i -t --rm --restart=Never --\
  mysql -h mysql-read -e "SELECT * FROM test.messages"

$ kubectl run mysql-client --image=mysql:5.7 -i -t --rm --restart=Never --\
>   mysql -h mysql-read -e "SELECT * FROM test.messages"
+---------+
| message |
+---------+
| hello   |
+---------+

要演示mysql-read服务在服务器之间分配连接,您可以SELECT @@server_id在循环中运行:

kubectl run mysql-client-loop --image=mysql:5.7 -i -t --rm --restart=Never --\
  bash -ic "while sleep 1; do mysql -h mysql-read -e 'SELECT @@server_id,NOW()'; done"
# kubectl run mysql-client-loop --image=mysql:5.7 -i -t --rm --restart=Never --\
>   bash -ic "while sleep 1; do mysql -h mysql-read -e 'SELECT @@server_id,NOW()'; done"
If you don't see a command prompt, try pressing enter.
+-------------+---------------------+
| @@server_id | NOW()               |
+-------------+---------------------+
|         101 | 2019-04-12 07:27:37 |
+-------------+---------------------+
+-------------+---------------------+
| @@server_id | NOW()               |
+-------------+---------------------+
|         102 | 2019-04-12 07:27:38 |
+-------------+---------------------+
+-------------+---------------------+
| @@server_id | NOW()               |
+-------------+---------------------+
|         100 | 2019-04-12 07:27:39 |
+-------------+---------------------+
^C
pod default/mysql-client-loop terminated (Error)

模拟pod和节点停止

把mysql容器中的mysql重命名达到mysql -h 127.0.0.1 -e 'SELECT 1' 无法执行,达到容器异常效果

kubectl exec mysql-2 -c mysql -- mv /usr/bin/mysql /usr/bin/mysql.off

几秒钟后,Pod应该将其中一个容器报告为未就绪,您可以通过运行来检查:

$ kubectl get pod
NAME      READY     STATUS    RESTARTS   AGE
mysql-0   2/2       Running   0          40m
mysql-1   2/2       Running   0          39m
mysql-2   1/2       Running   0          39m

此时通过SELECT @@server_id查看会发现102消失不报告了,回想一下init-mysql脚本定义server-id100 + $ordinal,所以server ID 102对应Pod mysql-2

+-------------+---------------------+
| @@server_id | NOW()               |
+-------------+---------------------+
|         102 | 2019-04-12 07:30:43 |
+-------------+---------------------+
+-------------+---------------------+
| @@server_id | NOW()               |
+-------------+---------------------+
|         102 | 2019-04-12 07:30:44 |
+-------------+---------------------+
+-------------+---------------------+
| @@server_id | NOW()               |
+-------------+---------------------+
|         100 | 2019-04-12 07:30:45 |
+-------------+---------------------+
+-------------+---------------------+
| @@server_id | NOW()               |
+-------------+---------------------+
|         101 | 2019-04-12 07:30:46 |
+-------------+---------------------+
+-------------+---------------------+
| @@server_id | NOW()               |
+-------------+---------------------+
|         101 | 2019-04-12 07:30:47 |
+-------------+---------------------+
+-------------+---------------------+
| @@server_id | NOW()               |
+-------------+---------------------+
|         101 | 2019-04-12 07:30:48 |
+-------------+---------------------+
+-------------+---------------------+
| @@server_id | NOW()               |
+-------------+---------------------+
|         100 | 2019-04-12 07:30:49 |
+-------------+---------------------+

修复pod,几秒钟后应该重新出现在循环中

kubectl exec mysql-2 -c mysql -- mv /usr/bin/mysql.off /usr/bin/mysql
# kubectl run mysql-client-loop --image=mysql:5.7 -i -t --rm --restart=Never --  bash -ic "while sleep 1; do mysql -h mysql-read -e 'SELECT @@server_id,NOW()'; done"
If you don't see a command prompt, try pressing enter.
+-------------+---------------------+
| @@server_id | NOW()               |
+-------------+---------------------+
|         101 | 2019-04-12 07:36:53 |
+-------------+---------------------+
+-------------+---------------------+
| @@server_id | NOW()               |
+-------------+---------------------+
|         101 | 2019-04-12 07:36:54 |
+-------------+---------------------+
+-------------+---------------------+
| @@server_id | NOW()               |
+-------------+---------------------+
|         101 | 2019-04-12 07:36:55 |
+-------------+---------------------+
+-------------+---------------------+
| @@server_id | NOW()               |
+-------------+---------------------+
|         100 | 2019-04-12 07:36:56 |
+-------------+---------------------+
+-------------+---------------------+
| @@server_id | NOW()               |
+-------------+---------------------+
|         101 | 2019-04-12 07:36:57 |
+-------------+---------------------+
+-------------+---------------------+
| @@server_id | NOW()               |
+-------------+---------------------+
|         100 | 2019-04-12 07:36:58 |
+-------------+---------------------+
+-------------+---------------------+
| @@server_id | NOW()               |
+-------------+---------------------+
|         102 | 2019-04-12 07:36:59 |
+-------------+---------------------+

删除mysql-2

如果Pods被删除,StatefulSet也会重新创建Pods,类似于ReplicaSet对无状态Pod的处理。

这里不做演示,同样的删除了pod,sts会重新拉起,并创建一个具有相同名称的pod并链接到pvc,可以通过SELECT @@server_id,NOW()去观察102会消失一段时间又复原。

驱逐节点

模拟节点故障

首先确定mysql-2运行在哪个节点上:

$ kubectl get pod -o wide
NAME      READY     STATUS    RESTARTS   AGE       IP           NODE
mysql-0   2/2       Running   0          53m       172.16.1.8   10.0.128.14
mysql-1   2/2       Running   0          53m       172.16.2.4   10.0.128.13
mysql-2   2/2       Running   0          52m       172.16.0.5   10.0.128.9

然后驱逐10.0.128.9

kubectl drain 10.0.128.9 --force --delete-local-data --ignore-daemonsets

$ kubectl drain 10.0.128.9 --force --delete-local-data --ignore-daemonsets
node "10.0.128.9" cordoned
WARNING: Deleting pods with local storage: mysql-2; Ignoring DaemonSet-managed pods: ip-masq-agent-gjtvn
pod "kube-dns-898dbbfc6-fh94g" evicted
pod "mysql-2" evicted
pod "kube-dns-898dbbfc6-jd42p" evicted
node "10.0.128.9" drained

驱逐完成后查看pod状态,可以看到mysql-2又被拉起来并在13节点上启动

$ kubectl get pod  -o wide
NAME      READY     STATUS     RESTARTS   AGE       IP           NODE
mysql-0   2/2       Running    0          56m       172.16.1.8   10.0.128.14
mysql-1   2/2       Running    0          56m       172.16.2.4   10.0.128.13
mysql-2   0/2       Init:0/2   0          30s       <none>       10.0.128.13
$ kubectl get pod  -o wide
NAME      READY     STATUS    RESTARTS   AGE       IP           NODE
mysql-0   2/2       Running   0          57m       172.16.1.8   10.0.128.14
mysql-1   2/2       Running   0          56m       172.16.2.4   10.0.128.13
mysql-2   2/2       Running   0          1m        172.16.2.5   10.0.128.13

复原节点

$ kubectl uncordon 10.0.128.9
node "10.0.128.9" uncordoned

缩放slaves数量

扩容

对于mysql副本,通过添加从节点进行扩容。

$ kubectl scale statefulset mysql  --replicas=5
statefulset.apps "mysql" scaled

一旦它们启动,您应该看到服务器ID 103并104开始出现在SELECT @@server_id循环输出中。

$ kubectl get pod 
NAME      READY     STATUS     RESTARTS   AGE
mysql-0   2/2       Running    0          1h
mysql-1   2/2       Running    0          59m
mysql-2   2/2       Running    0          4m
mysql-3   2/2       Running    0          1m
mysql-4   0/2       Init:1/2   0          31s

还可以验证这些新实例是否包含您在存在之前添加的数据,这里链接mysql-3

$ kubectl run mysql-client --image=mysql:5.7 -i -t --rm --restart=Never --\
>   mysql -h mysql-3.mysql -e "SELECT * FROM test.messages"
+---------+
| message |
+---------+
| hello   |
+---------+

缩容

kubectl scale statefulset mysql --replicas=3

$ kubectl scale statefulset mysql --replicas=3
statefulset.apps "mysql" scaled

但是请注意,虽然扩容后会自动创建新的pvc,但是缩容后不会自动删除这些pvc,你可以选择保留这些pvc,或手动删除,保留将产生费用请自行评估(用也会产生费用,具体的看storageclasses如何配置,TKE集群默认会有一个名为cbs的storageclasses,按量计费,云硬盘类型为普通云硬盘)

运行这个查看pvc:

kubectl get pvc -l app=mysql

$kubectl get pvc 
NAME           STATUS    VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
data-mysql-0   Bound     pvc-518a14d5-5cef-11e9-b7b3-860056e03b4e   50Gi       RWO            cbs            1h
data-mysql-1   Bound     pvc-6e00af5f-5cef-11e9-b7b3-860056e03b4e   50Gi       RWO            cbs            1h
data-mysql-2   Bound     pvc-7f3d68cd-5cef-11e9-b7b3-860056e03b4e   50Gi       RWO            cbs            1h
data-mysql-3   Bound     pvc-9b71a84c-5cf7-11e9-b7b3-860056e03b4e   50Gi       RWO            cbs            7m
data-mysql-4   Bound     pvc-b4a1d96f-5cf7-11e9-b7b3-860056e03b4e   50Gi       RWO            cbs            6m
控制台展示

打扫干净

直接delete 即可,sts会一个一个回收,时间较长。

$ kubectl delete -f .
configmap "mysql" deleted
service "mysql" deleted
service "mysql-read" deleted

pvc需手动删除

到此结束。

原文链接:https://kubernetes.io/docs/tasks/run-application/run-replicated-stateful-application/

原文作者:kubernetes官方文档[run-replicated-stateful-application]

编辑于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券