在啟動(dòng)后,應(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)境驗(yàn)證
1)通過(guò)運(yùn)行一個(gè)臨時(shí)的容器(使用mysql:5.7鏡像),使用 MySQL 客戶端發(fā)送測(cè)試請(qǐng)求給 mysql master 節(jié)點(diǎn)( 主機(jī)名為 mysql-0.mysql;跨命名空間的話,主機(jī)名請(qǐng)使用 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é)點(diǎn)上創(chuàng)建demo數(shù)據(jù)庫(kù),并創(chuàng)建一個(gè)只有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)使用主機(jī)名為mysql-read來(lái)發(fā)送測(cè)試請(qǐng)求給服務(wù)器 (另開(kāi)窗口)
[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)滾動(dòng)更新、擴(kuò)展伸縮、更新策略、版本升級(jí)
RollingUpdate 更新策略在 StatefulSet 中實(shí)現(xiàn) Pod 的自動(dòng)滾動(dòng)更新。當(dāng)StatefulSet的 .spec.updateStrategy.type 設(shè)置為 RollingUpdate 時(shí),默認(rèn)為:RollingUpdate。StatefulSet 控制器將在 StatefulSet 中刪除并重新創(chuàng)建每個(gè) Pod。它將以與 Pod 終止相同的順序進(jìn)行(從最大的序數(shù)到最小的序數(shù)),每次更新一個(gè) Pod。在更新其前身之前,它將等待正在更新的 Pod 狀態(tài)變成正在運(yùn)行并就緒。
如下操作的滾動(dòng)更新是有 mysql 2-0的順序更新。
master、node節(jié)點(diǎn)下載測(cè)試的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....通過(guò)kubectl更新yaml文件[root@k8s-master01 mysql]# kubectl apply -f mysql-5.7.19-statefulset.yaml -n kube-publicstatefulset.apps/mysql configured查看滾動(dòng)更新的過(guò)程[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擴(kuò)容方法1:命令行,進(jìn)行擴(kuò)容[root@k8s-master01 mysql]# kubectl scale sts mysql --replicas=4 -n kube-publicstatefulset.apps/mysql scaled方法2:聲明式命令方式,直接修改stateful-demo-v2.yaml文件,進(jìn)行擴(kuò)容(推薦)[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:補(bǔ)丁式命令方式,直接編輯運(yùn)行中sts,進(jìn)行擴(kuò)容[root@k8s-master01 mysql]# kubectl edit sts mysql -n kube-public修改.spec.replicas的值spec: replicas: 4 方法4:補(bǔ)丁式命令方式,通過(guò)打補(bǔ)丁的方式,進(jìn)行擴(kuò)容[root@k8s-master01 mysql]# kubectl patch sts mysql -p '{"spec":{"replicas":4}}' -n kube-public 查看mysql pod動(dòng)態(tài)擴(kuò)容[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:命令行,進(jìn)行縮容[root@k8s-master01 mysql]# kubectl scale sts mysql --replicas=3 -n kube-publicstatefulset.apps/mysql scaled查看mysql pod動(dòng)態(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ù)動(dòng)態(tài)擴(kuò)容,自動(dòng)掛載原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ù)動(dòng)態(tài)擴(kuò)容,自動(dòng)掛載原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存儲(chǔ)目錄/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私有鏡像倉(cāng)庫(kù)部署-更新
Kubernetes kubectx/kubens切換context和namespace
Kubernetes 刪除namespace時(shí)卡在Terminating狀態(tài)
Kubernetes kubeadm初始化kubernetes集群延長(zhǎng)證書(shū)過(guò)期時(shí)間
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 存儲(chǔ)卷
Kubernetes 特殊存儲(chǔ)卷—Secret和ConfigMap
Kubernetes StatefulSet控制器
Kubernetes 認(rèn)證、授權(quán)和準(zhǔn)入控制
Kubernetes dashboard認(rèn)證訪問(wèn)
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 資源指標(biāo)和集群監(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