arrow_back

GKE Backup and Restore

登录 加入
访问 700 多个实验和课程

GKE Backup and Restore

实验 1 小时 universal_currency_alt 1 积分 show_chart 入门级
info 此实验可能会提供 AI 工具来支持您学习。
访问 700 多个实验和课程

GSP1110

Google Cloud Self-Paced Labs

Overview

Backup for GKE is a service for backing up and restoring workloads in GKE clusters. It has two components:

  • A Google Cloud API serves as the control plane for the service.
  • A GKE add-on (the Backup for GKE agent) must be enabled in each cluster for which you wish to perform backup and restore operations.

Backups of your workloads may be useful for disaster recovery, CI/CD pipelines, cloning workloads, or upgrade scenarios. Protecting your workloads can help you achieve business-critical recovery point objectives.

What you'll learn

In this lab, you will learn how to:

  • Enable Backup for a GKE cluster
  • Deploy a stateful application with a database on GKE
  • Plan and backup GKE workloads
  • Restore a backup

Prerequisites

Working knowledge of the following technologies is beneficial but not required for this lab:

Setup and Requirements

Before you click the Start Lab button

Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources will be made available to you.

This hands-on lab lets you do the lab activities yourself in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials that you use to sign in and access Google Cloud for the duration of the lab.

What you need

To complete this lab, you need:

  • Access to a standard internet browser (Chrome browser recommended).
  • Time to complete the lab.

Note: If you already have your own personal Google Cloud account or project, do not use it for this lab.

Note: If you are using a Chrome OS device, open an Incognito window to run this lab.

How to start your lab and sign in to the Google Cloud Console

  1. Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is a panel populated with the temporary credentials that you must use for this lab.

    Open Google Console

  2. Copy the username, and then click Open Google Console. The lab spins up resources, and then opens another tab that shows the Sign in page.

    Sign in

    Tip: Open the tabs in separate windows, side-by-side.

  3. In the Sign in page, paste the username that you copied from the left panel. Then copy and paste the password.

    Important: You must use the credentials from the left panel. Do not use your Google Cloud Training credentials. If you have your own Google Cloud account, do not use it for this lab (avoids incurring charges).

  4. Click through the subsequent pages:

    • Accept the terms and conditions.
    • Do not add recovery options or two-factor authentication (because this is a temporary account).
    • Do not sign up for free trials.

After a few moments, the Cloud Console opens in this tab.

Activate Cloud Shell

Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.

In the Cloud Console, in the top right toolbar, click the Activate Cloud Shell button.

Cloud Shell icon

Click Continue.

cloudshell_continue.png

It takes a few moments to provision and connect to the environment. When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. For example:

Cloud Shell Terminal

gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.

You can list the active account name with this command:

gcloud auth list

(Output)

Credentialed accounts: - <myaccount>@<mydomain>.com (active)

(Example output)

Credentialed accounts: - google1623327_student@qwiklabs.net

You can list the project ID with this command:

gcloud config list project

(Output)

[core] project = <project_ID>

(Example output)

[core] project = qwiklabs-gcp-44776a13dea667a6

Set up the environment

A GKE cluster and a static external IP address were provisioned as part of the lab setup.

  • Run the following commands to set the required environment variables:
echo "export ZONE=us-central1-a" >> ~/.bashrc echo "export REGION=us-central1" >> ~/.bashrc echo "export PROJECT_ID=`gcloud config get-value core/project`" >> ~/.bashrc echo "export BACKUP_PLAN=my-backup-plan" >> ~/.bashrc source ~/.bashrc echo "export EXTERNAL_ADDRESS=$(gcloud compute addresses describe app-address --format='value(address)' --region $REGION)" >> ~/.bashrc source ~/.bashrc

Task 1. Enable Backup for GKE

  1. Enable the Backup for GKE APIs:
gcloud services enable gkebackup.googleapis.com

You should see the following success message:

