0
0
Kubernetesdevops~10 mins

NodePort service type in Kubernetes - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - NodePort service type
Create Service YAML
kubectl apply -f service.yaml
Kubernetes API Server creates NodePort Service
Assign port in range 30000-32767
Service listens on all nodes at NodePort
External client sends request to NodeIP:NodePort
Request forwarded to target Pods via ClusterIP
This flow shows how a NodePort service is created and how it exposes pods on a port accessible from outside the cluster.
Execution Sample
Kubernetes
apiVersion: v1
kind: Service
metadata:
  name: my-nodeport-service
spec:
  type: NodePort
  selector:
    app: myapp
  ports:
  - port: 80
    targetPort: 8080
    nodePort: 30007
Defines a NodePort service exposing pods with label app=myapp on port 80, forwarding to pod port 8080, accessible externally on port 30007.
Process Table
StepActionResultDetails
1Create YAML file with NodePort service specYAML file readyDefines service type NodePort with nodePort 30007
2Run kubectl apply -f service.yamlService createdKubernetes API server processes the service definition
3Kubernetes assigns NodePort 30007Port reservedPort 30007 is within allowed range 30000-32767
4Service listens on all cluster nodes at port 30007NodePort activeExternal traffic can reach any node on port 30007
5External client sends request to NodeIP:30007Request receivedNode forwards request to service
6Service forwards request to pod on port 8080Pod receives requestPod with label app=myapp handles traffic
7Response sent back to clientCommunication completeClient receives pod response
8EndProcess completeNodePort service is operational
💡 NodePort service exposes pods externally via fixed port 30007 on all nodes
Status Tracker
VariableStartAfter Step 3After Step 4Final
nodePortundefined30007 assigned30007 listening on nodes30007 active for external access
serviceStatusNot createdCreatedActiveActive
podTrafficNo trafficNo trafficNo trafficTraffic flowing
Key Moments - 3 Insights
Why must the nodePort be in the range 30000-32767?
Kubernetes reserves ports in this range for NodePort services to avoid conflicts with system ports. See execution_table step 3 where port 30007 is assigned within this range.
Does NodePort expose the pod port directly to the outside?
No, NodePort exposes a port on each node, which forwards traffic to the pod's targetPort inside the cluster. See execution_table steps 5 and 6 for request forwarding.
Can external clients access the pod using the pod IP directly?
No, external clients access the node IP and NodePort. Kubernetes routes the traffic to pods internally. See execution_table step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the NodePort assigned?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Check the 'Action' column for 'Kubernetes assigns NodePort' in execution_table.
According to variable_tracker, what is the state of 'podTraffic' after step 4?
ATraffic forwarded
BNo traffic
CTraffic flowing
DUndefined
💡 Hint
Look at the 'podTraffic' row under 'After Step 4' in variable_tracker.
If the nodePort was set outside the allowed range, what would happen in the execution flow?
ANodePort would be assigned anyway at step 3
BTraffic would be forwarded incorrectly at step 6
CService creation would fail at step 2
DNo effect, service works normally
💡 Hint
Refer to step 3 in execution_table where port assignment must be in range.
Concept Snapshot
NodePort Service exposes pods on a static port (30000-32767) on each node.
External clients access NodeIP:NodePort to reach pods.
Service forwards traffic from NodePort to pod's targetPort.
NodePort must be in allowed range or Kubernetes assigns one.
Useful for simple external access without load balancer.
Full Transcript
This visual execution shows how a Kubernetes NodePort service works. First, you create a YAML file defining the service with type NodePort and specify ports. Then, you apply this file with kubectl. Kubernetes assigns a port in the range 30000-32767 if not specified. The service listens on this port on all cluster nodes. External clients send requests to any node's IP and this port. The service forwards the requests to the pods on their target port. Responses go back to the client. Variables like nodePort and podTraffic change state as the service becomes active and traffic flows. Key points include the port range restriction and that NodePort exposes node ports, not pod IPs directly. The quizzes test understanding of port assignment, traffic state, and error cases. This helps beginners see step-by-step how NodePort services expose pods externally.