How to get public IP from container in kubernetes

8/31/2017

I have multiple instance of containers deployed in kubernetes.

The application in the container requires the public IP from the pod / node.

From the deployment manifest, I tried to use the following,

  • status.podIP
  • spec.nodeName

But both of them return the private IP address of my node rather then the public IP.

Can someone help explain where the status.podIP value comes from? Is it coming from the node manifest? Or from the VM / docker configuration? Any suggestion on how I can get the public IP address into my container?

Thanks

-- Abelard Chow
docker
kubernetes

2 Answers

8/31/2017

Kubernetes exposes an external/public IP address (and port) through "load balanced" services. In most cases, containers do not need to know this external IP address; they only need to listen on the configured port. The service will ensure that traffic sent to the external/public IP is routed to your (hopefully replicated w/ReplicaSet) container. Please check out the following Kubernetes Service Example.

-- Sean Sullivan
Source: StackOverflow

8/31/2017

Public IP may not be available through Kubernetes. But you can use external services to query the IP.

$ curl ipinfo.io/ip

Is one possible way. But they have rate limiting. You might find other options also. Next option is to set a Nginx server on a know machine with below config

location = /ip {
    add_header Last-Modified $date_gmt;
    add_header Cache-Control 'private no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
    if_modified_since off;
    expires off;
    etag off;

    default_type text/plain;
    return 200 "$remote_addr";
}

And then call this endpoint using the public IP of the nginx hosting this

$ curl <publicipofnginx>/ip

This will give the public of the machine making the curl call.

-- Tarun Lalwani
Source: StackOverflow