在啟動后,應(yīng)該能夠看到如下的信息:[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部署環(huán)境驗證
1)通過運行一個臨時的容器(使用mysql:5.7鏡像),使用 MySQL 客戶端發(fā)送測試請求給 mysql master 節(jié)點( 主機名為 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節(jié)點上創(chuàng)建demo數(shù)據(jù)庫,并創(chuàng)建一個只有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來發(fā)送測試請求給服務(wù)器 (另開窗口)
[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 中實現(xiàn) Pod 的自動滾動更新。當StatefulSet的 .spec.updateStrategy.type 設(shè)置為 RollingUpdate 時,默認為:RollingUpdate。StatefulSet 控制器將在 StatefulSet 中刪除并重新創(chuàng)建每個 Pod。它將以與 Pod 終止相同的順序進行(從最大的序數(shù)到最小的序數(shù)),每次更新一個 Pod。在更新其前身之前,它將等待正在更新的 Pod 狀態(tài)變成正在運行并就緒。
如下操作的滾動更新是有 mysql 2-0的順序更新。
master、node節(jié)點下載測試的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動態(tài)擴容[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動態(tài)縮容[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 關(guān)聯(lián)還在,方便后續(xù)動態(tài)擴容,自動掛載原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 關(guān)聯(lián)還在,方便后續(xù)動態(tài)擴容,自動掛載原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刪除,數(shù)據(jù)還在)[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 基礎(chǔ)概念
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狀態(tài)
Kubernetes kubeadm初始化kubernetes集群延長證書過期時間
Kubernetes kubectl命令
Kubernetes 創(chuàng)建、更新應(yīng)用
Kubernetes 資源清單
Kubernetes Pod狀態(tài)和生命周期管理
Kubernetes Pod控制器
Kubernetes ReplicaSet控制器
Kubernetes Deployment控制器
Kubernetes DamonSet控制器
Kubernetes 服務(wù)發(fā)現(xiàn)Service
Kubernetes Nginx Ingress Controller
Kubernetes 存儲卷
Kubernetes 特殊存儲卷—Secret和ConfigMap
Kubernetes StatefulSet控制器
Kubernetes 認證、授權(quán)和準入控制
Kubernetes dashboard認證訪問
Kubernetes 網(wǎng)絡(luò)模型和網(wǎng)絡(luò)策略
Kubernetes 網(wǎng)絡(luò)原理解析
Kubernetes 網(wǎng)絡(luò)插件-flannel
Kubernetes 網(wǎng)絡(luò)插件-calico
Kubernetes Pod資源調(diào)度
Kubernetes 資源指標和集群監(jiān)控
Kubernetes 集群中部署Prometheus+Grafana+Alertmanager監(jiān)控告警系統(tǒng)
Kubernetes Prometheus監(jiān)控Nginx
Kubernetes Prometheus監(jiān)控MySQL
Kubernetes Prometheus監(jiān)控tomcat