k8s使用statefulset部署mysql一主多从集群_k8s部署mysql集群南

部署原理

在这里插入图片描述

1、准备环境

服务器2核4G
139.198.38.94
139.198.41.101
139.198.31.125

需要提前准备好NFS挂载,此处用的是静态nfs

#所有机器安装
yum install -y nfs-utils

主节点

#nfs主节点
echo "/nfs/data/ *(insecure,rw,sync,no_root_squash)" > /etc/exports

mkdir -p /nfs/data
systemctl enable rpcbind --now
systemctl enable nfs-server --now
#配置生效
exportfs -r

从节点

showmount -e 139.198.38.94

#执行以下命令挂载 nfs 服务器上的共享目录到本机路径 /root/nfsmount
mkdir -p /nfs/data

mount -t nfs 139.198.38.94:/nfs/data /nfs/data

静态pv池

#nfs主节点
mkdir -p /nfs/data/01
mkdir -p /nfs/data/02
mkdir -p /nfs/data/03

二、部署MySQL

(1)从以下的 YAML 配置文件创建 ConfigMap

configmap(作用配置文件和镜像解藕)

[root@master mysql]# cat cm.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql
  labels:
    app: mysql
data:
  master.cnf: |
    [mysqld]
    log-bin     # 主mysql激活二进制日志
  slave.cnf: |
    [mysqld]
    super-read-only     # 从mysql上面设置为只读

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

ConfigMap 本身没有什么特别之处,它可以使不同部分应用于不同的 Pod。 每个 Pod 都会决定在初始化时要看基于 StatefulSet 控制器提供的信息。

(2)从以下 YAML 配置文件创建服务

[root@master mysql]# cat svc.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

Headless Service 给 StatefulSet 控制器为集合中每个 Pod 创建的 DNS 条目提供了一个宿主。因为 Headless Service 名为 mysql,所以可以通过在同一 Kubernetes 集群和 namespace 中的任何其他 Pod 内解析 .mysql来访问 Pod。

客户端 Service 称为 mysql-read,是一种常规 Service,具有其自己的群集 IP,该群集 IP 在报告为就绪的所有MySQL Pod 中分配连接。可能端点的集合包括 MySQL 主节点和所有从节点。

请注意,只有读取查询才能使用负载平衡的客户端 Service。因为只有一个 MySQL 主服务器,所以客户端应直接连接到 MySQL 主服务器 Pod (通过其在 Headless Service 中的 DNS 条目)以执行写入操作。

(3)从以下 YAML 配置文件创建PV

[root@master mysql]# cat pv.yaml 
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-nfs-mysql01
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: slow
  nfs:
    path: /nfs/data/01
    server: 192.168.10.11
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-nfs-mysql02
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: slow
  nfs:
    path: /nfs/data/02
    server: 192.168.10.11
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-nfs-mysql03
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: slow
  nfs:
    path: /nfs/data/03
    server: 192.168.10.11

master写,slave读。NFS映射mysql文件到本地,删除或重启pod或容器不会删除文件。

(4)最后,从以下 YAML 配置文件创建 StatefulSet pod

