Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kubernetes: multiple pods in a node when each pod exposes a port

I was following along with the Hello, World example in Kubernetes getting started guide.

In that example, a cluster with 3 nodes/instances is created on Google Container Engine.

The container to be deployed is a basic nodejs http server, which listens on port 8080.

Now when I run
kubectl run hello-node --image <image-name> --port 8080
it creates a pod and a deployment, deploying the pod on one of nodes.

Running the
kubectl scale deployment hello-node --replicas=4
command increases the number of pods to 4.

But since each pod exposes the 8080 port, will it not create a port conflict on the pod where two nodes are deployed? I can see 4 pods when I do kubernetes get pods, however what the behaviour will be in this case?

like image 448
Jatin Avatar asked Jul 12 '16 09:07

Jatin


2 Answers

Got some help in #kubernetes-users channel on slack :

  1. The port specified in kubectl run ... is that of a pod. And each pod has its unique IP address. So, there are no port conflicts.
  2. The pods won’t serve traffic until and unless you expose them as a service.
  3. Exposing a service by running kubectl expose ... assigns a NodePort (which is in range 30000-32000) on every node. This port must be unique for every service.
  4. If a node has multiple pods kube-proxy balances the traffic between those pods.

Also, when I accessed my service from the browser, I was able to see logs in all the 4 pods, so the traffic was served from all the 4 pods.

like image 171
Jatin Avatar answered Sep 16 '22 15:09

Jatin


There is a difference between the port that your pod exposes and the physical ports on your node. Those need to be linked by for instance a kubernetes service or a loadBalancer as discussed a bit further in the hello-world documentation http://kubernetes.io/docs/hellonode/#allow-external-traffic

like image 35
Mark van Straten Avatar answered Sep 18 '22 15:09

Mark van Straten