kubelet 如何解释 healthcheck 端点的结果?

问题描述

以下是用于部署容器的 Pod 资源类型:

apiVersion: v1
kind: Pod
Metadata:
  name: my-container
  labels:
    app: myapp
    rel: stable
spec:
  containers:
  - name: my-container
    image: myimage:latest
    resources:
      limits:
        memory: "128Mi" #128 MB
        cpu: "200m" #200 millicpu (.2 cpu or 20% of the cpu)
    ports:
    - containerPort: 80
    livenessProbe:
      httpGet:
        path: /health-check
        port: 80
      initialDelaySeconds: 15
      timeoutSeconds: 2 # Default is 1
      periodSeconds: 5 # Default is 10
      failureThreshold: 1 # Default is 3
    readinessProbe:
      httpGet:
        path: /health-check
        port: 80
      initialDelaySeconds: 3
      periodSeconds: 5 # Default is 10
      failureThreshold: 1 # Default is 3

/health-check 端点返回 http 状态 200 状态,带有以下 json:

{
    "details": {
        "app": {
            "framework": "gin","name": "my-app-local","version": "v1"
        },"databases": [
            {
                "database": "my_db","host": "localhost","name": "MysqL","status": "normal"
            }
        ]
    },"status": "normal"
}

鉴于上面的 Pod yaml,kubelet 如何读取 status"databases""app" 值?作为 livenessProbereadinessProbe 的一部分,以确保容器运行良好。

解决方法

它不是那样工作的,它检查http请求的返回码。以下是文档的片段(在本例中为端口 80):

为了执行探测,kubelet 发送一个 HTTP GET 请求到 在容器中运行并侦听端口 8080 的服务器。

如果服务器的 /healthz 路径的处理程序返回成功代码, kubelet 认为容器是活的和健康的。如果 处理程序返回失败代码,kubelet 杀死容器并 重新启动它。任何大于或等于 200 且小于 400 的代码 表示成功。任何其他代码表示失败

如果您确实需要阅读json 响应,请执行curl 并使用jqgrep 查找json 响应中的状态。对于 jq 类似 jq '.details.databases[].status'

livenessProbe:
  exec:
    command:
    - sh
    - -c
    - curl --silent http://localhost:80/healthz | grep .......