博客园  :: 首页  :: 管理

1、还是先创建一个pod的yaml配置文件,在StartupProbe-启动探针的基础上,再添加上livenessProbe-存活探针

apiVersion: v1      # api 文档版本
kind: Pod           # 资源对象类型,也可以配置为像Deployment、StatefulSet这一类的对象
metadata:           # Pod 相关的元数据,用于描述 Pod 的数据
  name: nginx-demo  # Pod 的名称
  namespace: 'default' # 命名空间的配置
spec:                  # 期望 Pod 按照这里面的描述进行创建
  containers:          # 对于 Pod 中的容器描述
  - name: nginx        # 容器的名称
    image: nginx:1.7.9 # 指定容器的镜像
    imagePullPolicy: IfNotPresent # 镜像拉取策略,指定如果本地有就用本地的,如果没有就拉取远程的

    startupProbe:   # 应用启动探针配置
      exec:
        command:
        - sh
        - -c
        - "sleep 1;echo success > /inited"

      failureThreshold: 3   # 失败多少次才算真正失败
      periodSeconds: 10     # 间隔时间
      successThreshold: 1   # 多少次监测成功才算真正的成功
      timeoutSeconds: 5     # 请求的超时时间

    livenessProbe:   # 应用存活探针配置
      httpGet:
        path: /started.html
        port: 80

      failureThreshold: 3   # 失败多少次才算真正失败
      periodSeconds: 10     # 间隔时间
      successThreshold: 1   # 多少次监测成功才算真正的成功
      timeoutSeconds: 5     # 请求的超时时间

    ports:
    - name: http        # 端口名称
      containerPort: 80 # 描述容器内要暴露什么端口
      protocol: TCP     # 描述该端口是基于哪种协议通信的

  restartPolicy: OnFailure # 重启策略,只有失败的情况才会重启

2、然后再创建pod,使用 kubectl create -f nginx-demo.yaml 命令进行pod的创建

3、然后我们看一下过程,注:中途执行了 kubectl cp started.html nginx-demo:/usr/share/nginx/html/  改变探针的结果 

[root@k8s-master ~]# kubectl get po
NAME         READY   STATUS    RESTARTS   AGE
nginx-demo   0/1     Running   0          5s
[root@k8s-master ~]# kubectl get po
NAME         READY   STATUS    RESTARTS   AGE
nginx-demo   0/1     Running   0          9s
[root@k8s-master ~]# kubectl describe po nginx-demo
......................省略部分..............................
Events:
  Type     Reason     Age   From               Message
  ----     ------     ----  ----               -------
  Normal   Scheduled  30s   default-scheduler  Successfully assigned default/nginx-demo to k8s-node2
  Normal   Pulled     28s   kubelet            Container image "nginx:1.7.9" already present on machine
  Normal   Created    28s   kubelet            Created container nginx
  Normal   Started    28s   kubelet            Started container nginx
  Warning  Unhealthy  10s   kubelet            Liveness probe failed: HTTP probe failed with statuscode: 404
[root@k8s-master ~]# kubectl get po
NAME         READY   STATUS    RESTARTS   AGE
nginx-demo   1/1     Running   0          37s
[root@k8s-master ~]# kubectl get po
NAME         READY   STATUS    RESTARTS     AGE
nginx-demo   0/1     Running   1 (1s ago)   42s
[root@k8s-master ~]# kubectl get po
NAME         READY   STATUS    RESTARTS     AGE
nginx-demo   0/1     Running   1 (4s ago)   45s
[root@k8s-master ~]# kubectl describe po nginx-demo
......................省略部分..............................
Events:
  Type     Reason     Age               From               Message
  ----     ------     ----              ----               -------
  Normal   Scheduled  49s               default-scheduler  Successfully assigned default/nginx-demo to k8s-node2
  Warning  Unhealthy  9s (x3 over 29s)  kubelet            Liveness probe failed: HTTP probe failed with statuscode: 404
  Normal   Killing    9s                kubelet            Container nginx failed liveness probe, will be restarted
  Normal   Pulled     8s (x2 over 47s)  kubelet            Container image "nginx:1.7.9" already present on machine
  Normal   Created    8s (x2 over 47s)  kubelet            Created container nginx
  Normal   Started    8s (x2 over 47s)  kubelet            Started container nginx
