微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Kubernetes postStart 生命周期总是失败

如何解决Kubernetes postStart 生命周期总是失败

尝试使用 postStart 生命周期解决 Pod 之间的依赖关系。

用例:微服务A应该在微服务B启动之后启动。

为此,我们添加一个容器 (curl),它将使用 curl 命令检查依赖服务是否已启动。

但是当我们在 postStart 生命周期钩子中添加任何命令时,pod 会不断重启并进入 crashlookbackoff 状态

部署.yaml:

kind: Deployment
Metadata:
 name: Microservice-A-deployment
spec:
 replicas: 1
 selector:
   matchLabels:
     app: Microservice-A
 template:
   Metadata:
     labels:
       app: Microservice-A
       date: 20thJune2021
     annotations:
       sidecar.istio.io/rewriteAppHTTPProbers: "false"
       proxy.istio.io/config: '{ "holdApplicationUntilProxyStarts": true }'
   spec:
     containers:
       - name: curl
         image: ewoutp/docker-Nginx-curl
         imagePullPolicy: IfNotPresent
         command: [ 'sh','-c','touch /tmp/healthy; echo The Pod is running && sleep 50' ]
         livenessProbe:
           exec:
             command:
               - cat
               - /tmp/healthy
           initialDelaySeconds: 15
           periodSeconds: 5
         lifecycle:
           postStart:
             exec:
               command: [ "/bin/sh","-c",'sleep 10;until [ $(eval curl -o -I -L -s -w "%{http_code}" http://microservice-B-api-service:9439/manage/health) -eq 200 ]; do echo "Waiting for microservice-B API";sleep 10;  done; exit 0' ]
       - name: Microservice-A
         image: microserviceA:latest
         imagePullPolicy: Always
         ports:[![enter image description here][1]][1]
           - name: port
             containerPort: 8080
         livenessProbe:
           httpGet:
             path: /actuator/health
             port: 8080
           initialDelaySeconds: 120
           periodSeconds: 30
           timeoutSeconds: 30
     imagePullSecrets:
       - name: dockersecret

注意:不使用 init-container 的原因:因为我们已经使用严格的 MTLS 策略实现了 Istio。 https://github.com/istio/istio/issues/32039

在互联网上搜索此问题时发现如下。

enter image description here

解决方法

您也可以像这样将 readinessProbe 与 livenessProbe 一起使用:

        readinessProbe:
            httpGet:
                path: /api/health
                port: 8080
        initialDelaySeconds: 10
        periodSeconds: 5
        failureThreshold: 8

        // for tcpSocket use:
            readinessProbe:
                tcpSocket:
                    port: 3306 
,

这是因为您在 postStart 中的命令休眠了 10 秒,而您的 LivenessProbe 配置为在 5 秒后失败。

也许增加 initialDelaySeconds 或增加一个 failureThreshold

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。