服務探針與回調hook(健康檢查)
分布式系統和微服務體系結構的挑戰之一是自動檢測不正常的應用程序,並將請求(request)重新路由到其他可用系統,恢復損壞的組件。健康檢查是應對該挑戰的一種可靠方法。使用 Kubernetes,可以通過探針配置運行狀況檢查,以確定每個 Pod 的狀態
Kubernetes提供了健康檢查服務,對於檢測到故障服務會被及時自動下線,以及通過重啟服務的方式使服務自動恢復
pod生命周期
一、存活性探針(LivenessProbe)
存活性探針用於判斷容器是否存活,即Pod是否為running狀態
如果LivenessProbe探針探測到容器不健康,則kubelet將kill掉容器,並根據容器的重啟策略判斷按照那種方式重啟,
如果一個容器不包含LivenessProbe探針,則Kubelet認為容器的LivenessProbe探針的返回值永遠成功
1、存活型檢查基本用法
1)用於判斷容器是否存活
2)處理的方式:如果判斷失敗,則重啟POD
3)存活性檢測使用exec,就緒性檢測使用tcpSocket,httpGet
#探針相關參數:
delay=0s :容器啟動多久開始探測
timeout=1s :容器探測超時時間
period=10s :探測的頻率
success=1 :探測成功多少次為成功
failure=3 :探測失敗多少次為失敗
2、存活性探針三種使用方式
ExecAction
TCPSocketAction
HTTPGetAction
【ExecAction】
通過執行一條命令,探測服務是否可以正常對外提供服務
#資源清單編寫:
[root@m01 /hzl]# cat LivenessProbe.yaml
---
kind: Deployment
apiVersion: apps/v1
metadata:
name: test-deployment
spec:
replicas: 1
selector:
matchLabels:
app: deployment
template:
metadata:
labels:
app: deployment
spec:
containers:
- name: nginx
image: alvinos/django:v1
livenessProbe:
exec:
command:
- "/bin/sh"
- "-c"
- "cat /root/test/manage.py"
---
kind: Service
apiVersion: v1
metadata:
name: test-svc
namespace: default
spec:
ports:
- port: 80
targetPort: 80
name: http
selector:
app: deployment
#創建pod
[root@m01 /hzl]# kubectl create -f LivenessProbe.yaml
deployment.apps/test-deployment created
service/test-svc created
#查看詳細信息
[root@m01 /hzl]# kubectl describe deployments.apps test-deployment
Name: test-deployment
Namespace: default
CreationTimestamp: Sat, 07 Aug 2021 12:14:10 +0800
Labels: <none>
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=deployment
Replicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=deployment
Containers:
nginx:
Image: alvinos/django:v1
Port: <none>
Host Port: <none>
#使用exec執行的命令,判斷是否存活(如果可以查看到manage文件,則表示表示探測成功,反則失敗)
Liveness: exec [/bin/sh -c cat /root/test/manage.py] delay=0s timeout=1s period=10s #success=
········
.....
#查看pod狀態
[root@m01 /hzl]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-6799fc88d8-xtg4p 1/1 Running 0 95m
test-deployment-8475c54c94-vkrk4 1/1 Running 0 15m
【TCPSocketAction】
通過ping某個端口的方式,探測服務是否可以正常對外提供服務
#資源清單編寫:
[root@m01 /hzl]# cat livenessProbe.yaml
---
kind: Deployment
apiVersion: apps/v1
metadata:
name: deployment
spec:
replicas: 1
selector:
matchLabels:
app: deployment
template:
metadata:
labels:
app: deployment
spec:
containers:
- name: nginx
image: alvinos/django:v1
livenessProbe:
tcpSocket:
port: 80
---
kind: Service
apiVersion: v1
metadata:
name: test-svc
namespace: default
spec:
ports:
- port: 80
targetPort: 80
name: http
selector:
app: deployment
#創建pod及svc
[root@m01 /hzl]# kubectl delete -f livenessProbe.yaml
deployment.apps "deployment" deleted
service "test-svc" deleted
#查看探測狀態:探測成功
[root@m01 /hzl]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deployment-569f4b99ff-tskhs 1/1 Running 0 3m4s
nginx-6799fc88d8-6c892 1/1 Running 0 15m
test-deployment-8475c54c94-nkqx6 1/1 Running 0 4m57s
#查看詳情:
[root@m01 /hzl]# kubectl describe pod test-deployment-569f4b99ff-mwxms
Name: test-deployment-569f4b99ff-mwxms
Namespace: default
Priority: 0
Node: nod02/192.168.15.57
Start Time: Sat, 07 Aug 2021 12:59:11 +0800
Labels: app=deployment
pod-template-hash=569f4b99ff
Annotations: <none>
Status: Running
IP: 10.244.2.48
IPs:
IP: 10.244.2.48
Controlled By: ReplicaSet/test-deployment-569f4b99ff
Containers:
nginx:
Container ID: docker://716695d00c0fb4538a4fd722233a25948b1d4bfdc62385dcc465f4f9f103a756
Image: alvinos/django:v1
Image ID: docker-pullable://alvinos/django@sha256:fc5ffecb7d5038940006d5d7e9ea6414fc4ec0f2509501aebdc3280f1a559723
Port: <none>
Host Port: <none>
State: Running
Started: Sat, 07 Aug 2021 12:59:32 +0800
Ready: True
Restart Count: 0
#使用socket探測,探測成功
Liveness: tcp-socket :80 delay=0s timeout=1s period=10s #success=1 #failure=3
################################## 測試 ################################################
#測試(更改端口測試)
[root@m01 /hzl]# vim livenessProbe.yaml
........
livenessProbe:
tcpSocket:
port: 8080
.......
..
#重新更新pod,svc
[root@m01 /hzl]# kubectl apply -f livenessProbe.yaml
deployment.apps/test-deployment created
service/test-svc created
#查看pod
[root@m01 /hzl]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deployment-569f4b99ff-smpkt 1/1 Running 0 8m8s
nginx-6799fc88d8-6c892 1/1 Running 0 26m
test-deployment-86788587f-jbnwm 1/1 Running 3 4m11s #探測不成功,已經重啟3次
#查看詳情
[root@m01 /hzl]# kubectl describe pod test-deployment-86788587f-jbnwm
Name: test-deployment-86788587f-jbnwm
Namespace: default
Priority: 0
Node: nod02/192.168.15.57
Start Time: Sat, 07 Aug 2021 13:01:56 +0800
Labels: app=deployment
pod-template-hash=86788587f
Annotations: <none>
Status: Running
IP: 10.244.2.49
IPs:
IP: 10.244.2.49
Controlled By: ReplicaSet/test-deployment-86788587f
Containers:
nginx:
Container ID: docker://9bd364e54903ae56906b1e311b6c3e6ef723566220bfd47aea1f6ad64d705c5d
Image: alvinos/django:v1
Image ID: docker-pullable://alvinos/django@sha256:fc5ffecb7d5038940006d5d7e9ea6414fc4ec0f2509501aebdc3280f1a559723
Port: <none>
Host Port: <none>
State: Waiting
Reason: CrashLoopBackOff
Last State: Terminated
Reason: Error
Exit Code: 137
Started: Sat, 07 Aug 2021 13:07:16 +0800
Finished: Sat, 07 Aug 2021 13:08:16 +0800
Ready: False
#探測不成功,重啟策略已經進行3次
Restart Count: 3
Liveness: tcp-socket :8080 delay=0s timeout=1s period=10s #success=1 #failure=3
【HTTPGetAction】
通過訪問某個URL的方式探測當前POD是否可以正常對外提供服務
#編寫資源清單:
[root@m01 /hzl]# vim livenessProbe.yaml
..........
.....
livenessProbe:
httpGet:
port: 80
path: /
.........
.....
#創建pod
[root@m01 /hzl]# kubectl apply -f livenessProbe.yaml
deployment.apps/test-deployment created
service/test-svc created
#查看pod
[root@m01 /hzl]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deployment-569f4b99ff-smpkt 1/1 Running 0 21m
nginx-6799fc88d8-6c892 1/1 Running 0 40m
test-deployment-59f6d99fdf-6hxpf 1/1 Running 0 28s
test-deployment-59f6d99fdf-r5kjs 1/1 Terminating 3 4m19s
#查看詳情:
[root@m01 /hzl]# kubectl describe pod test-deployment-59f6d99fdf-6hxpf
Name: test-deployment-59f6d99fdf-6hxpf
Namespace: default
Priority: 0
Node: nod02/192.168.15.57
Start Time: Sat, 07 Aug 2021 13:18:57 +0800
Labels: app=deployment
pod-template-hash=59f6d99fdf
Annotations: <none>
Status: Running
IP: 10.244.2.51
IPs:
IP: 10.244.2.51
Controlled By: ReplicaSet/test-deployment-59f6d99fdf
Containers:
nginx:
Container ID: docker://58a289dab5fb4d902da23352c1257874b8d9ce87cda3f7e0b00feffa514f0405
Image: alvinos/django:v1
Image ID: docker-pullable://alvinos/django@sha256:fc5ffecb7d5038940006d5d7e9ea6414fc4ec0f2509501aebdc3280f1a559723
Port: <none>
Host Port: <none>
State: Running
Started: Sat, 07 Aug 2021 13:19:17 +0800
Ready: True
#使用httpget方式,探測不成功,觸發重啟策略,已經重啟3次
Restart Count: 3
Liveness: http-get http://:80/ delay=0s timeout=1s period=10s #success=1 #failure=3
二、就緒型探針(ReadinessProbe)
用於判斷容器是否正常提供服務,即容器的Ready是否為True,是否可以接收請求,如果ReadinessProbe探測失敗,則容器的Ready將設置為False,控制器將此Pod的Endpoint從對應的service的Endpoint列表中移除,從此不再將任何請求調度此Pod上,直到下次探測成功。(剔除此pod,不參與接收請求不會將流量轉發給此Pod)
1、就緒型檢查用法
1、用於判斷容器是否正常提供服務
2、處理方式:下線負載均衡
3、存活性檢查和就緒性檢查是否可以同時存在呢?可以
2、ReadinessProbe的使用(多種使用方式同上)
#資源清單編寫:
[root@m01 /hzl]# cat ReadinessProbe.yaml
---
kind: Deployment
apiVersion: apps/v1
metadata:
name: test-deployment
spec:
replicas: 1
selector:
matchLabels:
app: deployment
template:
metadata:
labels:
app: deployment
spec:
containers:
- name: nginx
image: alvinos/django:v1
livenessProbe:
exec:
command:
- "/bin/sh"
- "-c"
- "cat /root/test/manage.py"
readinessProbe:
tcpSocket:
port: 80
---
kind: Service
apiVersion: v1
metadata:
name: test-svc
namespace: default
spec:
ports:
- port: 80
targetPort: 80
name: http
selector:
app: deployment
#創建pod
[root@m01 /hzl]# kubectl apply -f ReadinessProbe.yaml
deployment.apps/test-deployment configured
service/test-svc unchanged
#查看pod
[root@m01 /hzl]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deployment-569f4b99ff-smpkt 1/1 Running 0 44m
nginx-6799fc88d8-6c892 1/1 Running 0 63m
test-deployment-677d5c5bd7-wz8sp 1/1 Running 0 7m #創建成功
#查看創建狀態
[root@m01 /hzl]# kubectl describe endpoints test-svc
Name: test-svc
Namespace: default
Labels: <none>
Annotations: <none>
Subsets:
Addresses: 10.244.2.47,10.244.2.53 #表述創建成功的pod ip
NotReadyAddresses: <none> #表示創建不成功的pod ip
Ports:
Name Port Protocol
---- ---- --------
http 80 TCP
Events: <none>
###################################### 測試 ###########################################
#編寫資源清單:
[root@m01 /hzl]# vim ReadinessProbe.yaml
........
......
readinessProbe:
tcpSocket:
port: 8090 #端口更改
.......
.....
#更新資源清單,創建pod
[root@m01 /hzl]# kubectl apply -f ReadinessProbe.yaml
deployment.apps/test-deployment configured
service/test-svc unchanged
#查看pod
[root@m01 /hzl]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deployment-569f4b99ff-smpkt 1/1 Running 0 54m
nginx-6799fc88d8-6c892 1/1 Running 0 73m
test-deployment-677d5c5bd7-wz8sp 1/1 Running 0 17m #之前的pod也不會刪除下架
test-deployment-85ddccb5dd-h8zfw 0/1 Running 0 6m47s #新pod未就緒的一直存在
#使用測試命令實時檢測
(由端口不存在一直未在就緒狀態,舊的永遠刪除不了,新的永遠是未就緒狀態)
[root@m01 /hzl]# while true; do kubectl describe endpoints test-svc ;sleep 1; clean; done
Name: test-svc
Namespace: default
Labels: <none>
Annotations: endpoints.kubernetes.io/last-change-trigger-time: 2021-08-07T05:45:31Z
Subsets:
Addresses: 10.244.2.47,10.244.2.53 #就緒成功的ip pod
NotReadyAddresses: 10.244.2.54 #未就緒失敗的Ip pod
##################################### 測試 ############################
#更改資源配置清單(存在的端口)
[root@m01 /hzl]# vim ReadinessProbe.yaml
.........
readinessProbe:
tcpSocket:
port: 80 #端口更改為80
.......
.....
#創建pod
[root@m01 /hzl]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deployment-569f4b99ff-rfssh 1/1 Running 0 70m
nginx-6799fc88d8-dtg9b 1/1 Running 0 71m
test-deployment-677d5c5bd7-rl7lt 1/1 Running 0 44s #已經正常創建,pod全部通過就緒
#查看端點
[root@m01 /hzl]# kubectl describe endpoints test-svc
Name: test-svc
Namespace: default
Labels: <none>
Annotations: <none>
Subsets:
Addresses: 10.244.2.63,10.244.2.73 #全部正常通過就緒檢查的pod ip
NotReadyAddresses: <none>
Ports:
Name Port Protocol
---- ---- --------
http 80 TCP
############################## 測試二 #####################################
#設定副本數進行更新
[root@m01 /hzl]# kubectl scale deployment test-deployment --replicas=3
deployment.apps/test-deployment scaled
[root@m01 /hzl]# while true; do wget -q -O http://test-svc.default.svc.cluster.local/index; echo ;sleep 1; done
#查看svc
[root@m01 /hzl]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 25m
test-svc ClusterIP 10.107.80.105 <none> 80/TCP 14m
#使用網絡工具查看別名(集群內部解析名稱)
[root@m01 /hzl]# kubectl run test --rm -it --image=busybox:1.28
If you don't see a command prompt, try pressing enter. / # nslookup test-svc Server: 10.96.0.10 Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local Name: test-svc Address 1: 10.107.80.105 test-svc.default.svc.cluster.local #測試使用(實時查看過程狀態,發現有多個pod正在提供服務) / # while true; do wget -q -O- http://test-svc.default.svc.cluster.local/index; echo ;sleep 1; done 主機名:test-deployment-677d5c5bd7-hvn99,版本:v1 主機名:test-deployment-677d5c5bd7-m2wgq,版本:v1 主機名:test-deployment-677d5c5bd7-hvn99,版本:v1 主機名:test-deployment-677d5c5bd7-blkrj,版本:v1 #查看pod的狀態 [root@m01 /hzl]# kubectl get pod NAME READY STATUS RESTARTS AGE deployment-569f4b99ff-z5g6z 1/1 Running 0 45m nginx-6799fc88d8-5mckg 1/1 Running 0 45m test 1/1 Running 0 15m test-deployment-677d5c5bd7-bnzjl 0/1 Running 0 3s test-deployment-677d5c5bd7-m2wgq 1/1 Running 0 8m1s test-deployment-677d5c5bd7-rbvhr 0/1 ContainerCreating 0 3s test-deployment-677d5c5bd7-sknx5 0/1 ContainerCreating 0 3s test-deployment-677d5c5bd7-snvvj 0/1 ContainerCreating 0 3s #全部更新部署完成(5個) [root@m01 /hzl]# kubectl get pod NAME READY STATUS RESTARTS AGE deployment-569f4b99ff-z5g6z 1/1 Running 0 45m nginx-6799fc88d8-5mckg 1/1 Running 0 45m test 1/1 Running 0 15m test-deployment-677d5c5bd7-bnzjl 1/1 Running 0 18s test-deployment-677d5c5bd7-m2wgq 1/1 Running 0 8m16s test-deployment-677d5c5bd7-rbvhr 1/1 Running 0 18s test-deployment-677d5c5bd7-sknx5 1/1 Running 0 18s test-deployment-677d5c5bd7-snvvj 1/1 Running 0 18s #查看端點ip的狀態 [root@m01 /hzl]# kubectl describe endpoints test-svc Name: test-svc Namespace: default Labels: <none> Annotations: endpoints.kubernetes.io/last-change-trigger-time: 2021-08-07T18:24:43Z Subsets: Addresses: 10.244.1.61,10.244.1.62,10.244.2.77,10.244.2.85,10.244.2.86,10.244.2.87 #表示布成功pod ip NotReadyAddresses: <none> #未准備好的pod ip Ports: Name Port Protocol ---- ---- -------- http 80 TCP Events: <none> #重新設定更新副本數 [root@m01 /hzl]# kubectl scale deployment test-deployment --replicas=1 deployment.apps/test-deployment scaled #查看pod狀態 [root@m01 /hzl]# kubectl get pod #正在清除副本數 NAME READY STATUS RESTARTS AGE deployment-569f4b99ff-z5g6z 1/1 Running 0 48m nginx-6799fc88d8-5mckg 1/1 Running 0 48m test 1/1 Running 0 18m test-deployment-677d5c5bd7-bnzjl 1/1 Terminating 0 2m49s test-deployment-677d5c5bd7-m2wgq 1/1 Running 0 10m test-deployment-677d5c5bd7-rbvhr 1/1 Terminating 0 2m49s test-deployment-677d5c5bd7-sknx5 1/1 Terminating 0 2m49s test-deployment-677d5c5bd7-snvvj 1/1 Terminating 0 2m49s [root@m01 /hzl]# kubectl get pod NAME READY STATUS RESTARTS AGE deployment-569f4b99ff-z5g6z 1/1 Running 0 43m nginx-6799fc88d8-5mckg 1/1 Running 0 43m test 1/1 Running 0 13m test-deployment-677d5c5bd7-m2wgq 1/1 Running 0 6m21s #副本數為1 #測試檢測(實時查看過程狀態,發現已經從多個變為一個pod使用) [root@m01 /hzl]# kubectl run test --rm -it --image=busybox:1.28 If you don't see a command prompt, try pressing enter.
/ # while true; do wget -q -O- http://test-svc.default.svc.cluster.local/index; echo ;sleep 1; done
主機名:test-deployment-677d5c5bd7-m2wgq,版本:v1
主機名:test-deployment-677d5c5bd7-m2wgq,版本:v1
主機名:test-deployment-677d5c5bd7-m2wgq,版本:v1
#重新設定更新副本數
[root@m01 /hzl]# kubectl scale deployment test-deployment --replicas=1
deployment.apps/test-deployment scaled
3、多版本多資源(存活性與就緒型)平滑更新,平滑下線
【基礎參數】
1》failureThreshold: 最少連續幾次探測失敗的次數,滿足該次數則認為fail
2》initialDelaySeconds: 容器啟動之后開始進行存活性探測的秒數。不填立即進行
3》periodSeconds: 執行探測的頻率(秒)。默認為10秒。最小值為1。
4》successThreshold: 探測失敗后,最少連續探測成功多少次才被認定為成功,滿足該次數則認為success。(但是如果是liveness則必須是 1。最小值是 1。)
5》timeoutSeconds: 每次執行探測的超時時間,默認1秒,最小1秒
【多版本多資源更新的使用】
################################# 多版本 ##################################
#編寫資源清單:
[root@m01 /hzl]# cat ReadinessProbe.yaml
---
kind: Deployment
apiVersion: apps/v1
metadata:
name: test-deployment
spec:
replicas: 1
selector:
matchLabels:
app: deployment
template:
metadata:
labels:
app: deployment
spec:
containers:
- name: nginx
image: alvinos/django:v1
livenessProbe: #存活性檢查
exec:
command:
- "/bin/sh"
- "-c"
- "cat /root/test/manage.py"
initialDelaySeconds: 0
periodSeconds: 3
timeoutSeconds: 1
successThreshold: 1
failureThreshold: 3
readinessProbe: #就緒性檢查
tcpSocket:
port: 80
initialDelaySeconds: 30
periodSeconds: 1
timeoutSeconds: 1
successThreshold: 3
failureThreshold: 1
---
kind: Service
apiVersion: v1
metadata:
name: test-svc
namespace: default
spec:
ports:
- port: 80
targetPort: 80
name: http
selector:
app: deployment
#創建更新pod
[root@m01 /hzl]# kubectl apply -f ReadinessProbe.yaml
deployment.apps/test-deployment configured
service/test-svc unchanged
#查看pod
[root@m01 /hzl]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deployment-569f4b99ff-z5g6z 1/1 Running 0 57m
nginx-6799fc88d8-5mckg 1/1 Running 0 57m
test 1/1 Running 0 27m
test-deployment-5d765fb67d-jccrk 1/1 Running 0 2m44s
#使用工具測試(當前只有一個pod使用)
[root@m01 /hzl]# kubectl run test --rm -it --image=busybox:1.28
If you don't see a command prompt, try pressing enter. / # while true; do wget -q -O- http://test-svc.default.svc.cluster.local/index; echo ;sleep 1; done 主機名:test-deployment-677d5c5bd7-m2wgq,版本:v1 主機名:test-deployment-677d5c5bd7-m2wgq,版本:v1 主機名:test-deployment-677d5c5bd7-m2wgq,版本:v1 #設定更新副本數 [root@m01 /hzl]# kubectl scale deployment test-deployment --replicas=6 deployment.apps/test-deployment scaled #查看pod(正在創建) [root@m01 /hzl]# kubectl get pod NAME READY STATUS RESTARTS AGE deployment-569f4b99ff-z5g6z 1/1 Running 0 61m nginx-6799fc88d8-5mckg 1/1 Running 0 61m test 1/1 Running 0 31m test-deployment-5d765fb67d-bxkxk 0/1 ContainerCreating 0 3s test-deployment-5d765fb67d-fm75k 0/1 ContainerCreating 0 3s test-deployment-5d765fb67d-jccrk 1/1 Running 0 6m49s test-deployment-5d765fb67d-ppwwp 0/1 Running 0 3s test-deployment-5d765fb67d-q24kk 0/1 Running 0 3s test-deployment-5d765fb67d-xx4b2 0/1 ContainerCreating 0 3s #使用工具測試(創建新pod時,使用多個pod,滾動創建副本,也不影響集群外部的使用) [root@m01 /hzl]# kubectl run test --rm -it --image=busybox:1.28 If you don't see a command prompt, try pressing enter.
/ # while true; do wget -q -O- http://test-svc.default.svc.cluster.local/index; echo ;sleep 1; done
主機名:test-deployment-5d765fb67d-fm75k,版本:v1
主機名:test-deployment-5d765fb67d-ppwwp,版本:v1
主機名:test-deployment-5d765fb67d-ppwwp,版本:v1
主機名:test-deployment-5d765fb67d-xx4b2,版本:v1
主機名:test-deployment-5d765fb67d-bxkxk,版本:v1
主機名:test-deployment-5d765fb67d-fm75k,版本:v1
主機名:test-deployment-5d765fb67d-xx4b2,版本:v1
主機名:test-deployment-5d765fb67d-ppwwp,版本:v1
#################################### 滾動更新版本 ###################################
#配置資源清單(版本更新)
[root@m01 /hzl]# cat ReadinessProbe.yaml
..........
....
spec:
containers:
- name: nginx
image: alvinos/django:v2 #版本更新 v1-----> v2
........
.....
#創建pod
[root@m01 /hzl]# kubectl apply -f ReadinessProbe.yaml
deployment.apps/test-deployment configured
service/test-svc unchanged
#查看pod(更新新版本,舊版本不會立馬刪除,只有等新版本更新完畢,才會刪除舊版本)
[root@m01 /hzl]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deployment-569f4b99ff-z5g6z 1/1 Running 0 70m
nginx-6799fc88d8-5mckg 1/1 Running 0 70m
test 1/1 Running 0 40m
test-deployment-5d765fb67d-jccrk 1/1 Running 0 15m
test-deployment-d75444fb5-vndnx 0/1 Running 0 26s #新版本的pod
#使用工具實時檢測(版本更新很平滑,負載均衡會幫你調度,絲毫不會影響集群外部的使用)
[root@m01 /hzl]# kubectl run test --rm -it --image=busybox:1.28
If you don't see a command prompt, try pressing enter. / # while true; do wget -q -O- http://test-svc.default.svc.cluster.local/index; echo ;sleep 1; done 主機名:test-deployment-5d765fb67d-ppwwp,版本:v1 主機名:test-deployment-5d765fb67d-ppwwp,版本:v1 #舊版本v1 主機名:test-deployment-d75444fb5-vndnx,版本:v2 #新版本v2 主機名:test-deployment-d75444fb5-vndnx,版本:v2 主機名:test-deployment-d75444fb5-vndnx,版本:v2 #更新完畢(舊pod已經刪除) [root@m01 /hzl]# kubectl get pod NAME READY STATUS RESTARTS AGE deployment-569f4b99ff-z5g6z 1/1 Running 0 72m nginx-6799fc88d8-5mckg 1/1 Running 0 72m test 1/1 Running 0 42m test-deployment-d75444fb5-vndnx 1/1 Running 0 2m21s ################################### 多資源測試 ############################## #設定副本數 [root@m01 /hzl]# kubectl scale deployment test-deployment --replicas=6 deployment.apps/test-deployment scaled #更新pod(正在創建新pod) [root@m01 /hzl]# kubectl get pod NAME READY STATUS RESTARTS AGE deployment-569f4b99ff-z5g6z 1/1 Running 0 82m nginx-6799fc88d8-5mckg 1/1 Running 0 82m test 1/1 Running 0 52m test-deployment-d75444fb5-f5jp8 0/1 Running 0 4s test-deployment-d75444fb5-fk74b 0/1 Running 0 4s test-deployment-d75444fb5-jbc8n 0/1 ContainerCreating 0 4s test-deployment-d75444fb5-lc62q 0/1 ContainerCreating 0 4s test-deployment-d75444fb5-rqfrl 0/1 Running 0 4s test-deployment-d75444fb5-vndnx 1/1 Running 0 12m #使用工具測試(創建多個pod,滾動創建副本,也不影響集群外部的使用)集群平滑更新 [root@m01 /hzl]# kubectl run test --rm -it --image=busybox:1.28 If you don't see a command prompt, try pressing enter.
/ # while true; do wget -q -O- http://test-svc.default.svc.cluster.local/index; echo ;sleep 1; done
主機名:test-deployment-d75444fb5-jbc8n,版本:v2
主機名:test-deployment-d75444fb5-lc62q,版本:v2
主機名:test-deployment-d75444fb5-vndnx,版本:v2
主機名:test-deployment-d75444fb5-lc62q,版本:v2
主機名:test-deployment-d75444fb5-vndnx,版本:v2
主機名:test-deployment-d75444fb5-f5jp8,版本:v2
主機名:test-deployment-d75444fb5-jbc8n,版本:v2
主機名:test-deployment-d75444fb5-fk74b,版本:v2
主機名:test-deployment-d75444fb5-f5jp8,版本:v2
主機名:test-deployment-d75444fb5-lc62q,版本:v2
#查看pod(全部創建完畢)
[root@m01 /hzl]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deployment-569f4b99ff-z5g6z 1/1 Running 0 85m
nginx-6799fc88d8-5mckg 1/1 Running 0 85m
test 1/1 Running 0 55m
test-deployment-d75444fb5-f5jp8 1/1 Running 0 2m53s
test-deployment-d75444fb5-fk74b 1/1 Running 0 2m53s
test-deployment-d75444fb5-jbc8n 1/1 Running 0 2m53s
test-deployment-d75444fb5-lc62q 1/1 Running 0 2m53s
test-deployment-d75444fb5-rqfrl 1/1 Running 0 2m53s
test-deployment-d75444fb5-vndnx 1/1 Running 0 14m
#重新設定副本數(下線副本pod)
[root@m01 /hzl]# kubectl scale deployment test-deployment --replicas=1
deployment.apps/test-deployment scaled
#查看pod(整在下線5個pod)
[root@m01 /hzl]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deployment-569f4b99ff-z5g6z 1/1 Running 0 86m
nginx-6799fc88d8-5mckg 1/1 Running 0 86m
test 1/1 Running 0 56m
test-deployment-d75444fb5-f5jp8 1/1 Terminating 0 4m30s
test-deployment-d75444fb5-fk74b 1/1 Terminating 0 4m30s
test-deployment-d75444fb5-jbc8n 1/1 Running 0 4m30s
test-deployment-d75444fb5-lc62q 1/1 Terminating 0 4m30s
test-deployment-d75444fb5-rqfrl 1/1 Terminating 0 4m30s
test-deployment-d75444fb5-vndnx 1/1 Terminating 0 16m
#查看工具測試(已經從多副本下線至一個,集群外部狀態也不影響)
[root@m01 /hzl]# kubectl run test --rm -it --image=busybox:1.28
If you don't see a command prompt, try pressing enter.
/ # while true; do wget -q -O- http://test-svc.default.svc.cluster.local/index; echo ;sleep 1; done
主機名:test-deployment-d75444fb5-lc62q,版本:v2 #下線pod
主機名:test-deployment-d75444fb5-jbc8n,版本:v2 #下線最后的一個pod
主機名:test-deployment-d75444fb5-jbc8n,版本:v2
主機名:test-deployment-d75444fb5-jbc8n,版本:v2
主機名:test-deployment-d75444fb5-jbc8n,版本:v2
#查看pod狀態
[root@m01 /hzl]# kubectl get pod
NAME READY STATUS RESTARTS AGE
deployment-569f4b99ff-z5g6z 1/1 Running 0 90m
nginx-6799fc88d8-5mckg 1/1 Running 0 90m
test 1/1 Running 0 60m
test-deployment-d75444fb5-jbc8n 1/1 Running 0 8m4s #下線最后的一個pod
三、回調Hook
Pod是Kubernetes集群中的最小單元,而 Pod 是由容器組成的,首先了解容器的生命周期
1、回調hook概述:
實際上 Kubernetes 為容器提供了生命周期鈎子的,就是我們說的Pod Hook,Pod Hook 是由 kubelet 發起的,當容器中的進程啟動前或者容器中的進程終止之前運行,這是包含在容器的生命周期之中。我們可以同時為 Pod 中的所有容器都配置 hook
2、回調hook常用的兩種鈎子函數
1》PostStart:啟動回調hook
2》PreStop: 結束回調hook
1》# PostStart:
這個鈎子在容器創建后立即執行。
但是,並不能保證鈎子將在容器ENTRYPOINT之前運行,因為沒有參數傳遞給處理程序。
主要用於資源部署、環境准備等。不過需要注意的是如果鈎子花費太長時間以至於不能運行或者掛起, 容器將不能達到running狀態。
2》#PreStop:
這個鈎子在容器終止之前立即被調用。
它是阻塞的,意味着它是同步的, 所以它必須在刪除容器的調用發出之前完成。
主要用於優雅關閉應用程序、通知其他系統等。如果鈎子在執行期間掛起, Pod階段將停留在running狀態並且永不會達到failed狀態
ps :如果PostStart或者PreStop鈎子失敗, 它會殺死容器。所以我們應該讓鈎子函數盡可能的輕量。當然有些情況下,長時間運行命令是合理的, 比如在停止容器之前預先保存狀態
3、回調hook的使用方式
Exec :用於執行一段特定的命令,不過要注意的是該命令消耗的資源會被計入容器
HTTP : 對容器上的特定的端點執行HTTP請求
#查看容器的生命周期參數hook
[root@m01 /hzl]# kubectl explain deployment.spec.template.spec.containers.lifecycle
4、回調hook的基本用法
1》在pod啟動之后立即執行或者Pod終止之前立即執行
2》使用關鍵字lifecycle
3》回調鈎子也可以執行:exec、HttpGet、tcpSocket
4、回調hook的使用
【PostStart】
#編寫配置清單:(啟動回調hook與結束回調hook配置清單)
[root@m01 /hzl]# cat hook.yaml
---
kind: Deployment
apiVersion: apps/v1
metadata:
name: test-deployment
spec:
replicas: 1
selector:
matchLabels:
app: deployment
template:
metadata:
labels:
app: deployment
spec:
containers:
- name: nginx
image: alvinos/django:v1
lifecycle:
postStart:
exec:
command:
- "/bin/sh"
- "-c"
- "touch /root/1.txt"
preStop:
exec:
command:
- "/bin/sh"
- "-c"
- "echo 'hzl888' > /root/1.txt"
livenessProbe:
exec:
command:
- "/bin/sh"
- "-c"
- "cat /root/test/manage.py"
initialDelaySeconds: 0
periodSeconds: 3
timeoutSeconds: 1
successThreshold: 1
failureThreshold: 3
readinessProbe:
tcpSocket:
port: 80
initialDelaySeconds: 30
periodSeconds: 1
timeoutSeconds: 1
successThreshold: 3
failureThreshold: 1
---
kind: Service
apiVersion: v1
metadata:
name: test-svc
namespace: default
spec:
ports:
- port: 80
targetPort: 80
name: http
selector:
app: deployment
#創建pod
[root@m01 /hzl]# kubectl apply -f hook.yaml
deployment.apps/test-deployment configured
service/test-svc unchanged
################################### 測試狀態 #######################################
#查看pod
[root@m01 /hzl]# kubectl get pods
NAME READY STATUS RESTARTS AGE
deployment-569f4b99ff-z5g6z 1/1 Running 0 147m
nginx-6799fc88d8-5mckg 1/1 Running 0 147m
test 1/1 Running 0 117m
test-deployment-677d5c5bd7-fqd7z 1/1 Terminating 0 18m
test-deployment-9db95f48d-5jthp 1/1 Running 0 41s #回調hook已啟用
#進入容器內查看(回調hook是否啟用)
[root@m01 /hzl]# kubectl exec -it test-deployment-9db95f48d-5jthp -- bash
[root@test-deployment-9db95f48d-5jthp test]# cd /root/
[root@test-deployment-9db95f48d-5jthp ~]# ls
1.txt anaconda-ks.cfg test #1.txt文件已創建(啟動回調hook觸發啟用)
【PostStop】
#配置清單編寫:(同上)
[root@m01 /hzl]# cat hook.yaml
.........
...
spec:
containers:
- name: nginx
image: alvinos/django:v1
lifecycle:
postStart: #啟動回調hook
exec:
command:
- "/bin/sh"
- "-c"
- "touch /root/1.txt"
preStop: #容器結束回調hook
exec:
command:
- "/bin/sh"
- "-c"
- "echo 'hzl888' > /root/1.txt"
livenessProbe:
······
···
#創建pod
#創建pod
[root@m01 /hzl]# kubectl apply -f hook.yaml
deployment.apps/test-deployment configured
service/test-svc unchanged
#查看pod
[root@m01 /hzl]# kubectl get pods
NAME READY STATUS RESTARTS AGE
deployment-569f4b99ff-z5g6z 1/1 Running 0 163m
nginx-6799fc88d8-5mckg 1/1 Running 0 163m
test-deployment-9db95f48d-pwvvl 1/1 Running 0 47s #正常啟動pod
############################ 測試狀態 #######################################
1》#使用工具測試停止回調hook
[root@m01 /hzl]# kubectl exec -it test-deployment-9db95f48d-pwvvl -- bash
[root@test-deployment-9db95f48d-pwvvl /]# cd /root/[root@test-deployment-9db95f48d-pwvvl ~]# ls
1.txt anaconda-ks.cfg test
[root@test-deployment-9db95f48d-pwvvl ~]# while true; do cat 1.txt; sleep 1; clear; done #測試容器停止回調hook
........
...
2》#結束容器(觸發回調hook)
[root@m01 /hzl]# kubectl delete -f hook.yaml
deployment.apps "test-deployment" deleted
service "test-svc" deleted
3》#查看pod(容器正在刪除,觸發回調hook)
[root@m01 /hzl]# kubectl get pods
NAME READY STATUS RESTARTS AGE
deployment-569f4b99ff-z5g6z 1/1 Running 0 169m
nginx-6799fc88d8-5mckg 1/1 Running 0 169m
test-deployment-9db95f48d-pwvvl 1/1 Terminating 0 6m57s
4》#查看觸發狀態(檢查上面測試狀態)
[root@test-deployment-9db95f48d-pwvvl ~]# while true; do cat 1.txt; sleep 1; clear; done #測試容器停止回調hook
hzl888
hzl888
hzl888
....
...
5》#查看pod(容器生命周期)
[root@m01 /hzl]# kubectl get pods
NAME READY STATUS RESTARTS AGE
deployment-569f4b99ff-z5g6z 1/1 Running 0 175m
nginx-6799fc88d8-5mckg 1/1 Running 0 175m
ps : test-deployment.......已不存在,容器生命結束
5》#回調hook的觸發結果(檢查上面測試狀態)
........
......
hzl888
hzl888
command terminated with exit code 137
[root@m01 /hzl]# #容器生命周期結束,回調hook觸發完成結束