快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

Kubernetes使用StatefulSet部署MySQL高可用集群

MySQL简介

创新互联主营红寺堡网站建设的网络公司,主营网站建设方案,成都app软件开发,红寺堡h5小程序设计搭建,红寺堡网站营销推广欢迎红寺堡等地区企业咨询

MySQL是一个关系型数据库管理系统,由瑞典MySQL AB公司开发,属于 Oracle 旗下产品。MySQL 是最流行的关系型数据库管理系统之一,在 WEB 应用方面,MySQL是好的 RDBMS (Relational Database Management System,关系数据库管理系统) 应用软件之一。MySQL是一种关系型数据库管理系统,关系数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性。MySQL所使用的 SQL 语言是用于访问数据库的最常用标准化语言。MySQL 软件采用了双授权政策,分为社区版和商业版,由于其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,一般中小型网站的开发都选择 MySQL 作为网站数据库。

MySQL高可用方案

下文介绍的MySQL高可用方案采用“主从复制+读写分离”,即由单一的master和多个slave所构成。其中,客户端通过master对数据库进行写操作,什么叫软文营销?通过slave端进行读操作。master出现问题后,可以将应用切换到slave端。 此方案是MySQL官方提供的一种高可用解决方案,节点间的数据同步采用MySQL Replication技术。MySQL Replication从一个MySQL数据库服务器(master)的数据复制到一个或多个MySQL数据库服务器(slave)。在默认情况下,复制是异步的;slave不需要一直接收来自主机的更新。根据配置,可以复制数据库中的所有数据库、选定的数据库,或者特定的表。

预配置storageclass,实现nfs做存储类的动态供给pv

1、安装NFS服务器

#安装NFS服务端,选择k8s集群的master1节点(192.168.56.10)[root@k8s-master01 ~]# yum install nfs-utils -y (在所有节点安装nfs-utils,并在node节点测试挂载)[root@k8s-master01 ~]# systemctl start nfs[root@k8s-master01 ~]# systemctl enable nfs#在master1上创建nfs共享目录[root@k8s-master01 ~]# mkdir -p /data/v1[root@k8s-master01 ~]# chown -R 1000:1000 /data/v1[root@k8s-master01 ~]# vim /etc/exports/data/v1 192.168.56.0/24(rw,no_root_squash)[root@k8s-master01 ~]# exportfs -arv 使配置文件生效[root@k8s-master01 ~]# systemctl restart nfs[root@k8s-master01 data]# showmount -eExport list for k8s-master01:/data/v1 192.168.56.0/24#在node01和node02节点上安装nfs-utils,并测试挂载[root@k8s-node01 ~]# yum install -y nfs-utils[root@k8s-node02 ~]# yum install -y nfs-utils[root@k8s-node01 ~]# mount -t nfs 192.168.56.10:/data/v1 /mnt[root@k8s-node01 ~]# mount......192.168.56.10:/data/v1 on /mnt type nfs4 (rw,relatime,vers=4.1,rsize=1048576,wsize=1048576,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=192.168.56.11,local_lock=none,addr=192.168.56.10)[root@k8s-node01 ~]# umount /mnt/

2、实现nfs做storageclass存储类的动态供给pv

创建运行nfs-provisioner的sa账号

[root@k8s-master01 ~]# mkdir mysql && cd mysql/[root@k8s-master01 mysql]# vim serviceaccount.yamlapiVersion: v1kind: ServiceAccountmetadata: name: nfs-provisioner通过kubectl更新yaml文件 [root@k8s-master01 mysql]# kubectl apply -f serviceaccount.yamlserviceaccount/nfs-provisioner created[root@k8s-master01 mysql]# kubectl get saNAME SECRETS AGEdefault 1 4d18hnfs-provisioner 1 33s

对nfs-provisioner这个sa账号做rbac授权

