Using Service's ClusterIP Address inside POD

9/10/2018

I am using Kubernetes Service of type Cluster IP, which will expose a deployment. In my container I want to use the Service IP (cluster IP). Is there any way I can get the IP Address inside the Pod/container?

Is it possible to get the cluster IP from Service name?

-- Karthik
activemq
kube-proxy
kubernetes
kubernetes-service

2 Answers

9/10/2018

Yes, via the environment-level service discovery mechanism. Note, however that any service that you want to access like this must be created before the pod itself has been launched, otherwise the environment variables are not populated.

-- Michael Hausenblas
Source: StackOverflow

9/10/2018

Within the containers/POD, you can use a dns name which refers to the service. Basically, the file /etc/hosts is populated with the kube-dns edress. You can just use it as like:

<svc-name>.<namespace>.svc.cluster.local

If you must need the IP, you can use ENV Variablse or just ping the DNS name.

-- Giorgio Cerruti
Source: StackOverflow