健康性检查
使用probe探针
配置探测器
Probe 有很多配置字段,可以使用这些字段精确的控制存活和就绪检测的行为:
initialDelaySeconds
:容器启动后要等待多少秒后存活和就绪探测器才被初始化,默认是 0 秒,最小值是 0。periodSeconds
:执行探测的时间间隔(单位是秒)。默认是 10 秒。最小值是 1。timeoutSeconds
:探测的超时后等待多少秒。默认值是 1 秒。最小值是 1。successThreshold
:探测器在失败后,被视为成功的最小连续成功数。默认值是 1。 存活和启动探测的这个值必须是 1。最小值是 1。failureThreshold
:当探测失败时,Kubernetes 的重试次数。 存活探测情况下的放弃就意味着重新启动容器。 就绪探测情况下的放弃 Pod 会被打上未就绪的标签。默认值是 3。最小值是 1。
liveness probe
通过重启来解决问题 --- 重启大法 --- 所谓的重启就是删除这个pod创建同名的pod
检测方式
- command
- httpGget
- tcpSocket
command
apiVersion: v1
kind: Pod
metadata:
labels:
test: liveness
name: pod-liveness
spec:
containers:
- name: liveness
image: busybox
imagePullPolicy: IfNotPresent
args:
- /bin/sh
- -c
- touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600
livenessProbe:
exec:
command:
- cat
- /tmp/healthy
initialDelaySeconds: 5 #执行探测前等待5s
periodSeconds: 5 #没5s执行一次存活探测
当容器启动时,执行如下的命令:
/bin/sh -c "touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600"
这个容器生命前30秒 /tmp/healthy这个文件是存在的,所以cat /tmp/healthy 会返回成功代码,30秒之后容器执行 rm -rf /tmp/healthy ,文件被删除,再次cat 该文件,返回错误代码。kubelet就是通过执行 cat /tmp/healthy 这个命令来探测容器,如果返回为0,kubelet就会认为这个容器是健康存活的,反之kubelet会杀死这个容器并重启。
创建容器
[root@cka-master 7-probe]# kubectl apply -f pod1.yaml
pod/pod-liveness created
[root@cka-master 7-probe]# kubectl get pods
NAME READY STATUS RESTARTS AGE
pod-liveness 1/1 Running 0 3s
容器大概在75秒之后会重启一次
[root@cka-master 7-probe]# kubectl get pods
NAME READY STATUS RESTARTS AGE
pod-liveness 1/1 Running 0 73s
[root@cka-master 7-probe]# kubectl get pods
NAME READY STATUS RESTARTS AGE
pod-liveness 1/1 Running 0 74s
[root@cka-master 7-probe]# kubectl get pods
NAME READY STATUS RESTARTS AGE
pod-liveness 1/1 Running 0 75s
[root@cka-master 7-probe]# kubectl get pods
NAME READY STATUS RESTARTS AGE #此时RESTARTS了1次
pod-liveness 1/1 Running 1 75s
当35秒是检查到文件不在了,这里没指定failureThreshold 所以默认检测3次也就是到了45秒之后就认定容器失效,此时就会删除容器,并重新创建一个同名的容器,删除容器需要30秒,所以到75秒时,容器重启。
readiness probe
是不重启的,只是用户发送过来的请求不在转发到此pod上了
检测方式
- command
- httpGget
- tcpSocket
command
vim pod2.yaml
apiVerapiVersion: v1
kind: Pod
metadata:
labels:
test: liveness
name: pod1
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
lifecycle:
postStart:
exec:
command: ["sh","-c","touch /tmp/healthy"]
readinessProbe:
exec:
command:
- cat
- /tmp/healthy
initialDelaySeconds: 5 #执行探测前等待5s
periodSeconds: 5
#先创建3个pod
[root@cka-master 7-probe]# kubectl apply -f pod2.yaml
pod/pod1 created
[root@cka-master 7-probe]# sed 's/pod1/pod3/' pod2.yaml | kubectl apply -f -
pod/pod3 created
[root@cka-master 7-probe]# sed 's/pod1/pod2/' pod2.yaml | kubectl apply -f -
#改下默认首页
[root@cka-master 7-probe]# kubectl exec pod1 -- sh -c "echo 111 > /usr/share/nginx/html/index.html"
[root@cka-master 7-probe]# kubectl exec pod2 -- sh -c "echo 222 > /usr/share/nginx/html/index.html"
[root@cka-master 7-probe]# kubectl exec pod3 -- sh -c "echo 333 > /usr/share/nginx/html/index.html"
‘
#创建service
[root@cka-master 7-probe]# kubectl expose --name web pod pod1 --port=80
service/web exposed
#这里标签都是一样的,所以web后有3个pod
[root@cka-master 7-probe]# kubectl get service web --show-labels
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE LABELS
web ClusterIP 10.100.246.176 <none> 80/TCP 6m13s test=liveness
#访问测试
[root@cka-master 7-probe]# while :; do curl 10.100.246.176 ; sleep 1; done
222
333
111
333
111
333
222
111
可以看到每个pod都访问到了
#删除pod3 的/tmp/healthy
[root@cka-master 7-probe]# kubectl exec pod3 -- ls /tmp/healthy
/tmp/healthy
[root@cka-master 7-probe]# kubectl exec pod3 -- rm /tmp/healthy
[root@cka-master 7-probe]# kubectl exec pod3 -- ls /tmp/healthy
ls: cannot access '/tmp/healthy': No such file or directory
command terminated with exit code 2
#查看pod,不是READY状态
[root@cka-master 7-probe]# kubectl get pods
NAME READY STATUS RESTARTS AGE
pod1 1/1 Running 0 19m
pod2 1/1 Running 0 19m
pod3 0/1 Running 0 19m
[root@cka-master 7-probe]# kubectl describe pod pod3
... ...
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 19m default-scheduler Successfully assigned 7-probe/pod3 to cka-node2
Normal Pulled 19m kubelet Container image "nginx" already present on machine
Normal Created 19m kubelet Created container nginx
Normal Started 19m kubelet Started container nginx
Warning Unhealthy 1s (x11 over 51s) kubelet Readiness probe failed: cat: /tmp/healthy: No such file or directory
#此时没检测到文件
#此时再去访问发现,pod3就不会在被调度到了
[root@cka-master 7-probe]# while :; do curl 10.100.246.176 ; sleep 1; done
111
111
111
111
222
222
111
222
^C