如何在k8s pod中配置具有容器到容器网络的TCP活动探测?

dtu*_*ski 2 sockets tcp kubernetes

我注意到pod中的容器可以使用localhost按照所宣传的方式相互通信.例如,一个容器在localhost:9999上启动服务器套接字,第二个容器可以连接到该地址.如果我公开服务器容器的端口,则会失败.如果我在该端口上创建TCP活动探测,它也会失败.看来liveness探针使用pod IP地址,除非暴露,否则无法连接到localhost:9999.如果两个容器都使用pod IP,即$ HOSTNAME:9999,并且端口被暴露,那么一切正常.有没有一个例子适用于每个容器使用localhost并且TCP探测器工作的地方?

Sim*_*sar 5

下面是一个示例部署,使用TCP活动探测,TCP就绪探测以及在容器中容器与容器暴露的服务器容器端口之间的网络:

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'
Run Code Online (Sandbox Code Playgroud)

创建和验证部署:

> 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
[…]
Run Code Online (Sandbox Code Playgroud)