kubernetes的有状态服务statefulset实现MySQL主从复制案

 

 

 

client --> headless serivce name --> Pod-0,pod-1,
client --> serivce name --> clusterIP --> Pod-0,pod-1,

 

MySQL读写分离

传统方式:MyCAT,proxy

K8s读写分离:statefulset,headless,coredns

mysql-0.mysql.default.svc.clsuter.local


mysql.default.svc.clsuter.local
mysql-read.default.svc.clsuter.local


mysql-0.mysql.default.svc.clsuter.local
mysql-1.mysql.default.svc.clsuter.local
mysql-2.mysql.default.svc.clsuter.local

 

 

 

案例:MySQL 主从复制集群

 

https://kubernetes.io/zh-cn/docs/tasks/run-application/run-replicated-statefulapplication/

image

 

 

image

 

准备 NFS 服务

 

配置动态置备的SC

 

准备基于NFS的 StorageClass

创建 NFS 服务

#NFS服务器软件安装

# 安装 nfs-utils(包含 nfs-server 服务)
dnf install -y nfs-utils

# 启动并设置开机自启
systemctl enable --now nfs-server

# 验证状态
systemctl status nfs-server

 

mkdir -p /data/sc-nfs/

 #授权worker节点的网段可以挂载

[root@master1 ~]# cat /etc/exports
#/data/nfsshare  192.168.3.0/24(rw,sync,no_root_squash)
#/nfsdata *(rw,no_root_squash)
/data/sc-nfs *(rw,no_root_squash) 

 

[root@master1 ~]# exportfs -r
[root@master1 ~]# exportfs -v
/data/sc-nfs    <world>(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,no_root_squash,no_all_squash)

 

#并在所有worker节点安装NFS客户端

 dnf install -y nfs-utils

 

#在所有节点节点添加nfs..org的域名解析指定NFS服务器的地址

