How to Create A NodePort Service & Deploy NGINX on It — Kubernetes Assignment 2

Visal Tyagi
DevOps-Guides
Published in
3 min readJul 11, 2024

You have been asked to:

● Create a service of type NodePort for nginx deployment

● Check the nodeport service on a browser to verify

Note: We will use a New Kubernetes Machine here, because t2.medium charges in Free Tier also. We create separate machine in the every assignment.

For Kubernetes Installation, Read this guide:

It will be more helpful to you. First, you have to install “Kubernetes”.

How to Create A NodePort Service & Deploy NGINX on It — Kubernetes Assignment 2
How to Create A NodePort Service & Deploy NGINX on It — Kubernetes Assignment 2

Check the Git Hub Repository for this Assignment to Copy the Commands:

Problem (1) Solution: Create a service of type NodePort for nginx deployment

Step 1: First, we will create service to access the application externally.

Use the below-given command to create a service.yaml file:

nano service.yaml
Create a service.yaml file
Create a service.yaml file

It will create a service.yaml file.

Note: We are not using the sudo command because we are not working as a root user, only work as a normal user. Take care if you are working as a root user then use sudo otherwise start command without sudo. Otherwise, in the deployment creation, you will face the issue.

Step 2: Paste this content into “service.yaml” file.

apiVersion: v1
kind: Service
metadata:
name: nginx-deployment
spec:
type: NodePort
ports:
- targetPort: 80
port: 80
nodePort: 30008
selector:
app: nginx
service.yaml file code
service.yaml file code

Do “CTRL+X” to Exit & Type “Yes” to save the file. Press “Enter” from the keyboard & you will get out of the service.yaml file.

Step 3: Run the below-given command to create a service on node port 30008.

kubectl apply –f service.yaml
Service Created
Service Created

The service will be successfully created.

Step 4: Check the service is successfully created or not. Use the below-given command:

kubectl get svc
NodePort Created
NodePort Created

Problem (2) Solution: Check the nodeport service on a browser to verify

Step 1: Now, copy the slave machine IP Address & put it into browser address bar. Put 30008 behind the IP Address & press enter from the keyboard.

Copy the Slave1 IP Address
Copy the Slave1 IP Address

Step 2: Your “nginx” app will be successfully accessed on Node port “30008”.

NGINX
NGINX

Next Kubernetes Assignments:

How to Change the Replicas for the Deployment — Kubernetes Assignment 3

How to Change the NodePort Service to Cluster IP — Kubernetes Assignment 4

How to Deploy Ingress Service Using MiniKube in Kubernetes — Kubernetes Assignment 5

How to Deploy a Sample Website on Kubernetes Using Ingress — Kubernetes Case Study

--

--