[root@master mysql]# cat sts.yaml 
# statefulset.yml
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
          # Generate mysql server-id from pod ordinal index.
          [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
          ordinal=${BASH_REMATCH[1]}
          echo [mysqld] > /mnt/conf.d/server-id.cnf
          # Add an offset to avoid reserved server-id=0 value.
          echo server-id=$((100 + $ordinal)) >> /mnt/conf.d/server-id.cnf
          # Copy appropriate conf.d files from config-map to emptyDir.
          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
          # Skip the clone if data already exists.
          [[ -d /var/lib/mysql/mysql ]] && exit 0
          # Skip the clone on master (ordinal index 0).
          [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
          ordinal=${BASH_REMATCH[1]}
          [[ $ordinal -eq 0 ]] && exit 0
          # Clone data from previous peer.
          ncat --recv-only mysql-$(($ordinal-1)).mysql 3307 | xbstream -x -C /var/lib/mysql
          # Prepare the backup.
          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: 512Mi
        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: ist0ne/xtrabackup  #此处镜像我做了修改
        ports:
        - name: xtrabackup
          containerPort: 3307
        command:
        - bash
        - "-c"
        - |
          set -ex
          cd /var/lib/mysql

          # Determine binlog position of cloned data, if any.
          if [[ -f xtrabackup_slave_info && "x$(<xtrabackup_slave_info)" != "x" ]]; then
            # XtraBackup already generated a partial "CHANGE MASTER TO" query
            # because we're cloning from an existing slave. (Need to remove the tailing semicolon!)
            cat xtrabackup_slave_info | sed -E 's/;$//g' > change_master_to.sql.in
            # Ignore xtrabackup_binlog_info in this case (it's useless).
            rm -f xtrabackup_slave_info xtrabackup_binlog_info
          elif [[ -f xtrabackup_binlog_info ]]; then
            # We're cloning directly from master. Parse binlog position.
            [[ `cat xtrabackup_binlog_info` =~ ^(.*?)[[:space:]]+(.*?)$ ]] || exit 1
            rm -f xtrabackup_binlog_info xtrabackup_slave_info
            echo "CHANGE MASTER TO MASTER_LOG_FILE='${BASH_REMATCH[1]}',\
                  MASTER_LOG_POS=${BASH_REMATCH[2]}" > change_master_to.sql.in
          fi

          # Check if we need to complete a clone by starting replication.
          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"
            mysql -h 127.0.0.1 \
                  -e "$(<change_master_to.sql.in), \
                          MASTER_HOST='mysql-0.mysql', \
                          MASTER_USER='root', \
                          MASTER_PASSWORD='', \
                          MASTER_CONNECT_RETRY=10; \
                        START SLAVE;" || exit 1
            # In case of container restart, attempt this at-most-once.
            mv change_master_to.sql.in change_master_to.sql.orig
          fi

          # Start a server to send backups when requested by peers.
          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: 5Gi
      storageClassName: slow
生成配置

在启动 Pod 规约中的任何容器之前,Pod 首先按顺序运行所有的 Init 容器。

第一个名为 init-mysql 的 Init 容器根据序号索引生成特殊的 MySQL 配置文件。

该脚本通过从 Pod 名称的末尾提取索引来确定自己的序号索引,而 Pod 名称由 hostname 命令返回。 然后将序数(带有数字偏移量以避免保留值)保存到 MySQL conf.d 目录中的文件 server-id.cnf。 这一操作将 StatefulSet 所提供的唯一、稳定的标识转换为 MySQL 服务器的 ID, 而这些 ID 也是需要唯一性、稳定性保证的。

通过将内容复制到 conf.d 中,init-mysql 容器中的脚本也可以应用 ConfigMap 中的 primary.cnf 或 replica.cnf。 由于示例部署结构由单个 MySQL 主节点和任意数量的副本节点组成, 因此脚本仅将序数 0 指定为主节点,而将其他所有节点指定为副本节点。

克隆现有数据

通常,当新 Pod 作为副本节点加入集合时,必须假定 MySQL 主节点可能已经有数据。 还必须假设复制日志可能不会一直追溯到时间的开始。

这些保守的假设是允许正在运行的 StatefulSet 随时间扩大和缩小而不是固定在其初始大小的关键。

第二个名为 clone-mysql 的 Init 容器,第一次在带有空 PersistentVolume 的副本 Pod 上启动时,会在从属 Pod 上执行克隆操作。 这意味着它将从另一个运行中的 Pod 复制所有现有数据,使此其本地状态足够一致, 从而可以开始从主服务器复制。

MySQL 本身不提供执行此操作的机制,因此本示例使用了一种流行的开源工具 Percona XtraBackup。 在克隆期间,源 MySQL 服务器性能可能会受到影响。 为了最大程度地减少对 MySQL 主服务器的影响,该脚本指示每个 Pod 从序号较低的 Pod 中克隆。 可以这样做的原因是 StatefulSet 控制器始终确保在启动 Pod N + 1 之前 Pod N 已准备就绪。

开始复制

Init 容器成功完成后,应用容器将运行。 MySQL Pod 由运行实际 mysqld 服务的 mysql 容器和充当 辅助工具 的 xtrabackup 容器组成。

xtrabackup sidecar 容器查看克隆的数据文件,并确定是否有必要在副本服务器上初始化 MySQL 复制。 如果是这样,它将等待 mysqld 准备就绪,然后使用从 XtraBackup 克隆文件中提取的复制参数 执行 CHANGE MASTER TO 和 START SLAVE 命令。

一旦副本服务器开始复制后,它会记住其 MySQL 主服务器,并且如果服务器重新启动或 连接中断也会自动重新连接。 另外,因为副本服务器会以其稳定的 DNS 名称查找主服务器(mysql-0.mysql), 即使由于重新调度而获得新的 Pod IP,它们也会自动找到主服务器。

最后,开始复制后,xtrabackup 容器监听来自其他 Pod 的连接,处理其数据克隆请求。 如果 StatefulSet 扩大规模,或者下一个 Pod 失去其 PersistentVolumeClaim 并需要重新克隆, 则此服务器将无限期保持运行。

一段时间后,您应该看到所有3个 Pod 都开始运行

[root@master mysql]# kubectl get pod
NAME      READY   STATUS    RESTARTS   AGE
mysql-0   2/2     Running   0          7m32s
mysql-1   2/2     Running   0          5m25s
mysql-2   2/2     Running   0          34s

查看pv,pvc的绑定情况

[root@master mysql]# kubectl get pvc,pv
NAME                                 STATUS   VOLUME           CAPACITY   ACCESS MODES   STORAGECLASS   AGE
persistentvolumeclaim/data-mysql-0   Bound    pv-nfs-mysql01   5Gi        RWO            slow           9m9s
persistentvolumeclaim/data-mysql-1   Bound    pv-nfs-mysql02   5Gi        RWO            slow           7m2s
persistentvolumeclaim/data-mysql-2   Bound    pv-nfs-mysql03   5Gi        RWO            slow           2m11s

NAME                              CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                  STORAGECLASS   REASON   AGE
persistentvolume/pv-nfs-mysql01   5Gi        RWO            Retain           Bound    default/data-mysql-0   slow                    9m27s
persistentvolume/pv-nfs-mysql02   5Gi        RWO            Retain           Bound    default/data-mysql-1   slow                    9m27s
persistentvolume/pv-nfs-mysql03   5Gi        RWO            Retain           Bound    default/data-mysql-2   slow                    9m27s

查看service

[root@master mysql]# kubectl get svc
NAME         TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)    AGE
kubernetes   ClusterIP   10.96.0.1     <none>        443/TCP    19h
mysql        ClusterIP   None          <none>        3306/TCP   13h
mysql-read   ClusterIP   10.96.35.19   <none>        3306/TCP   13h

测试

测试用例:

#登录主的mysql
mysql -h mysql-0.mysql  
[root@master mysql]# kubectl run test --image=mysql:5.7 -it -- bash
If you don't see a command prompt, try pressing enter.
root@test:/# mysql -h mysql-0.mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 955
Server version: 5.7.36-log 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                    |
| xtrabackup_backupfiles |
+------------------------+
5 rows in set (0.01 sec)

创建创建数据库

mysql> create database testdb;
Query OK, 1 row affected (0.01 sec)

mysql> show databases;
+------------------------+
| Database               |
+------------------------+
| information_schema     |
| mysql                  |
| performance_schema     |
| sys                    |
| testdb                 |
| xtrabackup_backupfiles |
+------------------------+
6 rows in set (0.00 sec)

分别查看两个从的mysql,都自动复制了testdb数据库

root@test:/# mysql -h mysql-1.mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1248
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                    |
| testdb                 |
| xtrabackup_backupfiles |
+------------------------+
6 rows in set (0.00 sec)
root@test:/# mysql -h mysql-2.mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1244
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                    |
| testdb                 |
| xtrabackup_backupfiles |
+------------------------+
6 rows in set (0.01 sec)
[root@master mysql]# kubectl get pod -o wide
NAME      READY   STATUS    RESTARTS   AGE   IP                NODE    NOMINATED NODE   READINESS GATES
mysql-0   2/2     Running   0          44m   192.168.166.147   node1   <none>           <none>
mysql-1   2/2     Running   0          41m   192.168.104.22    node2   <none>           <none>
mysql-2   2/2     Running   0          37m   192.168.104.23    node2   <none>           <none>
test      1/1     Running   2          16m   192.168.166.148   node1   <none>           <none>

在主mysql中插入数据进行测试

[root@master mysql]# kubectl attach test -it
Defaulting container name to test.
Use 'kubectl describe pod/test -n default' to see all of the containers in this pod.
If you don't see a command prompt, try pressing enter.
root@test:/# mysql -h mysql-0.mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2096
Server version: 5.7.36-log 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                    |
| testdb                 |
| xtrabackup_backupfiles |
+------------------------+
6 rows in set (0.01 sec)

mysql> use testdb
Database changed

mysql> create table people(id int,name varchar(255),age int);
Query OK, 0 rows affected (0.02 sec)

mysql> show tables;
+------------------+
| Tables_in_testdb |
+------------------+
| people           |
+------------------+
1 row in set (0.01 sec)

mysql> insert into people value (1,"linda",26);
Query OK, 1 row affected (0.01 sec)

mysql> select * from people;
+------+-------+------+
| id   | name  | age  |
+------+-------+------+
|    1 | linda |   26 |
+------+-------+------+
1 row in set (0.00 sec)

分别查看两个从的mysql,看其是否复制过来数据

root@test:/# mysql -h mysql-1.mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2158
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                    |
| testdb                 |
| xtrabackup_backupfiles |
+------------------------+
6 rows in set (0.01 sec)

mysql> use testdb
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+------------------+
| Tables_in_testdb |
+------------------+
| people           |
+------------------+
1 row in set (0.00 sec)

mysql> select * from people;
+------+-------+------+
| id   | name  | age  |
+------+-------+------+
|    1 | linda |   26 |
+------+-------+------+
1 row in set (0.00 sec)

root@test:/# mysql -h mysql-2.mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2171
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                    |
| testdb                 |
| xtrabackup_backupfiles |
+------------------------+
6 rows in set (0.01 sec)

mysql> use testdb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+------------------+
| Tables_in_testdb |
+------------------+
| people           |
+------------------+
1 row in set (0.01 sec)

mysql> select * from people
    -> ^C
mysql> select * from people;
+------+-------+------+
| id   | name  | age  |
+------+-------+------+
|    1 | linda |   26 |
+------+-------+------+
1 row in set (0.00 sec)

查看挂在的目录内容

[root@master ~]# cd /nfs/data/
[root@master data]# ll
total 4
drwxr-xr-x 3 root root 19 May 20 10:27 01
drwxr-xr-x 3 root root 19 May 20 10:32 02
drwxr-xr-x 3 root root 19 May 20 10:32 03
-rw-r--r-- 1 root root 56 May 19 20:57 a.txt
[root@master data]# cd 01/
[root@master 01]# ll
total 4
drwxr-xr-x 7 polkitd root 4096 May 20 10:58 mysql
[root@master 01]# cd mysql/
[root@master mysql]# ll
total 191508
-rw-r----- 1 polkitd input       56 May 20 10:27 auto.cnf
-rw------- 1 polkitd input     1680 May 20 10:27 ca-key.pem
-rw-r--r-- 1 polkitd input     1112 May 20 10:27 ca.pem
-rw-r--r-- 1 polkitd input     1112 May 20 10:27 client-cert.pem
-rw------- 1 polkitd input     1680 May 20 10:27 client-key.pem
-rw-r----- 1 polkitd input     1370 May 20 10:27 ib_buffer_pool
-rw-r----- 1 polkitd input 79691776 May 20 11:30 ibdata1
-rw-r----- 1 polkitd input 50331648 May 20 11:30 ib_logfile0
-rw-r----- 1 polkitd input 50331648 May 20 10:27 ib_logfile1
-rw-r----- 1 polkitd input 12582912 May 20 10:28 ibtmp1
drwxr-x--- 2 polkitd input     4096 May 20 10:27 mysql
-rw-r----- 1 polkitd input      177 May 20 10:27 mysql-0-bin.000001
-rw-r----- 1 polkitd input  3084237 May 20 10:27 mysql-0-bin.000002
-rw-r----- 1 polkitd input      790 May 20 11:30 mysql-0-bin.000003
-rw-r----- 1 polkitd input       63 May 20 10:27 mysql-0-bin.index
drwxr-x--- 2 polkitd input     8192 May 20 10:27 performance_schema
-rw------- 1 polkitd input     1680 May 20 10:27 private_key.pem
-rw-r--r-- 1 polkitd input      452 May 20 10:27 public_key.pem
-rw-r--r-- 1 polkitd input     1112 May 20 10:27 server-cert.pem
-rw------- 1 polkitd input     1680 May 20 10:27 server-key.pem
drwxr-x--- 2 polkitd input     8192 May 20 10:27 sys
drwxr-x--- 2 polkitd input       56 May 20 11:29 testdb
drwxr-x--- 2 root    root         6 May 20 10:32 xtrabackup_backupfiles
[root@master mysql]# cd testdb/
[root@master testdb]# ll
total 112
-rw-r----- 1 polkitd input    65 May 20 10:58 db.opt
-rw-r----- 1 polkitd input  8614 May 20 11:29 people.frm
-rw-r----- 1 polkitd input 98304 May 20 11:30 people.ibd

[root@master data]# cd 02/
[root@master 02]# ll
total 4
drwxr-xr-x 7 polkitd root 4096 May 20 10:58 mysql
[root@master 02]# cd mysql/
[root@master mysql]# ll
total 196716
-rw-r----- 1 polkitd input       56 May 20 10:32 auto.cnf
-rw-r----- 1 polkitd root       427 May 20 10:32 backup-my.cnf
-rw------- 1 polkitd input     1676 May 20 10:32 ca-key.pem
-rw-r--r-- 1 polkitd input     1112 May 20 10:32 ca.pem
-rw-r--r-- 1 root    root        81 May 20 10:32 change_master_to.sql.orig
-rw-r--r-- 1 polkitd input     1112 May 20 10:32 client-cert.pem
-rw------- 1 polkitd input     1680 May 20 10:32 client-key.pem
-rw-r----- 1 polkitd root      1370 May 20 10:32 ib_buffer_pool
-rw-r----- 1 polkitd root  79691776 May 20 11:30 ibdata1
-rw-r----- 1 polkitd root  50331648 May 20 11:30 ib_logfile0
-rw-r----- 1 polkitd root  50331648 May 20 10:32 ib_logfile1
-rw-r----- 1 polkitd input 12582912 May 20 10:32 ibtmp1
-rw-r----- 1 polkitd input      122 May 20 11:35 master.info
drwxr-x--- 2 polkitd root      4096 May 20 10:32 mysql
-rw-r----- 1 polkitd input      209 May 20 10:32 mysql-1-relay-bin.000001
-rw-r----- 1 polkitd input      958 May 20 11:30 mysql-1-relay-bin.000002
-rw-r----- 1 polkitd input       54 May 20 10:32 mysql-1-relay-bin.index
drwxr-x--- 2 polkitd root      8192 May 20 10:32 performance_schema
-rw------- 1 polkitd input     1680 May 20 10:32 private_key.pem
-rw-r--r-- 1 polkitd input      452 May 20 10:32 public_key.pem
-rw-r----- 1 polkitd input       63 May 20 11:30 relay-log.info
-rw-r--r-- 1 polkitd input     1112 May 20 10:32 server-cert.pem
-rw------- 1 polkitd input     1680 May 20 10:32 server-key.pem
drwxr-x--- 2 polkitd root      8192 May 20 10:32 sys
drwxr-x--- 2 polkitd input       56 May 20 11:29 testdb
drwxr-x--- 2 polkitd root        20 May 20 10:32 xtrabackup_backupfiles
-rw-r--r-- 1 polkitd root        27 May 20 10:32 xtrabackup_binlog_pos_innodb
-rw-r----- 1 polkitd root       115 May 20 10:32 xtrabackup_checkpoints
-rw-r----- 1 polkitd root       502 May 20 10:32 xtrabackup_info
-rw-r----- 1 polkitd root   8388608 May 20 10:32 xtrabackup_logfile
[root@master mysql]# cd testdb/
[root@master testdb]# ll
total 112
-rw-r----- 1 polkitd input    65 May 20 10:58 db.opt
-rw-r----- 1 polkitd input  8614 May 20 11:29 people.frm
-rw-r----- 1 polkitd input 98304 May 20 11:30 people.ibd

从的mysql只能读数据,而不能写入数据

[root@master ~]# kubectl attach test -it
Defaulting container name to test.
Use 'kubectl describe pod/test -n default' to see all of the containers in this pod.
If you don't see a command prompt, try pressing enter.
root@test:/# mysql -h mysql-1.mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2381
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                    |
| testdb                 |
| xtrabackup_backupfiles |
+------------------------+
6 rows in set (0.01 sec)

mysql> use testdb
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> insert into value(2,"jacson",22);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '2,"jacson",22)' at line 1
[root@master mysql]# kubectl get svc
NAME         TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)    AGE
kubernetes   ClusterIP   10.96.0.1     <none>        443/TCP    19h
mysql        ClusterIP   None          <none>        3306/TCP   13h
mysql-read   ClusterIP   10.96.35.19   <none>        3306/TCP   13h

