Skip to main content

Lab 4.1 - ClusterIP and NodePort Services

Objectives

By the end of this lab, you will be able to:

  • Create ClusterIP Services
  • Create NodePort Services
  • Verify Endpoints
  • Test connectivity

Exercise 1: ClusterIP Service

Step 1: Create a Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
name: web-deployment
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: nginx
image: nginx:1.20

Step 2: Create a ClusterIP Service

apiVersion: v1
kind: Service
metadata:
name: web-service
spec:
type: ClusterIP
selector:
app: web
ports:
- port: 80
targetPort: 80

Step 3: Test

# View the Service
kubectl get service web-service

# View the Endpoints
kubectl get endpoints web-service

# Test from a Pod
kubectl run test-pod --image=busybox --rm -it -- sh
# Inside the Pod: wget -O- http://web-service

Exercise 2: NodePort Service

Step 1: Create a NodePort Service

apiVersion: v1
kind: Service
metadata:
name: web-nodeport
spec:
type: NodePort
selector:
app: web
ports:
- port: 80
targetPort: 80
nodePort: 30080

Step 2: Test

# View the Service
kubectl get service web-nodeport

# Access via NodePort
# curl http://<node-ip>:30080

Cleanup

kubectl delete deployment web-deployment
kubectl delete service web-service web-nodeport

Next Steps

Lab 4.2: Service Discovery with DNS


Lab created: December 2024