Pod 探针

Posted by Vito on January 13, 2024

StartupProbe

k8s 1.16 版本新增的探针,用于判断应用程序是否已经启动了。

当配置了 startupProbe 后,会先禁用其他探针,直到 startupProbe 成功后,其他探针才会继续。

作用:由于有时候不能准确预估应用一定是多长时间启动成功,因此配置另外两种探针时不方便配置初始化时长来检测,而配置了 statupProbe 后,只有在应用启动成功了,才会执行另外两种探针,可以更加方便的结合使用另外两种探针使用。

LivenessProbe

用于探测容器中的应用是否运行,如果探测失败,kubelet 会根据配置的重启策略进行重启,若没有配置,默认就认为容器启动成功,不会执行重启策略。

ReadinessProbe

用于探测容器内的程序是否健康,它的返回值如果返回 success,那么就认为该容器已经完全启动,并且该容器是可以接收外部流量。

探测方式

  • ExecAction

    在容器内部执行一个命令,如果命令退出时返回码为 0 则认为诊断成功。

    1
    2
    3
    4
    5
    
    livenessProbe:
      exec:
        command:
          - cat
          - /health
    
  • TCPSocketAction

    通过 tcp 连接监测容器内端口是否开放,如果端口打开,则诊断被认为是成功的

    1
    2
    3
    
    livenessProbe:
      tcpSocket:
        port: 80
    
  • HTTPGetAction

    生产环境用的较多的方式,发送 HTTP 请求到容器内的应用程序,如果接口返回的状态码在 200~400 之间,则认为容器健康。

    1
    2
    3
    4
    5
    6
    7
    8
    
    livenessProbe:
      httpGet:
        path: /health
        port: 8080
        scheme: HTTP
        httpHeaders:
          - name: xxx
            value: xxx
    

    参数配置

  • initialDelaySeconds: 60 # 执行第一次探测前的等待时间,单位秒
  • timeoutSeconds: 2 # 超时时间,单位秒

  • periodSeconds: 5 # 监测间隔时间,单位秒

  • successThreshold: 1 # 检查 1 次成功就表示成功

  • failureThreshold: 5 # 监测失败 5 次就表示失败

样例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
[root@k8s-master1 test]# vim vito-demo-pod-nginx.yaml
apiVersion: v1
kind: Pod
metadata:
  name: vito-demo-pod-nginx
  labels:
    type: app
    ver: 1.0.0
  namespace: 'default'
spec:
  containers:
  - name: demo-container-nginx
    image: nginx:1.7.9
    startupProbe: # 探针类型 
      httpGet: # 探测方式
        path: /index.html
        port: 80
      timeoutSeconds: 2 # 超时时间
      periodSeconds: 5 # 监测间隔时间
      successThreshold: 1 # 检查 1 次成功就表示成功
      failureThreshold: 5 # 监测失败 5 次就表示失败
    livenessProbe:
      tcpSocket:
        port: 80
      timeoutSeconds: 3
      periodSeconds: 5
      successThreshold: 1
      failureThreshold: 5
    readinessProbe:
      exec:
        command:
        - sh
        - -c
        - "sleep 2; echo 'success' > /inited"
      timeoutSeconds: 3
      periodSeconds: 5
      successThreshold: 1
      failureThreshold: 5
    imagePullPolicy: IfNotPresent
    command:
    - nginx
    - -g
    - 'daemon off;'
    workingDir: /usr/share/nginx/html
    ports:
    - name: http
      containerPort: 80
      protocol: TCP
    - name: https
      containerPort: 443
    env:
    - name: JVM_OPTS
      value: '-Xms128m -Xmx128m'
    resources:
      requests:
        cpu: 100m
        memory: 128Mi
      limits:
        cpu: 200m
        memory: 256Mi
  restartPolicy: OnFailure

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
[root@k8s-master1 test]# kubectl create -f vito-demo-pod-nginx.yaml 
pod/vito-demo-pod-nginx created

[root@k8s-master1 test]# kubectl get pods -o wide
NAME                  READY   STATUS    RESTARTS   AGE   IP              NODE                 NOMINATED NODE   READINESS GATES
vito-demo-pod-nginx   1/1     Running   0          28s   10.244.37.203   k8s-node1.zhch.lan   <none>           <none>

# kubectl exec -it PodName -c ContainerName -- Command
[root@k8s-master1 test]# kubectl exec -it vito-demo-pod-nginx -c demo-container-nginx -- cat /inited
success

[root@k8s-master1 test]# kubectl describe pod vito-demo-pod-nginx
Name:             vito-demo-pod-nginx
Namespace:        default
Priority:         0
Service Account:  default
Node:             k8s-node1.zhch.lan/192.168.99.204
Start Time:       Thu, 14 Dec 2023 12:52:27 +0800
Labels:           type=app
                  ver=1.0.0
Annotations:      cni.projectcalico.org/containerID: 5b8a4c8ba81121256a2f98bfceaf9f6ddedba09109a9b625fca15eae7be6ed7d
                  cni.projectcalico.org/podIP: 10.244.37.203/32
                  cni.projectcalico.org/podIPs: 10.244.37.203/32
Status:           Running
IP:               10.244.37.203
IPs:
  IP:  10.244.37.203
Containers:
  demo-container-nginx:
    Container ID:  docker://185de3f33bb849efef2eab906c8073e35cdf32ad28869c6138a1bf024c975997
    Image:         nginx:1.7.9
    Image ID:      docker-pullable://nginx@sha256:e3456c851a152494c3e4ff5fcc26f240206abac0c9d794affb40e0714846c451
    Ports:         80/TCP, 443/TCP
    Host Ports:    0/TCP, 0/TCP
    Command:
      nginx
      -g
      daemon off;
    State:          Running
      Started:      Thu, 14 Dec 2023 12:52:28 +0800
    Ready:          True
    Restart Count:  0
    Limits:
      cpu:     200m
      memory:  256Mi
    Requests:
      cpu:      100m
      memory:   128Mi
    Liveness:   tcp-socket :80 delay=0s timeout=3s period=5s #success=1 #failure=5
    Readiness:  exec [sh -c sleep 2; echo 'success' > /inited] delay=0s timeout=3s period=5s #success=1 #failure=5
    Startup:    http-get http://:80/index.html delay=0s timeout=2s period=5s #success=1 #failure=5
    Environment:
      JVM_OPTS:  -Xms128m -Xmx128m
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-5bllv (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  kube-api-access-5bllv:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   Burstable
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  53s   default-scheduler  Successfully assigned default/vito-demo-pod-nginx to k8s-node1.zhch.lan
  Normal  Pulled     52s   kubelet            Container image "nginx:1.7.9" already present on machine
  Normal  Created    52s   kubelet            Created container demo-container-nginx
  Normal  Started    52s   kubelet            Started container demo-container-nginx
[root@k8s-master1 test]#