Istio 服务的健康检查

Kubernetes存活性探针和就绪性探针 描述了几种配置存活性探针和就绪性探针的方法

  1. 命令
  2. HTTP请求
  3. TCP探针
  4. gRPC探针

命令方法无需任何更改即可工作,但HTTP请求、TCP探针和gRPC探针需要Istio对Pod配置进行更改。

Kubelet发送到liveness-http服务的健康检查请求。当启用双向TLS时,这会成为问题,因为Kubelet没有Istio颁发的证书。因此,健康检查请求将失败。

TCP探针检查需要特殊处理,因为Istio将所有传入流量重定向到sidecar,因此所有TCP端口看起来都是打开的。Kubelet只是检查是否有某些进程在指定的端口上监听,因此只要sidecar正在运行,探针就会始终成功。

Istio通过重写应用程序PodSpec就绪性/存活性探针来解决这两个问题,以便将探针请求发送到sidecar代理

存活性探针重写示例

为了演示如何在应用程序PodSpec级别重写就绪性/存活性探针,让我们使用liveness-http-same-port示例

首先为示例创建并标记一个命名空间

$ kubectl create namespace istio-io-health-rewrite
$ kubectl label namespace istio-io-health-rewrite istio-injection=enabled

然后部署示例应用程序

$ kubectl apply -f - <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
  name: liveness-http
  namespace: istio-io-health-rewrite
spec:
  selector:
    matchLabels:
      app: liveness-http
      version: v1
  template:
    metadata:
      labels:
        app: liveness-http
        version: v1
    spec:
      containers:
      - name: liveness-http
        image: docker.io/istio/health:example
        ports:
        - containerPort: 8001
        livenessProbe:
          httpGet:
            path: /foo
            port: 8001
          initialDelaySeconds: 5
          periodSeconds: 5
EOF

部署完成后,您可以检查Pod的应用程序容器以查看更改后的路径

$ kubectl get pod "$LIVENESS_POD" -n istio-io-health-rewrite -o json | jq '.spec.containers[0].livenessProbe.httpGet'
{
  "path": "/app-health/liveness-http/livez",
  "port": 15020,
  "scheme": "HTTP"
}

原始livenessProbe路径现在映射到sidecar容器环境变量ISTIO_KUBE_APP_PROBERS中的新路径

$ kubectl get pod "$LIVENESS_POD" -n istio-io-health-rewrite -o=jsonpath="{.spec.containers[1].env[?(@.name=='ISTIO_KUBE_APP_PROBERS')]}"
{
  "name":"ISTIO_KUBE_APP_PROBERS",
  "value":"{\"/app-health/liveness-http/livez\":{\"httpGet\":{\"path\":\"/foo\",\"port\":8001,\"scheme\":\"HTTP\"},\"timeoutSeconds\":1}}"
}

对于HTTP和gRPC请求,sidecar代理将请求重定向到应用程序并剥离响应正文,仅返回响应代码。对于TCP探针,sidecar代理将执行端口检查,同时避免流量重定向。

所有内置Istio配置配置文件默认情况下都启用了有问题的探针的重写,但可以按如下所述禁用。

使用命令方法的存活性探针和就绪性探针

Istio提供了一个存活性示例,该示例实现了这种方法。为了演示它在启用双向TLS的情况下如何工作,首先为示例创建一个命名空间

$ kubectl create ns istio-io-health

要配置严格的双向TLS,请运行

$ kubectl apply -f - <<EOF
apiVersion: security.istio.io/v1
kind: PeerAuthentication
metadata:
  name: "default"
  namespace: "istio-io-health"
spec:
  mtls:
    mode: STRICT
EOF

接下来,更改到Istio安装的根目录,并运行以下命令以部署示例服务

压缩
$ kubectl -n istio-io-health apply -f <(istioctl kube-inject -f @samples/health-check/liveness-command.yaml@)

要确认存活性探针是否正常工作,请检查示例Pod的状态以验证它是否正在运行。

$ kubectl -n istio-io-health get pod
NAME                             READY     STATUS    RESTARTS   AGE
liveness-6857c8775f-zdv9r        2/2       Running   0           4m

使用HTTP、TCP和gRPC方法的存活性探针和就绪性探针

如前所述,Istio默认使用探针重写来实现HTTP、TCP和gRPC探针。您可以为特定Pod或全局禁用此功能。

禁用Pod的探针重写

您可以使用注释Pod,并使用sidecar.istio.io/rewriteAppHTTPProbers: "false"来禁用探针重写选项。请确保将注释添加到Pod资源中,因为它在其他任何地方(例如,在包含的部署资源上)都会被忽略。

kubectl apply -f - <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
  name: liveness-http
spec:
  selector:
    matchLabels:
      app: liveness-http
      version: v1
  template:
    metadata:
      labels:
        app: liveness-http
        version: v1
      annotations:
        sidecar.istio.io/rewriteAppHTTPProbers: "false"
    spec:
      containers:
      - name: liveness-http
        image: docker.io/istio/health:example
        ports:
        - containerPort: 8001
        livenessProbe:
          httpGet:
            path: /foo
            port: 8001
          initialDelaySeconds: 5
          periodSeconds: 5
EOF

这种方法允许您在各个部署上逐渐禁用健康检查探针重写,而无需重新安装Istio。

全局禁用探针重写

安装Istio,使用--set values.sidecarInjectorWebhook.rewriteAppHTTPProbe=false全局禁用探针重写。**或者**,更新Istio sidecar注入器的配置映射

$ kubectl get cm istio-sidecar-injector -n istio-system -o yaml | sed -e 's/"rewriteAppHTTPProbe": true/"rewriteAppHTTPProbe": false/' | kubectl apply -f -

清理

删除用于示例的命名空间

$ kubectl delete ns istio-io-health istio-io-health-rewrite
这些信息是否有用?
您对改进有什么建议?

感谢您的反馈!