1、Pod的健康检查,也叫做探针,探针的种类有两种。
答:1)、livenessProbe,健康状态检查,周期性检查服务是否存活,检查结果失败,将重启容器。
2)、readinessProbe,可用性检查,周期性检查服务是否可用,不可用将从service的endpoints中移除。
2、探针的检测方法。
答:1)、exec,执行一段命令。
2)、httpGet,检测某个http请求的返回状态码。
3)、tcpSocket,测试某个端口是否能够连接。
3、创建exec的探针检测方法。
1 [root@k8s-master health]# vim pod_nginx_exec.yaml
1 apiVersion: v1 2 kind: Pod 3 metadata: 4 name: exec 5 spec: 6 containers: 7 - name: nginx 8 image: 192.168.110.133:5000/nginx:1.13 9 ports: 10 - containerPort: 80 11 args: 12 - /bin/sh 13 - -c 14 - touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600 15 livenessProbe: 16 exec: 17 command: 18 - cat 19 - /tmp/healthy 20 initialDelaySeconds: 5 21 periodSeconds: 5 22
开始创建这个Pod,操作如下所示:
1 [root@k8s-master health]# kubectl create -f pod_nginx_exec.yaml 2 pod "exec" created 3 [root@k8s-master health]# kubectl get all -o wide 4 NAME DESIRED CURRENT READY AGE CONTAINER(S) IMAGE(S) SELECTOR 5 rc/mysql 1 1 1 1h mysql 192.168.110.133:5000/mysql:5.7.30 app=mysql 6 rc/myweb 1 1 1 1h myweb 192.168.110.133:5000/tomcat:latest app=myweb 7 8 NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR 9 svc/kubernetes 10.254.0.1 <none> 443/TCP 13d <none> 10 svc/mysql 10.254.207.238 <none> 3306/TCP 1h app=mysql 11 svc/myweb 10.254.29.22 <nodes> 8080:30008/TCP 1h app=myweb 12 13 NAME READY STATUS RESTARTS AGE IP NODE 14 po/busybox2 1/1 Running 1 35m 172.16.16.3 k8s-master 15 po/exec 1/1 Running 0 15s 172.16.59.4 k8s-node2 16 po/mysql-lmx4s 1/1 Running 0 1h 172.16.59.2 k8s-node2 17 po/myweb-hsdwn 1/1 Running 0 1h 172.16.32.3 k8s-node3 18 [root@k8s-master health]#
可用通过详细查看Pod的启动状况。
1 [root@k8s-master health]# kubectl describe pod exec 2 Name: exec 3 Namespace: default 4 Node: k8s-node2/192.168.110.134 5 Start Time: Thu, 18 Jun 2020 16:36:16 +0800 6 Labels: <none> 7 Status: Running 8 IP: 172.16.59.4 9 Controllers: <none> 10 Containers: 11 nginx: 12 Container ID: docker://54e08c980bd1ad05c756de4de50b0f61c448a01b7ffcda2b97dc8b5c3a6e2749 13 Image: 192.168.110.133:5000/nginx:1.13 14 Image ID: docker-pullable://192.168.110.133:5000/nginx@sha256:e4f0474a75c510f40b37b6b7dc2516241ffa8bde5a442bde3d372c9519c84d90 15 Port: 80/TCP 16 Args: 17 /bin/sh 18 -c 19 touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600 20 State: Running 21 Started: Thu, 18 Jun 2020 16:37:32 +0800 22 Last State: Terminated 23 Reason: Error 24 Exit Code: 137 25 Started: Thu, 18 Jun 2020 16:36:18 +0800 26 Finished: Thu, 18 Jun 2020 16:37:32 +0800 27 Ready: True 28 Restart Count: 1 29 Liveness: exec [cat /tmp/healthy] delay=5s timeout=1s period=5s #success=1 #failure=3 30 Volume Mounts: <none> 31 Environment Variables: <none> 32 Conditions: 33 Type Status 34 Initialized True 35 Ready True 36 PodScheduled True 37 No volumes. 38 QoS Class: BestEffort 39 Tolerations: <none> 40 Events: 41 FirstSeen LastSeen Count From SubObjectPath Type Reason Message 42 --------- -------- ----- ---- ------------- -------- ------ ------- 43 1m 1m 1 { default-scheduler } Normal Scheduled Successfully assigned exec to k8s-node2 44 1m 1m 1 {kubelet k8s-node2} spec.containers{nginx} Normal Created Created container with docker id ff079949b2cb; Security:[seccomp=unconfined] 45 1m 1m 1 {kubelet k8s-node2} spec.containers{nginx} Normal Started Started container with docker id ff079949b2cb 46 1m 1m 3 {kubelet k8s-node2} spec.contai