Connect to other pod from a pod

user9874638 picture user9874638 · Sep 11, 2018 · Viewed 11.8k times · Source

Basically, i have a Deployment that creates 3 containers which scale automatically: PHP-FPM, NGINX and the container that contains the application, all set up with secrets, services and ingress. The application also share the project between PHP-FPM and NGINX, so it's all set up.

Since i want to explore more with K8s, i decided to create a pod with Redis that also mounts a persistent disk (but that's not important). I have also created a service for redis and all works perfectly fine if i SSH into the Redis container and run redis-cli.

The fun part is that the project can't connect to the pod on which Redis is on. I understand that the containers between pods share the same "local" network and they can be accessed using localhost.

How do i connect my project to the redis server that is running in other pod, that scales independently? What's wrong with the Redis service?


My Redis service is this:

apiVersion: v1
kind: Service
metadata:
  name: redis-service
spec:
  ports:
  - port: 6379
    targetPort: 6379
  selector:
    app: redis

My Redis pod is powered by a deployment configuration file (i don't necessarily scale it, but i'll look forward into it):

apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis
  labels:
    app: redis
spec:
  selector:
    matchLabels:
      app: redis
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: redis
    spec:
      volumes:
        - name: redis-persistent-volume
          persistentVolumeClaim:
            claimName: redis-pvc
      containers:
        - image: redis:4.0.11
          command: ['redis-server']
          name: redis
          imagePullPolicy: Always
          resources:
            limits:
              cpu: 250m
              memory: 512Mi
            requests:
              cpu: 250m
              memory: 512Mi
          ports:
            - containerPort: 6379
              name: redis
          volumeMounts:
            - name: redis-persistent-volume
              mountPath: /data

Also, when i tap into the kubectl get service, the Redis server has a Cluster IP:

NAME            TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes      ClusterIP   10.96.0.1       <none>        443/TCP        21h
nginx-service   NodePort    10.100.111.16   <none>        80:30312/TCP   21h
redis-service   ClusterIP   10.99.80.141    <none>        6379/TCP       6s

Answer

Const picture Const · Sep 11, 2018

How do I connect my project to the redis server that is running in other pod, that scales independently?

You have three possible states here:

  • To connect to Redis pod from within any other pod running in the same namespace as Redis pod is running. In this case you will use service name redis-service and designates service port 6379 to reach it over it's current ClusterIP (kube-dns is making DNS resolution for you there). I'm guessing that you are asking for this scenario.

  • Here is just an example of accessing one pod from within another pod (in your case). First run:

         kubectl run -it --rm test --image=busybox --restart=Never -- sh
    

    this will run new test pod and give you sh within that pod. Now if you type nslookup redis-service there (within test pod) you will check that DNS is working correctly between pods. You can also try to see if redis port is actually open with nc -zv redis-service 6379. If your kube-dns is working properly you should see that the port is opened.

  • To connect to Redis pod from within any other pod running in the same kubernetes cluster but in different namespace. In this case, you will use FQDN consisting of the service name and namespace name like it is given in the documentation.

  • To connect to Redis pod from outside of the kubernetes cluster. In this case, you will need some kind of ingress or nodePort of similar mechanism to expose redis service to outside world. More on this you can read in the official documentation.