Kube expose, requests to any node ip seem to hit the right pod

12/5/2016

I've been messing around with Kubernetes and have a small lab cluster of centos hosts (3 nodes, 1 master) running.

$ kubectl get nodes
NAME                           STATUS    AGE
centos-kube-minion-1   Ready     2d
centos-kube-minion-2   Ready     2d
centos-kube-minion-3   Ready     2d

I've manged to follow the interactive tutorial in my lab env (http://kubernetes.io/docs/tutorials/kubernetes-basics/expose-interactive/).

I've arrived at the part where you expose the demo app externally (Module 4 Step 1 of 3):

$ kubectl expose deployment/kubernetes-bootcamp --type="NodePort" --port 8080

Kubectl describe outputs as follows:

$ kubectl describe services/kubernetes-bootcamp
Name:                   kubernetes-bootcamp
Namespace:              default
Labels:                 run=kubernetes-bootcamp
Selector:               run=kubernetes-bootcamp
Type:                   NodePort
IP:                     10.254.224.254
Port:                   <unset> 8080/TCP
NodePort:               <unset> 31686/TCP
Endpoints:              172.30.3.2:8080
Session Affinity:       None
No events.

The next step in the tutorial calls for a curl on the node dns name(or IP) where the pod is running, and the exposed port (31686 in my case).

My /etc/hosts looks like this:

$ cat etc/hosts
...
10.10.0.231 centos-kube-master
10.10.0.236 centos-kube-minion-1
10.10.0.232 centos-kube-minion-2
10.10.0.237 centos-kube-minion-3

And the actual pod is running on minion-3:

   $ kubectl describe pod kubernetes-bootcamp-428840972-ukl15
    Name:           kubernetes-bootcamp-428840972-ukl15
    Namespace:      default
    Node:           centos-kube-minion-3/10.10.0.237

Yet, (and here's the actual question), when I curl any of those DNS entries and the exposed port, I hit the app:

$ curl centos-kube-minion-1:31686
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-428840972-ukl15 | v=1

$ curl centos-kube-minion-2:31686
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-428840972-ukl15 | v=1

$ curl centos-kube-minion-3:31686
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-428840972-ukl15 | v=1

I also curled against the actual IP to make sure nothing crazy was happening with the DNS entries.

$ curl 10.10.0.236:31686
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-428840972-ukl15 | v=1
$ curl 10.10.0.237:31686
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-428840972-ukl15 | v=1
$ curl 10.10.0.232:31686
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-428840972-ukl15 | v=1

So my question is: is this working as intended (I suspect yes)? (If so, can anyone explain/link to an explanation of the magic?)

-- mnort
kubernetes

1 Answer

12/5/2016

Yes it is working as intended: nodePort exposes the Service on all nodes on the same port.

Basically a Service is a proxy to the Pod running the Application. KubeProxy runs on every node, so when you specify nodePort, every node gets notified to redirect traffic coming from the port defined to the Service, which knows where the Pod is located through the API.

http://kubernetes.io/docs/user-guide/services/#type-nodeport

-- MrE
Source: StackOverflow