在启动后,应该能够看到如下的信息:[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