在Pod的生命周期中,可以执行多种操作如下图:
在Kubernetes中,探针(Probe)用于管理容器的生命周期,主要包括以下三种类型:
通过合理配置这三类探针,可以确保Kubernetes中的容器在生命周期的各个阶段都处于健康状态,并能够及时处理异常情况。
highlighter- makefile
执行一段命令,根据返回值判断执行结果。返回值为0, 非0两种结果,可以理解为"echo $?"。
highlighter- properties
通过发起HTTTP协议的GET请求检测某个http请求的返回状态码,从而判断服务是否正常。 常见的状态码分为很多类,比如: "2xx,3xx"正常, "4xx,5xx"错误。200: 返回状态码成功。301: 永久跳转,会将跳转信息缓存到浏览器本地。302: 临时跳转,并不会将本次跳转缓存到本地。401: 验证失败。403: 权限被拒绝。404: 文件找不到。413: 文件上传过大。500: 服务器内部错误。502: 无效的请求。504: 后端应用网关相应超时。
测试某个TCP端口是否能够连接,类似于telnet这样的工具。
每次探测都将获得以下三种结果之一:
Success(成功):容器通过了诊断。 Failure(失败):容器未通过诊断。 Unknown(未知):诊断失败,因此不会采取任何行动。yaml
apiVersion: v1 kind: Pod metadata: name: livenessprobe-exec spec: containers: - name: nginx image: nginx:1.18 ports: - containerPort: 80 args: - /bin/sh - -c - touch /tmp/healthy; sleep 10; rm -rf /tmp/healthy; sleep 600 livenessProbe: exec: command: - cat - /tmp/healthy initialDelaySeconds: 15 periodSeconds: 5 timeoutSeconds: 1 successThreshold: 1 failureThreshold: 3
yaml
apiVersion: v1 kind: Pod metadata: name: livenessprobe-httpGet spec: containers: - name: nginx image: nginx:1.18 ports: - containerPort: 80 livenessProbe: httpGet: path: /index.html port: 80 initialDelaySeconds: 15 periodSeconds: 5 timeoutSeconds: 1 successThreshold: 1 failureThreshold: 3
yaml
apiVersion: apps/v1 kind: Deployment metadata: name: livenessprobe-tcpsocket spec: replicas: 1 selector: matchLabels: app: livenessprobe-tcpsocket template: metadata: labels: app: livenessprobe-tcpsocket spec: containers: - name: nginx image: registry.cn-guangzhou.aliyuncs.com/jiajia-k8s/nginx:1.21 ports: - containerPort: 80 args: - /bin/sh - -c - tail -f /etc/hosts livenessProbe: tcpSocket: port: 80 initialDelaySeconds: 10 periodSeconds: 3 successThreshold: 1 failureThreshold: 2
highlighter- llvm
很明显,上述案例暴露了80端口,由于启动容器时并没有启动nginx,而是去使用tail指令去查看一个文件内容达到阻塞容器的目的,因此在容器启动10后就开始第一次检查,而后每个3秒检查1次,达到指定次数后会触发重启操作,可以看到55秒内探测了4次,52秒内重启了两次
yaml
apiVersion: apps/v1 kind: Deployment metadata: name: readinessProbe-exec labels: apps: nginx spec: replicas: 3 selector: matchLabels: apps: nginx template: metadata: name: nginx labels: apps: nginx spec: containers: - name: nginx image: nginx:1.18 command: - /bin/bash - -c - touch /tmp/nginx-healthy; sleep 60; rm -f /tmp/nginx-healthy; sleep 60; livenessProbe: exec: command: - cat - /tmp/nginx-healthy failureThreshold: 3 initialDelaySeconds: 15 periodSeconds: 1 successThreshold: 1 timeoutSeconds: 1 readinessProbe: exec: command: - cat - /tmp/nginx-healthy-2023 failureThreshold: 3 initialDelaySeconds: 15 periodSeconds: 1 successThreshold: 1 timeoutSeconds: 1
从事件日志来看,Liveness Probe 和 Readiness Probe 都因为无法找到 /tmp/nginx-healthy 文件而失败。这是由于容器中的命令在 60 秒后删除了该文件,导致探针在检查时无法访问该文件。
Liveness Probe 失败触发了容器重启。此探针用于检测容器是否健康,不健康时会重启容器。 Readiness Probe 失败使得 Pod 从 Service 的 Endpoints 列表中移除,导致 Pod 无法接收流量。这表示 Pod 当前无法处理请求。
yaml
apiVersion: apps/v1 kind: Deployment metadata: name: readinessprobe-httpget spec: replicas: 1 selector: matchLabels: app: readinessprobe-httpget template: metadata: labels: app: readinessprobe-httpget spec: containers: - name: nginx image: nginx:1.21 ports: - containerPort: 80 readinessProbe: httpGet: path: /healthz port: 80 initialDelaySeconds: 15 periodSeconds: 5 failureThreshold: 3 successThreshold: 1 timeoutSeconds: 1
yaml
apiVersion: apps/v1 kind: Deployment metadata: name: readinessprobe-tcpsocket spec: replicas: 1 selector: matchLabels: app: readinessprobe-tcpsocket template: metadata: labels: app: readinessprobe-tcpsocket spec: containers: - name: nginx image: nginx:1.21 ports: - containerPort: 80 livenessProbe: tcpSocket: port: 80 failureThreshold: 3 initialDelaySeconds: 30 periodSeconds: 1 successThreshold: 1 timeoutSeconds: 1 readinessProbe: tcpSocket: port: 80 initialDelaySeconds: 15 periodSeconds: 5 failureThreshold: 3 successThreshold: 1 timeoutSeconds: 1
yaml
apiVersion: apps/v1 kind: Deployment metadata: name: startupprobe-httpget spec: replicas: 1 selector: matchLabels: app: startupprobe-httpget template: metadata: labels: app: startupprobe-httpget spec: containers: - name: nginx image: nginx:1.21 ports: - containerPort: 80 startupProbe: httpGet: path: /startup port: 80 initialDelaySeconds: 30 periodSeconds: 10 failureThreshold: 10 timeoutSeconds: 5
yaml
apiVersion: apps/v1 kind: Deployment metadata: name: startupprobe-tcpsocket spec: replicas: 1 selector: matchLabels: app: startupprobe-tcpsocket template: metadata: labels: app: startupprobe-tcpsocket spec: containers: - name: nginx image: nginx:1.21 ports: - containerPort: 80 startupProbe: tcpSocket: port: 80 initialDelaySeconds: 30 periodSeconds: 10 failureThreshold: 10 timeoutSeconds: 5 简介什么是探针 Liveness Probe(存活探针) Readiness Probe(就绪探针) Startup Probe(启动探针)什么时候使用探针? 何时使用存活探针(Liveness Probe) 何时使用就绪探针(Readiness Probe) 何时使用启动探针(Startup Probe)容器探测方法 exec httpGet tcpSocket容器探测使用 livenessProbe使用 exec使用 httpGet使用 tcpSocket readinessProbe使用 exec使用 httpGet使用 tcpSocket使用 startupProbe使用 httpGet使用 tcpSocket使用
__EOF__
本文作者: Unstoppable9527 本文链接: https://www.cnblogs.com/Unstoppable9527/p/18352747 关于博主: 评论和私信会在第一时间回复。或者直接私信我。 版权声明: 除特殊说明外,转载请注明出处~[知识共享署名-相同方式共享 4.0 国际许可协议] 声援博主: 如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。
相关知识
[云原生] Kubernetes(k8s)健康检查详解与实战演示(就绪性探针 和 存活性探针)
Docker安全性:最佳实践和常见安全考虑
《西尔斯怀孕百科》威廉·西尔斯/全新升级典藏版
spring boot 应用在 k8s 中的健康检查(一)
k8s健康检查 spring k8s健康检查探针多个地址
要想Pod好
ASP.NET Core 中的健康狀態檢查
如何为托管到SAE的应用配置健康检查
健康狀態監視
[健康] 大健康行业概述
网址: Kubernetes https://m.trfsz.com/newsview905352.html