分布式系统和微服务体系结构的挑战之一是自动检测不正常的应用程序,并将请求(request)重新路由到其他可用系统,恢复损坏的组件。健康检查是应对该挑战的一种可靠方法。使用 Kubernetes,可以通过探针配置运行状况检查,以确定每个 Pod 的状态
Kubernetes提供了健康检查服务,对于检测到故障服务会被及时自动下线,以及通过重启服务的方式使服务自动恢复
存活性探针用于判断容器是否存活,即Pod是否为running状态
如果LivenessProbe探针探测到容器不健康,则kubelet将kill掉容器,并根据容器的重启策略判断按照那种方式重启,
如果一个容器不包含LivenessProbe探针,则Kubelet认为容器的LivenessProbe探针的返回值永远成功
1)用于判断容器是否存活
2)处理的方式:如果判断失败,则重启POD
3)存活性检测使用exec,就绪性检测使用tcpSocket,httpGet
#探针相关参数:
delay=0s :容器启动多久开始探测
timeout=1s :容器探测超时时间
period=10s :探测的频率
success=1 :探测成功多少次为成功
failure=3 :探测失败多少次为失败
ExecAction
TCPSocketAction
HTTPGetAction
通过执行一条命令,探测服务是否可以正常对外提供服务
#资源清单编写:
[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
通过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
通过访问某个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
用于判断容器是否正常提供服务,即容器的Ready是否为True,是否可以接收请求,如果ReadinessProbe探测失败,则容器的Ready将设置为False,控制器将此Pod的Endpoint从对应的service的Endpoint列表中移除,从此不再将任何请求调度此Pod上,直到下次探测成功。(剔除此pod,不参与接收请求不会将流量转发给此Pod)
1、用于判断容器是否正常提供服务
2、处理方式:下线负载均衡
3、存活性检查和就绪性检查是否可以同时存在呢?可以
#资源清单编写:
[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:
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: #未准备好的pod ip
Ports:
Name Port Protocol
---- ---- --------
http 80 TCP
Events:
#重新设定更新副本数
[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
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
Pod是Kubernetes集群中的最小单元,而 Pod 是由容器组成的,首先了解容器的生命周期
实际上 Kubernetes 为容器提供了生命周期钩子的,就是我们说的Pod Hook,Pod Hook 是由 kubelet 发起的,当容器中的进程启动前或者容器中的进程终止之前运行,这是包含在容器的生命周期之中。我们可以同时为 Pod 中的所有容器都配置 hook
1》PostStart:启动回调hook
2》PreStop: 结束回调hook
1》# PostStart:
这个钩子在容器创建后立即执行。
但是,并不能保证钩子将在容器ENTRYPOINT之前运行,因为没有参数传递给处理程序。
主要用于资源部署、环境准备等。不过需要注意的是如果钩子花费太长时间以至于不能运行或者挂起, 容器将不能达到running状态。
2》#PreStop:
这个钩子在容器终止之前立即被调用。
它是阻塞的,意味着它是同步的, 所以它必须在删除容器的调用发出之前完成。
主要用于优雅关闭应用程序、通知其他系统等。如果钩子在执行期间挂起, Pod阶段将停留在running状态并且永不会达到failed状态
ps :如果PostStart或者PreStop钩子失败, 它会杀死容器。所以我们应该让钩子函数尽可能的轻量。当然有些情况下,长时间运行命令是合理的, 比如在停止容器之前预先保存状态
Exec :用于执行一段特定的命令,不过要注意的是该命令消耗的资源会被计入容器
HTTP : 对容器上的特定的端点执行HTTP请求
【官网回调hook】
#查看容器的生命周期参数hook
[root@m01 /hzl]# kubectl explain deployment.spec.template.spec.containers.lifecycle
1》在pod启动之后立即执行或者Pod终止之前立即执行
2》使用关键字lifecycle
3》回调钩子也可以执行:exec、HttpGet、tcpSocket
#编写配置清单:(启动回调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触发启用)
#配置清单编写:(同上)
[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触发完成结束