pod健康状态检查

pod将康状态检查
提供 Probe 机制,有以下两种类型:
livenessProbe
如果检查失败,将杀死容器,根据Pod的restartPolicy来操作。
readinessProbe
如果检查失败,Kubernetes会把Pod从service endpoints中剔除。
Probe 支持以下三种检查方法:
httpGet
发送HTTP请求,返回200-400范围状态码为成功。
exec
执行Shell命令返回状态码是0为成功。
tcpSocket
发起TCP Socket建立成功。

[root@k8s-master ~]# vim pod.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx
    ports:
    - containerPort: 80
    livenessProbe:
      httpGet:
        path: /index.html
        port: 80
[root@k8s-master ~]# kubectl create -f pod.yaml

查看详细信息,可以看到这里容器访问80端口的信息

[root@k8s-master ~]# kubectl describe po/nginx-pod
 Restart Count:  0
    Liveness:       http-get http://:80/index.html delay=0s timeout=1s period=10s #success=1 #failure=3
Environment:    

通过日志可以看到这里使用kube-probe来访问

[root@k8s-master ~]# kubectl logs nginx-pod -f
172.17.80.1 - - [08/Jul/2019:10:08:14 +0000] "GET /index.html HTTP/1.1" 200 612 "-" "kube-probe/1.9" "-"
172.17.80.1 - - [08/Jul/2019:10:08:24 +0000] "GET /index.html HTTP/1.1" 200 612 "-" "kube-probe/1.9" "-"

再开一个窗口,进入容器删除index.html看看日志的变化

[root@k8s-master ~]# kubectl exec -it nginx-pod bash
root@nginx-pod:/# cd /usr/share/nginx/html/
root@nginx-pod:/usr/share/nginx/html# ls
50x.html  index.html
root@nginx-pod:/usr/share/nginx/html# rm index.html 
root@nginx-pod:/usr/share/nginx/html# exit

这边10秒后显示404找不到页面了

2019/07/08 10:17:14 [error] 6#6: *55 open() "/usr/share/nginx/html/index.html" failed (2: No such file or directory), client: 172.17.80.1, server: localhost, request: "GET /index.html HTTP/1.1", host: "172.17.80.4:80"
172.17.80.1 - - [08/Jul/2019:10:17:14 +0000] "GET /index.html HTTP/1.1" 404 153 "-" "kube-probe/1.9" "-"
172.17.80.1 - - [08/Jul/2019:10:17:24 +0000] "GET /index.html HTTP/1.1" 404 153 "-" "kube-probe/1.9" "-"

查看pod的详细信息,已经输出404页面,但是它还会再创建一个容器并启动

[root@k8s-master ~]# kubectl describe pod nginx-pod
 Warning  Unhealthy              22s (x3 over 42s)  kubelet, 192.168.30.23  Liveness probe failed: HTTP probe failed with statuscode: 404

 Normal   Created                5m (x2 over 14m)  kubelet, 192.168.30.23  Created container
  Normal   Started                5m (x2 over 14m)  kubelet, 192.168.30.23  Started container

进入容器再看,容器又有了

root@nginx-pod:~# cd /usr/share/nginx/html
root@nginx-pod:/usr/share/nginx/html# ls
50x.html  index.html

你可能感兴趣的:(pod健康状态检查)