GKE - Google Kubernetes Engine

Plan Deploy And Manage Workloads On GKE

Demo Managing a rolling update on GKE cluster

In this tutorial, you’ll learn how to perform and manage rolling updates on a Google Kubernetes Engine (GKE) cluster. These concepts apply equally to upstream Kubernetes, enabling you to update container images with zero downtime and perform safe rollbacks when needed.

Prerequisites


1. Configure and Create the Cluster

First, set your Compute Engine zone and create a single-node GKE cluster named gke-deep-dive.

# Set your desired zone
gcloud config set compute/zone us-west1-a

# Create a cluster with one node
gcloud container clusters create gke-deep-dive \
  --num-nodes=1 \
  --machine-type=e2-medium \
  --disk-type=pd-standard \
  --disk-size=10

Note

Cluster provisioning can take a few minutes. You can monitor progress in the Google Cloud Console under Kubernetes Engine > Clusters.


2. Deploy a Simple Nginx Application

Create a Deployment manifest gke-deep-dive-app.yaml with two replicas of Nginx:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80

Apply the manifest and verify the pods:

kubectl apply -f gke-deep-dive-app.yaml
kubectl get deployments
kubectl get pods
RESOURCE TYPECOMMANDDESCRIPTION
Deploymentkubectl apply -f gke-deep-dive-app.yamlCreate or update the deployment
Podskubectl get podsList running pods and their status

3. Perform Rolling Updates

GKE supports in-place rolling updates, ensuring that at least one replica remains available while others are replaced.

ActionCommandDescription
Update imagekubectl set image deployment/nginx-deployment nginx=nginx:1.9.1Change the container image to 1.9.1
Monitor rolloutkubectl rollout status deployment/nginx-deploymentWait until the rollout completes

3.1 Update to Nginx 1.9.1

kubectl set image deployment/nginx-deployment nginx=nginx:1.9.1
kubectl rollout status deployment/nginx-deployment

Verify the new pods are running:

kubectl get pods

3.2 Update to Nginx 1.21.0

Perform another image update to observe an in-progress rollout:

kubectl set image deployment/nginx-deployment nginx=nginx:1.21.0
kubectl rollout status deployment/nginx-deployment

4. Rollback a Deployment

If a rollout introduces an issue, you can revert to a previous revision.

4.1 Rollback to the Previous Revision

kubectl rollout undo deployment/nginx-deployment
kubectl describe deployment/nginx-deployment | grep Image

4.2 Rollback to a Specific Revision

  1. View rollout history:

    kubectl rollout history deployment/nginx-deployment
    
  2. Roll back to revision 1 (original version):

    kubectl rollout undo deployment/nginx-deployment --to-revision=1
    kubectl describe deployment/nginx-deployment | grep Image
    

Warning

Rolling back to a previous revision may disrupt your application. Always test in a staging environment before rolling back in production.


Watch Video

Watch video content

Previous
Performing rolling updates on a GKE Cluster