云容器实例 CCI-存活探针(liveness probe):HTTP GET

时间:2023-12-21 21:07:00

HTTP GET

HTTP GET方式是最常见的探测方法,其具体机制是向容器发送HTTP GET请求,如果probe收到2xx或3xx,说明容器是健康的,定义方法如下所示。

apiVersion: v1
kind: Pod
metadata:
  name: liveness-http
spec:
  containers:
  - name: liveness
    image: k8s.gcr.io/liveness
    args:
    - /server
    livenessProbe:           # liveness probe
      httpGet:               # HTTP GET定义
        path: /healthz
        port: 8080

创建这个Pod。

$ kubectl create -f liveness-http.yaml -n $namespace_name
pod/liveness-http created

如上,这个probe往容器的8080端口发送HTTP GET请求,上面的程序会在第五次请求会返回500状态码,这时Kubernetes会去重启容器。

查看Pod详情。

$ kubectl describe po liveness-http -n $namespace_name
Name:         liveness-http
......
Containers:
  container-0:
    ......
    State:          Running
      Started:      Mon, 12 Nov 2018 22:57:28 +0800
    Last State:     Terminated
      Reason:       Error
      Exit Code:    137
      Started:      Mon, 12 Nov 2018 22:55:40 +0800
      Finished:     Mon, 12 Nov 2018 22:57:27 +0800
    Ready:          True
    Restart Count:  1
    Liveness:       http-get http://:8080/ delay=0s timeout=1s period=10s #success=1 #failure=3
......
Events:
  Type     Reason     Age                 From               Message
  ----     ------     ----                ----               -------
  Normal   Scheduled  3m5s                default-scheduler  Successfully assigned default/pod-liveness to node2
  Normal   Pulling    74s (x2 over 3m4s)  kubelet, node2     pulling image "pod-liveness"
  Normal   Killing    74s                 kubelet, node2     Killing container with id docker://container-0:Container failed liveness probe.. Container will be killed and recreated.

可以看到Pod当前状态是Running,Last State是Terminated,Restart Count为1,说明已经重启1次,另外从事件中也可以看到 Killing container with id docker://container-0:Container failed liveness probe.. Container will be killed and recreated.

另外,容器Kill后会重新创建一个新容器,不只是之前的容器重启。

support.huaweicloud.com/devg-cci/cci_05_0013.html