NodePort Service Example
Download the YAML configuration files for the NodePort service example
Deploy the App
Applies the configuration from the YAML file to create the Nginx deployment.
Deploy the NodePort Service
Creates a NodePort service to expose the deployment externally.
Get the Pods List
Lists all pods with additional details including IP addresses.
Get Node Information
Displays node information including IP addresses for accessing the service.
Cleanup - Delete Service
Deletes the NodePort service.
Cleanup - Delete Deployment
Deletes the Nginx deployment and its pods.
YAML Configuration Files
deploy-app.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deploy-nginx
labels:
app: nginx
env: prod
spec:
replicas: 2
revisionHistoryLimit: 3
selector:
matchLabels:
app: nginx
env: prod
template:
metadata:
labels:
app: nginx
env: prod
spec:
containers:
- name: nginx
image: nginx:alpine
ports:
- containerPort: 80
Deployment Configuration Explanation:
Deployment Structure:
- apiVersion: apps/v1 → Specifies the Kubernetes API version for Deployments
- kind: Deployment → Defines this as a Deployment resource
- metadata.name: deploy-nginx → Names the deployment "deploy-nginx"
- metadata.labels → Adds labels for resource identification
Deployment Spec:
- replicas: 2 → Creates 2 identical pod instances
- revisionHistoryLimit: 3 → Keeps 3 old ReplicaSets for rollback
- selector.matchLabels → Defines how the Deployment finds which Pods to manage
Pod Template:
- template.metadata.labels → Labels applied to created Pods
- containers → Defines the container specification
- image: nginx:alpine → Uses the lightweight Alpine-based Nginx image
- containerPort: 80 → Exposes port 80 for web traffic
nodeport.yaml
apiVersion: v1
kind: Service
metadata:
name: svc-example
spec:
type: NodePort
selector:
app: nginx
env: prod
ports:
- nodePort: 32410
protocol: TCP
port: 80
targetPort: 80
NodePort Service Configuration Explanation:
Service Structure:
- apiVersion: v1 → Specifies the Kubernetes API version for Services
- kind: Service → Defines this as a Service resource
- metadata.name: svc-example → Names the service "svc-example"
Service Spec:
- type: NodePort → Exposes the service on each Node's IP at a static port
- selector → Identifies which Pods the service should target
- app: nginx, env: prod → Matches Pods with these labels
Port Configuration:
- nodePort: 32410 → The port exposed on each node (30000-32767 range)
- protocol: TCP → The network protocol (TCP/UDP)
- port: 80 → The port the service exposes internally
- targetPort: 80 → The port on the Pods that the service forwards to
How It Works:
The NodePort service exposes your application externally by opening a specific port (32410) on each node in your cluster. Traffic arriving at any node's IP address on this port is forwarded to the service, which then routes it to one of the Pods matching the selector labels.
With Docker Desktop, you can access the service using
localhost:32410. In a cloud environment, you would use any node's IP
address with port 32410.