0
0
Kubernetesdevops~5 mins

NodePort service type in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to make your app inside Kubernetes accessible from outside the cluster. NodePort service type opens a specific port on every node to forward traffic to your app.
When you want to expose a simple app running in Kubernetes to your local network for testing.
When you need to access your app from outside the cluster without setting up a cloud load balancer.
When you want to quickly share your app with teammates on the same network.
When you run Kubernetes on your laptop or bare metal and want external access.
When you want to debug or demo your app without complex networking.
Config File - nodeport-service.yaml
nodeport-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: example-nodeport-service
spec:
  type: NodePort
  selector:
    app: example-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
      nodePort: 30080

This file creates a Service named example-nodeport-service.

The type: NodePort tells Kubernetes to open port 30080 on every node.

The selector matches pods with label app: example-app.

The service listens on port 80 and forwards traffic to pod port 8080.

The nodePort: 30080 is the external port on each node.

Commands
This command creates the NodePort service in Kubernetes using the configuration file.
Terminal
kubectl apply -f nodeport-service.yaml
Expected OutputExpected
service/example-nodeport-service created
This command checks that the service is created and shows its details including the NodePort.
Terminal
kubectl get services example-nodeport-service
Expected OutputExpected
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE example-nodeport-service NodePort 10.96.123.45 <none> 80:30080/TCP 10s
This command lists the pods that the service will send traffic to, ensuring they are running.
Terminal
kubectl get pods -l app=example-app
Expected OutputExpected
NAME READY STATUS RESTARTS AGE example-app-abc123def-xyz789 1/1 Running 0 5m
Key Concept

If you remember nothing else from this pattern, remember: NodePort opens a fixed port on every node to let external traffic reach your app inside Kubernetes.

Common Mistakes
Not specifying the nodePort field and expecting a specific port.
Kubernetes will assign a random port in the default range, which may be hard to find or blocked by firewalls.
Always specify nodePort explicitly if you need a known port number.
Using NodePort without matching pod labels correctly.
The service won't forward traffic to any pods if the selector labels don't match, causing connection failures.
Ensure the service selector matches the labels on your pods exactly.
Trying to access the NodePort on the cluster IP instead of the node IP.
NodePort listens on the node's IP address, not the cluster IP, so connections to cluster IP won't work externally.
Use the IP address of any cluster node with the NodePort to access the service.
Summary
Create a NodePort service YAML to expose your app on a fixed port on all nodes.
Apply the YAML with kubectl apply to create the service.
Verify the service and pods with kubectl get commands to ensure proper setup.