[root@k8s-master01 mysql]# vim rbac.yamlkind: ClusterRoleapiVersion: rbac.authorization.k8s.io/v1metadata: name: nfs-provisioner-runnerrules: - 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"] - apiGroups: [""] resources: ["services", "endpoints"] verbs: ["get"] - apiGroups: ["extensions"] resources: ["podsecuritypolicies"] resourceNames: ["nfs-provisioner"] verbs: ["use"]---kind: ClusterRoleBindingapiVersion: rbac.authorization.k8s.io/v1metadata: name: run-nfs-provisionersubjects: - kind: ServiceAccount name: nfs-provisioner namespace: defaultroleRef: kind: ClusterRole name: nfs-provisioner-runner apiGroup: rbac.authorization.k8s.io---kind: RoleapiVersion: rbac.authorization.k8s.io/v1metadata: name: leader-locking-nfs-provisionerrules: - apiGroups: [""] resources: ["endpoints"] verbs: ["get", "list", "watch", "create", "update", "patch"]---kind: RoleBindingapiVersion: rbac.authorization.k8s.io/v1metadata: name: leader-locking-nfs-provisionersubjects: - kind: ServiceAccount name: nfs-provisioner namespace: defaultroleRef: kind: Role name: leader-locking-nfs-provisioner apiGroup: rbac.authorization.k8s.io通过kubectl更新yaml文件 [root@k8s-master01 mysql]# kubectl apply -f rbac.yamlclusterrole.rbac.authorization.k8s.io/nfs-provisioner-runner createdclusterrolebinding.rbac.authorization.k8s.io/run-nfs-provisioner createdrole.rbac.authorization.k8s.io/leader-locking-nfs-provisioner createdrolebinding.rbac.authorization.k8s.io/leader-locking-nfs-provisioner created[root@k8s-master01 mysql]# kubectl get rolebindingsNAME ROLE AGEleader-locking-nfs-provisioner Role/leader-locking-nfs-provisioner 36s

通过deployment创建pod用来运行nfs-provisioner

[root@k8s-master01 mysql]# vim deployment.yamlkind: DeploymentapiVersion: apps/v1metadata: name: nfs-provisionerspec: selector: matchLabels: app: nfs-provisioner replicas: 1 strategy: type: Recreate template: metadata: labels: app: nfs-provisioner spec: serviceAccount: nfs-provisioner containers: - name: nfs-provisioner image: registry.cn-hangzhou.aliyuncs.com/open-ali/nfs-client-provisioner:latest imagePullPolicy: IfNotPresent volumeMounts: - name: nfs-client-root mountPath: /persistentvolumes env: - name: PROVISIONER_NAME value: example.com/nfs - name: NFS_SERVER value: 192.168.56.10 - name: NFS_PATH value: /data/v1 volumes: - name: nfs-client-root nfs: server: 192.168.56.10 path: /data/v1通过kubectl更新yaml文件 [root@k8s-master01 mysql]# kubectl apply -f deployment.yamldeployment.apps/nfs-provisioner created[root@k8s-master01 mysql]# kubectl get podsNAME READY STATUS RESTARTS AGEnfs-provisioner-6dccbfd48f-p4cc4 1/1 Running 0 19s
注:上面yaml文件说明: - name: PROVISIONER_NAME value: example.com/nfs#PROVISIONER_NAME是example.com/nfs#example.com/nfs需要跟后面的storageclass的provisinoer保持一致 - name: NFS_SERVER value: 192.168.56.10 #这个需要写nfs服务端所在的ip地址,大家需要写自己的nfs地址 - name: NFS_PATH value: /data/v1 #这个是nfs服务端共享的目录 volumes: - name: nfs-client-root nfs: server: 192.168.56.10 #nfs服务端ip path:/data/v1 #nfs服务端共享目录
kubectl describe pod nfs-provisioner-6dccbfd48f-p4cc4 查看pod详情kubectl logs -f nfs-provisioner-6dccbfd48f-p4cc4 查看pod日志、排错

创建storageclass存储类

[root@k8s-master01 mysql]# vim storageclass.yamlapiVersion: storage.k8s.io/v1kind: StorageClassmetadata: name: nfsprovisioner: example.com/nfs注意:provisioner:example.com/nfs #该值需要和provisioner配置的保持一致通过kubectl更新yaml文件[root@k8s-master01 mysql]# kubectl apply -f storageclass.yaml storageclass.storage.k8s.io/nfs created[root@k8s-master01 mysql]# kubectl get scNAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGEnfs example.com/nfs Delete Immediate false 6s

MySQL高可用集群部署

1、创建ConfigMap

通过yaml文件创建名为mysql的ConfigMap

