简体   繁体   中英

Container in one pod communicating with one in a multicontainer pod

  • I am trying to figure out the networking in Kubernetes, and especially the handling of multicontainer pods. In my simple scenario, I have total of 3 pods. One has two containers in it and the other one has only one container which wants to communicate with a specific container in that multicontainer pod. I want to figure out how kubernetes handles the communication between such containers.

    For this purpose I have simple multicontainer pod in a "sidecar architecture" the YAML file is as follows:

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  containers:
 - name: nginx-container
    image: nginx
    ports:
    - containerPort: 80
 - name: sidecar
    image: curlimages/curl
    command: ["/bin/sh"]
    args: ["-c", "echo Hello from the sidecar container; sleep 300"]
    ports:
    - containerPort: 5000
  • What I want to achieve with this YAML file is that, in the pod "nginx", have two containers, one running nginx and listens on the port 80 of that pod the other running a simple curl image (anything different than nginx to not violate one container per pod convention of kubernetes) and can listen communication on pod's port 5000.

    Then I have another YAML file again running an nginx image. This container is going to trying to communicate with the nginx and curl images on the other pod. The YAML file is as follows:

apiVersion: v1
kind: Pod
metadata:
  name: nginx-simple
  labels:
    app: nginx
spec:
  containers:
 - name: nginx-container
    image: nginx
    ports:
    - containerPort: 80
  • After deploying the pods I expose the nginx pod simply using the following command:

    kubectl expose pods/nginx

  • Then I start a terminal inside the nginx-simple container.(Pod with one container). When I curl the ip address I get from kubectl get svc which is the service generated from my previous expose command with port 80, I can easily get the welcome message of nginx. However, the problem starts when I try to communicate with the curl container. When I curl the same ip adrress but this time with port 5000(containerPort I set in the Yaml file), I get a connection refused error. What am I doing wrong here?

Thanks in advance.

PS: I would also be more than happy to hear your learning material suggestions for this topic. Thank you so much.

curl is a command line tool. It is not a server that is listening to a port, but a client tool that can be used to access servers.

This container does not contain a server that listen to a port:

 - name: sidecar
   image: curlimages/curl
   command: ["/bin/sh"]
   args: ["-c", "echo Hello from the sidecar container; sleep 300"]
   ports:
   - containerPort: 5000

Services deployed on Kubernetes are typically containers containing some form of webserver, but might be other kind of services as well.


shouldnt i at least be able to ping the curl container?

Nope, containers are not Virtual Machines. Containers typically only contain a single process and a container can only do what that process do. On Kubernetes these processes are typically webservers listening eg on port 8080, so commonly you can only check if they are alive by sending them an HTTP-request. See eg Configure Liveness, Readiness and Startup Probes .

When i run telnet pod-ip 5000 i cannot ping this curl container.

The curl binary is not a process that listen to any port. Eg it cannot respond toICMP . You can typically ping nodes but not containers. Curl is a http-client that typically is used to **send and http-request, wait for the http-response and then the process terminates. You can probably see this by inspecting the Pod, that the curl container has terminated.

I am trying to figure out how communication is handled in a multicontainer pod. Each pod has their own unique ip address and containers in the pod can use localhost. I get it but how a container in a different pod can target a specific container in a multicontainer pod?

I suggest that you add two webservers (eg two nginx containers) to a pod. But they have to listen to different ports, eg port 8080 and port 8081. A client can choose what container it want to interact with by using the Pod IP and the container Port, <Pod IP>:<containerPort> . Eg add two nginx-containers, configure them to listen to different ports and let them serve different content.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM