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?
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.
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.