Connect to other pod from a pod

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.


Indeed, using the service name as hostname worked. I didn't know that Kubernetes's DNS also manages to do this. I have used it before in Docker Composer, and in my containerized application i have used it to connect to the services (ie: MySQL, Redis, Elasticsearch).

To clarify this, in my app, i set the hostname to the service name, like redis-service. If i'd like to connect to the Postgres, i can do using pgsql-service or whatever the name of that service is.

THANK YOU!