FAUN — Developer Community 🐾

We help developers learn and grow by keeping them up with what matters. 👉 www.faun.dev

Follow publication

Accessing Pods Outside Of The Cluster In Kubernetes

--

Vingardium Le Google!

In this tutorial, we will talk a little bit about pods and a little bit about accessing them using the magical NodePort.

Note: Before you proceed, make sure you know the basic architecture of Kubernetes and the corresponding commands and the structure of yaml files in Kubernetes.

I am assuming most of us know what pods are but in case you don’t, it is the smallest unit in Kubernetes and is essentially what holds the docker containers running applications. Consider it as a little box.

Now, how do you access something inside of a port? Can you simply use the IP of the pod and access it? Well no you can’t. Kubernetes is designed in such a way that pods are only accessible inside the cluster through their IPs. It is a closed ecosystem in that aspect and If you want to access something inside the cluster from outside then you have to use services.

Kubernetes offers a number of services but what we are interested in at the moment is NodePort. It is basically a service that connects pods to the Node IP.

It is basically an outlet for the pods, deployments, etc because it maps the IP of the pod, deployment, etc respectively to a port on the Node.

Let’s say your pod has an IP 127.10.1.111 and your node has an IP 198.16.0.101 then you can access your pod using the node IP followed by the nodeport

We will follow this through a live example, we will start by creating 2 files.

pod.yaml

As you can see we are spinning up an Nginx server which by default runs on port 80 , note that we are also providing resources and labels to the pod so that the service can identify the pods it's supposed to map itself to. Therefore labels become an important factor here for this to work.

You can spin this up by using kubectl apply -f pod.yaml and you can see if its status by kubectl get pods

service.yaml

As you can see, service has been created here and we will the selector tag to target the pod we can to map along with ports and target ports we want to use which in our case is 80 , now you can also see a nodePort with the value of 30007. This serves as the port at the node that will be used to access the Nginx service.

A very common mistake that people and even I did is to try and access Nginx using http://10.102.7.141:80 which is the cluster IP. Yes, you can access the pod with this too but only when you are inside the cluster, not when you are outside it and trying to access it from your browser or any external means.
If you want to access it in your browser, run kubectl get nodes -o wide to know your nodes IP which in my case happens to be 192.168.49.2

Finally, you can access the Nginx server by http://192.168.49.2:30007 where 30007 is the node port and that’s it you are done! You hit enter and you will see the Nginx page.

You can use the exact same method to expose deployments, replicasets, etc with minor changes in the service configuration.

Feel free to post your doubts in the comment section along with your suggestions!

Happy Learning! : )

Join FAUN: Website 💻|Podcast 🎙️|Twitter 🐦|Facebook 👥|Instagram 📷|Facebook Group 🗣️|Linkedin Group 💬| Slack 📱|Cloud Native News 📰|More.

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author 👇

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Published in FAUN — Developer Community 🐾

We help developers learn and grow by keeping them up with what matters. 👉 www.faun.dev

Written by Vic

A software engineer, orator, video editor, chef (At least for myself), singer (Not exactly good), artist (Decent one) and a writer (Okayish one).

No responses yet

Write a response