我有两个应用程序 - app1和app2,其中app1是app2的config server
配置.我在app1中定义了端点,需要等到它返回状态才能启动app2的 pod ./readiness
OK
至关重要的是app2的部署要等到app1中的/ readyiness端点kubernetes
接收Http Status OK
,因为它是配置服务器,并且为app2保留了重要的配置.
是否可以执行此类部署依赖性?
Vis*_*ant 23
您需要使用initContainers
. 以下是您如何在YAML
文件中执行的示例
initContainers:
- name: wait-for-other-pod
image: docker.some.image
args:
- /bin/sh
- -c
- >
set -x;
while [ $(curl -sw '%{http_code}' "http://www.<your_pod_health_check_end_point>.com" -o /dev/null) -ne 200 ]; do
sleep 15;
done
Run Code Online (Sandbox Code Playgroud)
我曾经使用curl
过健康检查端点,您可以使用任何其他 UNIX 命令来检查其他 pod 是否准备就绪。
是的,可以使用Init Containers(也可以参考此博客文章了解一些后台重新计时),但更好的,更多Kubernetes本地模式是使用重试和超时,而不是以这种方式硬编码依赖项.
就我而言,应用程序没有 /readiness 端点。我们有主 Pod 和工作 Pod,我们希望工作 Pod 仅在主 Pod 启动并运行后启动。主 Pod 运行一个在 TCP 端口 80 上侦听的应用程序。我最终在工作 Pod 的 init 容器中使用了 netcat。
nc -z <host> <port>
Run Code Online (Sandbox Code Playgroud)
nc将检查端口是否打开,成功时返回 0,失败时返回 1。
initContainers:
- name: wait-for-master-before-starup
image: busybox
command: ["sh", "-c", "until nc -z master-service 80 > /dev/null; do echo Waiting for master.; sleep 2; done;"]
Run Code Online (Sandbox Code Playgroud)
如果您已经有了健康/准备端点,那么最好像此处其他答案中提到的那样使用它。
initContainers:
- name: wait-for-dependent-service
image: stefanevinance/wait-for-200
env:
- name: URL
value: http://dependent-service.{{.Release.Namespace}}.svc.cluster.local:3000
Run Code Online (Sandbox Code Playgroud)
使用https://hub.docker.com/r/stefanevinance/wait-for-200/
使用wait-for-it.sh其实很简单:
initContainers:
- name: wait-for-app1
image: image-docker-containing-sh
args:
- /bin/sh
- -c
- /usr/app/wait-for-it.sh app1:<portapp1> -t 0
Run Code Online (Sandbox Code Playgroud)
当然,重试和超时是可行的方法,但这作为一种解决方法非常有效。
归档时间: |
|
查看次数: |
11078 次 |
最近记录: |