Search code examples
kuberneteskubernetes-pod

How to communicate between containers in same POD in Kubernetes?


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
    
        

Solution

  • 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