Google Kubernetes Engine (GKE) is a powerful tool for running containerized applications. But what happens if your GKE cluster crashes? Or if your application data is corrupted? Backup for GKE is a new feature that can help you protect your GKE clusters and applications.
In part two of this series, we will demonstrate how to create a backup plan for your GKE stateful workloads.
From our partners:
Why are Backups for GKE plans useful?
Backup plans are a useful tool for managing & maintaining GKE environments. They can be used for disaster recovery, compliance and audit, CI/CD pipelines, cloning workloads, and upgrade scenarios.
- Disaster recovery: Backup plans can be used to restore GKE clusters in the event of a disaster. This can be done by restoring the cluster from a backup or by creating a new cluster from a backup.
- Compliance and audit: Backup plans can be used to meet compliance and audit requirements. This can be done by storing backups in a secure location or by providing access to backups to authorized users.
- CI/CD pipelines: Backup plans can be used in CI/CD pipelines to create and restore GKE clusters. This can be done by using the backup plan to create a new cluster or by using the backup plan to restore a cluster to a specific point in time.
- Cloning workloads: Backup plans can be used to clone workloads. This can be done by using the backup plan to create a new cluster or by using the backup plan to restore a cluster to a specific point in time.
- Upgrade scenarios: Backup plans can be used in upgrade scenarios. This can be done by using the backup plan to create a new cluster or by using the backup plan to restore a cluster to a specific point in time.
With the above in mind, let’s walk through how to set up a backup plan. The steps below take you through setting up a GKE Autopilot cluster, enabling backups, deploying an example StatefulSet and creating a backup plan.
Setting up Backup for GKE
Prerequisites – In my case, I made a VPC named gke-vpc, subnet gke-vpc and an automatic subnet
- Project Created – name gke-backup
- VPC(gke-vpc), subnet(gke-vpc) and firewall rules
gcloud container --project "gke-backup" clusters create-auto "gke-autopilot" --region "us-central1" --release-channel "regular" --network "projects/gke-backup/global/networks/gke-vpc" --subnetwork "projects/gke-backup/regions/us-central1/subnetworks/gke-vpc" --cluster-ipv4-cidr "/17" --services-ipv4-cidr "/22"
gcloud services enable gkebackup.googleapis.com
gcloud container clusters update gke-autopilot \ --project=gke-backup \ --region=us-central1 \ --update-addons=BackupRestore=ENABLED
Deploy the below statefulset yaml to your cluster.
apiVersion: v1 kind: Service metadata: name: nginx labels: app: nginx spec: ports: - port: 80 name: web clusterIP: None selector: app: nginx --- apiVersion: apps/v1 kind: StatefulSet metadata: name: web spec: serviceName: "nginx" replicas: 2 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: registry.k8s.io/nginx-slim:0.8 ports: - containerPort: 80 name: web volumeMounts: - name: www mountPath: /usr/share/nginx/html volumeClaimTemplates: - metadata: name: www spec: accessModes: [ "ReadWriteOnce" ] resources: requests: storage: 1Gi
If in terminal, run below commands:
- touch statefulset.yaml
- vi statefulset.yaml
- enter :wq
- kubectl create -f statefulset.yaml
gcloud beta container backup-restore backup-plans create stateful-plan \ --project=gke-backup \ --location=us-central1 \ --cluster=projects/gke-backup/locations/us- central1/clusters/gke-autopilot \ --all-namespaces \ --backup-retain-days=5 \ --cron-schedule="0 * * * *" \ --include-volume-data
gcloud beta container backup-restore backups create example-backup \ --project=gke-backup \ --location=us-central1 \ --backup-plan=stateful-plan \ --wait-for-completion
Setting up Backup for GKE
Restore plans can be used to restore a cluster to a specific point in time, or to a specific version of GKE. This can be helpful if you need to recover from a data loss or if you need to roll back to a previous version of GKE.
Restore plans can also be used to restore a cluster to a different environment such as a different region. This can be helpful if you need to move your cluster to a new environment or if you need to follow new regulations.
Overall, GKE restore plans are a useful tool for managing clusters. They can help you to automate the process of restoring a cluster, restoring a cluster to a specific point in time or version, or restoring a cluster to a different environment.
The below takes you through setting up creating a restore plan and running it.
Creating your Restore Plan
gcloud beta container backup-restore restore-plans create example-restore \ --project=gke-backup \ --location=us-central1 \ --backup-plan=projects/gke-backup/locations/us- central1/backupPlans/stateful-plan \ --cluster=projects/gke-backup/locations/us- central1/clusters/gke-autopilot \ --namespaced-resource-restore-mode=delete-and- restore \ --all-namespaces
Restoring your GKE backup
gcloud beta container backup-restore restores create example-restore \ --project=gke-backup \ --location=us-central1 \ --restore-plan=example-restore \ --backup=projects/gke-backup/locations/us-central1/backupPlans/stateful-plan/backups/example-backup
There you have it! You now have a backup-plan for your stateful cluster in the default namespace. Next, get started with GKE Network Policies this quick tutorial.
Reference: GKE-Backup-Concepts (https://cloud.google.com/kubernetes-engine/docs/add-on/backup-for-gke/concepts/backup-for-gke#architecture)
 Project Creation (https://cloud.google.com/resource-manager/docs/creating-managing-projects#creating_a_project)
 VPC, Subnets and Firewall rules (https://cloud.google.com/vpc/docs/create-modify-vpc-networks)
 GKE-AutoPilot (https://cloud.google.com/kubernetes-engine/docs/how-to/creating-an-autopilot-cluster)
 GKE-Backup-Enable (https://cloud.google.com/kubernetes-engine/docs/add-on/backup-for-gke/how-to/install)
 GKE-Statefulset-example (https://kubernetes.io/docs/tutorials/stateful-application/basic-stateful-set/)
 GKE-Backup-Planning-Backup (https://cloud.google.com/kubernetes-engine/docs/how-to/creating-an-autopilot-cluster)
 GKE Restore Plan (https://cloud.google.com/kubernetes-engine/docs/add-on/backup-for-gke/how-to/restore-plan)
 GKE Restore Backup (https://cloud.google.com/kubernetes-engine/docs/add-on/backup-for-gke/how-to/restore)
By: Spencer Patrick Bischof (Customer Engineer) and Hakim Graia (Customer Engineer)
Originally published at: Google Cloud Blog
For enquiries, product placements, sponsorships, and collaborations, connect with us at firstname.lastname@example.org. We'd love to hear from you!
Our humans need coffee too! Your support is highly appreciated, thank you!