Skip to main content

Deployment and Service Update

·292 words·2 mins
Jack Warner
Author
Jack Warner
A little blog by me

Scenario
#

An existing application running on Kubernetes needs updates:

  • Deployment: nginx-deployment
  • Service: nginx-service

Requirements:

  1. Change service NodePort from 30008 to 32165
  2. Increase replicas from 1 to 5
  3. Update image from nginx:1.19 to nginx:latest

Constraint: Do not delete the deployment or service

Initial State
#

kubectl get deployment
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   1/1     1            1           20s
kubectl get service
NAME            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   28s

Solution
#

Step 1: Update Service NodePort
#

Edit the service:

kubectl edit service nginx-service

In vi, find the nodePort field under ports:

/nodePort

Change from:

spec:
  ports:
  - nodePort: 30008
    port: 80
    protocol: TCP
    targetPort: 80

To:

spec:
  ports:
  - nodePort: 32165
    port: 80
    protocol: TCP
    targetPort: 80

Save and exit: :wq

Step 2: Update Deployment (Replicas & Image)
#

Edit the deployment:

kubectl edit deployment nginx-deployment

Change replicas - search for replicas:

/replicas

Update from:

spec:
  replicas: 1

To:

spec:
  replicas: 5

Change image - search for nginx:1.19:

/nginx:1.19

Update from:

spec:
  containers:
  - image: nginx:1.19

To:

spec:
  containers:
  - image: nginx:latest

Save and exit: :wq

Alternative: Imperative Commands
#

Update replicas:
#

kubectl scale deployment nginx-deployment --replicas=5

Update image:
#

kubectl set image deployment/nginx-deployment nginx-container=nginx:latest

Update 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 Image

Expected 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

Related