[root@k8s-master01 mysql]# vim mysql-configmap.yamlapiVersion: v1kind: ConfigMapmetadata: name: mysql labels: app: mysqldata: master.cnf: | log-bin log_bin_trust_function_creators=1 lower_case_table_names=1 slave.cnf: | super-read-only log_bin_trust_function_creators=1 通过kubectl更新yaml文件[root@k8s-master01 mysql]# kubectl apply -f mysql-configmap.yaml -n kube-publicconfigmap/mysql created[root@k8s-master01 mysql]# kubectl get configmap -n kube-publicNAME DATA AGEcluster-info 2 4d18hmysql 2 11s

2、创建Services

通过yaml文件创建两个service,分别是 mysql 和 mysql-read

[root@k8s-master01 mysql]# vim mysql-service.yamlapiVersion: v1kind: Servicemetadata: name: mysql labels: app: mysqlspec: ports: - name: mysql port: 3306 clusterIP: None selector: app: mysql---apiVersion: v1kind: Servicemetadata: name: mysql-read labels: app: mysqlspec: ports: - name: mysql port: 3306 selector: app: mysql通过kubectl更新yaml文件[root@k8s-master01 mysql]# kubectl apply -f mysql-service.yaml -n kube-publicservice/mysql createdservice/mysql-read created[root@k8s-master01 mysql]# kubectl get svc -n kube-publicNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEmysql ClusterIP None 3306/TCP 6smysql-read ClusterIP 10.96.89.246 3306/TCP 6s
master、node节点下载测试的image镜像docker pull registry.cn-hangzhou.aliyuncs.com/yangb/mysql5.7:latestdocker pull registry.cn-hangzhou.aliyuncs.com/test_k8s/xtrabackup:1.0docker tag registry.cn-hangzhou.aliyuncs.com/yangb/mysql5.7:latest mysql:5.7docker tag registry.cn-hangzhou.aliyuncs.com/test_k8s/xtrabackup:1.0 gcr.io/google-samples/xtrabackup:1.0

3、创建StatefulSet

通过yaml文件创建名为mysql的StatefulSet

[root@k8s-master01 mysql]# vim mysql-statefulset.yamlapiVersion: apps/v1kind: StatefulSetmetadata: name: mysqlspec: selector: matchLabels: app: mysql serviceName: mysql replicas: 3 volumeClaimTemplates: - metadata: name: data annotations: volume.beta.kubernetes.io/storage-class: "nfs" spec: accessModes: [ "ReadWriteOnce" ] resources: requests: storage: 10Gi template: metadata: labels: app: mysql spec: initContainers: - name: init-mysql image: mysql:5.7 command: - bash - "-c" - | set -ex [[ `hostname` =~ -(+)$ ]] || exit 1 ordinal=${BASH_REMATCH} echo >/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: gcr.io/google-samples/xtrabackup:1.0 command: - bash - "-c" - | set -ex [[ -d /var/lib/mysql/mysql ]] && exit 0 [[ `hostname` =~ -(+)$ ]] || exit 1 ordinal=${BASH_REMATCH} [[ $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: gcr.io/google-samples/xtrabackup:1.0 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` =~ ^(.*?)[[ ]]+(.*?)$ ]] || exit 1 rm xtrabackup_binlog_info echo "CHANGE MASTER TO MASTER_LOG_FILE='${BASH_REMATCH}',\ MASTER_LOG_POS=${BASH_REMATCH}" >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 <
在启动后,应该能够看到如下的信息:[root@k8s-master01 mysql]# kubectl get pods -lapp=mysql --watch -n kube-publicNAME READY STATUS RESTARTS AGEmysql-0 2/2 Running 0 16mmysql-1 2/2 Running 1 15mmysql-2 2/2 Running 1 15m[root@k8s-master01 mysql]# kubectl get pod -o wide -n kube-publicNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESmysql-0 2/2 Running 0 16m 10.244.2.12 k8s-node02 mysql-1 2/2 Running 1 15m 10.244.1.13 k8s-node01 mysql-2 2/2 Running 1 15m 10.244.1.14 k8s-node01

MySQL部署环境验证

1)通过运行一个临时的容器(使用mysql:5.7镜像),使用 MySQL 客户端发送测试请求给 mysql master 节点( 主机名为 mysql-0.mysql;跨命名空间的话,主机名请使用 mysql-0.mysql.kube-public )

