而不是 readinessProbe 您可以只使用InitContainer。
您创建一个 pod/deployment X,创建服务 X,并创建一个正在搜索服务 X 的 initContainer。
如果他找到它-> 他将制作吊舱。
如果他找不到 -> 他将继续寻找,直到创建服务 X。
只是一个简单的例子,我们使用kubectl apply -f nginx.yaml
.
nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
spec:
selector:
matchLabels:
run: my-nginx
replicas: 2
template:
metadata:
labels:
run: my-nginx
spec:
containers:
- name: my-nginx
image: nginx
ports:
- containerPort: 80
然后我们创建initContainer
initContainer.yaml
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app: myapp
spec:
containers:
- name: myapp-container
image: busybox:1.28
command: ['sh', '-c', 'echo The app is running! && sleep 3600']
initContainers:
- name: init-myservice
image: busybox:1.28
command: ['sh', '-c', 'until nslookup my-nginx; do echo waiting for myapp-pod2; sleep 2; done;']
initContainer 将寻找服务my-nginx,直到您创建它,它才会处于Init:0/1
状态。
NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 Init:0/1 0 15m
在您添加服务后,例如使用kubectl expose deployment/my-nginx
和 initContainer 会找到 my-nginx 服务,他将被创建。
NAME READY STATUS RESTARTS AGE
myapp-pod 1/1 Running 0 35m
结果:
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled <unknown> default-scheduler Successfully assigned default/myapp-pod to kubeadm2
Normal Pulled 20s kubelet, kubeadm2 Container image "busybox:1.28" already present on machine
Normal Created 20s kubelet, kubeadm2 Created container init-myservice
Normal Started 20s kubelet, kubeadm2 Started container init-myservice
Normal Pulled 20s kubelet, kubeadm2 Container image "busybox:1.28" already present on machine
Normal Created 20s kubelet, kubeadm2 Created container myapp-container
Normal Started 20s kubelet, kubeadm2 Started container myapp-container
让我知道这是否回答了您的问题。