Sunday, April 28, 2024
No menu items!
HomeCloud ComputingBackup for GKE - Part 2 - backup & restore plan

Backup for GKE – Part 2 – backup & restore plan

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.

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[1]

Prerequisites – In my case, I made a VPC named gke-vpc, subnet gke-vpc and an automatic subnet

Project Created – name gke-backup[2]

VPC(gke-vpc), subnet(gke-vpc) and firewall rules[3]

GKE-Autopilot Creation[4]

code_block[StructValue([(u’code’, u’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″‘), (u’language’, u”), (u’caption’, <wagtail.wagtailcore.rich_text.RichText object at 0x3e386261d210>)])]

Enable Backups[5]

code_block[StructValue([(u’code’, u’gcloud services enable gkebackup.googleapis.com’), (u’language’, u”), (u’caption’, <wagtail.wagtailcore.rich_text.RichText object at 0x3e3862088610>)])]
code_block[StructValue([(u’code’, u’gcloud container clusters update gke-autopilot \rn –project=gke-backup \rn –region=us-central1 \rn –update-addons=BackupRestore=ENABLED’), (u’language’, u”), (u’caption’, <wagtail.wagtailcore.rich_text.RichText object at 0x3e3862fffb90>)])]

Stateful set[6]

Deploy the below statefulset yaml to your cluster.

code_block[StructValue([(u’code’, u’apiVersion: v1rnkind: Servicernmetadata:rn name: nginxrn labels:rn app: nginxrnspec:rn ports:rn – port: 80rn name: webrn clusterIP: Nonern selector:rn app: nginxrn—rnapiVersion: apps/v1rnkind: StatefulSetrnmetadata:rn name: webrnspec:rn serviceName: “nginx”rn replicas: 2rn selector:rn matchLabels:rn app: nginxrn template:rn metadata:rn labels:rn app: nginxrn spec:rn containers:rn – name: nginxrn image: registry.k8s.io/nginx-slim:0.8rn ports:rn – containerPort: 80rn name: webrn volumeMounts:rn – name: wwwrn mountPath: /usr/share/nginx/htmlrn volumeClaimTemplates:rn – metadata:rn name: wwwrn spec:rn accessModes: [ “ReadWriteOnce” ]rn resources:rn requests:rn storage: 1Gi’), (u’language’, u”), (u’caption’, <wagtail.wagtailcore.rich_text.RichText object at 0x3e3862ffff90>)])]

If in terminal, run below commands:

touch statefulset.yaml

vi statefulset.yaml

copy/paste 

enter :wq

kubectl create -f statefulset.yaml

Backup Plan[7]

code_block[StructValue([(u’code’, u’gcloud beta container backup-restore backup-plans create stateful-plan \rn –project=gke-backup \rn –location=us-central1 \rn –cluster=projects/gke-backup/locations/us- rn central1/clusters/gke-autopilot \rn –all-namespaces \rn –backup-retain-days=5 \rn –cron-schedule=”0 * * * *” \rn –include-volume-data’), (u’language’, u”), (u’caption’, <wagtail.wagtailcore.rich_text.RichText object at 0x3e3861b04950>)])]
code_block[StructValue([(u’code’, u’gcloud beta container backup-restore backups create example-backup \rn –project=gke-backup \rn –location=us-central1 \rn –backup-plan=stateful-plan \rn –wait-for-completion’), (u’language’, u”), (u’caption’, <wagtail.wagtailcore.rich_text.RichText object at 0x3e3861b04850>)])]

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[8]

code_block[StructValue([(u’code’, u’gcloud beta container backup-restore restore-plans create example-restore \rn –project=gke-backup \rn –location=us-central1 \rn –backup-plan=projects/gke-backup/locations/us-rn central1/backupPlans/stateful-plan \rn –cluster=projects/gke-backup/locations/us-rn central1/clusters/gke-autopilot \rn –namespaced-resource-restore-mode=delete-and-rn restore \rn –all-namespaces’), (u’language’, u”), (u’caption’, <wagtail.wagtailcore.rich_text.RichText object at 0x3e3813a5aa10>)])]

Restoring your GKE backup[9]

code_block[StructValue([(u’code’, u’gcloud beta container backup-restore restores create example-restore \rn –project=gke-backup \rn –location=us-central1 \rn –restore-plan=example-restore \ rn–backup=projects/gke-backup/locations/us-central1/backupPlans/stateful-plan/backups/example-backup’), (u’language’, u”), (u’caption’, <wagtail.wagtailcore.rich_text.RichText object at 0x3e38625a9ad0>)])]

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: 

[1] GKE-Backup-Concepts
[2] Project Creation
[3] VPC, Subnets and Firewall rules
[2] GKE-AutoPilot
[3]GKE-Backup-Enable
[4] GKE-Statefulset-example
[5] GKE-Backup-Planning-Backup
[6] GKE Restore Plan
[7] GKE Restore Backup

Related Article

Backup for GKE – concepts – Part 1 – how it works

Automate your kubernetes backup and restore for better resiliency and disaster response.

Read Article

Cloud BlogRead More

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments