尝试使用 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