📖 Scenario: You have a simple web application running inside a Kubernetes cluster. You want to expose this application so that it can be accessed from outside the cluster on a specific port.
🎯 Goal: Learn how to create a Kubernetes NodePort service to expose a pod on a fixed port accessible from outside the cluster.
📋 What You'll Learn
Create a pod running the nginx image
Create a NodePort service exposing the pod on port 80 internally
Set the NodePort to 30080
Verify the service is created and accessible
💡 Why This Matters
🌍 Real World
NodePort services are used to expose applications running inside Kubernetes clusters to external clients by opening a port on each node.
💼 Career
Understanding how to expose services with NodePort is essential for DevOps engineers managing Kubernetes clusters and deploying applications accessible outside the cluster.
Progress0 / 4 steps
1
Create a pod running nginx
Create a YAML file named nginx-pod.yaml that defines a pod called nginx-pod running the nginx image with container port 80.
Kubernetes
Hint
Use kind: Pod and specify containers with image: nginx and containerPort: 80. Add label app: nginx under metadata.
2
Create a NodePort service configuration
Create a YAML file named nginx-service.yaml that defines a service called nginx-service of type NodePort. It should select the pod with label app: nginx, expose port 80 internally, and use NodePort 30080.
Kubernetes
Hint
Use kind: Service with type: NodePort. Set selector: app: nginx and ports with port: 80 and nodePort: 30080.
3
Apply the pod and service to the cluster
Use kubectl apply -f nginx-pod.yaml and kubectl apply -f nginx-service.yaml commands to create the pod and NodePort service in the cluster.
Kubernetes
Hint
Use kubectl apply -f with the YAML filenames to create resources.
4
Check the NodePort service status
Run kubectl get service nginx-service and print the output to verify the service is created with the correct NodePort 30080.
Kubernetes
Hint
Use kubectl get service nginx-service to see the service details including the NodePort.
Practice
(1/5)
1. What does a Kubernetes NodePort service do?
easy
A. Exposes the service on a static port on each node's IP address
B. Creates an internal cluster IP only accessible inside the cluster
C. Routes traffic through an external load balancer automatically
D. Runs the service only on a single node in the cluster
Solution
Step 1: Understand NodePort service purpose
A NodePort service exposes the application on a fixed port on every node's IP, allowing external access.
Step 2: Compare with other service types
ClusterIP is internal only, LoadBalancer uses external balancers, and NodePort is not limited to a single node.
Final Answer:
Exposes the service on a static port on each node's IP address -> Option A
Quick Check:
NodePort = fixed port on all nodes [OK]
Hint: NodePort always opens a fixed port on every node IP [OK]