Liveness Probe
LivenessProbe:kubelet使用livenessProbe在容器出现异常的时候进行重启的操作。
ReadinessProbe:Kubelet使用ReadinessProbe来指定Container是否为Ready状态。
结合Service用来限定哪些Ready的Container来接受流量。因为Service会将Container状态为Ready的Pod添加到Endpoint里,进行流量的分发。

Crontainer诊断方法:
Exec:在Container内执行指定的命令。如果命令以状态代码0退出,则认为诊断成功。
TCPSocket:对指定端口上的Container的IP地址执行TCP检查。如果端口打开,则诊断被认为是成功的。
HTTPGet:对指定端口和路径上的Container的IP地址执行HTTP Get请求。如果响应的状态代码大于或等于200且小于400,则认为诊断成功。

  1. cat << EOF > pod-liveness-command.yaml
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5. labels:
  6. test: liveness
  7. name: liveness-exec
  8. spec:
  9. containers:
  10. - name: liveness
  11. image: radial/busyboxplus
  12. imagePullPolicy: IfNotPresent
  13. args:
  14. - /bin/sh
  15. - -c
  16. - touch /tmp/healthy; sleep 60; rm -rf /tmp/healthy; sleep 600
  17. livenessProbe:
  18. exec:
  19. command:
  20. - cat
  21. - /tmp/healthy
  22. initialDelaySeconds: 5
  23. periodSeconds: 5
  24. EOF

[root@master01 damon]# kubectl apply -f pod-liveness-command.yaml

[root@master01 damon]# watch -n1 kubectl get pod //通过查看发现liveness-exec的RESTARTS 在30秒后由于检测到不健康一直在重启
NAME READY STATUS RESTARTS AGE
liveness-exec 1/1 Running 0 3s
nginx-deployment-676cc869-bnnvd 1/1 Running 0 30m
nginx-deployment-676cc869-ft8bw 1/1 Running 0 26m

[root@master01 damon]# kubectl get event // 查看Kubernetes事件

liveness HTTP request
**

  1. cat << EOF > pod-liveness-http.yaml
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5. labels:
  6. test: liveness
  7. name: liveness-http
  8. spec:
  9. containers:
  10. - name: liveness
  11. image: seedoflife/liveness
  12. imagePullPolicy: IfNotPresent
  13. args:
  14. - /server
  15. livenessProbe:
  16. httpGet:
  17. path: /healthz
  18. port: 8080
  19. httpHeaders:
  20. - name: X-Custom-Header
  21. value: Awesome
  22. initialDelaySeconds: 3
  23. periodSeconds: 3
  24. EOF

[root@master01 damon]# kubectl apply -f pod-liveness-http.yaml

[root@master01 damon]# curl -v 192.168.1.62:8080/healthz
About to connect() to 192.168.1.62 port 8080 (#0)
Trying 192.168.1.62…
Connected to 192.168.1.62 (192.168.1.62) port 8080 (#0)
> GET /healthz HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 192.168.1.62:8080
> Accept:
/
>
< HTTP/1.1 200 OK // OK显示正常
< Date: Tue, 20 Nov 2018 12:01:26 GMT
< Content-Length: 2
< Content-Type: text/plain; charset=utf-8
<
Connection #0 to host 192.168.1.62 left intact
ok[root@master01 damon]# curl -v 192.168.1.62:8080/healthz
About to connect() to 192.168.1.62 port 8080 (#0)
Trying 192.168.1.62…
Connected to 192.168.1.62 (192.168.1.62) port 8080 (#0)
> GET /healthz HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 192.168.1.62:8080
> Accept:
/
>
< HTTP/1.1 500 Internal Server Error // error检测异常
< Date: Tue, 20 Nov 2018 12:01:39 GMT
< Content-Length: 19
< Content-Type: text/plain; charset=utf-8
<
Connection #0 to host 192.168.1.62 left intact

TCP liveness probe

  1. cat << EOF > pod-liveness-tcp.yaml
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5. name: liveness-tcp
  6. labels:
  7. app: liveness-tcp
  8. spec:
  9. containers:
  10. - name: liveness-tcp
  11. image: python:2.7
  12. imagePullPolicy: IfNotPresent
  13. command: ["bash", "-c", "echo test > index.html && sleep 30 && python -m SimpleHTTPServer 8080"]
  14. ports:
  15. - containerPort: 8080
  16. readinessProbe:
  17. tcpSocket:
  18. port: 8080
  19. initialDelaySeconds: 35
  20. periodSeconds: 10
  21. livenessProbe:
  22. tcpSocket:
  23. port: 8080
  24. initialDelaySeconds: 15
  25. periodSeconds: 20
  26. EOF

[root@master01 ~]# kubectl apply -f pod-liveness-tcp.yaml

[root@master01 ~]# kubectl get pod —watch //参看30秒以内podr的R
NAME READY STATUS RESTARTS AGE
goproxy 1/1 Running 1 5h46m
liveness-tcp 0/1 Running 0 4s

readinessProbe


readinessProbe + livenessProbe+Service**
探针具有许多字段,可用于更精确地控制活动性和准备情况检查的行为:
initialDelaySeconds:启动容器后,启动活动或就绪探测器的秒数。默认为0秒。最小值为0。
periodSeconds:执行探测的频率(以秒为单位)。默认为10秒。最小值为1。
timeoutSeconds:探测超时的秒数。默认为1秒。最小值为1。
successThreshold:探测失败后,连续最小成功探测为成功。默认值为1,最小值为1。
failureThreshold:当Pod启动并且探测失败时,Kubernetes会尝试failureThreshold多次,当多次探测失败后执行重启容器的动作。放弃。默认值为3,最小值为1

  1. cat << EOF > service-healthcheck.yaml
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: service-health
  6. spec:
  7. replicas: 2
  8. selector:
  9. matchLabels:
  10. app: service-health
  11. template:
  12. metadata:
  13. labels:
  14. app: service-health
  15. spec:
  16. containers:
  17. - name: service-health
  18. image: python:2.7
  19. imagePullPolicy: IfNotPresent
  20. command: ["/bin/bash","-c","echo \$(hostname) > index.html && sleep 30 && python -m SimpleHTTPServer 8080"]
  21. ports:
  22. - containerPort: 8080
  23. readinessProbe:
  24. tcpSocket:
  25. port: 8080
  26. initialDelaySeconds: 10
  27. periodSeconds: 10
  28. EOF

**
kubectl apply -f service-healthcheck.yaml

创建service
kubectl expose deployment service-health

[root@master01 ~]# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 443/TCP 6d
service-health ClusterIP 10.109.21.81 8080/TCP 4s

curl 10.109.21.81:8080