[root@k8s-master01 mysql]# kubectl run mysql-client --image=mysql:5.7 -it --rm --restart=Never -- mysql -h mysql-0.mysql.kube-publicIf you don't see a command prompt, try pressing enter.Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 625Server version: 5.7.16-log MySQL Community Server (GPL)Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.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.在master节点上创建demo数据库,并创建一个只有message字段的demo.messages的表,并为message字段插入hello值。mysql>CREATE DATABASE demo;Query OK, 1 row affected (0.02 sec)mysql>CREATE TABLE demo.messages (message VARCHAR(250));Query OK, 0 rows affected (0.10 sec)mysql>INSERT INTO demo.messages VALUES ('hello');Query OK, 1 row affected (0.02 sec)

2)使用主机名为mysql-read来发送测试请求给服务器 (另开窗口)

[root@k8s-master01 mysql]# kubectl run mysql-client1 --image=mysql:5.7 -i -t --rm --restart=Never -- mysql -h mysql-read.kube-publicIf you don't see a command prompt, try pressing enter.Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 608Server version: 5.7.16 MySQL Community Server (GPL)Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.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 || demo || mysql || performance_schema || sys || xtrabackup_backupfiles |+------------------------+6 rows in set (0.02 sec)mysql>select * from demo.messages;+---------+| message |+---------+| hello |+---------+1 row in set (0.00 sec)

滚动更新、扩展伸缩、更新策略、版本升级

RollingUpdate 更新策略在 StatefulSet 中实现 Pod 的自动滚动更新。当StatefulSet的 .spec.updateStrategy.type 设置为 RollingUpdate 时,默认为:RollingUpdate。StatefulSet 控制器将在 StatefulSet 中删除并重新创建每个 Pod。它将以与 Pod 终止相同的顺序进行(从大的序数到最小的序数),每次更新一个 Pod。在更新其前身之前,它将等待正在更新的 Pod 状态变成正在运行并就绪。

如下操作的滚动更新是有 mysql 2-0的顺序更新。

master、node节点下载测试的image镜像docker pull registry.cn-hangzhou.aliyuncs.com/chad/mysql-5.7.19:latestdocker tag registry.cn-hangzhou.aliyuncs.com/chad/mysql-5.7.19:latest mysql:5.7.19修改mysql image版本mysql:5.7.19[root@k8s-master01 mysql]# cp mysql-statefulset.yaml mysql-5.7.19-statefulset.yaml[root@k8s-master01 mysql]# vim mysql-5.7.19-statefulset.yamlo-v2.yaml..... image: mysql:5.7.19 image: mysql:5.7.19....通过kubectl更新yaml文件[root@k8s-master01 mysql]# kubectl apply -f mysql-5.7.19-statefulset.yaml -n kube-publicstatefulset.apps/mysql configured查看滚动更新的过程[root@k8s-master01 ~]# kubectl get pods -w -n kube-publicNAME READY STATUS RESTARTS AGEmysql-0 2/2 Running 0 64mmysql-1 2/2 Running 1 64mmysql-2 2/2 Running 1 63mmysql-2 2/2 Terminating 1 63mmysql-2 0/2 Terminating 1 64mmysql-2 0/2 Terminating 1 64mmysql-2 0/2 Terminating 1 64mmysql-2 0/2 Pending 0 0smysql-2 0/2 Pending 0 0smysql-2 0/2 Init:0/2 0 0smysql-2 0/2 Init:0/2 0 1smysql-2 0/2 Init:1/2 0 3smysql-2 0/2 PodInitializing 0 4smysql-2 1/2 Running 0 5smysql-2 2/2 Running 0 9smysql-1 2/2 Terminating 1 65mmysql-1 0/2 Terminating 1 65mmysql-1 0/2 Terminating 1 65mmysql-1 0/2 Terminating 1 65mmysql-1 0/2 Pending 0 0smysql-1 0/2 Pending 0 0smysql-1 0/2 Init:0/2 0 0smysql-1 0/2 Init:0/2 0 1smysql-1 0/2 Init:1/2 0 2smysql-1 0/2 PodInitializing 0 3smysql-1 1/2 Running 0 6smysql-1 2/2 Running 0 11smysql-0 2/2 Terminating 0 66mmysql-0 1/2 Terminating 0 66mmysql-0 0/2 Terminating 0 66mmysql-0 0/2 Terminating 0 67mmysql-0 0/2 Terminating 0 67mmysql-0 0/2 Pending 0 0smysql-0 0/2 Pending 0 0smysql-0 0/2 Init:0/2 0 0smysql-0 0/2 Init:0/2 0 1smysql-0 0/2 Init:1/2 0 3smysql-0 0/2 PodInitializing 0 7smysql-0 1/2 Running 0 9smysql-0 2/2 Running 0 13s
[root@k8s-master01 mysql]# kubectl get sts -o wide -n kube-public NAME READY AGE CONTAINERS IMAGESmysql 3/3 77m mysql,xtrabackup mysql:5.7.19,gcr.io/google-samples/xtrabackup:1.0扩容方法1:命令行,进行扩容[root@k8s-master01 mysql]# kubectl scale sts mysql --replicas=4 -n kube-publicstatefulset.apps/mysql scaled方法2:声明式命令方式,直接修改stateful-demo-v2.yaml文件,进行扩容(推荐)[root@k8s-master01 mysql]# vim mysql-5.7.19-statefulset.yaml修改.spec.replicas的值spec: replicas: 4 [root@k8s-master01 mysql]# kubectl apply -f mysql-5.7.19-statefulset.yaml 方法3:补丁式命令方式,直接编辑运行中sts,进行扩容[root@k8s-master01 mysql]# kubectl edit sts mysql -n kube-public修改.spec.replicas的值spec: replicas: 4 方法4:补丁式命令方式,通过打补丁的方式,进行扩容[root@k8s-master01 mysql]# kubectl patch sts mysql -p '{"spec":{"replicas":4}}' -n kube-public 查看mysql pod动态扩容[root@k8s-master01 ~]# kubectl get pods -w -n kube-publicNAME READY STATUS RESTARTS AGEmysql-0 2/2 Running 0 16mmysql-1 2/2 Running 0 17mmysql-2 2/2 Running 0 17mmysql-3 0/2 Pending 0 0smysql-3 0/2 Pending 0 0smysql-3 0/2 Pending 0 2smysql-3 0/2 Init:0/2 0 2smysql-3 0/2 Init:0/2 0 4smysql-3 0/2 Init:1/2 0 5smysql-3 0/2 Init:1/2 0 6smysql-3 0/2 PodInitializing 0 27smysql-3 1/2 Error 0 28smysql-3 1/2 Running 1 29smysql-3 2/2 Running 1 34s查看mysql pod信息[root@k8s-master01 mysql]# kubectl get pod -n kube-public NAME READY STATUS RESTARTS AGEmysql-0 2/2 Running 0 16mmysql-1 2/2 Running 0 17mmysql-2 2/2 Running 0 17mmyapp-3 1/1 Running 0 102s查看pvc[root@k8s-master01 mysql]# kubectl get pvc -n kube-publicNAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGEdata-mysql-0 Bound pvc-fba11f83-cf29-4b76-b66a-3a58f91738a9 10Gi RWO nfs 93mdata-mysql-1 Bound pvc-6c434fb2-1076-4912-80fb-eee6c0e3b957 10Gi RWO nfs 93mdata-mysql-2 Bound pvc-d33d34dd-a80f-41bd-82d0-58f7126fed13 10Gi RWO nfs 92mdata-mysql-3 Bound pvc-202370f2-a2aa-456c-8cb0-81686a4ea39e 10Gi RWO nfs 11m查看pv[root@k8s-master01 mysql]# kubectl get pvNAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGEpvc-202370f2-a2aa-456c-8cb0-81686a4ea39e 10Gi RWO Delete Bound kube-public/data-mysql-3 nfs 11mpvc-6c434fb2-1076-4912-80fb-eee6c0e3b957 10Gi RWO Delete Bound kube-public/data-mysql-1 nfs 92mpvc-d33d34dd-a80f-41bd-82d0-58f7126fed13 10Gi RWO Delete Bound kube-public/data-mysql-2 nfs 92mpvc-fba11f83-cf29-4b76-b66a-3a58f91738a9 10Gi RWO Delete Bound kube-public/data-mysql-0 nfs 93m缩容方法1:命令行,进行缩容[root@k8s-master01 mysql]# kubectl scale sts mysql --replicas=3 -n kube-publicstatefulset.apps/mysql scaled查看mysql pod动态缩容[root@k8s-master01 ~]# kubectl get pods -w -n kube-publicNAME READY STATUS RESTARTS AGEmysql-0 2/2 Running 0 31mmysql-1 2/2 Running 0 32mmysql-2 2/2 Running 0 32mmysql-3 2/2 Running 1 16mmysql-3 2/2 Terminating 1 16mmysql-3 0/2 Terminating 1 16mmysql-3 0/2 Terminating 1 16mmysql-3 0/2 Terminating 1 16m查看mysql pod信息[root@k8s-master01 mysql]# kubectl get pod -n kube-publicNAME READY STATUS RESTARTS AGEmysql-0 2/2 Running 0 33mmysql-1 2/2 Running 0 34mmysql-2 2/2 Running 0 34m查看pvc (pod删除,pv和pvc 关联还在,方便后续动态扩容,自动挂载原pvc和pv)[root@k8s-master01 mysql]# kubectl get pvc -n kube-publicNAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGEdata-mysql-0 Bound pvc-fba11f83-cf29-4b76-b66a-3a58f91738a9 10Gi RWO nfs 100mdata-mysql-1 Bound pvc-6c434fb2-1076-4912-80fb-eee6c0e3b957 10Gi RWO nfs 100mdata-mysql-2 Bound pvc-d33d34dd-a80f-41bd-82d0-58f7126fed13 10Gi RWO nfs 99mdata-mysql-3 Bound pvc-202370f2-a2aa-456c-8cb0-81686a4ea39e 10Gi RWO nfs 18m查看pv (pod删除,pv和pvc 关联还在,方便后续动态扩容,自动挂载原pvc和pv)[root@k8s-master01 mysql]# kubectl get pvNAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGEpvc-202370f2-a2aa-456c-8cb0-81686a4ea39e 10Gi RWO Delete Bound kube-public/data-mysql-3 nfs 18mpvc-6c434fb2-1076-4912-80fb-eee6c0e3b957 10Gi RWO Delete Bound kube-public/data-mysql-1 nfs 100mpvc-d33d34dd-a80f-41bd-82d0-58f7126fed13 10Gi RWO Delete Bound kube-public/data-mysql-2 nfs 99mpvc-fba11f83-cf29-4b76-b66a-3a58f91738a9 10Gi RWO Delete Bound kube-public/data-mysql-0 nfs 100m查看nfs存储目录/data/v1 (pod删除,数据还在)[root@k8s-master01 mysql]# ll /data/v1/total 0drwxrwxrwx 3 root root 19 Jul 8 11:00 kube-public-data-mysql-0-pvc-fba11f83-cf29-4b76-b66a-3a58f91738a9drwxrwxrwx 3 root root 19 Jul 8 11:00 kube-public-data-mysql-1-pvc-6c434fb2-1076-4912-80fb-eee6c0e3b957drwxrwxrwx 3 root root 19 Jul 8 11:01 kube-public-data-mysql-2-pvc-d33d34dd-a80f-41bd-82d0-58f7126fed13drwxrwxrwx 3 root root 19 Jul 8 12:22 kube-public-data-mysql-3-pvc-202370f2-a2aa-456c-8cb0-81686a4ea39e

