【问题标题】:How do you configure a TCP liveness probe with container-to-container networking in a k8s pod?如何在 k8s pod 中使用容器到容器网络配置 TCP 活跃度探测?
【发布时间】:2018-03-01 20:50:07
【问题描述】:
我注意到 pod 中的容器可以使用 localhost 来相互通信,正如所宣传的那样。例如,一个容器在 localhost:9999 上启动一个服务器套接字,第二个容器可以连接到该地址。如果我公开服务器容器的端口,这将失败。如果我在该端口上创建 TCP 活跃度探测,它也会失败。看来 liveness probe 使用的是 pod IP 地址,除非暴露,否则无法连接到 localhost:9999。如果两个容器都使用 pod IP,即 $HOSTNAME:9999,并且端口被公开,那么一切正常。有没有人有一个例子,每个容器都使用 localhost 并且 TCP 探测工作?
【问题讨论】:
标签:
sockets
tcp
kubernetes
【解决方案1】:
这是一个使用 TCP liveness probe、TCP readiness probe 和 Pod 中容器之间网络的示例部署,其中服务器容器端口暴露:
test.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
[…]