Operation "operations/####.##-############-########-####-####-####-############" finished successfully.
  1. Enable Backup for GKE on an existing cluster. You can enable Backup when you create a new cluster, but for this lab you will enable it on an existing cluster:
gcloud beta container clusters update lab-cluster \ --project=$PROJECT_ID \ --update-addons=BackupRestore=ENABLED \ --zone=$ZONE

Your results should look like this:

Updating lab-cluster...done. Updated [https://container.googleapis.com/v1beta1/projects/qwiklabs-gcp-##-############/zones/us-central1-a/clusters/lab-cluster]. To inspect the contents of your cluster, go to: https://console.cloud.google.com/kubernetes/workload_/gcloud/us-central1-a/lab-cluster?project=qwiklabs-gcp-##-############
  1. Verify Backup for GKE is enabled on the cluster
gcloud beta container clusters describe lab-cluster \ --project=$PROJECT_ID \ --zone=$ZONE | grep -A 1 gkeBackupAgentConfig:

Click Check my progress to verify the objective.

Enable Backup for GKE

Task 2. Create a backup plan

  1. Run the following to create a backup plan:
gcloud beta container backup-restore backup-plans create $BACKUP_PLAN \ --project=$PROJECT_ID \ --location=$REGION \ --cluster=projects/${PROJECT_ID}/locations/${ZONE}/clusters/lab-cluster \ --all-namespaces \ --include-secrets \ --include-volume-data \ --cron-schedule="10 3 * * *" \ --backup-retain-days=30

You will see the following when it is complete.

Create request issued for: [my-backup-plan] Waiting for operation [projects/qwiklabs-gcp-##-############/locations/us-central1/operations/operation-#############-#############-########-########] to complete...working... Waiting for operation [projects/qwiklabs-gcp-##-############/locations/us-central1/operations/operation-#############-#############-########-########] to complete...done. ㅤ Created backup plan [my-backup-plan].
  1. Verify the backup plans was created:
gcloud beta container backup-restore backup-plans list \ --project=$PROJECT_ID \ --location=$REGION

You will see the following verification:

NAME: my-backup-plan LOCATION: us-central1 CLUSTER: lab-cluster ACTIVE: Y PAUSED: N
  1. View the details of a backup plan:
gcloud beta container backup-restore backup-plans describe $BACKUP_PLAN \ --project=$PROJECT_ID \ --location=$REGION

You details should look like this:

backupConfig: allNamespaces: true includeSecrets: true includeVolumeData: true backupSchedule: cronSchedule: 10 3 * * * cluster: projects/qwiklabs-gcp-##-############/locations/us-central1-a/clusters/lab-cluster createTime: 'YYYY-MM-DDTHH:MM:SS.NNNNNNNNNZ' etag: '#' name: projects/qwiklabs-gcp-##-############/locations/us-central1/backupPlans/my-backup-plan retentionPolicy: backupRetainDays: 30 uid: ########-####-####-####-############ updateTime: 'YYYY-MM-DDTHH:MM:SS.NNNNNNNNNZ'

Click Check my progress to verify the objective.

Create a Backup plan

Task 3. Deploy WordPress with MySQL to the cluster

  1. Get credentials for lab-cluster:
gcloud container clusters get-credentials lab-cluster \ --zone=$ZONE

You credentials should look like the following:

Fetching cluster endpoint and auth data. kubeconfig entry generated for existing-cluster.
  1. Ensure the reserved static IP address for the application is set:
echo "EXTERNAL_ADDRESS=${EXTERNAL_ADDRESS}"

The output should look like this:

EXTERNAL_ADDRESS=###.###.###.###

Task 4. Deploy the application

You are now ready to deploy a stateful application. You will deploy the WordPress application using MySQL as the database.

  1. Run the following commands create persistent volumes for the application and database. The service will also be exposed through a Google Cloud external load balancer:
# Password for lab only. Change to a strong one in your environment. YOUR_SECRET_PASSWORD=1234567890 kubectl create secret generic mysql-pass --from-literal=password=${YOUR_SECRET_PASSWORD?} kubectl apply -f https://k8s.io/examples/application/wordpress/mysql-deployment.yaml kubectl apply -f https://k8s.io/examples/application/wordpress/wordpress-deployment.yaml

Your results should look like the following:

secret/mysql-pass created service/wordpress-mysql created persistentvolumeclaim/mysql-pv-claim created deployment.apps/wordpress-mysql created service/wordpress created persistentvolumeclaim/wp-pv-claim created deployment.apps/wordpress created
  1. Patch the service to use EXTERNAL_ADDRESS:
patch_file=/tmp/loadbalancer-patch.yaml cat <<EOF > ${patch_file} spec: loadBalancerIP: ${EXTERNAL_ADDRESS} EOF kubectl patch service/wordpress --patch "$(cat ${patch_file})"
  1. Wait for the application to be accessible:
while ! curl --fail --max-time 5 --output /dev/null --show-error --silent http://${EXTERNAL_ADDRESS}; do sleep 5 done echo -e "\nhttp://${EXTERNAL_ADDRESS} is accessible\n"

When the application is accessible, you should see the following output:

... curl: (28) Connection timed out after 5001 milliseconds curl: (28) Connection timed out after 5001 milliseconds curl: (28) Connection timed out after 5001 milliseconds curl: (28) Connection timed out after 5001 milliseconds curl: (28) Connection timed out after 5001 milliseconds curl: (28) Connection timed out after 5001 milliseconds curl: (28) Connection timed out after 5001 milliseconds curl: (28) Connection timed out after 5000 milliseconds curl: (28) Connection timed out after 5000 milliseconds curl: (28) Connection timed out after 5000 milliseconds curl: (28) Connection timed out after 5000 milliseconds ㅤ http://###.###.###.### is accessible ㅤ

Click Check my progress to verify the objective.

Deploy the application and patch the service

Task 5. Verify the deployed workload

  1. In the Cloud console, navigate to Kubernetes Engine > Workload. You should see the WordPress application and its database.

gke_wordpress

  1. Open a browser window and paste in the URL from the previous step. You should see the following page:

wordpress_start

  1. Click the Continue button and type in the required info. For example:

wordpress_info

  1. Make a note of the password and click the Install WordPress button.

After you log in to the WordPress application, try to create some new posts and add a few comments to existing posts. After backup/restore, you want to verify your input still exists.

Click Check my progress to verify the objective.

Install the WordPress application

Task 6. Create a backup

  1. Create a backup based on the backup plan:
gcloud beta container backup-restore backups create my-backup1 \ --project=$PROJECT_ID \ --location=$REGION \ --backup-plan=$BACKUP_PLAN \ --wait-for-completion

Your results should look like this:

Create in progress for backup my-backup1 [projects/qwiklabs-gcp-##-############/locations/us-central1/operations/operation-#############-#############-########-########]. Creating backup my-backup1...done. Waiting for backup to complete... Backup state: IN_PROGRESS. Waiting for backup to complete... Backup state: IN_PROGRESS. Waiting for backup to complete... Backup state: IN_PROGRESS. Waiting for backup to complete... Backup state: IN_PROGRESS. Waiting for backup to complete... Backup state: IN_PROGRESS. Waiting for backup to complete... Backup state: IN_PROGRESS. Waiting for backup to complete... Backup state: IN_PROGRESS. Waiting for backup to complete... Backup state: IN_PROGRESS. Waiting for backup to complete... Backup state: IN_PROGRESS. Backup completed. Backup state: SUCCEEDED
  1. View the backups:
gcloud beta container backup-restore backups list \ --project=$PROJECT_ID \ --location=$REGION \ --backup-plan=$BACKUP_PLAN

Your backups should look like this:

NAME: my-backup1 LOCATION: us-central1 BACKUP_PLAN: my-backup-plan CREATE_TIME: YYYY-MM-DDTHH:MM:SS UTC COMPLETE_TIME: YYYY-MM-DDTHH:MM:SS UTC STATE: SUCCEEDED
  1. View the details of the backup:
gcloud beta container backup-restore backups describe my-backup1 \ --project=$PROJECT_ID \ --location=$REGION \ --backup-plan=$BACKUP_PLAN

Details of the backup should be similar to this:

allNamespaces: true clusterMetadata: backupCrdVersions: backupjobs.gkebackup.gke.io: v1 protectedapplicationgroups.gkebackup.gke.io: v1 protectedapplications.gkebackup.gke.io: v1 restorejobs.gkebackup.gke.io: v1 cluster: projects/qwiklabs-gcp-##-############/locations/us-central1-a/clusters/lab-cluster gkeVersion: v##.##.##-gke.### k8sVersion: '##.##' completeTime: 'YYYY-MM-DDTHH:MM:SS.NNNNNNNNNZ' configBackupSizeBytes: '######' containsSecrets: true containsVolumeData: true createTime: 'YYYY-MM-DDTHH:MM:SS.NNNNNNNNNZ' deleteLockExpireTime: 'YYYY-MM-DDTHH:MM:SS.NNNNNNNNNZ' etag: '#' manual: true name: projects/qwiklabs-gcp-##-############/locations/us-central1/backupPlans/my-backup-plan/backups/my-backup1 podCount: 2 resourceCount: 1396 retainDays: 30 retainExpireTime: 'YYYY-MM-DDTHH:MM:SS.NNNNNNNNNZ' sizeBytes: '########' state: SUCCEEDED uid: ########-####-####-####-############ updateTime: 'YYYY-MM-DDTHH:MM:SS.NNNNNNNNNZ' volumeCount: 2

Click Check my progress to verify the objective.

Create a Backup

Task 7. Delete the application

You can restore the backup on the same cluster or a different one. In this lab, you will perform a restore on the same cluster. B.

  1. Delete the running application:
kubectl delete secret mysql-pass kubectl delete -f https://k8s.io/examples/application/wordpress/mysql-deployment.yaml kubectl delete -f https://k8s.io/examples/application/wordpress/wordpress-deployment.yaml

When the applications are deleted, you will see the following:

secret "mysql-pass" deleted service "wordpress-mysql" deleted persistentvolumeclaim "mysql-pv-claim" deleted deployment.apps "wordpress-mysql" deleted service "wordpress" deleted persistentvolumeclaim "wp-pv-claim" deleted deployment.apps "wordpress" deleted
  1. Verify the workload is deleted from the GKE workload page.

Or from CloudShell by running the following:

kubectl get pods

You will see that nothing is found:

No resources found in default namespace.
  1. Verify you cannot access the application
echo -e "\nWordPress URL: http://${EXTERNAL_ADDRESS}\n"

Click on the URL and verify it is not functional.

Click Check my progress to verify the objective.

Delete the Application

Task 8. Plan a restore

  1. Create a restore plan:
gcloud beta container backup-restore restore-plans create my-restore-plan1 \ --project=$PROJECT_ID \ --location=$REGION \ --backup-plan=projects/${PROJECT_ID}/locations/${REGION}/backupPlans/$BACKUP_PLAN \ --cluster=projects/${PROJECT_ID}/locations/${ZONE}/clusters/lab-cluster \ --namespaced-resource-restore-mode=delete-and-restore \ --volume-data-restore-policy=restore-volume-data-from-backup \ --all-namespaces

Your output should look like the following:

Create request issued for: [my-restore-plan1] Waiting for operation [projects/qwiklabs-gcp-##-############/locations/us-central1/operations/ operation-#############-#############-########-########] to complete...working. Waiting for operation [projects/qwiklabs-gcp-##-############/locations/us-central1/operations/operation-#############-#############-########-########] to complete...done. Created restore plan [my-restore-plan1].
  1. View the restore plans:
gcloud beta container backup-restore restore-plans list \ --project=$PROJECT_ID \ --location=$REGION
  1. View the details of a restore plan
gcloud beta container backup-restore restore-plans describe my-restore-plan1 \ --project=$PROJECT_ID \ --location=$REGION

Your restore plan should look like the following:

backupPlan: projects/qwiklabs-gcp-##-############/locations/us-central1/backupPlans/my-backup-plan cluster: projects/qwiklabs-gcp-##-############/locations/us-central1-a/clusters/lab-cluster createTime: 'YYYY-MM-DDTHH:MM:SS.NNNNNNNNNZ' etag: '1' name: projects/qwiklabs-gcp-##-############/locations/us-central1/restorePlans/my-restore-plan1 restoreConfig: allNamespaces: true namespacedResourceRestoreMode: DELETE_AND_RESTORE volumeDataRestorePolicy: RESTORE_VOLUME_DATA_FROM_BACKUP uid: ########-####-####-####-############ updateTime: 'YYYY-MM-DDTHH:MM:SS.NNNNNNNNNZ'

Click Check my progress to verify the objective.

Plan a Restore

Task 9. Restore a backup

  1. Restore from the backup
gcloud beta container backup-restore restores create my-restore1 \ --project=$PROJECT_ID \ --location=$REGION \ --restore-plan=my-restore-plan1 \ --backup=projects/${PROJECT_ID}/locations/${REGION}/backupPlans/${BACKUP_PLAN}/backups/my-backup1 \ --wait-for-completion

Your progress should look like this:

Create in progress for restore my-restore1 [projects/qwiklabs-gcp-##-############/locations/us-central1/operations/operation-#############-#############-########-########]. Creating restore my-restore1...done. Waiting for restore to complete... Restore state: CREATING. Waiting for restore to complete... Restore state: CREATING. Waiting for restore to complete... Restore state: IN_PROGRESS. Restore completed. Restore state: SUCCEEDED
  1. Verify the application is running:
kubectl get pods
  1. Wait until the all pods have a STATUS of RUNNING
NAME READY STATUS RESTARTS AGE wordpress-##########-##### 1/1 Running 0 ##s wordpress-mysql-#########-##### 1/1 Running 0 ##s
  1. Verify you can access the application
echo -e "\nWordPress URL: http://${EXTERNAL_ADDRESS}\n"
  1. Click on the URL and verify the application is functional.

Click Check my progress to verify the objective.

Restore a Backup

Congratulations!

Google Cloud Training & Certification

...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.

Manual Last Updated: December 15, 2022

Manual Last tested: January 30, 2023

Copyright 2023 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.

准备工作

  1. 实验会创建一个 Google Cloud 项目和一些资源,供您使用限定的一段时间
  2. 实验有时间限制,并且没有暂停功能。如果您中途结束实验,则必须重新开始。
  3. 在屏幕左上角,点击开始实验即可开始

使用无痕浏览模式

  1. 复制系统为实验提供的用户名密码
  2. 在无痕浏览模式下,点击打开控制台

登录控制台

  1. 使用您的实验凭证登录。使用其他凭证可能会导致错误或产生费用。
  2. 接受条款,并跳过恢复资源页面
  3. 除非您已完成此实验或想要重新开始,否则请勿点击结束实验,因为点击后系统会清除您的工作并移除该项目

此内容目前不可用

一旦可用,我们会通过电子邮件告知您

太好了!

一旦可用,我们会通过电子邮件告知您

一次一个实验

确认结束所有现有实验并开始此实验

使用无痕浏览模式运行实验

请使用无痕模式或无痕式浏览器窗口运行此实验。这可以避免您的个人账号与学生账号之间发生冲突,这种冲突可能导致您的个人账号产生额外费用。