往期文章

Kubernetes 是什么?

Kubernetes 组件

Kubernetes 基础概念

Kubernetes 1.18.2集群部署 (单Master)+docker

Kubernetes 1.18.2集群部署 (多Master)+docker

Kubernetes Harbor v2.0.0私有镜像仓库部署-更新

Kubernetes kubectx/kubens切换context和namespace

Kubernetes 删除namespace时卡在Terminating状态

Kubernetes kubeadm初始化kubernetes集群延长证书过期时间

Kubernetes kubectl命令

Kubernetes 创建、更新应用

Kubernetes 资源清单

Kubernetes Pod状态和生命周期管理

Kubernetes Pod控制器

Kubernetes ReplicaSet控制器

Kubernetes Deployment控制器

Kubernetes DamonSet控制器

Kubernetes 服务发现Service

Kubernetes Nginx Ingress Controller

Kubernetes 存储卷

Kubernetes 特殊存储卷—Secret和ConfigMap

Kubernetes StatefulSet控制器

Kubernetes 认证、授权和准入控制

Kubernetes dashboard认证访问

Kubernetes 网络模型和网络策略

Kubernetes 网络原理解析

Kubernetes 网络插件-flannel

Kubernetes 网络插件-calico

Kubernetes Pod资源调度

Kubernetes 资源指标和集群监控

Kubernetes 集群中部署Prometheus+Grafana+Alertmanager监控告警系统

Kubernetes Prometheus监控Nginx

Kubernetes Prometheus监控MySQL

Kubernetes Prometheus监控tomcat


当前题目:Kubernetes使用StatefulSet部署MySQL高可用集群
标题链接:http://6mz.cn/article/chgjji.html

其他资讯