可通过mysql -h 10.96.35.19或者mysql -h mysql-read进入任意一个mysql中,其是随机选择一个mysql来进行操作。

验证这一说法:

[root@master 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()               |
+-------------+---------------------+
|         100 | 2022-05-20 15:36:00 |
+-------------+---------------------+
+-------------+---------------------+
| @@server_id | NOW()               |
+-------------+---------------------+
|         102 | 2022-05-20 15:36:01 |
+-------------+---------------------+
+-------------+---------------------+
| @@server_id | NOW()               |
+-------------+---------------------+
|         101 | 2022-05-20 15:36:02 |
+-------------+---------------------+
^C
pod "mysql-client-loop" deleted
pod default/mysql-client-loop terminated (Error)

随机选取到mysql-1,因此不发完成插入数据操作

[root@master ~]# kubectl attach test -it
Defaulting container name to test.
Use 'kubectl describe pod/test -n default' to see all of the containers in this pod.
If you don't see a command prompt, try pressing enter.
root@test:/# mysql -h mysql-read
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 52148
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> SELECT @@server_id;
+-------------+
| @@server_id |
+-------------+
|         101 |
+-------------+
1 row in set (0.00 sec)

mysql> use testdb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> insert into table people value(4,"lili",28);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'table people value(4,"lili",28)' at line 1

[root@master mysql]# kubectl attach test -it
Defaulting container name to test.
Use 'kubectl describe pod/test -n default' to see all of the containers in this pod.
If you don't see a command prompt, try pressing enter.
root@test:/# mysql -h 10.96.35.19
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1131
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                    |
| testdb                 |
| xtrabackup_backupfiles |
+------------------------+
6 rows in set (0.00 sec)
[root@master mysql]# kubectl run mysql-client --image=mysql:5.7 -i -t --rm --restart=Never --  mysql -h mysql-read -e "SELECT * FROM testdb.people"
+------+--------+------+
| id   | name   | age  |
+------+--------+------+
|    1 | linda  |   26 |
|    2 | jacson |   22 |
|    3 | admin  |   24 |
+------+--------+------+
pod "mysql-client" deleted
模拟 Pod 和 Node 的宕机时间
破坏就绪态探测

为了证明从副本节点缓存而不是单个服务器读取数据的可用性提高,请在使 Pod 退出 Ready 状态时,保持上述 SELECT @@server_id 循环一直运行。

迫使就绪态探测失败的一种方法就是中止该命令:

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

此命令会进入 Pod mysql-2 的实际容器文件系统,重命名 mysql 命令,导致就绪态探测无法找到它。 几秒钟后, Pod 会报告其中一个容器未就绪。你可以通过运行以下命令进行检查:

此时,你应该会看到 SELECT @@server_id 循环继续运行,尽管它不再报告 102。 回想一下,init-mysql 脚本将 server-id 定义为 100 + $ordinal, 因此服务器 ID 102 对应于 Pod mysql-2。

[root@master mysql]# kubectl exec mysql-2 -c mysql -- mv /usr/bin/mysql /usr/bin/mysql.off
[root@master mysql]# kubectl get pod
NAME      READY   STATUS    RESTARTS   AGE
mysql-0   2/2     Running   0          13h
mysql-1   2/2     Running   0          13h
mysql-2   1/2     Running   0          13h
test      1/1     Running   6          12h
[root@master 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()               |
+-------------+---------------------+
|         100 | 2022-05-20 15:51:58 |
+-------------+---------------------+
+-------------+---------------------+
| @@server_id | NOW()               |
+-------------+---------------------+
|         100 | 2022-05-20 15:51:59 |
+-------------+---------------------+
+-------------+---------------------+
| @@server_id | NOW()               |
+-------------+---------------------+
|         101 | 2022-05-20 15:52:00 |
+-------------+---------------------+

现在修复 Pod,几秒钟后它应该重新出现在循环输出中:

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

如果删除了 Pod,则 StatefulSet 还会重新创建 Pod,类似于 ReplicaSet 对无状态 Pod 所做的操作。

kubectl delete pod mysql-2

StatefulSet 控制器注意到不再存在 mysql-2 Pod,于是创建一个具有相同名称并链接到相同 PersistentVolumeClaim 的新 Pod。 你应该看到服务器 ID 102 从循环输出中消失了一段时间,然后又自行出现。

本实例参考官方文档:链接

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值