Kubernetes expose a service does not assign the external ip

6/28/2018

I have an internal service that is created via the first cmd. Then i run kubectl expose on that service that is created.

kubectl -n XXX create -f service.yml
kubectl -n XXX get svc
NAME                                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                                                                            AGE
foo                           ClusterIP   10.152.183.41    <none>        8089/TCP


kubectl -n XXX expose service foo --type=NodePort --name=foo-ext
kubectl -n XXX get svc
NAME                                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                                                                            AGE
foo                           ClusterIP   10.152.183.41    <none>        8089/TCP                                                                           29m
foo-ext                   NodePort    10.152.183.177   <none>        8089:30406/TCP

Where the external ip is I would have figured kubectl expose would have assigned an external ip to the external service, is there an additional flag i need to pass?

-- BLang
kubectl
kubernetes

2 Answers

6/28/2018

LoadBalacer type service "assigns" an external IP, but it is cloud provider specific, so it won't work. Unless you have a load balancer pointing to the cluster (with an external IP, of course).

From the other services, your only option would be NodePort type service, which would map your pod port to a port on the node, so it will be accessible from outside world. The ugly part is that it is going to be on a port like 30021 (between 30000-32767).

-- suren
Source: StackOverflow

6/28/2018

According to the Kubernetes docs:

If you set the type field to NodePort, the Kubernetes master will allocate a port from a range specified by --service-node-port-range flag (default: 30000-32767), and each Node will proxy that port (the same port number on every Node) into your Service.

So, Kubernetes will ensure that the same port number is exposed on all nodes that are hosting this service. The external IP, in this case, is the IP address of the node.

Whereas Kubernetes says this about External IPs:

If there are external IPs that route to one or more cluster nodes, Kubernetes services can be exposed on those externalIPs. Traffic that ingresses into the cluster with the external IP (as destination IP), on the service port, will be routed to one of the service endpoints. externalIPs are not managed by Kubernetes and are the responsibility of the cluster administrator.

The downside to a NodePort type of service is that, by itself, it's not a scalable approach and lacks service discoverability.

If you want to expose a service through a single IP address to consumers outside of your Kubernetes cluster, you may want to look into LoadBalancer type of service if have a public cloud or hybrid cloud Kubernetes deployment, or you may want to look into Ingress if you have an on-premises Kubernetes deployment.

-- Peter Benjamin
Source: StackOverflow