kubernetes Pod 控制器器应用进阶

本文深入探讨了Kubernetes中Pod的livenessProbe和readinessProbe,解释了它们的区别和作用,以及如何通过exec、HTTPGet和TCPSocket等方式设置探针。同时,介绍了生命周期钩子postStart和preStop的用法,强调了这些机制在确保容器和服务稳定运行中的重要性。
摘要由CSDN通过智能技术生成

实现容器探测的几种方式:   探针类型有三种:    ExecAtion     TCPSocketAction    HTTPGetAction   

那么探针针对于存活性来做的话,就是存活性探针,针对就绪来做,就是就绪性探针 

那么 livenessProbe 是做存活性探测 ,存活未必就绪

  livenessProbe    <Object>
     Periodic probe of container liveness. Container will be restarted if the
     probe fails. Cannot be updated. More info:
     https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes

readinessProbe 是做就绪性探测的 

 readinessProbe    <Object>
     Periodic probe of container service readiness. Container will be removed
     from service endpoints if the probe fails. Cannot be updated. More info:
     https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes

   lifecycle    <Object>
     Actions that the management system should take in response to container
     lifecycle events. Cannot be updated.

[root@server1 ~]# kubectl explain pods.spec.containers.livenessProbe

下面是三种定义的探针,那么我们每次定义的时候都定义三种中的一种就可以了

   exec    <Object>
     One and only one of the following should be specified. Exec specifies the
     action to take.

  httpGet    <Object>
     HTTPGet specifies the http request to perform.

   tcpSocket    <Object>
     TCPSocket specifies an action involving a TCP port. TCP hooks not yet
     supported

这个字段的意思是,探针在探测的过程中,并不是一开始探测的时候,探测到失败就认为是失败的,那么只有探测3次以上才认为它是失败的。

   failureThreshold    <integer>
     Minimum consecutive failures for the probe to be considered failed after
     having succeeded. Defaults to 3. Minimum value is 1.

这个字段表示探测的生命周期,也就是间隔多长时间进行探测一次 ,默认是10秒探测一次

   periodSeconds    <integer>
     How often (in seconds) to perform the probe. Default to 10 seconds. Minimum
     value is 1.

那么在探测的时候始终没有响应应该等多久,默认是等待的1秒钟

 timeoutSeconds    <integer>
     Number of seconds after which the probe times out. Defaults to 1 second.
     Minimum value is 1. More info:
     https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes

因为容器在启动的时候是需要进行初始化的,因此在初始化期间是不能被探测的,那么在初始化的这个时间段,我们延迟探测的时间,因此我们要定义这个时间段在容器初始化完成后进行探测,那么如果不定义的话,那么默认的情况下是容器一启动就会进行探测,那么容器启动并不代表主进程可以对外服务了 

   initialDelaySeconds    <integer>
     Number of seconds after the container has started before liveness probes
     are initiated. More info:
     https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes

那么下来写一个探测性的pod对象的用于检测的exec探针的pod对象

[root@server1 ~]# vim liveness-exec.yaml

apiVersion: v1
kind: Pod
metadata:
  name: liveness-exec-pod
  namespace: default
spec:
  containers:
  - name: liveness-exec-container
    image: busybox:latest
    imagePullPolicy: IfNotPresent        镜像的拉取策略,如果不再则拉取,在再则不拉取
    command: ["bin/sh","-c","touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 3600"]     在容器内部执行的命令
    livenessProbe:         做存活性探测  
      exec:           使用exec的探针 
        command: ["test","-e","/tmp/healthy"]      exec 默认使用命令做探针 -e 检测创建的文件是否存在
      initialDela

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值