15,容器探测,探针,重启策略

容器探测用于检测容器中的应用实例是否正常工作,是保障业务可用性的一种传统机制。如果经过探测,实例的状态不符合预期,那么kubernetes就会把该问题实例" 摘除 ",不承担业务流量。kubernetes提供了两种探针来实现容器探测,分别是:

  • liveness probes:存活性探针,用于检测应用实例当前是否处于正常运行状态,如果不是,k8s会重启容器
  • readiness probes:就绪性探针,用于检测应用实例当前是否可以接收请求,如果不能,k8s不会转发流量

livenessProbe 决定是否重启容器,readinessProbe 决定是否将请求转发给容器。

上面两种探针目前均支持三种探测方式:

  • Exec命令:在容器内执行一次命令,如果命令执行的退出码为0,则认为程序正常,否则不正常
  • TCPSocket:将会尝试访问一个用户容器的端口,如果能够建立这条连接,则认为程序正常,否则不正常
  • HTTPGet:调用容器内Web应用的URL,如果返回的状态码在200和399之间,则认为程序正常,否则不正常

下面以liveness probes为例,做几个演示:

方式一:Exec

#cat >pod-test.yaml <
apiVersion: v1
kind: Pod
metadata:
  name: pod-liveness-exec
  namespace: dev
spec:
  containers:
  - name: nginx
    image: nginx:1.17.1
    ports: 
    - name: nginx-port
      containerPort: 80
    livenessProbe:
      exec:
        command: ["/bin/cat","/tmp/hello.txt"] # 执行一个查看文件的命令
        #command: ["echo", "hello"]            # 改成能成功运行的命令就不会重启了
#EOF
'''
创建pod,发现pod不停重启,describe后发现如下log,
原因是pod启动之后就进行了健康检查,检查失败之后,容器被kill掉,然后尝试进行重启
Events:
  Type     Reason     Age                From               Message
  ----     ------     ----               ----               -------
  Normal   Scheduled  82s                default-scheduler  Successfully assigned dev/pod-liveness-exec to node2
  Normal   Pulled     30s (x3 over 82s)  kubelet, node2     Container image "nginx:1.17.1" already present on machine
  Normal   Created    30s (x3 over 82s)  kubelet, node2     Created container nginx
  Normal   Started    30s (x3 over 82s)  kubelet, node2     Started container nginx
  Warning  Unhealthy  0s (x9 over 80s)   kubelet, node2     Liveness probe failed: /bin/cat: /tmp/hello.txt: No such file or directory
  Normal   Killing    0s (x3 over 60s)   kubelet, node2     Container nginx failed liveness probe, will be restarted
'''

方式二:TCPSocket

#cat >pod-test.yaml <
apiVersion: v1
kind: Pod
metadata:
  name: pod-liveness-tcpsocket
  namespace: dev
spec:
  containers:
  - name: nginx
    image: nginx:1.17.1
    ports: 
    - name: nginx-port
      containerPort: 80
    livenessProbe:
      tcpSocket:
        port: 81 # 尝试访问81端口
        #port: 80 # 尝试访问80端口
#EOF
'''
尝试访问81端口,但是失败了,所以重启。改成80端口即可。这里tcp访问的是pod ip:port
Events:
  Type     Reason     Age                From               Message
  ----     ------     ----               ----               -------
  Normal   Scheduled  39s                default-scheduler  Successfully assigned dev/pod-liveness-tcpsocket to node2
  Normal   Pulled     10s (x2 over 39s)  kubelet, node2     Container image "nginx:1.17.1" already present on machine
  Normal   Created    10s (x2 over 39s)  kubelet, node2     Created container nginx
  Warning  Unhealthy  10s (x3 over 30s)  kubelet, node2     Liveness probe failed: dial tcp 10.244.2.55:81: connect: connection refused
  Normal   Killing    10s                kubelet, node2     Container nginx failed liveness probe, will be restarted
'''

方式三:HTTPGet

#cat >pod-test.yaml <
apiVersion: v1
kind: Pod
metadata:
  name: pod-liveness-httpget
  namespace: dev
spec:
  containers:
  - name: nginx
    image: nginx:1.17.1
    ports:
    - name: nginx-port
      containerPort: 80
    livenessProbe:
      httpGet:  # 其实就是访问http://127.0.0.1:80/hello  
        scheme: HTTP #支持的协议,http或者https
        port: 80 #端口号
        path: /hello #URI地址
        #path: / #URI地址
#EOF
'''
访问http://127.0.0.1:80/hello失败,重启pod。改个可访问的路径即可
Events:
  Type     Reason     Age               From               Message
  ----     ------     ----              ----               -------
  Normal   Scheduled  28s               default-scheduler  Successfully assigned dev/pod-liveness-httpget to node2
  Normal   Pulled     27s               kubelet, node2     Container image "nginx:1.17.1" already present on machine
  Normal   Created    27s               kubelet, node2     Created container nginx
  Normal   Started    27s               kubelet, node2     Started container nginx
  Warning  Unhealthy  9s (x2 over 19s)  kubelet, node2     Liveness probe failed: HTTP probe failed with statuscode: 404

'''

重启策略

在上一节中,一旦容器探测出现了问题,kubernetes就会对容器所在的Pod进行重启,其实这是由pod的重启策略决定的,pod的重启策略有 3 种,分别如下:

  • Always :容器失效时,自动重启该容器,这也是默认值。
  • OnFailure : 容器终止运行且退出码不为0时重启
  • Never : 不论状态为何,都不重启该容器

重启策略适用于pod对象中的所有容器,首次需要重启的容器,将在其需要时立即进行重启,随后再次需要重启的操作将由kubelet延迟一段时间后进行,且反复的重启操作的延迟时长以此为10s、20s、40s、80s、160s和300s,300s是最大延迟时长。

#cat >pod-test.yaml <
apiVersion: v1
kind: Pod
metadata:
  name: pod-restartpolicy
  namespace: dev
spec:
  containers:
  - name: nginx
    image: nginx:1.17.1
    ports:
    - name: nginx-port
      containerPort: 80
    livenessProbe:
      httpGet:
        scheme: HTTP
        port: 80
        path: /hello
  restartPolicy: Never # 设置重启策略为Never
#EOF
'''
检测失败,但设置重启策略为Never,容器不重启
Events:
  Type     Reason     Age                From               Message
  ----     ------     ----               ----               -------
  Normal   Scheduled  57s                default-scheduler  Successfully assigned dev/pod-restartpolicy to node2
  Normal   Pulled     57s                kubelet, node2     Container image "nginx:1.17.1" already present on machine
  Normal   Created    57s                kubelet, node2     Created container nginx
  Normal   Started    57s                kubelet, node2     Started container nginx
  Warning  Unhealthy  35s (x3 over 55s)  kubelet, node2     Liveness probe failed: HTTP probe failed with statuscode: 404
  Normal   Killing    35s                kubelet, node2     Stopping container nginx
'''

你可能感兴趣的:(k8s,nginx)