kubernetes livenessProbe探针的简单使用

  • 记录一个简单的livenessProb的使用

创建livenessProbe探针

具体的liveness-httpget.yaml文件如下:

apiVersion: v1
kind: Pod
metadata:
  name: liveness-httpget-pod
  namespace: default
spec:
  containers:
    - name: liveness-exec-container
      image: ikubernetes/myapp:v1
      imagePullPolicy: IfNotPresent
      ports:
        - name: http
          containerPort: 80
      livenessProbe:
        httpGet:
          port: http
          path: /index.html
        initialDelaySeconds: 1
        periodSeconds: 3
[root@master manifests]# cat liveness-httpget.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: liveness-httpget-pod
  namespace: default
spec:
  containers:
    - name: liveness-exec-container
      image: ikubernetes/myapp:v1
      imagePullPolicy: IfNotPresent
      ports:
        - name: http
          containerPort: 80
      livenessProbe:
        httpGet:
          port: http
          path: /index.html
        initialDelaySeconds: 1
        periodSeconds: 3

创建该pod

[root@master manifests]# kubectl create -f liveness-httpget.yaml 
pod/liveness-httpget-pod created

查看当前pod的状态

[root@master manifests]# kubectl describe pod liveness-httpget-pod
Name:               liveness-httpget-pod
Namespace:          default
Priority:           0
PriorityClassName:  
Node:               node02/192.168.18.130
Start Time:         Fri, 17 Aug 2018 18:24:26 +0800
Labels:             
Annotations:        
Status:             Running
IP:                 10.244.2.40
Containers:
  liveness-exec-container:
    Container ID:   docker://b00bc09eac7c4dcadb482dd9795a9f7e6d9b68ca63e75384da18feb0ef7bdc6d
    Image:          ikubernetes/myapp:v1
    Image ID:       docker-pullable://ikubernetes/myapp@sha256:9c3dc30b5219788b2b8a4b065f548b922a34479577befb54b03330999d30d513
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Fri, 17 Aug 2018 18:24:27 +0800
    Ready:          True
    Restart Count:  0
    Liveness:       http-get http://:http/index.html delay=1s timeout=1s period=3s #success=1 #failure=3
    Environment:    
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-qcfxf (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-qcfxf:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-qcfxf
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Pulled     37s   kubelet, node02    Container image "ikubernetes/myapp:v1" already present on machine
  Normal  Created    37s   kubelet, node02    Created container
  Normal  Started    37s   kubelet, node02    Started container
  Normal  Scheduled  32s   default-scheduler  Successfully assigned default/liveness-httpget-pod to node02

测试将容器内的index.html删除掉

登陆容器

使用命令 kubectl exec liveness-httpget-pod -c liveness-exec-container -it -- /bin/sh 登陆容器

[root@master manifests]# kubectl exec liveness-httpget-pod -c liveness-exec-container -it  -- /bin/sh
/ # ls
bin    dev    etc    home   lib    media  mnt    proc   root   run    sbin   srv    sys    tmp    usr    var
/ # mv /usr/share/nginx/html/index.html index.html
/ # command terminated with exit code 137

可以看到,当我们把index.html移走后,这个容器立马就退出了

此时,查看pod的信息

[root@master manifests]# kubectl describe pod liveness-httpget-pod
Name:               liveness-httpget-pod
Namespace:          default
Priority:           0
PriorityClassName:  
Node:               node02/192.168.18.130
Start Time:         Fri, 17 Aug 2018 18:24:26 +0800
Labels:             
Annotations:        
Status:             Running
IP:                 10.244.2.40
Containers:
  liveness-exec-container:
    Container ID:   docker://ac663b9e29c2cbc9c1d0283f1609914be69964149247945266ddcc8b33dde12f
    Image:          ikubernetes/myapp:v1
    Image ID:       docker-pullable://ikubernetes/myapp@sha256:9c3dc30b5219788b2b8a4b065f548b922a34479577befb54b03330999d30d513
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Fri, 17 Aug 2018 18:48:07 +0800
    Last State:     Terminated
      Reason:       Completed
      Exit Code:    0
      Started:      Fri, 17 Aug 2018 18:24:27 +0800
      Finished:     Fri, 17 Aug 2018 18:48:06 +0800
    Ready:          True
    Restart Count:  1
    Liveness:       http-get http://:http/index.html delay=1s timeout=1s period=3s #success=1 #failure=3
    Environment:    
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-qcfxf (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-qcfxf:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-qcfxf
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type     Reason     Age               From               Message
  ----     ------     ----              ----               -------
  Normal   Scheduled  24m               default-scheduler  Successfully assigned default/liveness-httpget-pod to node02
  Warning  Unhealthy  1m (x3 over 1m)   kubelet, node02    Liveness probe failed: HTTP probe failed with statuscode: 404
  Normal   Pulled     1m (x2 over 24m)  kubelet, node02    Container image "ikubernetes/myapp:v1" already present on machine
  Normal   Created    1m (x2 over 24m)  kubelet, node02    Created container
  Normal   Started    1m (x2 over 24m)  kubelet, node02    Started container
  Normal   Killing    1m                kubelet, node02    Killing container with id docker://liveness-exec-container:Container failed liveness probe.. Container will be killed and recreated.

看输出,容器由于健康检查未通过,pod会被杀掉,并重新创建

[root@master manifests]# kubectl get pods
NAME                   READY     STATUS    RESTARTS   AGE
liveness-httpget-pod   1/1       Running   1          33m

restarts 为 1

重新登陆容器查看

重新登陆容器,发现index.html又出现了,证明容器是被重拉了。

[root@master manifests]# kubectl exec liveness-httpget-pod -c liveness-exec-container -it  -- /bin/sh
/ # cat /usr/share/nginx/html/index.html 
Hello MyApp | Version: v1 | Pod Name

你可能感兴趣的:(探针,kubernetes)