其他分享
首页 > 其他分享> > k8s 存活探针LivenessProbe

k8s 存活探针LivenessProbe

作者:互联网

为什么需要存活探针

如果没有探针,k8s无法知道应用是否还活着,只要进程还在运行,k8s则认为容器是健康的。

k8s容器探测机制

http get

对容器的ip地址(指定的端口和路径)执行http get请求

如果探测器收到响应,并且响应码是2xx, 3xx,则认为探测成功。如果服务器没有响应或者返回错误响应则说明探测失败,容器将重启。

tcp socket

探针与容器指定端口建立tcp连接,如果连接建立则探测成功,否则探测失败容器重启。

exec

在容器内执行任意命令,并检查命令退出状态码,如果状态码为0,则探测成功,否则探测失败容器重启。

给pod添加存活探针:livenessProbe

apiVersion: v1
kind: Pod
metadata:
   namespace: ctg
   name: kubia-unhealthy
   labels:
      name: kubia-unhealthy
spec:
   containers:
   - image: luksa/kubia-unhealthy
     name: kubia
     livenessProbe:
       httpGet:
         path: /
         port: 8080

查看pod存活探针信息

通过describe可以查看当前pod详情。

里面有关于pod存活探针的信息:

livenessProbe描述 

Liveness:       http-get http://:8080/ delay=0s timeout=1s period=10s #success=1 #failure=3
 
delays: 延迟,delays=0s,表示在容器启动后立即开始探测
timeout: 超时,timeout=1s,表示容器必须在1s内进行响应,否则这次探测记作失败
period: 周期,period=10s,表示每10s探测一次容器
failure: 失败,failure=3,连续3次失败后重启容器
 
以上存活探针表示:容器启动后立即进行探测,如果1s内容器没有给出回应则记作探测失败。每次间隔10s进行一次探测,在探测连续失败3次后重启容器。

添加livenessProbe参数

initialDelaySeconds。如果没有设置,探针将在启动时立即探测,通常会导致探测失败,因为程序还没准备好开始接收请求。

这种情况很常见,看到容器在重启,使用describe会看到容器退出码为137或者143

Killing container with id docker://kubia:Container failed liveness probe.. Container will be killed and recreated.

如果在pod启动时遇到这种情况,一般是没有设置 initialDelaySeconds导致的

# 设置初始化延迟initialDelaySeconds。
livenessProbe:
       httpGet:
         path: /
         port: 8080
       initialDelaySeconds: 15 # 在k8s第一次探测前等待15s

 

标签:容器,LivenessProbe,探针,livenessProbe,探测,pod,k8s,kubia
来源: https://www.cnblogs.com/liujunjun/p/14383075.html