Public IP Address of the service/load balancer in Kubernetes from Env

8/29/2017

I have a nodejs app which has refers to the IP Address of the kubernetes service/load balancer public IP. Is there a way to get it from the process/env variables in runtime?

e..g process.env.ENV_VARIABLE and what is the env variable name

Thanks

-- Rajesh Jain
google-kubernetes-engine
kubernetes

1 Answer

8/29/2017

It's not available as an environment variable. Also keep in mind that a pod can receive traffic from multiple load balancers and also the external IP could change after the Pod started, leaving a stale value; so an IP in an environment variable might not be an adequate solution.

If you are using an Ingress object you can determine it's external IP by querying the Kubernetes apiserver (try kubectl get myingress -o yaml, see the status: part).

For a particular incoming http request check the X-Forwarded-For: header for the load balancer's IP.

Since the question is tagged google-container-engine, you can also use gcloud to get that IP (perhaps from an initContainer or at startup):

  • If you are using Ingress: gcloud compute forwarding-rules describe k8s-fw-YOURNAMESPACE-YOURINGRESSNAME--YOURHASHVALUE --global --format='value(IPAddress)'
  • If you have a static IP reserved: gcloud compute addresses describe my-static-ip-name --global --format='value(address)'
-- Janos Lenart
Source: StackOverflow