Simple networking in a kubernetes pod

5/7/2016

I would like to communicate between 2 containers in a pod and the host, to understand better kubernetes ketworking:

    #DockerfileA
    FROM debian:sid
    RUN /bin/bash -c "apt-get update && apt-get install -y netcat"
    CMD /bin/bash echo hello world | nc 127.0.0.1 40000

    #DockerfileB
    FROM debian:sid
    RUN /bin/bash -c "apt-get update && apt-get install -y netcat"
    EXPOSE 40000
    CMD /bin/bash nc -l 40000 | nc 127.0.0.1 50000

    #my_pod.yml
    apiVersion: v1
    kind: Pod
    metadata:
        name: netcat
        labels:
            app: netcat
    spec:
      containers:
      - name: my_container_a
        image: my_image_a:1
      - name: my_container_b
        image: my_image_b:1
        ports:
        - containerPort: 40000

    #my_service.yml        
    apiVersion: v1
    kind: Service
    metadata:
      name: netcat-service
    spec:
      ports:
      - port: 50000
        targetPort: 50000
        protocol: TCP
      selector:
        app: netcat

In practise I would like containerA to send via netcat "hello world" to containerB, and containerB to forward it to host.

But when I create the pod I got the following warning (kubectl describe pod netcat):

Warning FailedSync Error syncing pod, skipping: [failed to "StartContainer" for "my_container_a" with CrashLoopBackOff: "Back-off 10s restarting failed container=my_container_a pod=netcat_default(f141c598-1439-11e6-83fc-009c028bec97)" , failed to "StartContainer" for "my_container_b" with CrashLoopBackOff: "Back-off 10s restarting failed container=my_container_b pod=netcat_default(f141c598-1439-11e6-83fc-009c028bec97)"

Is there any mistake in the files?

Do I need the netcat-service to listen with netcat for the "hello world" message on the host, or is it unnecessary?

On which address:port should I use netcat/curl on the host?

Thank you!

--
dockerfile
kubernetes

1 Answer

5/8/2016

The following docker files work on my machine:

#Dockerfile A
FROM debian:sid
RUN /bin/bash -c "apt-get update && apt-get install -y netcat"
CMD sleep 10 && echo hello world | nc 127.0.0.1 40000

#Dockerfile B
FROM debian:sid
RUN /bin/bash -c "apt-get update && apt-get install -y netcat"
CMD nc -l -p 40000 | nc <your_host_ip> 50000

I don't think you need a service. I just opened a terminal on the host machine and ran "nc -l 50000". Your pod.yaml is correct.

Regarding the "Warning FailedSync Error syncing pod" error message, it seems normal. I see this message whenever a container has terminated. In your case, both containers are terminated because the command fails.

In case you don't know already, you can use "kubectl exec netcat -c mycontainera -i -t -- bash -il" to get into the container and try out your commands.

-- caesarxuchao
Source: StackOverflow