K8S学习之基础十二:容器的三种探测

容器的三种 探测:

startupprobe:探测容器中的应用是否已经启动。如果启动则禁用其他所有探测,知道它成功为止,如果启动探测失败,kubelet将杀死容器,按照重启策略进行。如果没有提供启动探测,默认状态为成功success

livenessporbe:用指定的方式(exec、tcp、http)检测pod中容器是否正常运行,如果失败则任务不健康,根据重启策略进行。如果没有配置linenessprobe,默认状态为成功success

readinessporbe:用于检测容器中的应用是否可以接受请求,当探针成功后才使pod对外提供网络访问,将容器标记为就绪状态,可以加到pod前段负载,如果失败,则将容器标记为未就绪,把pod从前端移除

顺序:先startupprobe,后livenessprobe和livenessprobe,存货和就绪无前后顺序

三种探针:

exec:容器中执行指定命令,成功退出码为0,则探测成功

TCPSocket:通过容器ip+port执行TCP,如果能建立了链接,表明容器健康

HTTPSocket:通过ip+port+dir调用HTTPGet方法,如果[200,400),容器健康

探针检测结果:

success:通过检测

failure:未通过检测

unknown:检测没有正常进行

pod探针相关属性

探针(Probe)有许多可选字段,可以用来更加精确的控制Liveness和Readiness两种探针的行为 initialDelaySeconds:容器启动后要等待多少秒后探针开始工作,单位"秒",默认是 0 秒,最小值是 0

periodSeconds: 执行探测的时间间隔(单位是秒),默认为 10s,单位"秒",最小值是1

timeoutSeconds: 探针执行检测请求后,等待响应的超时时间,默认为1,单位"秒"。

successThreshold:连续探测几次成功,才认为探测成功,默认为 1,在 Liveness 探针中必须为1,最小值为1。

failureThreshold: 探测失败的重试次数,重试一定次数后将认为失败,在 readiness 探针中,Pod会被标记为未就绪,默认为 3,最小值为 1

两种探针区别:

ReadinessProbe 和 livenessProbe 可以使用相同探测方式,只是对 Pod 的处置方式不同:

readinessProbe 当检测失败后,将 Pod 的 IP:Port 从对应的 EndPoint 列表中删除。

livenessProbe 当检测失败后,将杀死容器并根据 Pod 的重启策略来决定作出对应的措施。

1、启动 探测startuprpobe

1.1、exec模式
vi startup-exec.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: startupprobe
spec:
  containers:
  - name: startup
image: 172.16.80.140/tomcat/tomcat:v1
imagePullPolicy: IfNotPresent
    ports:
    - containerPort: 8080
    startupProbe:
     exec:
       command:
       - "/bin/sh"
       - "-c"
       - "ps aux | grep tomcat1"
     initialDelaySeconds: 5 #容器启动后多久开始探测
     periodSeconds: 5 #执行探测的时间间隔
     timeoutSeconds: 3 #探针执行检测请求后,等待响应的超时时间
     successThreshold: 1 #成功多少次才算成功
     failureThreshold: 3 #失败多少次才算失败
# 由于命令ps aux | grep tomcat1 没有返回结果
9.1. 2 tcpSocket 模式
vi startup-tcp.yaml
apiVersion: v1
kind: Pod
metadata:
  name: startupprobe
spec:
  containers:
  - name: startup
image: 172.16.80.140/tomcat/tomcat:v1
imagePullPolicy: IfNotPresent
    ports:
    - containerPort: 8080
    startupProbe:
      tcpSocket:
        port: 8080
      initialDelaySeconds: 5 #容器启动后多久开始探测
      periodSeconds: 5 #执行探测的时间间隔
      timeoutSeconds: 3 #探针执行检测请求后,等待响应的超时时间
      successThreshold: 1 #成功多少次才算成功
      failureThreshold: 3 #失败多少次才算失败
# 由于tcp8081无返回结果
9.1. 3 httpget 模式
vi startup-httpget
apiVersion: v1
kind: Pod
metadata:
  name: startupprobe
spec:
  containers:
  - name: startup
image: 172.16.80.140/tomcat/tomcat:v1
imagePullPolicy: IfNotPresent
    ports:
    - containerPort: 8080
    startupProbe:
      httpGet:
        path: /
        port: 8081
      initialDelaySeconds: 5 #容器启动后多久开始探测
      periodSeconds: 5 #执行探测的时间间隔
      timeoutSeconds: 3 #探针执行检测请求后,等待响应的超时时间
      successThreshold: 1 #成功多少次才算成功
      failureThreshold: 3 #失败多少次才算失败

9.2 存活探测livenessprobe

9.2.1 exec模式
vi liveness-exec.yaml
apiVersion: v1
kind: Pod
metadata:
  name: liveness-exec
  labels:
    app: liveness
spec:
  containers:
  - name: liveness
  image: 172.16.80.140/busybox/busybox:1.28
  imagePullPolicy: IfNotPresent
    args:                       #创建测试探针探测的文件
    - /bin/sh
    - -c
    - touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600
    livenessProbe:
      initialDelaySeconds: 10   #延迟检测时间
      periodSeconds: 5          #检测时间间隔
      exec:
        command:
        - cat
        - /tmp/healthy
# 10s开始检测,每隔5s检测一次,默认三次失败根据重启策略重启pod
9.2.2 tcpsocket模式
vi liveness-tcp.yaml
apiVersion: v1
kind: Pod
metadata:
  name: liveness-tcp
  labels:
    app: liveness
spec:
  containers:
  - name: liveness
    image: 172.16.80.140/nginx/nginx:1.26
    imagePullPolicy: IfNotPresent
    livenessProbe:
      initialDelaySeconds: 15
      periodSeconds: 20
      tcpSocket:
        port: 80
# 进入pod停掉容器nginx,由于配置了存活探测,根据重启策略会重启

9.2.3 httpget模式

vi liveness-httpget.yaml
apiVersion: v1
kind: Pod
metadata:
  name: liveness-http
  labels:
    test: liveness
spec:
  containers:
  - name: liveness
image: 172.16.80.140/tomcat/tomcat:v1
imagePullPolicy: IfNotPresent
    livenessProbe:
      initialDelaySeconds: 20   #延迟加载时间
      periodSeconds: 5          #重试时间间隔
      timeoutSeconds: 10        #超时时间设置
      httpGet:
        scheme: HTTP
        port: 8081
        path: /
# 存活探针每15s探测一次,如果失败就重启

9.3 就绪探测livenessprobe

9.3.1 httpget模式
vi readiness-exec.yaml
apiVersion: v1
kind: Service
metadata:
  name: tomcat
  labels:
    app: tomcat
spec:
  type: NodePort
  ports:
  - name: server
    port: 8080
    targetPort: 8080
    nodePort: 31180
  selector:
    app: tomcat
---
apiVersion: v1
kind: Pod
metadata:
  name: tomcat
  labels:
    app: tomcat
spec:
  containers:
  - name: tomcat
    image: 172.16.80.140/tomcat/tomcat:v1
    imagePullPolicy: IfNotPresent
    ports:
    - name: tomcat
      containerPort: 8080
    readinessProbe:
      initialDelaySeconds: 20   
      periodSeconds: 5          
      timeoutSeconds: 10   
      httpGet:
        scheme: HTTP
        port: 8080
        path: /
# 创建一个配置有就绪探测的pod,一个绑定pod的service,
# 通过浏览器访问service
9.3.2 tcpsocket模式

略,同httpget

9.3.3 exec模式

略,同httpget

相关推荐
JeffreyGu.3 小时前
Linux Shell脚本-分隔符问题
linux·服务器·学习
随机惯性粒子群3 小时前
wheel_legged_genesis 开源项目复现与问题记录
学习·开源·github·强化学习·genesis
栀寒老醑4 小时前
模板注入漏洞(SSTI)学习笔记
笔记·学习·安全·web安全·网络安全·系统安全·安全架构
charlie1145141915 小时前
从0开始的操作系统手搓教程21:进程子系统的一个核心功能——简单的进程切换
汇编·学习·操作系统·线程·进程·手搓教程
凉、介5 小时前
ARM 架构下 cache 一致性问题整理
linux·汇编·arm开发·学习·缓存·架构
虾球xz5 小时前
游戏引擎学习第137天
人工智能·学习·游戏引擎
ysy16480672396 小时前
Javase学习复习D4[流程控制]
学习
薛定谔的码*6 小时前
学习工具的一天之(burp)
学习
ooo-p6 小时前
FPGA学习篇——Verilog学习4
学习·fpga开发