K8s使用方法
1 查看k8s的版本信息
[root@k8s-master ~]# kubectl version
Client Version: version.Info{Major:"1", Minor:"5", GitVersion:"v1.5.2", GitCommit:"269f928217957e7126dc87e6adfa82242bfe5b1e", GitTreeState:"clean", BuildDate:"2017-07-03T15:31:10Z", GoVersion:"go1.7.4", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"5", GitVersion:"v1.5.2", GitCommit:"269f928217957e7126dc87e6adfa82242bfe5b1e", GitTreeState:"clean", BuildDate:"2017-07-03T15:31:10Z", GoVersion:"go1.7.4", Compiler:"gc", Platform:"linux/amd64"}
[root@k8s-master ~]#? kubectl api-versions
apps/v1beta1
authentication.k8s.io/v1beta1
authorization.k8s.io/v1beta1
autoscaling/v1
batch/v1
certificates.k8s.io/v1alpha1
extensions/v1beta1
policy/v1beta1
rbac.authorization.k8s.io/v1alpha1
storage.k8s.io/v1beta1
v1
2 查看各組件狀態(tài)
[root@k8s-master yamls]# kubectl -s http://localhost:8080 get componentstatuses
NAME???????????????? STATUS??? MESSAGE????????????? ERROR
etcd-0?????????????? Healthy?? {"health": "true"}??
scheduler??????????? Healthy?? ok??????????????????
controller-manager?? Healthy?? ok
3 查看k8s的組成節(jié)點(diǎn)(該節(jié)點(diǎn)指的是對應(yīng)的服務(wù)器節(jié)點(diǎn))
[root@k8s-master yamls]# kubectl get node
NAME????????????? STATUS??? AGE
192.168.248.142?? Ready???? 18h
192.168.248.143?? Ready???? 20s
4 確認(rèn)Deployment
[root@k8s-master ~]# kubectl get deployment
NAME?????? DESIRED?? CURRENT?? UP-TO-DATE?? AVAILABLE?? AGE
foo??????? 1???????? 1???????? 1??????????? 0?????????? 15h
my-nginx?? 2???????? 2???????? 2??????????? 2?????????? 14h
5 確認(rèn)pod(該pod指的是對應(yīng)的容器)
[root@k8s-master ~]# kubectl get pods
NAME?????????????????????? READY???? STATUS???????????? RESTARTS?? AGE
foo-1189255365-3j6fr?????? 0/1?????? CrashLoopBackOff?? 17???????? 15h
my-nginx-379829228-pw1wj?? 1/1?????? Running??????????? 1????????? 14h
my-nginx-379829228-wmlrt?? 1/1?????? Running??????????? 1????????? 14h
[root@k8s-master ~]# kubectl delete pods foo-1189255365-3j6fr
pod "foo-1189255365-3j6fr" deleted
確認(rèn)結(jié)果看到foo-1189255365-3j6f為狀態(tài)為CrashLoopBackOff這是確保replicas為1的動作,執(zhí)行如下指令再次確認(rèn)
[root@k8s-master ~]# kubectl get deployments
NAME?????? DESIRED?? CURRENT?? UP-TO-DATE?? AVAILABLE?? AGE
foo??????? 1???????? 1???????? 1??????????? 0?????????? 15h
my-nginx?? 2???????? 2???????? 2??????????? 2?????????? 14h
刪除deployment,直接刪除pod觸發(fā)replicas的確保機(jī)制,則可以刪除deployment
[root@k8s-master ~]# kubectl delete deployment foo
deployment "foo" deleted
[root@k8s-master ~]# kubectl get pods
NAME?????????????????????? READY???? STATUS??? RESTARTS?? AGE
my-nginx-379829228-pw1wj?? 1/1?????? Running?? 1????????? 14h
my-nginx-379829228-wmlrt?? 1/1?????? Running?? 1????????? 14h
7 通過yaml文件創(chuàng)建pod
[root@k8s-master yamls]# cat mysql.yaml
---
kind: ReplicationController
apiVersion: v1
metadata:
? name: mysql
spec:
? replicas: 1
? selector:
??? name: mysql
? template:
??? metadata:
????? labels:
??????? name: mysql
??? spec:
????? containers:
????? - name: mysql
??????? image: mysql:5.7.16
??????? ports:
??????? - containerPort: 3306
????????? protocol: TCP
??????? env:
????????? - name: MYSQL_ROOT_PASSWORD
??????????? value: "hello123"
[root@k8s-master yamls]# cat sonar.yaml
---
kind: ReplicationController
apiVersion: v1
metadata:
? name: sonarqube
spec:
? replicas: 1
? selector:
??? name: sonarqube
? template:
??? metadata:
????? labels:
??????? name: sonarqube
??? spec:
????? containers:
????? - name: sonarqube
??????? image: sonarqube:5.6.5
??????? ports:
??????? - containerPort: 9000
????????? protocol: TCP
8 創(chuàng)建mysql及sonarqube
創(chuàng)建mysql
[root@k8s-master yamls]# kubectl create -f mysql.yaml
replicationcontroller "mysql" created
[root@k8s-master yamls]# kubectl get pod
NAME?????????????????????? READY???? STATUS??? RESTARTS?? AGE
my-nginx-379829228-pw1wj?? 1/1?????? Running?? 1????????? 14h
my-nginx-379829228-wmlrt?? 1/1?????? Running?? 1????????? 14h
mysql-vn2k3??????????????? 1/1?????? Running?? 0????????? 1m
[root@k8s-master yamls]# kubectl get rc
NAME????? DESIRED?? CURRENT?? READY???? AGE
mysql???? 1???????? 1???????? 1???????? 2m
創(chuàng)建sonarqube
[root@k8s-master yamls]# kubectl create -f sonar.yaml
replicationcontroller "sonarqube" created
[root@k8s-master yamls]# kubectl get pod
NAME?????????????????????? READY???? STATUS??? RESTARTS?? AGE
my-nginx-379829228-pw1wj?? 1/1?????? Running?? 1????????? 15h
my-nginx-379829228-wmlrt?? 1/1?????? Running?? 1????????? 15h
mysql-vn2k3??????????????? 1/1?????? Running?? 0????????? 18m
sonarqube-438sd??????????? 1/1?????? Running?? 0????????? 2m
[root@k8s-master yamls]# kubectl delete -f mysql.yaml
[root@k8s-master yamls]# kubectl delete -f sonar.yaml
9 描述某一個pod的詳細(xì)信息
[root@k8s-master yamls]# kubectl describe pod mysql-vn2k3
Name:????? mysql-vn2k3
Namespace: default
Node:????? 192.168.248.142/192.168.248.142
Start Time:?? Thu, 11 Oct 2018 18:28:04 -0700
Labels:?????? name=mysql
Status:?????? Running
IP:??? 172.17.0.3
Controllers:? ReplicationController/mysql
Containers:
? mysql:
??? Container ID:??? docker://4cdbb18e4a4d863a6f777b7738125f42a80eb6c071bc62573d045b984df5744b
??? Image:??? mysql:5.7.16
??? Image ID:???? docker-pullable://docker.io/mysql@sha256:89cc6ff6a7ac9916c3384e864fb04b8ee9415b572f872a2a4cf5b909dbbca81b
??? Port:???? 3306/TCP
??? State:??? Running
????? Started:?????? Thu, 11 Oct 2018 18:29:44 -0700
??? Ready:??? True
??? Restart Count:?? 0
??? Volume Mounts:?? <none>
??? Environment Variables:
????? MYSQL_ROOT_PASSWORD:? hello123
Conditions:
? Type???? Status
? Initialized ??? True
? Ready ?? True
? PodScheduled ?? True
No volumes.
QoS Class: BestEffort
Tolerations:? <none>
Events:
? FirstSeen?? LastSeen?? Count? From????????????? SubObjectPath???? Type?????? Reason???? ??? Message
? ---------?? --------?? -----? ----????????????? -------------???? --------?? ------???? ??? -------
? 26m????? 26m??? 1?? {default-scheduler }?????????????? Normal???? Scheduled? ??? Successfully assigned mysql-vn2k3 to 192.168.248.142
? 26m????? 26m??? 1?? {kubelet 192.168.248.142}?? spec.containers{mysql}?? Normal ??? Pulling?????????? pulling image "mysql:5.7.16"
? 26m????? 25m??? 2?? {kubelet 192.168.248.142}????????????? Warning??? ??? MissingClusterDNS kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. Falling back to DNSDefault policy.
??25m????? 25m??? 1?? {kubelet 192.168.248.142}?? spec.containers{mysql}?? Normal ??? Pulled??????? Successfully pulled image "mysql:5.7.16"
? 24m????? 24m??? 1?? {kubelet 192.168.248.142}?? spec.containers{mysql}?? Normal ??? Created?????????? Created container with docker id 4cdbb18e4a4d; Security:[seccomp=unconfined]
? 24m????? 24m??? 1?? {kubelet 192.168.248.142}?? spec.containers{mysql}?? Normal ??? Started?????????? Started container with docker id 4cdbb18e4a4d
10 scale
scale用于程序在負(fù)載加重或縮小時副本進(jìn)行擴(kuò)容或縮小,如前面創(chuàng)建的nginx有兩個副本,可以輕松的使用scale命令對副本數(shù)進(jìn)行擴(kuò)展或縮小。
擴(kuò)展副本數(shù)到4:
[root@k8s-master yamls]# kubectl scale rc mysql --replicas=4?
replicationcontroller "mysql" scaled
[root@k8s-master yamls]# kubectl get rc
[root@k8s-master ~]# kubectl get rc
NAME??????? DESIRED?? CURRENT?? READY???? AGE
mysql?????? 2???????? 2???????? 2 ????????1h
sonarqube?? 1???????? 1???????? 1???????? 1h
重新縮減副本數(shù)到2:
[root@k8s-master yamls]# kubectl scale rc mysql --replicas=2
replicationcontroller "mysql" scaled
[root@k8s-master yamls]# kubectl get pod
NAME?????????????????????? READY???? STATUS??? RESTARTS?? AGE
my-nginx-379829228-pw1wj?? 1/1?????? Running?? 1????????? 15h
my-nginx-379829228-wmlrt?? 1/1?????? Running?? 1????????? 15h
mysql-8wz8j??????????????? 1/1?????? Running?? 0????????? 1m
mysql-vn2k3??????????????? 1/1?????? Running?? 0??????? ??50m
sonarqube-438sd??????????? 1/1?????? Running?? 0????????? 33m
11獲取指定json或ymal格式的KEY數(shù)據(jù),custom-columns=XXXXX(自定義列名):.status.hostIP(以“點(diǎn)開始”,然后寫路徑就可以):
[root@k8s-master yamls]# kubectl get pod mysql-vn2k3? -o custom-columns=HOST-IP:.status.hostIP,POD-IP:.status.podIP
HOST-IP?????????? POD-IP
192.168.248.142?? 172.17.0.3
12 查看某個pod重啟次數(shù)
[root@k8s-master ~]# kubectl get pod my-nginx-379829228-pw1wj --template="{{range .status.containerStatuses}}{{.name}}:{{.restartCount}}{{end}}"
my-nginx:1
13 查看pod的生命周期
[root@k8s-master ~]# kubectl get pod sonarqube-438sd --template="{{.status.phase}}"
Running
14 容器內(nèi)日志輸出
logs命令用于顯示pod運(yùn)行中,容器內(nèi)程序輸出到標(biāo)準(zhǔn)輸出的內(nèi)容。跟docker的logs命令類似。如果要獲得tail -f 的方式,也可以使用-f選項(xiàng)。
[root@k8s-master ~]# kubectl logs mysql-8wz8j
Initializing database
2018-10-12T02:16:34.904907Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-10-12T02:16:40.315744Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-10-12T02:16:44.441289Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-10-12T02:16:48.178465Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: df7fe121-cdc4-11e8-a664-0242ac110006.
2018-10-12T02:16:48.221676Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-10-12T02:16:48.239652Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
2018-10-12T02:16:53.319037Z 1 [Warning] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode.
2018-10-12T02:16:53.324377Z 1 [Warning] 'user' entry 'mysql.sys@localhost' ignored in --skip-name-resolve mode.
2018-10-12T02:16:53.324458Z 1 [Warning] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.
2018-10-12T02:16:53.327143Z 1 [Warning] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.
2018-10-12T02:16:53.327271Z 1 [Warning] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.
Database initialized
MySQL init process in progress...
MySQL init process in progress...
MySQL init process in progress...
MySQL init process in progress...
MySQL init process in progress...
2018-10-12T02:17:22.381789Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-10-12T02:17:25.089076Z 0 [Note] mysqld (mysqld 5.7.16) starting as process 53 ...
2018-10-12T02:17:25.419875Z 0 [Note] InnoDB: PUNCH HOLE support available
2018-10-12T02:17:25.422563Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2018-10-12T02:17:25.424559Z 0 [Note] InnoDB: Uses event mutexes
2018-10-12T02:17:25.424598Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
2018-10-12T02:17:25.425938Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.3
2018-10-12T02:17:25.426742Z 0 [Note] InnoDB: Using Linux native AIO
2018-10-12T02:17:25.434586Z 0 [Note] InnoDB: Number of pools: 1
2018-10-12T02:17:25.446547Z 0 [Note] InnoDB: Using CPU crc32 instructions
15 autoscale
scale雖然能夠很方便的對副本數(shù)進(jìn)行擴(kuò)展或縮小,但是仍然需要人工介入,不能實(shí)時自動的根據(jù)系統(tǒng)負(fù)載對副本數(shù)進(jìn)行擴(kuò)、縮。autoscale命令提供了自動根據(jù)pod負(fù)載對其副本進(jìn)行擴(kuò)縮的功能。
autoscale命令會給一個rc指定一個副本數(shù)的范圍,在實(shí)際運(yùn)行中根據(jù)pod中運(yùn)行的程序的負(fù)載自動在指定的范圍內(nèi)對pod進(jìn)行擴(kuò)容或縮容。如前面創(chuàng)建的nginx,可以用如下命令指定副本范圍在1~4
[root@k8s-master ~]# kubectl autoscale rc sonarqube --min=1 --max=4
replicationcontroller "sonarqube" autoscaled
16 查看容器運(yùn)行在哪個node節(jié)點(diǎn)
[root@k8s-master ~]# kubectl get pod -o wide
[root@k8s-master ~]# kubectl get pod -o wide
NAME??????????????????????? READY???? STATUS??? RESTARTS?? AGE?????? IP?????????? NODE
my-nginx-379829228-pw1wj??? 1/1??? ???Running?? 1????????? 17h?????? 172.17.0.2?? 192.168.248.142
my-nginx-379829228-wmlrt??? 1/1?????? Running?? 1????????? 17h?????? 172.17.0.4?? 192.168.248.142
mysql-8wz8j???????????????? 1/1?????? Running?? 0????????? 1h??????? 172.17.0.6?? 192.168.248.142
mysql-vn2k3???????????????? 1/1?????? Running?? 0????????? 2h??????? 172.17.0.3?? 192.168.248.142
sonarqube-438sd???????????? 1/1?????? Running?? 0????????? 1h??????? 172.17.0.5?? 192.168.248.142
testnginx-94870464-9nkxn??? 1/1?????? Running?? 0??????? ??1m??????? 172.17.0.5?? 192.168.248.143
testnginx-94870464-k3hdg??? 1/1?????? Running?? 0????????? 1m??????? 172.17.0.4?? 192.168.248.143
testnginx-94870464-r208x??? 1/1?????? Running?? 0????????? 1m??????? 172.17.0.7?? 192.168.248.142
testnginx-94870464-s9c9x??? 1/1?????? Running?? 0????????? 1m??????? 172.17.0.8?? 192.168.248.142
testnginx-94870464-sz6pb??? 1/1?????? Running?? 0????????? 1m??????? 172.17.0.3?? 192.168.248.143
younginx-2413665018-q2nvq?? 1/1?????? Running?? 0????????? 53m?????? 172.17.0.2?? 192.168.248.143
16 cordon, drain, uncordon
這三個命令是正式release的1.2新加入的命令,三個命令一起介紹,是因?yàn)槿齻€命令配合使用可以實(shí)現(xiàn)節(jié)點(diǎn)的維護(hù)。在1.2之前,因?yàn)闆]有相應(yīng)的命令支持,如果要維護(hù)一個節(jié)點(diǎn),只能stop該節(jié)點(diǎn)上的kubelet將該節(jié)點(diǎn)退出集群,是集群不在將新的pod調(diào)度到該節(jié)點(diǎn)上。如果該節(jié)點(diǎn)上本生就沒有pod在運(yùn)行,則不會對業(yè)務(wù)有任何影響。如果該節(jié)點(diǎn)上有pod正在運(yùn)行,kubelet停止后,master會發(fā)現(xiàn)該節(jié)點(diǎn)不可達(dá),而將該節(jié)點(diǎn)標(biāo)記為notReady狀態(tài),不會將新的節(jié)點(diǎn)調(diào)度到該節(jié)點(diǎn)上。同時,會在其他節(jié)點(diǎn)上創(chuàng)建新的pod替換該節(jié)點(diǎn)上的pod。這種方式雖然能夠保證集群的健壯性,但是任然有些暴力,如果業(yè)務(wù)只有一個副本,而且該副本正好運(yùn)行在被維護(hù)節(jié)點(diǎn)上的話,可能仍然會造成業(yè)務(wù)的短暫中斷。
1.2中新加入的這3個命令可以保證維護(hù)節(jié)點(diǎn)時,平滑的將被維護(hù)節(jié)點(diǎn)上的業(yè)務(wù)遷移到其他節(jié)點(diǎn)上,保證業(yè)務(wù)不受影響。如下圖所示是一個整個的節(jié)點(diǎn)維護(hù)的流程(為了方便demo增加了一些查看節(jié)點(diǎn)信息的操作):1)首先查看當(dāng)前集群所有節(jié)點(diǎn)狀態(tài),可以看到共四個節(jié)點(diǎn)都處于ready狀態(tài);2)查看當(dāng)前nginx兩個副本分別運(yùn)行在k8s-node和k8s-node1兩個節(jié)點(diǎn)上;3)使用cordon命令將k8s-node1標(biāo)記為不可調(diào)度;4)再使用kubectl get nodes查看節(jié)點(diǎn)狀態(tài),發(fā)現(xiàn)k8s-node1雖然還處于Ready狀態(tài),但是同時還被禁能了調(diào)度,這意味著新的pod將不會被調(diào)度到k8s-node1上。4)再查看nginx狀態(tài),沒有任何變化,兩個副本仍運(yùn)行在k8s-node和k8s-node1上;5)執(zhí)行drain命令,將運(yùn)行在k8s-node1上運(yùn)行的pod平滑的趕到其他節(jié)點(diǎn)上;6)再查看nginx的狀態(tài)發(fā)現(xiàn),k8s-node1上的副本已經(jīng)被遷移到k8s-node上;這時候就可以對k8s-node1進(jìn)行一些節(jié)點(diǎn)維護(hù)的操作,如升級內(nèi)核,升級Docker等;7)節(jié)點(diǎn)維護(hù)完后,使用uncordon命令解鎖k8s-node1,使其重新變得可調(diào)度;8)檢查節(jié)點(diǎn)狀態(tài),發(fā)現(xiàn)k8s-node1重新變回Ready狀態(tài)
[root@k8s-master ~]# kubectl get node
NAME????????????? STATUS??? AGE
192.168.248.142?? Ready???? 19h
192.168.248.143?? Ready???? 1h
[root@k8s-master ~]# kubectl cordon 192.168.248.143
node "192.168.248.143" cordoned
[root@k8s-master ~]# kubectl get node
NAME? ????????????STATUS???????????????????? AGE
192.168.248.142?? Ready????????????????????? 19h
192.168.248.143?? Ready,SchedulingDisabled?? 1h
[root@k8s-master ~]# kubectl get pod -o wide
NAME??????????????????????? READY???? STATUS??? RESTARTS?? AGE?????? IP?????????? NODE
my-nginx-379829228-pw1wj??? 1/1?????? Running?? 1????????? 17h?????? 172.17.0.2?? 192.168.248.142
my-nginx-379829228-wmlrt??? 1/1?????? Running?? 1????????? 17h?????? 172.17.0.4?? 192.168.248.142
mysql-8wz8j???????????????? 1/1?????? Running?? 0????????? 1h??????? 172.17.0.6?? 192.168.248.142
mysql-vn2k3???????????????? 1/1?????? Running?? 0????????? 2h??????? 172.17.0.3?? 192.168.248.142
sonarqube-438sd???????????? 1/1?????? Running?? 0????????? 2h??????? 172.17.0.5?? 192.168.248.142
testnginx-94870464-9nkxn??? 1/1?????? Running?? 0????????? 12m?????? 172.17.0.5?? 192.168.248.143
testnginx-94870464-k3hdg??? 1/1?????? Running?? 0????????? 12m?????? 172.17.0.4?? 192.168.248.143
testnginx-94870464-r208x??? 1/1?????? Running?? 0????????? 12m??? ???172.17.0.7?? 192.168.248.142
testnginx-94870464-s9c9x??? 1/1?????? Running?? 0????????? 12m?????? 172.17.0.8?? 192.168.248.142
testnginx-94870464-sz6pb??? 1/1?????? Running?? 0????????? 12m?????? 172.17.0.3?? 192.168.248.143
younginx-2413665018-q2nvq?? 1/1?????? Running?? 0????????? 1h??????? 172.17.0.2?? 192.168.248.143
[root@k8s-master ~]# kubectl drain 192.168.248.143
node "192.168.248.143" already cordoned
pod "testnginx-94870464-sz6pb" evicted
pod "testnginx-94870464-k3hdg" evicted
pod "younginx-2413665018-q2nvq" evicted
pod "testnginx-94870464-9nkxn" evicted
node "192.168.248.143" drained
[root@k8s-master ~]# kubectl uncordon 192.168.248.143
node "192.168.248.143" uncordoned
[root@k8s-master ~]# kubectl get pod -o wide
NAME??????????????????????? READY???? STATUS??? RESTARTS?? AGE?????? IP??????????? NODE
my-nginx-379829228-pw1wj??? 1/1?????? Running?? 1????????? 18h?????? 172.17.0.2??? 192.168.248.142
my-nginx-379829228-wmlrt??? 1/1?????? Running?? 1????????? 18h?????? 172.17.0.4??? 192.168.248.142
mysql-8wz8j???????????????? 1/1?????? Running?? 0????????? 2h??????? 172.17.0.6??? 192.168.248.142
mysql-vn2k3???????????????? 1/1?????? Running?? 0????????? 3h??????? 172.17.0.3??? 192.168.248.142
sonarqube-438sd???????????? 1/1?????? Running?? 0???????? ?3h??????? 172.17.0.5??? 192.168.248.142
testnginx-94870464-57pkj??? 1/1?????? Running?? 0????????? 55m?????? 172.17.0.11?? 192.168.248.142
testnginx-94870464-dgqdg??? 1/1?????? Running?? 0????????? 55m?????? 172.17.0.9??? 192.168.248.142
testnginx-94870464-r208x??? 1/1?????? Running?? 0????????? 1h??????? 172.17.0.7??? 192.168.248.142
testnginx-94870464-s9c9x??? 1/1?????? Running?? 0????????? 1h??????? 172.17.0.8??? 192.168.248.142
testnginx-94870464-zhq8g??? 1/1?????? Running?? 0????????? 55m?????? 172.17.0.10?? 192.168.248.142
younginx-2413665018-djpr4?? 1/1?????? Running?? 0????????? 55m?????? 172.17.0.12?? 192.168.248.142
17 rolling-update
rolling-update是一個非常重要的命令,對于已經(jīng)部署并且正在運(yùn)行的業(yè)務(wù),rolling-update提供了不中斷業(yè)務(wù)的更新方式。rolling-update每次起一個新的pod,等新pod完全起來后刪除一個舊的pod,然后再起一個新的pod替換舊的pod,直到替換掉所有的pod。
rolling-update需要確保新的版本有不同的name,Version和label,否則會報錯 。
18 edit
edit提供了另一種更新resource源的操作,通過edit能夠靈活的在一個common的resource基礎(chǔ)上,發(fā)展出更過的significant resource。
[root@k8s-master yamls]# kubectl get pod mysql-vn2k3 -o yaml >> /tmp/mysql-tmp.yaml
[root@k8s-master yamls]# cd /tmp
[root@k8s-master tmp]# ls
firefox_wzf?????? mysql-tmp.yaml?????????????????????????????????????????????????????????? systemd-private-d1eff1b44a034169825ab8953453f7b1-rtkit-daemon.service-mZYosX? vmware-config0
hsperfdata_root?? ssh-oGW5Z3EQV1MZ???????????????????????????????????????????????????????? Temp-06a78ebe-5e0b-4213-9d60-db80b8bb4c47???????????????????????????????????? vmware-root
ks-script-5YmpDP? systemd-private-d1eff1b44a034169825ab8953453f7b1-chronyd.service-n8j5ru? tmp.nLlvBLCtsm??????????????????????????????????????????????????????????????? yum.log
ks-script-IH2Z_2? systemd-private-d1eff1b44a034169825ab8953453f7b1-colord.service-HAlGh3?? tmp.tzkmKHEunU????????????????????????????????????????????????????? ??????????yum_save_tx.2018-10-11.00-34.fI_o2U.yumtx
lua_a0C7G7??????? systemd-private-d1eff1b44a034169825ab8953453f7b1-cups.service-bsrX2b???? tracker-extract-files.1000
[root@k8s-master tmp]# vim mysql-tmp.yaml
/做一些配置文件的修改/
kubectl replace –fmysql-tmp.yaml
19如果一個容器已經(jīng)在運(yùn)行,這時需要對一些容器屬性進(jìn)行修改,又不想刪除容器,或不方便通過replace的方式進(jìn)行更新。kubernetes還提供了一種在容器運(yùn)行時,直接對容器進(jìn)行修改的方式,就是patch命令。
如前面創(chuàng)建pod的label是app=nginx-2,如果在運(yùn)行過程中,需要把其label改為app=nginx-3。
[root@k8s-master ~]# kubectl patch pod my-nginx-379829228-pw1wj -p '{"metadata":{"labels":{"app":"nginx-3"}}}'
20 replace更新替換資源
replace命令用于對已有資源進(jìn)行更新、替換。如前面create中創(chuàng)建的mysql,當(dāng)我們需要更新resource的一些屬性的時候,如果修改副本數(shù)量,增加、修改label,更改image版本,修改端口等。都可以直接修改原yaml文件,然后執(zhí)行replace命令。
注:名字不能被更更新。另外,如果是更新label,原有標(biāo)簽的pod將會與更新label后的rc斷開聯(lián)系,有新label的rc將會創(chuàng)建指定副本數(shù)的新的pod,但是默認(rèn)并不會刪除原來的pod。所以此時如果使用get po將會發(fā)現(xiàn)pod數(shù)翻倍,進(jìn)一步check會發(fā)現(xiàn)原來的pod已經(jīng)不會被新rc控制
?kubectl replace -f mysql.yaml
21測試穩(wěn)定性
在node上通過docker stop $(docker ps -a -q)停止容器,發(fā)現(xiàn)k8s會自動重新生成新容器
[root@k8s-node ~]# docker stop $(docker ps -a -q)
d6245b7cd653
82c2032630cf
6c63dd03bd40
f2ed49c519d4
b3e1d844a4bd
bcde1cbffb77
4dad2b30e835
2b2c3c3bd62e
72535e112570
e04d5be2b2ca
8f49e0d5dcf4
9ec557300ec5
3469ee4b3fa6
cb8c74f84229
8d5a48ea55a7
f8f68f273f3a
911281e9ff32
6efd0d3ff39c
e893bceabce6
4cdbb18e4a4d
bf6c67772f1d
350faeb2a846
562c22f0f72c
3eb1470101f0
aaae22de2e1f
[root@k8s-node ~]# docker ps -a
CONTAINER ID??????? IMAGE??????????????????????????????????????????????????????? COMMAND????????????????? CREATED???????????? STATUS????????????????????????? PORTS???? ??????????NAMES
d7de526077a9??????? nginx??????????????????????????????????????????????????????? "nginx -g 'daemon ..."?? 1 second ago??????? Up Less than a second?????????????????????????????? k8s_testnginx.affadd93_testnginx-94870464-dgqdg_default_3f639a8e-cdd2-11e8-94c7-000c29ee98c6_429fea38
9ef0ed5e1452??????? registry.access.redhat.com/rhel7/pod-infrastructure:latest?? "/usr/bin/pod"?????????? 3 seconds ago?????? Up Less than a second?????????????????????????????? k8s_POD.1d520ba5_mysql-vn2k3_default_110cf8c4-cdbe-11e8-94c7-000c29ee98c6_c7954e7c
86d6620c4b61??????? mysql:5.7.16???????????????????????????????????????????????? "docker-entrypoint..."?? 3 seconds ago?????? Up 2 seconds??????????????????????????????????????? k8s_mysql.f4f82081_mysql-8wz8j_default_cfab4bb8-cdc4-11e8-94c7-000c29ee98c6_a9707fc2
20c1db52c18a??????? sonarqube:5.6.5????????????????????????????????????????????? "./bin/run.sh"?????????? 4 seconds ago?????? Up 2 seconds??????????????????????????????????????? k8s_sonarqube.5a6904a7_sonarqube-438sd_default_4eeb9745-cdc0-11e8-94c7-000c29ee98c6_3d66d17e
ca136f13a08d??????? registry.access.redhat.com/rhel7/pod-infrastructure:latest?? "/usr/bin/pod"?????????? 6 seconds ago?????? Up 3 seconds??????????????????????????????????????? k8s_POD.1d520ba5_mysql-8wz8j_default_cfab4bb8-cdc4-11e8-94c7-000c29ee98c6_bd4258af
49c191c3debe??????? nginx??????????????????????????????????????????????????????? "nginx -g 'daemon ..."?? 7 seconds ago?????? Up 3 seconds??????????????????????????????????????? k8s_my-nginx.a65fe6c_my-nginx-379829228-pw1wj_default_75a3218b-cd41-11e8-993b-000c29ee98c6_d78f4ca0
2ca27f74d8cc??????? registry.access.redhat.com/rhel7/pod-infrastructure:latest?? "/usr/bin/pod"?????????? 7 seconds ago?????? Up 4 seconds??????????????????????? ????????????????k8s_POD.17af0ba2_sonarqube-438sd_default_4eeb9745-cdc0-11e8-94c7-000c29ee98c6_4bc68f43
720ca258413e??????? registry.access.redhat.com/rhel7/pod-infrastructure:latest?? "/usr/bin/pod"?????????? 8 seconds ago?????? Up 4 seconds?????????????? ?????????????????????????k8s_POD.ae8ee9ac_testnginx-94870464-r208x_default_4d8fdaed-cdd0-11e8-94c7-000c29ee98c6_c1d37620
95dfffa18d88??????? registry.access.redhat.com/rhel7/pod-infrastructure:latest?? "/usr/bin/pod"?????????? 10 seconds ago????? Up 8 seconds??????????????????????????????????????? k8s_POD.ae8ee9ac_testnginx-94870464-57pkj_default_3f6368c2-cdd2-11e8-94c7-000c29ee98c6_2b0a6b54
5557753c36c8??????? registry.access.redhat.com/rhel7/pod-infrastructure:latest?? "/usr/bin/pod"?????????? 12 seconds ago????? Up 10 seconds?????????????????????????????????????? k8s_POD.a8590b41_my-nginx-379829228-wmlrt_default_75a284fc-cd41-11e8-993b-000c29ee98c6_5174be7e
9b99654e2f98??????? registry.access.redhat.com/rhel7/pod-infrastructure:latest?? "/usr/bin/pod"??? ???????12 seconds ago????? Up 10 seconds?????????????????????????????????????? k8s_POD.ae8ee9ac_testnginx-94870464-zhq8g_default_3f51dbae-cdd2-11e8-94c7-000c29ee98c6_b422e753
c76dcef68f3b??????? registry.access.redhat.com/rhel7/pod-infrastructure:latest?? "/usr/bin/pod"?????????? 14 seconds ago????? Up 11 seconds?????????????????????????????????????? k8s_POD.ae8ee9ac_testnginx-94870464-s9c9x_default_4d8f42b0-cdd0-11e8-94c7-000c29ee98c6_a07a56a6
bdee5d0dcf94??????? registry.access.redhat.com/rhel7/pod-infrastructure:latest?? "/usr/bin/pod"?????????? 15 seconds ago????? Up 13 seconds?????????????????????????????????????? k8s_POD.ae8ee9ac_younginx-2413665018-djpr4_default_3f52038e-cdd2-11e8-94c7-000c29ee98c6_581d115a
3e45d88946e4??????? registry.access.redhat.com/rhel7/pod-infrastructure:latest?? "/usr/bin/pod"?????????? 19 seconds ago????? Up 15 seconds?????????????????????????????????????? k8s_POD.ae8ee9ac_testnginx-94870464-dgqdg_default_3f639a8e-cdd2-11e8-94c7-000c29ee98c6_06050950
42d8b7f25787??????? registry.access.redhat.com/rhel7/pod-infrastructure:latest?? "/usr/bin/pod"?????????? 19 seconds ago????? Up 15 seconds?????????????????????????????????????? k8s_POD.a8590b41_my-nginx-379829228-pw1wj_default_75a3218b-cd41-11e8-993b-000c29ee98c6_3b93844c
8f017385d1f8??????? registry.access.redhat.com/rhel7/pod-infrastructure:latest?? "/usr/bin/pod"?????????? 22 seconds ago????? Created???????????????????????????????????????????? k8s_POD.ae8ee9ac_testnginx-94870464-r208x_default_4d8fdaed-cdd0-11e8-94c7-000c29ee98c6_db46c731
730a53238394??????? registry.access.redhat.com/rhel7/pod-infrastructure:latest?? "/usr/bin/pod"?????????? 22 seconds ago????? Created???????????????????????????????????????????? k8s_POD.1d520ba5_mysql-vn2k3_default_110cf8c4-cdbe-11e8-94c7-000c29ee98c6_23252339
06234eb7bc47??????? registry.access.redhat.com/rhel7/pod-infrastructure:latest?? "/usr/bin/pod"?????????? 23 seconds ago????? Created???????????????????????????????????????????? k8s_POD.1d520ba5_mysql-8wz8j_default_cfab4bb8-cdc4-11e8-94c7-000c29ee98c6_9f5893a2
59258eb463ad??????? registry.access.redhat.com/rhel7/pod-infrastructure:latest?? "/usr/bin/pod"?????????? 44 seconds ago????? Created???????????????????????????????????????????? k8s_POD.1d520ba5_mysql-vn2k3_default_110cf8c4-cdbe-11e8-94c7-000c29ee98c6_9fdc5868
8203403a4b26??????? registry.access.redhat.com/rhel7/pod-infrastructure:latest?? "/usr/bin/pod"?????????? 48 seconds ago????? Created???????????????????????????????????????????? k8s_POD.1d520ba5_mysql-8wz8j_default_cfab4bb8-cdc4-11e8-94c7-000c29ee98c6_89d3d41b
7fec434356b3??????? registry.access.redhat.com/rhel7/pod-infrastructure:latest?? "/usr/bin/pod"?????????? 51 seconds ago????? Created???????????????????????????????????????????? k8s_POD.a8590b41_my-nginx-379829228-wmlrt_default_75a284fc-cd41-11e8-993b-000c29ee98c6_72d9e2e8
d562725bb32a??????? registry.access.redhat.com/rhel7/pod-infrastructure:latest?? "/usr/bin/pod"?????????? 55 seconds ago????? Created???????????????????????????????????????????? k8s_POD.a8590b41_my-nginx-379829228-pw1wj_default_75a3218b-cd41-11e8-993b-000c29ee98c6_957a8f06
aed15bcffdad??????? registry.access.redhat.com/rhel7/pod-infrastructure:latest?? "/usr/bin/pod"?????????? 58 seconds ago????? Created???????????????????????????????????????????? k8s_POD.ae8ee9ac_testnginx-94870464-dgqdg_default_3f639a8e-cdd2-11e8-94c7-000c29ee98c6_f720e
備注:RC(Replication Controller)
??? Replication Controller確保任何時候Kubernetes集群中有指定數(shù)量的pod副本(replicas)在運(yùn)行, 如果少于指定數(shù)量的pod副本(replicas),Replication Controller會啟動新的Container,反之會殺死多余的以保證數(shù)量不變。Replication Controller使用預(yù)先定義的pod模板創(chuàng)建pods,一旦創(chuàng)建成功,pod 模板和創(chuàng)建的pods沒有任何關(guān)聯(lián),可以修改pod 模板而不會對已創(chuàng)建pods有任何影響,也可以直接更新通過Replication Controller創(chuàng)建的pods。對于利用pod 模板創(chuàng)建的pods,Replication Controller根據(jù)label selector來關(guān)聯(lián),通過修改pods的label可以刪除對應(yīng)的pods。
22 進(jìn)入創(chuàng)建的pod內(nèi)
[root@k8s-master ~]# kubectl get pod
NAME??????????????????????? READY???? STATUS??? RESTARTS?? AGE
my-nginx-379829228-pw1wj??? 1/1?????? Running?? 2????????? 19h
my-nginx-379829228-wmlrt??? 1/1?????? Running?? 2????????? 19h
mysql-8wz8j???????????????? 1/1?????? Running?? 1????????? 4h
mysql-vn2k3???????????????? 1/1?????? Running?? 1????????? 4h
sonarqube-438sd???????????? 1/1?? ????Running?? 1????????? 4h
testnginx-94870464-57pkj??? 1/1?????? Running?? 1????????? 2h
testnginx-94870464-dgqdg??? 1/1?????? Running?? 1????????? 2h
testnginx-94870464-r208x??? 1/1?????? Running?? 1????????? 2h
testnginx-94870464-s9c9x??? 1/1?????? Running?? 1????????? 2h
testnginx-94870464-zhq8g??? 1/1?????? Running?? 1????????? 2h
younginx-2413665018-djpr4?? 1/1?????? Running?? 1????????? 2h
[root@k8s-master ~]# kubectl exec my-nginx-379829228-pw1wj? date
Fri Oct 12 06:27:59 UTC 2018
[root@k8s-master ~]# kubectl exec my-nginx-379829228-pw1wj? -it bash
root@my-nginx-379829228-pw1wj:/# ifconfig
23 attach
? ? ?attach命令類似于docker的attach命令,可以直接查看容器中以daemon形式運(yùn)行的進(jìn)程的輸出,效果類似于logs -f,退出查看使用ctrl-c。
[root@k8s-master ~]# kubectl get pod
NAME??????????????????????? READY???? STATUS??? RESTARTS?? AGE
my-nginx-379829228-pw1wj??? 1/1?????? Running?? 2????????? 21h
my-nginx-379829228-wmlrt??? 1/1?????? Running?? 2????????? 21h
mysql-8wz8j???????????????? 1/1?????? Running?? 1????????? 5h
mysql-vn2k3???????????????? 1/1? ?????Running?? 1????????? 6h
sonarqube-438sd???????????? 1/1?????? Running?? 1????????? 6h
testnginx-94870464-57pkj??? 1/1?????? Running?? 1????????? 4h
testnginx-94870464-dgqdg??? 1/1?????? Running?? 1????????? 4h
testnginx-94870464-r208x??? 1/1?????? Running?? 1????????? 4h
testnginx-94870464-s9c9x??? 1/1?????? Running?? 1????????? 4h
testnginx-94870464-zhq8g??? 1/1?????? Running?? 1????????? 4h
younginx-2413665018-djpr4?? 1/1?????? Running?? 1????????? 4h
[root@k8s-master ~]# kubectl attach mysql-vn2k3
If you don't see a command prompt, try pressing enter.
Initializing database
2018-10-12T06:08:05.686626Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-10-12T06:08:08.351170Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-10-12T06:08:08.865590Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-10-12T06:08:08.976485Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 311992bb-cde5-11e8-beb9-0242ac11000c.
2018-10-12T06:08:08.979892Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-10-12T06:08:08.990000Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
2018-10-12T06:08:13.469651Z 1 [Warning] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode.
2018-10-12T06:08:13.473797Z 1 [Warning] 'user' entry 'mysql.sys@localhost' ignored in --skip-name-resolve mode.
2018-10-12T06:08:13.473872Z 1 [Warning] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.
2018-10-12T06:08:13.473904Z 1 [Warning] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.
2018-10-12T06:08:13.473994Z 1 [Warning] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.
Database initialized
MySQL init process in progress...
MySQL init process in progress...
2018-10-12T06:08:18.765789Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-10-12T06:08:18.853393Z 0 [Note] mysqld (mysqld 5.7.16) starting as process 53 ...
MySQL init process in progress...
MySQL init process in progress...
2018-10-12T06:08:42.181567Z 0 [Note] InnoDB: PUNCH HOLE support available
2018-10-12T06:08:42.376104Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2018-10-12T06:08:42.376825Z 0 [Note] InnoDB: Uses event mutexes
2018-10-12T06:08:42.376866Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
2018-10-12T06:08:42.376879Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.3
2018-10-12T06:08:42.376890Z 0 [Note] InnoDB: Using Linux native AIO
2018-10-12T06:08:42.384160Z 0 [Note] InnoDB: Number of pools: 1