我注意到 pod 中的容器可以像宣传的那样使用 localhost 相互通信。例如,一个容器在 localhost:9999 上启动一个服务器套接字,第二个容器可以连接到该地址。如果我公开服务器容器的端口,这将失败。如果我在该端口上创建 TCP active 探测,它也会失败。看起来 liveness probe 使用 pod IP 地址并且无法连接到 localhost:9999 除非它被暴露。如果两个容器都使用 pod IP,即 $HOSTNAME:9999,并且端口公开,则一切正常。有没有人有一个示例可以在每个容器使用 localhost 并且 TCP 探测有效的情况下工作?
最佳答案
这是一个示例部署,使用 TCP active 探测、TCP 就绪探测和 pod 中容器之间的网络,并暴露服务器容器端口:
测试.yml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: test
spec:
template:
metadata:
labels:
app: test
spec:
containers:
- name: server
image: alpine
command:
- '/bin/sh'
- '-c'
- 'nc -p 8080 -kle echo pong'
livenessProbe:
tcpSocket:
port: 8080
readinessProbe:
tcpSocket:
port: 8080
ports:
- containerPort: 8080
- name: client
image: alpine
command:
- '/bin/sh'
- '-c'
- 'while true; do echo -e | nc localhost 8080; sleep 1; done'
创建和验证部署:
> kubectl create -f test.yml
> kubectl get pod -l app=test
NAME READY STATUS RESTARTS AGE
test-620943134-fzm05 2/2 Running 0 1m
> kubectl log test-620943134-fzm05 client
pong
pong
pong
[…]
关于sockets - 如何在 k8s pod 中使用容器到容器网络配置 TCP active 探测?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46328039/