Pod调度运⾏时,如果应⽤不需要任何稳定的标示、有序的部署、删除和扩展,则应该使⽤⼀组⽆状态副本的控制器来部署应⽤,例如 Deployment 或 ReplicaSet更适合⽆状态服务需求,⽽StatefulSet适合管理所有有状态的服务,⽐如MySQL、MongoDB集群等。
2、StatefulSet控制器运行MySQL一主多从架构图2.1、StatefulSet控制器特点给每个pod分配固定且唯⼀的⽹络标识符给每个pod分配固定且持久化的外部存储对pod进⾏有序的部署和扩展对pod进有序的删除和终⽌对pod进有序的⾃动滚动更新2.2、StatefulSet的组成部分Headless Service:⽤来定义Pod⽹络标识( DNS domain),指的是短的service(丢失了domainname)。StatefulSet:定义具体应⽤,有多少个Pod副本,并为每个Pod定义了⼀个域名。volumeClaimTemplates: 存储卷申请模板,创建PVC,指定pvc名称⼤⼩,将⾃动创建pvc,且pvc必须由存储类供应。3、在k8s上部署mysql一主多从3.1、基础镜像准备3.1.1、准备mysql镜像StatefulSet本质上是Deployment的⼀种变体,在v1.9版本中已成为GA版本,它为了解决有状态服务的问题,它所管理的Pod拥有固定的Pod名称,启停顺序,在StatefulSet中,Pod名字称为⽹络标识(hostname),还必须要⽤到共享存储。在Deployment中,与之对应的服务是service,⽽在StatefulSet中与之对应的headless service,headless service,即⽆头服务,与service的区别就是它没有Cluster IP,解析它的名称时将返回该Headless Service对应的全部Pod的Endpoint列表。
下载镜像
【资料图】
root@k8s-master01:~# nerdctl pull mysql:5.7.36
修改镜像tag为本地harbor地址
root@k8s-master01:~# nerdctl tag mysql:5.7.36 harbor.ik8s.cc/magedu/mysql:5.7.36
上传镜像至本地harbor
root@k8s-master01:~# nerdctl push harbor.ik8s.cc/magedu/mysql:5.7.36
3.1.2、准备xtrabackup镜像下载镜像
root@k8s-master01:~# nerdctl pull registry.cn-hangzhou.aliyuncs.com/hxpdocker/xtrabackup:1.0
修改镜像tag为本地harbor地址
root@k8s-master01:~# nerdctl tag registry.cn-hangzhou.aliyuncs.com/hxpdocker/xtrabackup:1.0 harbor.ik8s.cc/magedu/xtrabackup:1.0
上传镜像至本地harbor
root@k8s-master01:~# nerdctl push harbor.ik8s.cc/magedu/xtrabackup:1.0
3.1.3、验证镜像是否上传至harbor?3.2、准备pv3.2.1、在nfs服务器上创建数据存储目录root@harbor:~# mkdir -pv /data/k8sdata/magedu/mysql-datadir-{1..5}mkdir: created directory "/data/k8sdata/magedu/mysql-datadir-1"mkdir: created directory "/data/k8sdata/magedu/mysql-datadir-2"mkdir: created directory "/data/k8sdata/magedu/mysql-datadir-3"mkdir: created directory "/data/k8sdata/magedu/mysql-datadir-4"mkdir: created directory "/data/k8sdata/magedu/mysql-datadir-5"root@harbor:~#
3.2.2、在nfs服务器上导出数据存储目录root@harbor:/data/k8sdata/magedu# cat /etc/exports # /etc/exports: the access control list for filesystems which may be exported# to NFS clients. See exports(5).## Example for NFSv2 and NFSv3:# /srv/homes hostname1(rw,sync,no_subtree_check) hostname2(ro,sync,no_subtree_check)## Example for NFSv4:# /srv/nfs4 gss/krb5i(rw,sync,fsid=0,crossmnt,no_subtree_check)# /srv/nfs4/homes gss/krb5i(rw,sync,no_subtree_check)#/data/k8sdata/kuboard *(rw,no_root_squash)/data/volumes *(rw,no_root_squash)/pod-vol *(rw,no_root_squash)/data/k8sdata/myserver *(rw,no_root_squash)/data/k8sdata/mysite *(rw,no_root_squash)/data/k8sdata/magedu/images *(rw,no_root_squash)/data/k8sdata/magedu/static *(rw,no_root_squash)/data/k8sdata/magedu/zookeeper-datadir-1 *(rw,no_root_squash)/data/k8sdata/magedu/zookeeper-datadir-2 *(rw,no_root_squash)/data/k8sdata/magedu/zookeeper-datadir-3 *(rw,no_root_squash)/data/k8sdata/magedu/redis-datadir-1 *(rw,no_root_squash) /data/k8sdata/magedu/redis0 *(rw,no_root_squash)/data/k8sdata/magedu/redis1 *(rw,no_root_squash)/data/k8sdata/magedu/redis2 *(rw,no_root_squash)/data/k8sdata/magedu/redis3 *(rw,no_root_squash)/data/k8sdata/magedu/redis4 *(rw,no_root_squash)/data/k8sdata/magedu/redis5 *(rw,no_root_squash)/data/k8sdata/magedu/mysql-datadir-1 *(rw,no_root_squash)/data/k8sdata/magedu/mysql-datadir-2 *(rw,no_root_squash)/data/k8sdata/magedu/mysql-datadir-3 *(rw,no_root_squash)/data/k8sdata/magedu/mysql-datadir-4 *(rw,no_root_squash)/data/k8sdata/magedu/mysql-datadir-5 *(rw,no_root_squash)root@harbor:/data/k8sdata/magedu# exportfs -avexportfs: /etc/exports [1]: Neither "subtree_check" or "no_subtree_check" specified for export "*:/data/k8sdata/kuboard". Assuming default behaviour ("no_subtree_check"). NOTE: this default has changed since nfs-utils version 1.0.xexportfs: /etc/exports [2]: Neither "subtree_check" or "no_subtree_check" specified for export "*:/data/volumes". Assuming default behaviour ("no_subtree_check"). NOTE: this default has changed since nfs-utils version 1.0.xexportfs: /etc/exports [3]: Neither "subtree_check" or "no_subtree_check" specified for export "*:/pod-vol". Assuming default behaviour ("no_subtree_check"). NOTE: this default has changed since nfs-utils version 1.0.xexportfs: /etc/exports [4]: Neither "subtree_check" or "no_subtree_check" specified for export "*:/data/k8sdata/myserver". Assuming default behaviour ("no_subtree_check"). NOTE: this default has changed since nfs-utils version 1.0.xexportfs: /etc/exports [5]: Neither "subtree_check" or "no_subtree_check" specified for export "*:/data/k8sdata/mysite". Assuming default behaviour ("no_subtree_check"). NOTE: this default has changed since nfs-utils version 1.0.xexportfs: /etc/exports [7]: Neither "subtree_check" or "no_subtree_check" specified for export "*:/data/k8sdata/magedu/images". Assuming default behaviour ("no_subtree_check"). NOTE: this default has changed since nfs-utils version 1.0.xexportfs: /etc/exports [8]: Neither "subtree_check" or "no_subtree_check" specified for export "*:/data/k8sdata/magedu/static". Assuming default behaviour ("no_subtree_check"). NOTE: this default has changed since nfs-utils version 1.0.xexportfs: /etc/exports [11]: Neither "subtree_check" or "no_subtree_check" specified for export "*:/data/k8sdata/magedu/zookeeper-datadir-1". Assuming default behaviour ("no_subtree_check"). NOTE: this default has changed since nfs-utils version 1.0.xexportfs: /etc/exports [12]: Neither "subtree_check" or "no_subtree_check" specified for export "*:/data/k8sdata/magedu/zookeeper-datadir-2". Assuming default behaviour ("no_subtree_check"). NOTE: this default has changed since nfs-utils version 1.0.xexportfs: /etc/exports [13]: Neither "subtree_check" or "no_subtree_check" specified for export "*:/data/k8sdata/magedu/zookeeper-datadir-3". Assuming default behaviour ("no_subtree_check"). NOTE: this default has changed since nfs-utils version 1.0.xexportfs: /etc/exports [16]: Neither "subtree_check" or "no_subtree_check" specified for export "*:/data/k8sdata/magedu/redis-datadir-1". Assuming default behaviour ("no_subtree_check"). NOTE: this default has changed since nfs-utils version 1.0.xexportfs: /etc/exports [18]: Neither "subtree_check" or "no_subtree_check" specified for export "*:/data/k8sdata/magedu/redis0". Assuming default behaviour ("no_subtree_check"). NOTE: this default has changed since nfs-utils version 1.0.xexportfs: /etc/exports [19]: Neither "subtree_check" or "no_subtree_check" specified for export "*:/data/k8sdata/magedu/redis1". Assuming default behaviour ("no_subtree_check"). NOTE: this default has changed since nfs-utils version 1.0.xexportfs: /etc/exports [20]: Neither "subtree_check" or "no_subtree_check" specified for export "*:/data/k8sdata/magedu/redis2". Assuming default behaviour ("no_subtree_check"). NOTE: this default has changed since nfs-utils version 1.0.xexportfs: /etc/exports [21]: Neither "subtree_check" or "no_subtree_check" specified for export "*:/data/k8sdata/magedu/redis3". Assuming default behaviour ("no_subtree_check"). NOTE: this default has changed since nfs-utils version 1.0.xexportfs: /etc/exports [22]: Neither "subtree_check" or "no_subtree_check" specified for export "*:/data/k8sdata/magedu/redis4". Assuming default behaviour ("no_subtree_check"). NOTE: this default has changed since nfs-utils version 1.0.xexportfs: /etc/exports [23]: Neither "subtree_check" or "no_subtree_check" specified for export "*:/data/k8sdata/magedu/redis5". Assuming default behaviour ("no_subtree_check"). NOTE: this default has changed since nfs-utils version 1.0.xexportfs: /etc/exports [27]: Neither "subtree_check" or "no_subtree_check" specified for export "*:/data/k8sdata/magedu/mysql-datadir-1". Assuming default behaviour ("no_subtree_check"). NOTE: this default has changed since nfs-utils version 1.0.xexportfs: /etc/exports [28]: Neither "subtree_check" or "no_subtree_check" specified for export "*:/data/k8sdata/magedu/mysql-datadir-2". Assuming default behaviour ("no_subtree_check"). NOTE: this default has changed since nfs-utils version 1.0.xexportfs: /etc/exports [29]: Neither "subtree_check" or "no_subtree_check" specified for export "*:/data/k8sdata/magedu/mysql-datadir-3". Assuming default behaviour ("no_subtree_check"). NOTE: this default has changed since nfs-utils version 1.0.xexportfs: /etc/exports [30]: Neither "subtree_check" or "no_subtree_check" specified for export "*:/data/k8sdata/magedu/mysql-datadir-4". Assuming default behaviour ("no_subtree_check"). NOTE: this default has changed since nfs-utils version 1.0.xexportfs: /etc/exports [31]: Neither "subtree_check" or "no_subtree_check" specified for export "*:/data/k8sdata/magedu/mysql-datadir-5". Assuming default behaviour ("no_subtree_check"). NOTE: this default has changed since nfs-utils version 1.0.xexporting *:/data/k8sdata/magedu/mysql-datadir-5exporting *:/data/k8sdata/magedu/mysql-datadir-4exporting *:/data/k8sdata/magedu/mysql-datadir-3exporting *:/data/k8sdata/magedu/mysql-datadir-2exporting *:/data/k8sdata/magedu/mysql-datadir-1exporting *:/data/k8sdata/magedu/redis5exporting *:/data/k8sdata/magedu/redis4exporting *:/data/k8sdata/magedu/redis3exporting *:/data/k8sdata/magedu/redis2exporting *:/data/k8sdata/magedu/redis1exporting *:/data/k8sdata/magedu/redis0exporting *:/data/k8sdata/magedu/redis-datadir-1exporting *:/data/k8sdata/magedu/zookeeper-datadir-3exporting *:/data/k8sdata/magedu/zookeeper-datadir-2exporting *:/data/k8sdata/magedu/zookeeper-datadir-1exporting *:/data/k8sdata/magedu/staticexporting *:/data/k8sdata/magedu/imagesexporting *:/data/k8sdata/mysiteexporting *:/data/k8sdata/myserverexporting *:/pod-volexporting *:/data/volumesexporting *:/data/k8sdata/kuboardroot@harbor:/data/k8sdata/magedu#
3.2.2、创建pv创建pv配置清单
---apiVersion: v1kind: PersistentVolumemetadata: name: mysql-datadir-1 namespace: mageduspec: capacity: storage: 50Gi accessModes: - ReadWriteOnce nfs: path: /data/k8sdata/magedu/mysql-datadir-1 server: 192.168.0.42---apiVersion: v1kind: PersistentVolumemetadata: name: mysql-datadir-2 namespace: mageduspec: capacity: storage: 50Gi accessModes: - ReadWriteOnce nfs: path: /data/k8sdata/magedu/mysql-datadir-2 server: 192.168.0.42---apiVersion: v1kind: PersistentVolumemetadata: name: mysql-datadir-3 namespace: mageduspec: capacity: storage: 50Gi accessModes: - ReadWriteOnce nfs: path: /data/k8sdata/magedu/mysql-datadir-3 server: 192.168.0.42---apiVersion: v1kind: PersistentVolumemetadata: name: mysql-datadir-4 namespace: mageduspec: capacity: storage: 50Gi accessModes: - ReadWriteOnce nfs: path: /data/k8sdata/magedu/mysql-datadir-4 server: 192.168.0.42---apiVersion: v1kind: PersistentVolumemetadata: name: mysql-datadir-5 namespace: mageduspec: capacity: storage: 50Gi accessModes: - ReadWriteOnce nfs: path: /data/k8sdata/magedu/mysql-datadir-5 server: 192.168.0.42
创建pv
root@k8s-master01:~/k8s-data/yaml/magedu/mysql# kubectl apply -f pv/mysql-persistentvolume.yamlpersistentvolume/mysql-datadir-1 createdpersistentvolume/mysql-datadir-2 createdpersistentvolume/mysql-datadir-3 createdpersistentvolume/mysql-datadir-4 createdpersistentvolume/mysql-datadir-5 createdroot@k8s-master01:~/k8s-data/yaml/magedu/mysql#
3.2.3、验证pvroot@k8s-master01:~/k8s-data/yaml/magedu/mysql# kubectl get pvNAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGEmysql-datadir-1 50Gi RWO Retain Available 5smysql-datadir-2 50Gi RWO Retain Available 5smysql-datadir-3 50Gi RWO Retain Available 5smysql-datadir-4 50Gi RWO Retain Available 5smysql-datadir-5 50Gi RWO Retain Available 5sredis-cluster-pv0 5Gi RWO Retain Bound magedu/data-redis-1 8dredis-cluster-pv1 5Gi RWO Retain Bound magedu/data-redis-3 8dredis-cluster-pv2 5Gi RWO Retain Bound magedu/data-redis-4 8dredis-cluster-pv3 5Gi RWO Retain Bound magedu/data-redis-0 8dredis-cluster-pv4 5Gi RWO Retain Bound magedu/data-redis-5 8dredis-cluster-pv5 5Gi RWO Retain Bound magedu/data-redis-2 8dredis-datadir-pv-1 10Gi RWO Retain Bound magedu/redis-datadir-pvc-1 9dzookeeper-datadir-pv-1 20Gi RWO Retain Bound magedu/zookeeper-datadir-pvc-1 10dzookeeper-datadir-pv-2 20Gi RWO Retain Bound magedu/zookeeper-datadir-pvc-2 10dzookeeper-datadir-pv-3 20Gi RWO Retain Bound magedu/zookeeper-datadir-pvc-3 10droot@k8s-master01:~/k8s-data/yaml/magedu/mysql#
3.3、运行mysql服务MYSQL CONFIGMAP配置清单
apiVersion: v1kind: ConfigMapmetadata: name: mysql namespace: magedu labels: app: mysqldata: master.cnf: | # Apply this config only on the master. [mysqld] log-bin log_bin_trust_function_creators=1 lower_case_table_names=1 slave.cnf: | # Apply this config only on slaves. [mysqld] super-read-only log_bin_trust_function_creators=1
MYSQL POD配置清单
apiVersion: apps/v1kind: StatefulSetmetadata: name: mysql namespace: mageduspec: selector: matchLabels: app: mysql serviceName: mysql replicas: 2 template: metadata: labels: app: mysql spec: initContainers: - name: init-mysql #初始化容器1、基于当前pod name匹配角色是master还是slave,并动态生成相对应的配置文件 image: harbor.ik8s.cc/magedu/mysql:5.7.36 command: - bash - "-c" - | set -ex # Generate mysql server-id from pod ordinal index. [[ `hostname` =~ -([0-9]+)$ ]] || exit 1 #匹配hostname的最后一位、最后是一个顺序叠加的整数 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 #如果是master、则cpmaster配置文件 cp /mnt/config-map/master.cnf /mnt/conf.d/ else #否则cp slave配置文件 cp /mnt/config-map/slave.cnf /mnt/conf.d/ fi volumeMounts: - name: conf #临时卷、emptyDir mountPath: /mnt/conf.d - name: config-map mountPath: /mnt/config-map - name: clone-mysql #初始化容器2、用于生成mysql配置文件、并从上一个pod完成首次的全量数据clone(slave 3从slave2 clone,而不是每个slave都从master clone实现首次全量同步,但是后期都是与master实现增量同步) image: harbor.ik8s.cc/magedu/xtrabackup:1.0 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 #如果最后一位是0(master)则退出clone过程 # Clone data from previous peer. ncat --recv-only mysql-$(($ordinal-1)).mysql 3307 | xbstream -x -C /var/lib/mysql #从上一个pod执行clone(binlog),xbstream为解压缩命令 # Prepare the backup.xue xtrabackup --prepare --target-dir=/var/lib/mysql #通过xtrabackup恢复binlog volumeMounts: - name: data mountPath: /var/lib/mysql subPath: mysql - name: conf mountPath: /etc/mysql/conf.d containers: - name: mysql #业务容器1(mysql主容器) image: harbor.ik8s.cc/magedu/mysql:5.7.36 env: - name: MYSQL_ALLOW_EMPTY_PASSWORD value: "1" ports: - name: mysql containerPort: 3306 volumeMounts: - name: data #挂载数据目录至/var/lib/mysql mountPath: /var/lib/mysql subPath: mysql - name: conf #配置文件/etc/mysql/conf.d mountPath: /etc/mysql/conf.d resources: #资源限制 requests: cpu: 500m memory: 1Gi 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 #业务容器2(xtrabackup),用于后期同步master 的binglog并恢复数据 image: harbor.ik8s.cc/magedu/xtrabackup:1.0 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 ]]; then # XtraBackup already generated a partial "CHANGE MASTER TO" query # because we"re cloning from an existing slave. mv xtrabackup_slave_info change_master_to.sql.in # Ignore xtrabackup_binlog_info in this case (it"s useless). rm -f 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 xtrabackup_binlog_info echo "CHANGE MASTER TO MASTER_LOG_FILE="${BASH_REMATCH[1]}",\ MASTER_LOG_POS=${BASH_REMATCH[2]}" > change_master_to.sql.in #生成CHANGE MASTER命令 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" # In case of container restart, attempt this at-most-once. mv change_master_to.sql.in change_master_to.sql.orig #执行CHANGE MASTER操作并启动SLAVE mysql -h 127.0.0.1 <
上述配置清单主要定义两个mysqlPod,其中每个pod中运行4个容器,第一个容器init容器主要目的是通过sts控制器创建pod的名称id来确定挂载master和slave配置;第二个初始化容器主要目的是用于克隆上一个pod的数据,即salve1从master上克隆数据,slave2从slave1克隆数据,以此类推;第三个就是mysql主容器,运行mysql实例;第四个容器主要用于给mysql主容器同步后期的增量数据的;
MYSQL SERVICE配置清单
# Headless service for stable DNS entries of StatefulSet members.apiVersion: v1kind: Servicemetadata: namespace: magedu name: mysql labels: app: mysqlspec: 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 master: mysql-0.mysql.apiVersion: v1kind: Servicemetadata: name: mysql-read namespace: magedu labels: app: mysqlspec: ports: - name: mysql port: 3306 selector: app: mysql
3.3.1、创建mysql pod上述配置清单主要创建了两个service,第一个是一个无头service 用户返回后端所有pod端点;第二个service是一个默认clusterip类型service,这两个service都是可以的;
root@k8s-master01:~/k8s-data/yaml/magedu/mysql# kubectl apply -f mysql-configmap.yaml -f mysql-services.yaml -f mysql-statefulset.yaml configmap/mysql createdservice/mysql createdservice/mysql-read createdstatefulset.apps/mysql createdroot@k8s-master01:~/k8s-data/yaml/magedu/mysql#
3.3.2、验证MySQL Pod状态root@k8s-master01:~/k8s-data/yaml/magedu/mysql# kubectl get pods -n magedu NAME READY STATUS RESTARTS AGEmagedu-nginx-deployment-5589bbf4bc-6gd2w 1/1 Running 8 (113m ago) 11dmagedu-tomcat-app1-deployment-7754c8549c-c7rtb 1/1 Running 4 (114m ago) 11dmagedu-tomcat-app1-deployment-7754c8549c-prglk 1/1 Running 4 (114m ago) 11dmysql-0 2/2 Running 0 47smysql-1 2/2 Running 0 23sredis-0 1/1 Running 2 (114m ago) 8dredis-1 1/1 Running 2 (114m ago) 8dredis-2 1/1 Running 2 (114m ago) 8dredis-3 1/1 Running 2 (114m ago) 8dredis-4 1/1 Running 2 (114m ago) 8dredis-5 1/1 Running 2 (114m ago) 8dubuntu1804 0/1 Completed 0 8dzookeeper1-675c5477cb-vmwwq 1/1 Running 4 (114m ago) 10dzookeeper2-759fb6c6f-7jktr 1/1 Running 4 (114m ago) 10dzookeeper3-5c78bb5974-vxpbh 1/1 Running 4 (114m ago) 10droot@k8s-master01:~/k8s-data/yaml/magedu/mysql#
修改配置清单pod副本为3,看看对应pod是否能够正常running?应用配置清单
3.3.3、验证MySQL主从同步是否正常验证master状态
root@k8s-master01:~# kubectl exec -it mysql-0 -n magedu bash kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.Defaulted container "mysql" out of: mysql, xtrabackup, init-mysql (init), clone-mysql (init)root@mysql-0:/# mysql Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 691Server 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 itsaffiliates. Other names may be trademarks of their respectiveowners.Type "help;" or "\h" for help. Type "\c" to clear the current input statement.mysql> show master status\G*************************** 1. row *************************** File: mysql-0-bin.000003 Position: 313 Binlog_Do_DB: Binlog_Ignore_DB: Executed_Gtid_Set: 1 row in set (0.01 sec)mysql> exitByeroot@mysql-0:/# exitexitroot@k8s-master01:~#
验证slave状态
root@k8s-master01:~# kubectl exec -it mysql-1 -n magedu bash kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.Defaulted container "mysql" out of: mysql, xtrabackup, init-mysql (init), clone-mysql (init)root@mysql-1:/# mysqlWelcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 708Server 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 itsaffiliates. Other names may be trademarks of their respectiveowners.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: 313 Relay_Log_File: mysql-1-relay-bin.000002 Relay_Log_Pos: 481 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: 313 Relay_Log_Space: 690 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: 0Master_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: 72724f41-0b65-11ee-b561-16065cd6961a 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)mysql> exit Byeroot@mysql-1:/# exitexitroot@k8s-master01:~# kubectl exec -it mysql-2 -n magedu bash kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.Defaulted container "mysql" out of: mysql, xtrabackup, init-mysql (init), clone-mysql (init)root@mysql-2:/# mysqlWelcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 632Server 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 itsaffiliates. Other names may be trademarks of their respectiveowners.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: 313 Relay_Log_File: mysql-2-relay-bin.000002 Relay_Log_Pos: 481 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: 313 Relay_Log_Space: 690 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: 0Master_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: 72724f41-0b65-11ee-b561-16065cd6961a 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)mysql> exitByeroot@mysql-2:/# exitexitroot@k8s-master01:~#
能够在slave上看到IO和SQL线程正常running,说明slave状态没有问题;
进入mysql-0创建数据库
root@k8s-master01:~/k8s-data/yaml/magedu/mysql# kubectl exec -it mysql-0 -n magedu bashkubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.Defaulted container "mysql" out of: mysql, xtrabackup, init-mysql (init), clone-mysql (init)root@mysql-0:/# mysqlWelcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 355Server 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 itsaffiliates. Other names may be trademarks of their respectiveowners.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.02 sec)mysql> create database mydb;Query OK, 1 row affected (0.00 sec)mysql> show databases;+------------------------+| Database |+------------------------+| information_schema || mydb || mysql || performance_schema || sys || xtrabackup_backupfiles |+------------------------+6 rows in set (0.00 sec)mysql>
在slave pod中验证数据是否同步?
root@k8s-master01:~/k8s-data/yaml/magedu/mysql# kubectl exec -it mysql-1 -n magedu bash kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.Defaulted container "mysql" out of: mysql, xtrabackup, init-mysql (init), clone-mysql (init)root@mysql-1:/# mysqlWelcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 437Server 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 itsaffiliates. Other names may be trademarks of their respectiveowners.Type "help;" or "\h" for help. Type "\c" to clear the current input statement.mysql> show databases;+------------------------+| Database |+------------------------+| information_schema || mydb || mysql || performance_schema || sys || xtrabackup_backupfiles |+------------------------+6 rows in set (0.01 sec)mysql> exitByeroot@mysql-1:/# exitexitroot@k8s-master01:~/k8s-data/yaml/magedu/mysql# kubectl exec -it mysql-2 -n magedu bash kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.Defaulted container "mysql" out of: mysql, xtrabackup, init-mysql (init), clone-mysql (init)root@mysql-2:/# mysqlWelcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 361Server 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 itsaffiliates. Other names may be trademarks of their respectiveowners.Type "help;" or "\h" for help. Type "\c" to clear the current input statement.mysql> show databases;+------------------------+| Database |+------------------------+| information_schema || mydb || mysql || performance_schema || sys || xtrabackup_backupfiles |+------------------------+6 rows in set (0.02 sec)mysql> exitByeroot@mysql-2:/# exitexitroot@k8s-master01:~/k8s-data/yaml/magedu/mysql#
3.3.4、mysql⾼可⽤测试3.3.4.1、删除master pod验证mysql高可用性能够通过上述在主库创建数据库,从库能够正常同步,则说明mysql主从同步没有问题;
进入master,验证master数据是否丢失?
3.3.4.2、删除slave pod验证mysql高可用性能够看到master数据并未丢失,这是因为该数据存放至远端nfs存储之上;
root@k8s-master01:~# kubectl get pods -n magedu NAME READY STATUS RESTARTS AGEmagedu-nginx-deployment-5589bbf4bc-6gd2w 1/1 Running 8 (143m ago) 11dmagedu-tomcat-app1-deployment-7754c8549c-c7rtb 1/1 Running 4 (143m ago) 11dmagedu-tomcat-app1-deployment-7754c8549c-prglk 1/1 Running 4 (143m ago) 11dmysql-0 2/2 Running 0 3m6smysql-1 2/2 Running 0 29mmysql-2 2/2 Running 0 27mredis-0 1/1 Running 2 (143m ago) 8dredis-1 1/1 Running 2 (143m ago) 8dredis-2 1/1 Running 2 (143m ago) 8dredis-3 1/1 Running 2 (143m ago) 8dredis-4 1/1 Running 2 (143m ago) 8dredis-5 1/1 Running 2 (143m ago) 8dubuntu1804 0/1 Completed 0 8dzookeeper1-675c5477cb-vmwwq 1/1 Running 4 (143m ago) 10dzookeeper2-759fb6c6f-7jktr 1/1 Running 4 (143m ago) 10dzookeeper3-5c78bb5974-vxpbh 1/1 Running 4 (143m ago) 10droot@k8s-master01:~# kubectl delete pods mysql-1 -n magedu pod "mysql-1" deletedroot@k8s-master01:~# kubectl get pods -n magedu NAME READY STATUS RESTARTS AGEmagedu-nginx-deployment-5589bbf4bc-6gd2w 1/1 Running 8 (144m ago) 11dmagedu-tomcat-app1-deployment-7754c8549c-c7rtb 1/1 Running 4 (145m ago) 11dmagedu-tomcat-app1-deployment-7754c8549c-prglk 1/1 Running 4 (145m ago) 11dmysql-0 2/2 Running 0 4m55smysql-1 2/2 Running 0 55smysql-2 2/2 Running 0 28mredis-0 1/1 Running 2 (145m ago) 8dredis-1 1/1 Running 2 (145m ago) 8dredis-2 1/1 Running 2 (145m ago) 8dredis-3 1/1 Running 2 (145m ago) 8dredis-4 1/1 Running 2 (145m ago) 8dredis-5 1/1 Running 2 (145m ago) 8dubuntu1804 0/1 Completed 0 8dzookeeper1-675c5477cb-vmwwq 1/1 Running 4 (145m ago) 10dzookeeper2-759fb6c6f-7jktr 1/1 Running 4 (145m ago) 10dzookeeper3-5c78bb5974-vxpbh 1/1 Running 4 (145m ago) 10droot@k8s-master01:~#
进入slave验证数据
-
天天快播:k8s实战案例之基于StatefulSet控制器运行MySQL一主多从StatefulSet本质上是Deployment的⼀种变体,在v1 9版本中已成为GA版本
-
nr238硬改_nr238 世界新资讯1、1200m路由器适合八人用2、多台电脑共享宽带的话,最好不要用TP,腾
-
每日消息!深蓝S7超级增程有何实力?馈电油耗低于5L!6月15日,以“超级增程实力担当”为主题的深蓝S7超级增程动态品鉴在重
-
华为携三大运营商共建CAMARA能力开放未来2023开放原子全球开源峰会于6月11日至13日在北京市经济开发区亦创国际
-
6月15日十大人气股:难兄难弟们回来了(名单)_全球速看没有只跌不涨的股票亦不会有只跌不涨的指数6月15日三大股指全线上涨创
-
IBM陈旭东:人工智能引领技术创新,推动企业高质量发展-今头条6月15日,国药现代公告,公司全资子公司国药集团威奇达药业有限公司收
-
焦点精选!七彩虹bios设置全解(七彩虹主板怎么进入BIOS)七彩虹主板进入BIOS可以在电脑进入开机界面时长按DEL键,这时就可以引
-
巅峰枪神_全球通讯1、《巅峰枪神》是一本游戏小说,作者是东坡酥。2、。本文到此分享完毕
-
太行山线稿前一段时间喜欢上了水彩,每天一张,直到前几天被一片树林和水边船
-
塔城:端午粽飘香 共建社区情为进一步弘扬中华民族传统文化,丰富节日文化内涵,加强邻里沟通交流,
-
天天微动态丨黑龙江省齐齐哈尔市市场监管局走进企业强化品牌意识北大仓集团是黑龙江省齐齐哈尔市乃至黑龙江省西部地区最大的民营股份制
-
环球消息!阳光照明: 国内房地产市场低迷导致我国照明行业发展整体承压放缓阳光照明60026106月15日在投资者关系平台上答复了投资者关心的问题投资
-
安次区葛渔城镇安全生产志愿服务队1、安次区葛渔城镇安全生产志愿服务队是由廊坊市安次区安全生产志愿服
-
国家统计局:随着稳定房地产市场政策显效 房地产市场有望逐步企稳中新网6月15日电 15日,国新办就2023年5月份国民经济运行情况举行
-
boot priority order正确顺序(bootpriority)1、引导优先级即启动优先级,是系统在引导时优先读取设备的行为。一般
-
水利部长江水利委员会会商部署新一轮强降雨防御和重要水库调度工作据水利部6月15日消息,6月14日,长江水利委员会副主任吴道喜主持会商,
-
通用10年内无法放弃内燃机卡车和SUV据路透社报道,根据分析师和供应商提供的消息,通用汽车在最近一系列关
-
总预算超77.2亿!中国移动启动云化核心网设备新建部分集采-焦点热门总预算超77 2亿!中国移动启动云化核心网设备新建部分集采,C114讯6月15
-
dcm溶剂_dcm是什么溶剂_焦点关注1、TFA,三氟乙酸TEA三乙胺补充:DCM是二氯甲烷,由dichloromethane一词
-
A股“应退尽退”成趋势:上市12年和佳医疗进入退市倒计时21世纪经济报道记者魏笑实习生宋家哲深圳报道近日,深交所发布公告,决
-
理想ONE停产竟是华为逼的,李想深夜猛料:赔了供应商10多亿,全面学习华为理想ONE停产竟是华为逼的,李想深夜猛料:赔了供应商10多亿,全面学习
-
【天天聚看点】插牢是关键 新显卡16针供电烧熔比率不高在越来越多的用户报告中,有更多的16针电源连接器熔断,Cablemod在Redd
-
老婆大人闹“分家”,两加盟店被迫关门?记者走访:已恢复营业 天天热消息前段时间,老婆大人公司股东发生纠纷,多家加盟店暂停使用会员卡的消息
-
余额宝上线第十年:累计为用户赚了3867亿元收益 每日头条文 羊城晚报全媒体记者沈钊图 受访者供图6月13日是余额宝上线十周年,
-
家居用品板块6月14日涨0.57%,茶花股份领涨,主力资金净流出3768.48万元6月14日家居用品板块较上一交易日上涨0 57%,茶花股份领涨。当日上证指
-
广告公司经营范围怎么写全面(广告公司的经营范围怎么写)来为大家解答以下的问题,告公司经营范围怎么写全面,广告公司的经营范
-
环球热资讯!东方绯想天隐藏人物怎么获得_东方绯想天出招表1、0ω0)ノLZ您好,很高兴为您解决问题~♪格斗游戏的招式中的数字一般
-
曾经的英文名词(曾经的英文)1、曾经1 atonetime2 once3 ever4 onceuponatime。本文就为大家分享到这里,
-
昆仑万维:控股子公司拟发行股份收购Singularity AI全部股权证券时报e公司讯,昆仑万维(300418)6月14日晚间公告,公司控股子公司St
-
环球速讯:桃李满天下 春晖遍四方的意思_王马共天下的意思1、“王与马,共天下”。2、意思就是王氏同皇族司马氏共同掌握了东晋的