Scenario #
An existing application running on Kubernetes needs updates:
- Deployment:
nginx-deployment - Service:
nginx-service
Requirements:
- Change service NodePort from
30008to32165 - Increase replicas from
1to5 - Update image from
nginx:1.19tonginx:latest
Constraint: Do not delete the deployment or service
Initial State #
kubectl get deploymentNAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 1/1 1 1 20skubectl get serviceNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 25m
nginx-service NodePort 10.96.38.135 <none> 80:30008/TCP 28sSolution #
Step 1: Update Service NodePort #
Edit the service:
kubectl edit service nginx-serviceIn vi, find the nodePort field under ports:
/nodePortChange from:
spec:
ports:
- nodePort: 30008
port: 80
protocol: TCP
targetPort: 80To:
spec:
ports:
- nodePort: 32165
port: 80
protocol: TCP
targetPort: 80Save and exit: :wq
Step 2: Update Deployment (Replicas & Image) #
Edit the deployment:
kubectl edit deployment nginx-deploymentChange replicas - search for replicas:
/replicasUpdate from:
spec:
replicas: 1To:
spec:
replicas: 5Change image - search for nginx:1.19:
/nginx:1.19Update from:
spec:
containers:
- image: nginx:1.19To:
spec:
containers:
- image: nginx:latestSave and exit: :wq
Alternative: Imperative Commands #
Update replicas: #
kubectl scale deployment nginx-deployment --replicas=5Update image: #
kubectl set image deployment/nginx-deployment nginx-container=nginx:latestUpdate service (requires patch): #
kubectl patch service nginx-service --type='json' -p='[{"op": "replace", "path": "/spec/ports/0/nodePort", "value":32165}]'Verification #
# Check deployment rollout status
kubectl rollout status deployment/nginx-deployment
# Verify replicas are scaled
kubectl get deployment nginx-deployment
kubectl get pods
# Verify service NodePort changed
kubectl get service nginx-service
# Check pod details for image version
kubectl describe deployment nginx-deployment | grep ImageExpected output:
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 5/5 5 5 5m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-service NodePort 10.96.38.135 <none> 80:32165/TCP 5m