18

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
    
        
Jonas
  • 121,568
  • 97
  • 310
  • 388
User1984
  • 587
  • 2
  • 5
  • 14
  • 2
    Any reason to not split this into three `Deployments` and three `Services`? The problem in this design is that you can't guarantee high availability from network perspective. So in this case when you want to expose multiple ports, it's better to have them separated. If they are split, you will be able to reach each other via `Service` hostnames. – laimison Apr 12 '21 at 16:34
  • ok thanks.Is it possible to create single container with multiple image? – User1984 Apr 12 '21 at 17:00
  • As a `image: ` value you can specify only one image. It's possible to combine multiple images into one by merging manually `Dockerfile`s, but to be honest I don't see the purpose of this work practically. Quite likely, you will end up not using this solution because `livenessProbe` is applied per pod and it usually has a single port to check, port exposing is focused to a single port and `command` is usually just one command. As soon as you will separate them to separate `kind: Deployment` it will add sense. To simplify this: one port = one `Deployment` (in majority of cases). – laimison Apr 12 '21 at 17:10

2 Answers2

27

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
  • 121,568
  • 97
  • 310
  • 388
  • yes I have used local host(127.0.0.1) only .But I am getting/usr/local/bundle/gems/redis-3.3.5/lib/redis/client.rb:345:in `rescue in establish_connection': Error connecting to Redis on redis:6379 (Errno::EHOSTUNREACH) (Redis::CannotConnectError) – User1984 Apr 13 '21 at 03:23
  • you must use 127.0.0.1:6379 and not redis:6379 – Jonas Apr 13 '21 at 05:58
  • thanks.Now it is connecting.How to check db is connected inside pod? – User1984 Apr 13 '21 at 12:57
  • If I want to expose rails redis and nginx. Can I create loadbalacer or nodeport to call these containers? – User1984 Apr 13 '21 at 13:03
  • PG::ConnectionBad (could not translate host name "db" to address: Name or service not known in kubernates – User1984 Apr 13 '21 at 13:20
  • https://stackoverflow.com/questions/67076264/configure-postgresql-in-kubernetes – User1984 Apr 13 '21 at 13:54
  • I have created one more post.please check – User1984 Apr 13 '21 at 13:55
  • one more query.I have created separate pod for redis and nginx.Now I am getting error Error connecting to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED) (Redis::CannotConnectError) – User1984 Apr 13 '21 at 15:53
  • I get "Could not connect. Will try again. getaddrinfo ENOTFOUND host.docker.internal" when using localhost or 127.0.0.1 – majorgear Mar 03 '23 at 04:48
6

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
  • 11,492
  • 5
  • 29
  • 37