Steps to access the POD from outside the cluster using Nodeport

In this blog, we will show you the steps to access the POD from outside the cluster.

Create a Kubernetes pod.

E.X: We create an Nginx pod and try to access it outside the world.
1. create a YAML file using vim <filename.yaml>
2. nginx sample yaml file.
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
name: nginx
spec:
containers:
— image: nginx
name: nginx
Nginxpod.yaml
*We tied pods with services using Labels.
3. create a pod using below comment
kubectl create -f <filename.yaml>
4. To check pod is created or not.
kubectl get pod

Create a service YAML file for Nginx using Nodeport

  1. create a service.yaml file.
vim service.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
name: nginx
spec:
type: NodePort
ports:
— port: 80
nodePort: 30080
name: http
— port: 443
nodePort: 30443
name: https
selector:
name: nginx
service.yaml
The service.yaml file we used Nodeport as a 30080.
*Nodeport range: 30,000 TO 32767

Selector :

The selector work here is to choose a specific pod among many pods, so here we give the Nginx pod labels name.
so selector only chooses Nginx pod.
2. Create a service file.
kubectl create -f <filename.yaml>
3. To check service is created or not use below cmd.
kubectl get svc
cmd
4. To cross-check the Nodeport you must describe the svc using below cmd.
kubectl describe svc <servicename>
SVC describe output

VERIFICATION:

Open the web browser from the local machine and access type the Kubernetes node IP along with the Node port.
Nginxwebpage.
We are able to access the Nginx homepage successfully. It’s a containerized web server listening in the port 80 and we mapped it to 30080 in every node in the cluster.
WRITTEN BY

Harshal Kathar

DevOps Engineer

Comments