[root@node1 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.3.60 kubeapi.org kubeapi
192.168.3.60 master1.org master1
192.168.3.60 nfs.org master1
192.168.3.61 master2.org master2
192.168.3.62 master3.org master3
192.168.3.63 node1.org node1
192.168.3.64 node2.org node2
192.168.3.65 node3.org node3
192.168.3.66 ha1.org ha1
192.168.3.67 ha2.org ha2

 

创建 ServiceAccount 并授权

#建议创建独立的名称空间

kubectl create ns sc-nfs

[root@master1 sc-nfs]# cat rbac.yaml 
apiVersion: v1
kind: ServiceAccount
metadata:
  name: nfs-client-provisioner
  # replace with namespace where provisioner is deployed
  #namespace: default
  namespace: sc-nfs
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: nfs-client-provisioner-runner
rules:
  - apiGroups: [""]
    resources: ["nodes"]
    verbs: ["get", "list", "watch"]
  - apiGroups: [""]
    resources: ["persistentvolumes"]
    verbs: ["get", "list", "watch", "create", "delete"]
  - apiGroups: [""]
    resources: ["persistentvolumeclaims"]
    verbs: ["get", "list", "watch", "update"]
  - apiGroups: ["storage.k8s.io"]
    resources: ["storageclasses"]
    verbs: ["get", "list", "watch"]
  - apiGroups: [""]
    resources: ["events"]
    verbs: ["create", "update", "patch"]
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: run-nfs-client-provisioner
subjects:
  - kind: ServiceAccount
    name: nfs-client-provisioner
    # replace with namespace where provisioner is deployed
    #namespace: default
    namespace: sc-nfs
roleRef:
  kind: ClusterRole
  name: nfs-client-provisioner-runner
  apiGroup: rbac.authorization.k8s.io
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: leader-locking-nfs-client-provisioner
  # replace with namespace where provisioner is deployed
  #namespace: default
  namespace: sc-nfs
rules:
  - apiGroups: [""]
    resources: ["endpoints"]
    verbs: ["get", "list", "watch", "create", "update", "patch"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: leader-locking-nfs-client-provisioner
  # replace with namespace where provisioner is deployed
  #namespace: default
  namespace: sc-nfs
subjects:
  - kind: ServiceAccount
    name: nfs-client-provisioner
    # replace with namespace where provisioner is deployed
    #namespace: default
    namespace: sc-nfs
roleRef:
  kind: Role
  name: leader-locking-nfs-client-provisioner
  apiGroup: rbac.authorization.k8s.io

 

kubectl apply -f rbac.yaml

[root@master1 sc-nfs-1]# kubectl get sa -n sc-nfs
NAME                     AGE
default                  3d8h
nfs-client-provisioner   3d8h

 

部署 NFS-Subdir-External-Provisioner 对应的 Deployment

[root@master1 sc-nfs-1]# cat nfs-client-provisioner.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nfs-client-provisioner
  labels:
    app: nfs-client-provisioner
  # replace with namespace where provisioner is deployed
  #namespace: default
  namespace: sc-nfs
spec:
  replicas: 1
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: nfs-client-provisioner
  template:
    metadata:
      labels:
        app: nfs-client-provisioner
    spec:
      serviceAccountName: nfs-client-provisioner
      containers:
        - name: nfs-client-provisioner
          image: registry.cn-beijing.aliyuncs.com/wangxiaochun/nfs-subdir-external-provisioner:v4.0.2
          #image: wangxiaochun/nfs-subdir-external-provisioner:v4.0.2
          #image: k8s.gcr.io/sig-storage/nfs-subdir-external-provisioner:v4.0.2
          imagePullPolicy: IfNotPresent
          volumeMounts:
            - name: nfs-client-root
              mountPath: /persistentvolumes
          env:
            - name: PROVISIONER_NAME
              value: k8s-sigs.io/nfs-subdir-external-provisioner #名称确保与 nfs-StorageClass.yaml文件中的provisioner名称保持一致
            - name: NFS_SERVER
              value: nfs.org # NFS SERVER_IP 
            - name: NFS_PATH
              value: /data/sc-nfs  # NFS 共享目录
      volumes:
        - name: nfs-client-root
          nfs:
            server: nfs.org  # NFS SERVER_IP 
            path: /data/sc-nfs  # NFS 共享目录

 

通过第三方软件管理实现nfs动态置备功能

需要设置环境变量

通过第三方软件管理nfs服务器 /data/sc-nfs下的子目录 每个子目录自动创建对应pv

kubectl apply -f nfs-client-provisioner.yaml

[root@master1 sc-nfs]# kubectl get pod -n sc-nfs -o wide
NAME                                     READY   STATUS    RESTARTS   AGE   IP             NODE        NOMINATED NODE   READINESS GATES
nfs-client-provisioner-d5dc647db-w8gxw   1/1     Running   0          28s   10.244.2.187   node2.org   <none>           <none>

 

#注意:如果失败,检查是否worker节点安装了nfs-client

 

创建 NFS 资源的 StorageClass

StorageClass 作用声明使用第三方置备软件管理pv

[root@master1 sc-nfs-1]# cat nfs-StorageClass.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: sc-nfs 
  annotations:
    storageclass.kubernetes.io/is-default-class: "true"  # 是否设置为默认的storageclass
provisioner: k8s-sigs.io/nfs-subdir-external-provisioner # or choose another name, must match deployment's env PROVISIONER_NAME'
parameters:
  archiveOnDelete: "true" # 设置为"false"时删除PVC不会保留数据,"true"则保留数据

 

#应用

kubectl apply -f nfs-StorageClass.yaml 

#查看是否创建

[root@master1 sc-nfs-1]# kubectl get sc -n sc-nfs 
NAME               PROVISIONER                                   RECLAIMPOLICY   VOLUMEBINDINGMODE   ALLOWVOLUMEEXPANSION   AGE
sc-nfs (default)   k8s-sigs.io/nfs-subdir-external-provisioner   Delete          Immediate           false                  3d8h

#提前准备名为sc-nfs的storage Class

 

创建 ConfigMap

#https://k8s.io/examples/application/mysql/mysql-configmap.yaml
#MySQL的配置

apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql
  labels:
    app: mysql
    app.kubernetes.io/name: mysql
data:
  primary.cnf: |
    # Apply this config only on the primary.
    [mysqld]
    log-bin
  replica.cnf: |
    # Apply this config only on replicas.
    [mysqld]
    super-read-only

kubectl apply -f statefulset-mysql-configmap.yaml

 

创建 Service

# https://k8s.io/examples/application/mysql/mysql-services.yaml
# 为 StatefulSet 成员提供稳定的 DNS 表项的无头服务(Headless Service)
# 主节点的对应的Service

 

# 用于连接到任一 MySQL 实例执行读操作的客户端服务
# 对于写操作,必须连接到主服务器:mysql-0.mysql
# 从节点的对应的Service,注意:此处无需无头服务(Headless Service)
# 下面的service可以不创建,直接使用无头服务mysql也可以

[root@master1 sc-nfs-mysql]# cat statefulset-mysql-svc.yaml
# Headless service for stable DNS entries of StatefulSet members.
apiVersion: v1
kind: Service
metadata:
  name: mysql
  labels:
    app: mysql
    app.kubernetes.io/name: mysql
spec:
  ports:
  - name: mysql
    port: 3306
  clusterIP: None
  selector:
    app: mysql
---
# Client service for connecting to any MySQL instance for reads.
# For writes, you must instead connect to the primary: mysql-0.mysql.
apiVersion: v1
kind: Service
metadata:
  name: mysql-read
  labels:
    app: mysql
    app.kubernetes.io/name: mysql
    readonly: "true"
spec:
  ports:
  - name: mysql
    port: 3306
  selector:
    app: mysql

kubectl apply -f statefulset-mysql-svc.yaml 

 

[root@master1 sc-nfs-mysql]# kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP    18d
mysql        ClusterIP   None             <none>        3306/TCP   14s
mysql-read   ClusterIP   10.102.236.133   <none>        3306/TCP   14s

 

 创建 statefulset

[root@master1 sc-nfs-mysql]# cat statefulset-mysql-statefulset.yaml
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql
spec:
  selector:
    matchLabels:
      app: mysql
      app.kubernetes.io/name: mysql
  serviceName: mysql
  replicas: 3
  template:
    metadata:
      labels:
        app: mysql
        app.kubernetes.io/name: mysql
    spec:
      initContainers:
      - name: init-mysql
        #image: mysql:5.7
        image: registry.cn-beijing.aliyuncs.com/wangxiaochun/mysql:5.7
        command:
        - bash
        - "-c"
        - |
          set -ex
          # 基于 Pod 序号生成 MySQL 服务器的 ID。
          [[ $HOSTNAME =~ -([0-9]+)$ ]] || exit 1
          ordinal=${BASH_REMATCH[1]} #BASH_REMATCH是bash内置数组变量,存放从前面的=~正则表达式匹配的分组的结果,BASH_REMATCH[1]存放第1个分组
          echo [mysqld] > /mnt/conf.d/server-id.cnf
          # 添加偏移量以避免使用 server-id=0 这一保留值。
          echo server-id=$((100 + $ordinal)) >> /mnt/conf.d/server-id.cnf
          # 将合适的 conf.d 文件从 config-map 复制到 emptyDir。
          if [[ $ordinal -eq 0 ]]; then
            cp /mnt/config-map/primary.cnf /mnt/conf.d/
          else
            cp /mnt/config-map/replica.cnf /mnt/conf.d/
          fi          
        volumeMounts:
        - name: conf
          mountPath: /mnt/conf.d
        - name: config-map
          mountPath: /mnt/config-map
      - name: clone-mysql
        #image: gcr.io/google-samples/xtrabackup:1.0
        image: registry.cn-beijing.aliyuncs.com/wangxiaochun/xtrabackup:1.0
        command:
        - bash
        - "-c"
        - |
          set -ex
          # 如果已有数据,则跳过克隆。
          [[ -d /var/lib/mysql/mysql ]] && exit 0
          # 跳过主实例(序号索引 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
        image: registry.cn-beijing.aliyuncs.com/wangxiaochun/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:
            # 检查我们是否可以通过 TCP 执行查询(skip-networking 是关闭的)。
            command: ["mysql", "-h", "127.0.0.1", "-e", "SELECT 1"]
          initialDelaySeconds: 5
          periodSeconds: 2
          timeoutSeconds: 1
      - name: xtrabackup
        #image: gcr.io/google-samples/xtrabackup:1.0
        image: registry.cn-beijing.aliyuncs.com/wangxiaochun/xtrabackup:1.0
        ports:
        - name: xtrabackup
          containerPort: 3307
        command:
        - bash
        - "-c"
        - |
          set -ex
          cd /var/lib/mysql

          # 确定克隆数据的 binlog 位置(如果有的话)。
          if [[ -f xtrabackup_slave_info && "x$(<xtrabackup_slave_info)" != "x" ]]; then
            # XtraBackup 已经生成了部分的 “CHANGE MASTER TO” 查询
            # 因为从一个现有副本进行克隆。(需要删除末尾的分号!)
            cat xtrabackup_slave_info | sed -E 's/;$//g' > change_master_to.sql.in
            # 在这里要忽略 xtrabackup_binlog_info (它是没用的)。
            rm -f xtrabackup_slave_info xtrabackup_binlog_info
          elif [[ -f xtrabackup_binlog_info ]]; then
            # 直接从主实例进行克隆。解析 binlog 位置。
            [[ `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

          # 检查是否需要通过启动复制来完成克隆。
          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
            # 如果容器重新启动,最多尝试一次。
            mv change_master_to.sql.in change_master_to.sql.orig
          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"]
      storageClassName: "sc-nfs"  #如果使用StorageClass,启用此行
      resources:
        requests:
          storage: 10Gi

 

kubectl apply -f statefulset-mysql-statefulset.yaml

[root@master1 sc-nfs-mysql]# kubectl get pod -o wide
NAME           READY   STATUS         RESTARTS       AGE    IP            NODE        NOMINATED NODE   READINESS GATES
mysql-0        2/2     Running        0              16m    10.244.2.40   node2.org   <none>           <none>
mysql-1        2/2     Running        1 (14m ago)    15m    10.244.1.44   node1.org   <none>           <none>
mysql-2        2/2     Running        1 (113s ago)   3m6s   10.244.3.68   node3.org   <none>           <none>

 

验证

#创建一个测试Pod

kubectl run mysql-client --rm --tty -i --restart='Never' \
--image registry.cn-beijing.aliyuncs.com/wangxiaochun/mysql:8.0.29-oracle \
--namespace default \
--command -- bash

 

进入主节点

[root@master1 ~]# kubectl run mysql-client --rm --tty -i --restart='Never' \
> --image registry.cn-beijing.aliyuncs.com/wangxiaochun/mysql:8.0.29-oracle \
> --namespace default \
> --command -- bash
All commands and output from this session will be recorded in container logs, including credentials and sensitive information passed through the command prompt.
If you don't see a command prompt, try pressing enter.
bash-4.4# 
bash-4.4# 

 

有两个dump线程

bash-4.4# mysql -uroot -hmysql-0.mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 811
Server version: 5.7.13-log MySQL Community Server (GPL)

Copyright (c) 2000, 2022, 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 processlist;
+-----+------+-------------------+------+-------------+------+---------------------------------------------------------------+------------------+
| Id  | User | Host              | db   | Command     | Time | State                                                         | Info             |
+-----+------+-------------------+------+-------------+------+---------------------------------------------------------------+------------------+
|  52 | root | 10.244.1.44:58298 | NULL | Binlog Dump | 1275 | Master has sent all binlog to slave; waiting for more updates | NULL             |
| 499 | root | 10.244.3.68:60176 | NULL | Binlog Dump |  530 | Master has sent all binlog to slave; waiting for more updates | NULL             |
| 811 | root | 10.244.1.45:55210 | NULL | Query       |    0 | starting                                                      | show processlist |
+-----+------+-------------------+------+-------------+------+---------------------------------------------------------------+------------------+
3 rows in set (0.01 sec)

mysql> 

两个dump 从节点ip

[root@master1 sc-nfs-mysql]# kubectl get pod -o wide
NAME           READY   STATUS    RESTARTS        AGE     IP            NODE        NOMINATED NODE   READINESS GATES
mysql-0        2/2     Running   0               24m     10.244.2.40   node2.org   <none>           <none>
mysql-1        2/2     Running   1 (22m ago)     23m     10.244.1.44   node1.org   <none>           <none>
mysql-2        2/2     Running   1 (9m37s ago)   10m     10.244.3.68   node3.org   <none>           <none>
mysql-client   1/1     Running   0               2m59s   10.244.1.45   node1.org   <none>           <none>

 

mysql> show master logs;
+--------------------+-----------+
| Log_name           | File_size |
+--------------------+-----------+
| mysql-0-bin.000001 |     99474 |
| mysql-0-bin.000002 |   2995461 |
| mysql-0-bin.000003 |       154 |
+--------------------+-----------+
3 rows in set (0.00 sec)

 

进入从节点

#验证主从关系

bash-4.4# mysql -uroot -hmysql-1.mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 897
Server version: 5.7.13 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, 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> 
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: mysql-0.mysql
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 10
              Master_Log_File: mysql-0-bin.000003
          Read_Master_Log_Pos: 154
               Relay_Log_File: mysql-1-relay-bin.000002
                Relay_Log_Pos: 322
        Relay_Master_Log_File: mysql-0-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 154
              Relay_Log_Space: 531
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 100
                  Master_UUID: 24c39922-5a89-11f1-a274-4250b4c8694e
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

ERROR: 
No query specified

 

bash-4.4# mysql -uroot -hmysql-2.mysql 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 495
Server version: 5.7.13 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, 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 slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: mysql-0.mysql
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 10
              Master_Log_File: mysql-0-bin.000003
          Read_Master_Log_Pos: 154
               Relay_Log_File: mysql-2-relay-bin.000002
                Relay_Log_Pos: 322
        Relay_Master_Log_File: mysql-0-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 154
              Relay_Log_Space: 531
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 100
                  Master_UUID: 24c39922-5a89-11f1-a274-4250b4c8694e
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

ERROR: 
No query specified

 

进入主节点创建数据

bash-4.4# mysql -uroot -hmysql-0.mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1062
Server version: 5.7.13-log MySQL Community Server (GPL)

Copyright (c) 2000, 2022, 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> 
mysql> show databases;
+------------------------+
| Database               |
+------------------------+
| information_schema     |
| mysql                  |
| performance_schema     |
| sys                    |
| xtrabackup_backupfiles |
+------------------------+
5 rows in set (0.00 sec)

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

mysql> use db22;
Database changed
mysql> create table students(id int,name char(10));
Query OK, 0 rows affected (0.02 sec)

mysql> insert students values(1,'tom');
Query OK, 1 row affected (0.02 sec)

mysql> insert students values(2,'jack'); 
Query OK, 1 row affected (0.00 sec)

主从同步

bash-4.4# mysql -uroot -hmysql-1.mysql 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1120
Server version: 5.7.13 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, 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> 
mysql> select * from db22.students;
+------+------+
| id   | name |
+------+------+
|    1 | tom  |
|    2 | jack |
+------+------+
2 rows in set (0.00 sec)

 

用无头服务service 读数据

bash-4.4# mysql -uroot -hmysql.default.svc.cluster.local -e 'select * from db22.students;'
+------+------+
| id   | name |
+------+------+
|    1 | tom  |
|    2 | jack |
+------+------+

 

[root@master1 sc-nfs-mysql]# kubectl get ep mysql
Warning: v1 Endpoints is deprecated in v1.33+; use discovery.k8s.io/v1 EndpointSlice
NAME    ENDPOINTS                                            AGE
mysql   10.244.1.44:3306,10.244.2.40:3306,10.244.3.68:3306   38m

 

用有头服务 mysql-read service 可以读

bash-4.4# mysql -uroot -hmysql-read.default.svc.cluster.local -e 'select * from db22.students;'
+------+------+
| id   | name |
+------+------+
|    1 | tom  |
|    2 | jack |
+------+------+
bash-4.4# 

 

读3个pod 轮询了 随机调度到后端的pod上

bash-4.4# mysql -uroot -hmysql-read.default.svc.cluster.local -e 'select * from db22.students;select @@hostname;' 
+------+------+
| id   | name |
+------+------+
|    1 | tom  |
|    2 | jack |
+------+------+
+------------+
| @@hostname |
+------------+
| mysql-2    |
+------------+
bash-4.4# 
bash-4.4# mysql -uroot -hmysql-read.default.svc.cluster.local -e 'select * from db22.students;select @@hostname;'
+------+------+
| id   | name |
+------+------+
|    1 | tom  |
|    2 | jack |
+------+------+
+------------+
| @@hostname |
+------------+
| mysql-0    |
+------------+
bash-4.4# mysql -uroot -hmysql-read.default.svc.cluster.local -e 'select * from db22.students;select @@hostname;'
+------+------+
| id   | name |
+------+------+
|    1 | tom  |
|    2 | jack |
+------+------+
+------------+
| @@hostname |
+------------+
| mysql-1    |
+------------+
bash-4.4# mysql -uroot -hmysql-read.default.svc.cluster.local -e 'select * from db22.students;select @@hostname;'
+------+------+
| id   | name |
+------+------+
|    1 | tom  |
|    2 | jack |
+------+------+
+------------+
| @@hostname |
+------------+
| mysql-2    |
+------------+

 

 

无头服务 使用mysql service  会 会话粘上

bash-4.4# 
bash-4.4# bash-4.4# mysql -uroot -hmysql-read.default.svc.cluster.local -e 'select * from db22.students;select @@hostname;'
bash: bash-4.4#: command not found
bash-4.4# 
bash-4.4# 
bash-4.4# mysql -uroot -hmysql.default.svc.cluster.local -e 'select * from db22.students;select @@hostname;'               
+------+------+
| id   | name |
+------+------+
|    1 | tom  |
|    2 | jack |
+------+------+
+------------+
| @@hostname |
+------------+
| mysql-1    |
+------------+
bash-4.4# mysql -uroot -hmysql.default.svc.cluster.local -e 'select * from db22.students;select @@hostname;'
+------+------+
| id   | name |
+------+------+
|    1 | tom  |
|    2 | jack |
+------+------+
+------------+
| @@hostname |
+------------+
| mysql-1    |
+------------+
bash-4.4# mysql -uroot -hmysql.default.svc.cluster.local -e 'select * from db22.students;select @@hostname;'
+------+------+
| id   | name |
+------+------+
|    1 | tom  |
|    2 | jack |
+------+------+
+------------+
| @@hostname |
+------------+
| mysql-1    |
+------------+
bash-4.4# mysql -uroot -hmysql.default.svc.cluster.local -e 'select * from db22.students;select @@hostname;'
+------+------+
| id   | name |
+------+------+
|    1 | tom  |
|    2 | jack |
+------+------+
+------------+
| @@hostname |
+------------+
| mysql-1    |
+------------+

 

 

 

posted @ 2026-05-28 20:19  minger_lcm  阅读(9)  评论(0)    收藏  举报