[root@k8s-master ~]# kubectl get po
NAME         READY   STATUS    RESTARTS      AGE
nginx-demo   1/1     Running   1 (20s ago)   61s
[root@k8s-master ~]# kubectl get po
NAME         READY   STATUS    RESTARTS      AGE
nginx-demo   1/1     Running   1 (27s ago)   68s
[root@k8s-master ~]# kubectl describe po nginx-demo
......................省略部分..............................
Events:
  Type     Reason     Age                From               Message
  ----     ------     ----               ----               -------
  Normal   Scheduled  70s                default-scheduler  Successfully assigned default/nginx-demo to k8s-node2
  Normal   Killing    30s                kubelet            Container nginx failed liveness probe, will be restarted
  Normal   Pulled     29s (x2 over 68s)  kubelet            Container image "nginx:1.7.9" already present on machine
  Normal   Created    29s (x2 over 68s)  kubelet            Created container nginx
  Normal   Started    29s (x2 over 68s)  kubelet            Started container nginx
  Warning  Unhealthy  10s (x4 over 50s)  kubelet            Liveness probe failed: HTTP probe failed with statuscode: 404
[root@k8s-master ~]# kubectl get po
NAME         READY   STATUS    RESTARTS     AGE
nginx-demo   0/1     Running   2 (6s ago)   86s
[root@k8s-master ~]# kubectl get po
NAME         READY   STATUS    RESTARTS      AGE
nginx-demo   1/1     Running   2 (24s ago)   104s
[root@k8s-master ~]# kubectl cp started.html nginx-demo:/usr/share/nginx/html/
[root@k8s-master ~]# kubectl get po
NAME         READY   STATUS    RESTARTS      AGE
nginx-demo   1/1     Running   2 (29s ago)   109s
[root@k8s-master ~]# kubectl get po
NAME         READY   STATUS    RESTARTS      AGE
nginx-demo   1/1     Running   2 (35s ago)   115s
[root@k8s-master ~]# kubectl describe po nginx-demo
......................省略部分..............................
Events:
  Type     Reason     Age                 From               Message
  ----     ------     ----                ----               -------
  Normal   Scheduled  2m                  default-scheduler  Successfully assigned default/nginx-demo to k8s-node2
  Normal   Killing    40s (x2 over 80s)   kubelet            Container nginx failed liveness probe, will be restarted
  Normal   Pulled     39s (x3 over 118s)  kubelet            Container image "nginx:1.7.9" already present on machine
  Normal   Created    39s (x3 over 118s)  kubelet            Created container nginx
  Normal   Started    39s (x3 over 118s)  kubelet            Started container nginx
  Warning  Unhealthy  20s (x7 over 100s)  kubelet            Liveness probe failed: HTTP probe failed with statuscode: 404
[root@k8s-master ~]# kubectl get po
NAME         READY   STATUS    RESTARTS      AGE
nginx-demo   1/1     Running   2 (49s ago)   2m9s
[root@k8s-master ~]# kubectl get po
NAME         READY   STATUS    RESTARTS      AGE
nginx-demo   1/1     Running   2 (50s ago)   2m10s
[root@k8s-master ~]# kubectl get po
NAME         READY   STATUS    RESTARTS       AGE
nginx-demo   1/1     Running   2 (100s ago)   3m
[root@k8s-master ~]# kubectl get po
NAME         READY   STATUS    RESTARTS        AGE
nginx-demo   1/1     Running   2 (5m44s ago)   7m4s
[root@k8s-master ~]# kubectl describe po nginx-demo
......................省略部分..............................
Events:
  Type     Reason     Age                    From               Message
  ----     ------     ----                   ----               -------
  Normal   Scheduled  7m7s                   default-scheduler  Successfully assigned default/nginx-demo to k8s-node2
  Normal   Killing    5m47s (x2 over 6m27s)  kubelet            Container nginx failed liveness probe, will be restarted
  Normal   Pulled     5m46s (x3 over 7m5s)   kubelet            Container image "nginx:1.7.9" already present on machine
  Normal   Created    5m46s (x3 over 7m5s)   kubelet            Created container nginx
  Normal   Started    5m46s (x3 over 7m5s)   kubelet            Started container nginx
  Warning  Unhealthy  5m27s (x7 over 6m47s)  kubelet            Liveness probe failed: HTTP probe failed with statuscode: 404

这里最后都正常了,但是描述里还是能看到 Liveness probe failed: HTTP probe failed with statuscode: 404

 

 

 

尊重别人的劳动成果 转载请务必注明出处:https://www.cnblogs.com/5201351/p/17686277.html