How to communicate between containers in same POD in Kubernetes?

4/12/2021

For one POD, three images has been created. The problem here is that there is no communication between containers within same pod. How should my application connected with these three containers?

My pod have below containers.

[dev-application dev-app-nginx dev-app-redis]

Here I am able see only rails is running but redis and nginx is not running. Because Redis and nix is running as different containers in same pod.

kubectl exec -ti test-deployment-5f59864c8b-mv4kk sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
Defaulting container name to dev-application.
Use 'kubectl describe pod/test-deployment-5f59864c8b-mv4kk -n dev-app' to see all of the containers in this pod.
# rails -v
Rails 4.2.11.3
# redis -v
sh: 2: redis: not found
# nginx -v
sh: 3: nginx: not found
# 

Below the yam file I am using

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  namespace: dev-app
  name: test-deployment
spec:
  replicas: 1 
  template:
    metadata:
      labels:
        app: Dev-app
    spec:
      nodeSelector:
       cloud.io/sec-zone-green: "true"
      containers:
        - name: dev-application
          image: hub.docker.net/appautomation/dev.app.1.0:latest
            command: ["/bin/sh"]
            args: ["-c", "while true; do echo test; sleep 20;done"]
          resources:
            limits:
              memory: 8Gi
              cpu: 5
            requests:
              memory: 8Gi
              cpu: 5
          ports:
            - containerPort: 3000
        - name: dev-app-nginx
          image: hub.docker.net/appautomation/dev.nginx.1.0:latest
          resources:
            limits:
              memory: 4Gi
              cpu: 4
            requests:
              memory: 4Gi
              cpu: 4
          ports:
            - containerPort: 80
                       
        - name: dev-app-redis
          image: hub.docker.net/appautomation/dev.redis.1.0:latest
          
          resources:
            limits:
              memory: 4Gi
              cpu: 4
            requests:
              memory: 4Gi
              cpu: 4
          ports:
            - containerPort: 6379
    
        
-- User1984
kubernetes
kubernetes-pod

2 Answers

4/12/2021

Use localhost to communicate with other containers within the same pod.

E.g. the addresses to the containers are

  • 127.0.0.1:3000
  • 127.0.0.1:80
  • 127.0.0.1:6379
-- Jonas
Source: StackOverflow

4/13/2021

Jonas is right but I would like to expand this topic a little bit.

Let's discuss two methods that containers can use in order to communicate with each other in Kubernetes:

  1. Inter-Process Communications (IPC):

Containers in a Pod share the same IPC namespace, which means they can also communicate with each other using standard inter-process communications such as SystemV semaphores or POSIX shared memory. Containers use the strategy of the localhost hostname for communication within a Pod.

Containers in a Pod are accessible via “localhost”; they use the same network namespace. Also, for containers, the observable host name is a Pod’s name. Because containers share the same IP address and port space, you should use different ports in containers for incoming connections. In other words, applications in a Pod must coordinate their usage of ports. So, each container can access the other containers in the pod as different ports on localhost.

  1. Shared Volumes in a Kubernetes Pod:

In Kubernetes, you can use a shared Kubernetes Volume as a simple and efficient way to share data between containers in a Pod. For most cases, it is sufficient to use a directory on the host that is shared with all containers within a Pod.

If you want to explore the second method more than I suggest going through the official guide: Communicate Between Containers in the Same Pod Using a Shared Volume:

This page shows how to use a Volume to communicate between two Containers running in the same Pod.

Also, below you will find the full source article with more details and examples:

-- Wytrzymały Wiktor
Source: StackOverflow