Create and stage a blue-green deployment

This document describes how to create and stage a Cloud SQL blue-green deployment to perform major version upgrades or configuration changes.

Before you begin

To create and stage a blue-green deployment, verify that you have the required roles and that your source instance meets the deployment prerequisites.

Required roles and permissions

To get the permissions that you need to create and stage a blue-green deployment, ask your administrator to grant you the following IAM roles on your project:

  • Cloud SQL Editor (roles/cloudsql.editor)
  • Cloud SQL Admin (roles/cloudsql.admin)

For custom roles, ensure that you have the following permissions:

  • cloudsql.blueGreenDeployments.create
  • cloudsql.blueGreenDeployments.get
  • cloudsql.instances.get
  • cloudsql.instances.create
  • cloudsql.instances.update
  • cloudsql.operations.get

For more information about IAM roles and permissions in Cloud SQL, see Roles and permissions.

Instance prerequisites

Before creating a blue-green deployment, verify that your production (blue) instance meets the following requirements:

  1. Database engine, edition, and version: your instance must run Cloud SQL for MySQL version 5.7, 8.0, or 8.4 on Cloud SQL Enterprise edition or Cloud SQL Enterprise Plus edition. Version 8.0.18 isn't supported. For major version upgrades, you can upgrade from version 8.0 to 8.4.
  2. Binary logging and automated backups: you must enable binary logging and automated backups on the blue instance to establish continuous logical replication to the green environment.
  3. Instance status: the blue instance must be in a RUNNING state with no ongoing operations or conflicting maintenance windows.
  4. Unsupported features: verify that your instance doesn't use any features that aren't supported in blue-green deployments, which include the following:

  5. Latest maintenance version: your instance must run the latest maintenance version before you create a blue-green deployment. For more information, see Self-service maintenance.

  6. Network architecture: your instance must use the new network architecture. Instances that use the old network architecture aren't supported.

When you trigger deployment creation, Cloud SQL automatically runs a series of automated prechecks to validate replication compatibility and flags. For major version upgrades, Cloud SQL also runs the major version upgrade pre-check API on the source instance to validate upgrade readiness before proceeding with the workflow. If prechecks fail, deployment creation stops and returns an error in the operation status.

Create a blue-green deployment

You can create a blue-green deployment to perform a major version upgrade (by specifying a target database version), or to stage hardware or configuration changes at your current database version.

Console

  1. In the Google Cloud console, go to the Cloud SQL Instances page.

    Go to Cloud SQL Instances

  2. To open the Overview page of an instance, click the instance name.
  3. In the Configuration section, click Create blue green deployment.
  4. On the Create Blue Green Deployment page, in the Deployment information section, enter a name for the deployment in the Deployment name field. The name must be unique within your project and region. If a deployment with the same name already exists in the region, deployment creation fails.
  5. In the Deployment use case list, select one of the following options:
    • Option A (Major version upgrade): to stage and test a major version upgrade on the green instance before switchover, select Major version upgrade. In the Target database version list, select the target database version for the green target instance (MySQL 8.4). Major version upgrades are supported only from MySQL 8.0 (excluding 8.0.18) to MySQL 8.4.

      When you create a deployment for a major version upgrade, Cloud SQL automatically runs the major version upgrade precheck as part of the operation before creating the green instance. Alternatively, you can run the major version upgrade precheck before creating the deployment to identify any upgrade blockers.

    • Option B (Default): to create a green staging environment at the same database version as the source instance to test configuration or hardware changes, select Default.
  6. Click Create.

gcloud

To create a blue-green deployment by using the gcloud CLI, run the gcloud sql blue-green-deployments create command.

Option A: Major version upgrade

To create a deployment for a major version upgrade, specify the --target-database-version flag. When you specify a target database version, Cloud SQL automatically runs the major version upgrade precheck as part of the operation before creating the green instance. Alternatively, you can run the major version upgrade precheck on your blue instance before creating the deployment to identify any upgrade blockers:

gcloud sql blue-green-deployments create DEPLOYMENT_NAME \
  --source-instance=SOURCE_INSTANCE_ID \
  --target-database-version=TARGET_DATABASE_VERSION \
  --region=REGION \
  --async

Replace the following:

  • DEPLOYMENT_NAME: a name for your deployment (4 to 63 lowercase letters, numbers, and hyphens, starting with a lowercase letter and not ending with a hyphen). The name must be unique within your project and region. If a deployment with the same name already exists in the region, deployment creation fails.
  • SOURCE_INSTANCE_ID: the name of your blue source instance.
  • TARGET_DATABASE_VERSION: the target database version for the green target instance (MYSQL_8_4). Major version upgrades are supported only from MySQL 8.0 (excluding 8.0.18) to MySQL 8.4, and the target database version can't be changed after the deployment is created.
  • REGION: the Google Cloud region of your blue instance.

Option B: Hardware or configuration changes

To provision a green environment at the same database version as the blue instance to test hardware or configuration changes, omit the --target-database-version flag:

gcloud sql blue-green-deployments create DEPLOYMENT_NAME \
  --source-instance=SOURCE_INSTANCE_ID \
  --region=REGION \
  --async

Creating a blue-green deployment takes several minutes to complete, especially when performing a major version upgrade. You might see a message indicating that the operation is taking longer than expected. You can either ignore this message or run the gcloud sql operations wait command to dismiss the message and wait for the operation to complete:

gcloud sql operations wait OPERATION_ID

Replace OPERATION_ID with the operation ID returned by the command or displayed in the message.

Terraform

To create a blue-green deployment using Terraform, add the blue_green_deployment code block to your primary google_sql_database_instance resource with the active_environment field set to "SOURCE".

Option A: Major version upgrade

To create a deployment for a major version upgrade, keep the top-level database_version field set to your current blue instance version, and set the target_database_version field in the blue_green_deployment code block to the target major version:

resource "google_sql_database_instance" "primary" {
  name             = "SOURCE_INSTANCE_ID"
  region           = "REGION"
  database_version = "SOURCE_DATABASE_VERSION"

  settings {
    tier              = "MACHINE_TIER"
    edition           = "EDITION"
    availability_type = "REGIONAL"

    backup_configuration {
      enabled            = true
      binary_log_enabled = true
    }
  }

  blue_green_deployment {
    deployment_name         = "DEPLOYMENT_NAME"
    target_database_version = "TARGET_DATABASE_VERSION"
    active_environment      = "SOURCE"
  }
}

Replace the following:

  • SOURCE_INSTANCE_ID: the name of your blue source instance.
  • REGION: the Google Cloud region of your blue instance.
  • SOURCE_DATABASE_VERSION: the current database version of your blue instance (for example, MYSQL_8_0). For major version upgrades, the blue instance must run MySQL 8.0 (excluding 8.0.18).
  • MACHINE_TIER: the machine type of your instance (for example, db-custom-2-7680).
  • EDITION: the Cloud SQL edition of your instance (ENTERPRISE or ENTERPRISE_PLUS).
  • DEPLOYMENT_NAME: a name for your deployment (4 to 63 lowercase letters, numbers, and hyphens, starting with a lowercase letter and not ending with a hyphen). The name must be unique within your project and region. If a deployment with the same name already exists in the region, deployment creation fails.
  • TARGET_DATABASE_VERSION: the target database version for the green target instance (MYSQL_8_4). Major version upgrades are supported only from MySQL 8.0 (excluding 8.0.18) to MySQL 8.4, and the target database version can't be changed after the deployment is created.

Option B: Hardware or configuration changes

To provision a green environment at the same database version as the blue instance to test hardware or configuration changes, omit the target_database_version field in the blue_green_deployment code block:

resource "google_sql_database_instance" "primary" {
  name             = "SOURCE_INSTANCE_ID"
  region           = "REGION"
  database_version = "SOURCE_DATABASE_VERSION"

  settings {
    tier              = "MACHINE_TIER"
    edition           = "EDITION"
    availability_type = "REGIONAL"

    backup_configuration {
      enabled            = true
      binary_log_enabled = true
    }
  }

  blue_green_deployment {
    deployment_name    = "DEPLOYMENT_NAME"
    active_environment = "SOURCE"
  }
}

To apply your Terraform configuration in a Google Cloud project, complete the steps in the following sections.

Prepare Cloud Shell

  1. Launch Cloud Shell.
  2. Set the default Google Cloud project where you want to apply your Terraform configurations.

    You only need to run this command once per project, and you can run it in any directory.

    export GOOGLE_CLOUD_PROJECT=PROJECT_ID

    Environment variables are overridden if you set explicit values in the Terraform configuration file.

Prepare the directory

Each Terraform configuration file must have its own directory (also called a root module).

  1. In Cloud Shell, create a directory and a new file within that directory. The filename must have the .tf extension—for example main.tf. In this tutorial, the file is referred to as main.tf.
    mkdir DIRECTORY && cd DIRECTORY && touch main.tf
  2. If you are following a tutorial, you can copy the sample code in each section or step.

    Copy the sample code into the newly created main.tf.

    Optionally, copy the code from GitHub. This is recommended when the Terraform snippet is part of an end-to-end solution.

  3. Review and modify the sample parameters to apply to your environment.
  4. Save your changes.
  5. Initialize Terraform. You only need to do this once per directory.
    terraform init

    Optionally, to use the latest Google provider version, include the -upgrade option:

    terraform init -upgrade

Apply the changes

  1. Review the configuration and verify that the resources that Terraform is going to create or update match your expectations:
    terraform plan

    Make corrections to the configuration as necessary.

  2. Apply the Terraform configuration by running the following command and entering yes at the prompt:
    terraform apply

    Wait until Terraform displays the "Apply complete!" message.

  3. Open your Google Cloud project to view the results. In the Google Cloud console, navigate to your resources in the UI to make sure that Terraform has created or updated them.

REST v1

To create a blue-green deployment using the Cloud SQL Admin API, send a POST request to the blueGreenDeployments.create method.

Option A: Major version upgrade

To create a deployment for a major version upgrade, specify the databaseVersion field in the requestedConfig object. When you specify a target database version, Cloud SQL automatically runs the major version upgrade precheck as part of the operation before creating the green instance. Alternatively, you can run the major version upgrade precheck on your blue instance before creating the deployment to identify any upgrade blockers:

POST https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/
  locations/REGION/
  blueGreenDeployments?blueGreenDeploymentId=DEPLOYMENT_NAME
{
  "sourceInstance": "SOURCE_INSTANCE_ID",
  "requestedConfig": {
    "databaseVersion": "TARGET_DATABASE_VERSION"
  }
}

Replace the following:

  • PROJECT_ID: the ID of your Google Cloud project.
  • REGION: the Google Cloud region of your blue instance.
  • DEPLOYMENT_NAME: a name for your deployment (4 to 63 lowercase letters, numbers, and hyphens, starting with a lowercase letter and not ending with a hyphen). The name must be unique within your project and region. If a deployment with the same name already exists in the region, deployment creation fails.
  • SOURCE_INSTANCE_ID: the name of your blue source instance.
  • TARGET_DATABASE_VERSION: the target database version for the green target instance (MYSQL_8_4). Major version upgrades are supported only from MySQL 8.0 (excluding 8.0.18) to MySQL 8.4, and the target database version can't be changed after the deployment is created.

Option B: Hardware or configuration changes

To provision a green environment at the same database version as the blue instance to test hardware or configuration changes, omit the requestedConfig field:

POST https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/
  locations/REGION/
  blueGreenDeployments?blueGreenDeploymentId=DEPLOYMENT_NAME
{
  "sourceInstance": "SOURCE_INSTANCE_ID"
}

REST v1beta4

To create a blue-green deployment using the Cloud SQL Admin API, send a POST request to the blueGreenDeployments.create method.

Option A: Major version upgrade

To create a deployment for a major version upgrade, specify the databaseVersion field in the requestedConfig object. When you specify a target database version, Cloud SQL automatically runs the major version upgrade precheck as part of the operation before creating the green instance. Alternatively, you can run the major version upgrade precheck on your blue instance before creating the deployment to identify any upgrade blockers:

POST https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/
  locations/REGION/
  blueGreenDeployments?blueGreenDeploymentId=DEPLOYMENT_NAME
{
  "sourceInstance": "SOURCE_INSTANCE_ID",
  "requestedConfig": {
    "databaseVersion": "TARGET_DATABASE_VERSION"
  }
}

Replace the following:

  • PROJECT_ID: the ID of your Google Cloud project.
  • REGION: the Google Cloud region of your blue instance.
  • DEPLOYMENT_NAME: a name for your deployment (4 to 63 lowercase letters, numbers, and hyphens, starting with a lowercase letter and not ending with a hyphen). The name must be unique within your project and region. If a deployment with the same name already exists in the region, deployment creation fails.
  • SOURCE_INSTANCE_ID: the name of your blue source instance.
  • TARGET_DATABASE_VERSION: the target database version for the green target instance (MYSQL_8_4). Major version upgrades are supported only from MySQL 8.0 (excluding 8.0.18) to MySQL 8.4, and the target database version can't be changed after the deployment is created.

Option B: Hardware or configuration changes

To provision a green environment at the same database version as the blue instance to test hardware or configuration changes, omit the requestedConfig field:

POST https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/
  locations/REGION/
  blueGreenDeployments?blueGreenDeploymentId=DEPLOYMENT_NAME
{
  "sourceInstance": "SOURCE_INSTANCE_ID"
}

Monitor deployment status

Creation is a long-running operation (LRO). During staging, Cloud SQL provisions the green instance, upgrades it (if a major version upgrade was requested), and starts continuous logical replication.

Check the progress and status of your blue-green deployment:

Console

  1. In the Google Cloud console, go to the Cloud SQL Instances page.

    Go to Cloud SQL Instances

  2. To open the Overview page of an instance, click the instance name.
  3. Locate the Blue Green Deployment Status card to view the status of the deployment.
  4. To view detailed provisioning tasks and progress, click Details to open the Deployment overview page.

gcloud

To check the progress and status of your blue-green deployment by using the gcloud CLI, run the gcloud sql blue-green-deployments describe command:

gcloud sql blue-green-deployments describe DEPLOYMENT_NAME \
  --region=REGION

Replace the following:

  • DEPLOYMENT_NAME: the name of your blue-green deployment.
  • REGION: the Google Cloud region where the deployment was created.

To wait for the creation operation to complete, run the gcloud sql operations wait command:

gcloud sql operations wait OPERATION_ID

Replace OPERATION_ID with the ID of the creation operation.

REST v1

To check the progress and status of your blue-green deployment using the Cloud SQL Admin API, send a GET request to the blueGreenDeployments.get method:

GET https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/
  locations/REGION/
  blueGreenDeployments/DEPLOYMENT_NAME

Replace the following:

  • PROJECT_ID: the ID of your Google Cloud project.
  • REGION: the Google Cloud region where the deployment was created.
  • DEPLOYMENT_NAME: the name of your blue-green deployment.

REST v1beta4

To check the progress and status of your blue-green deployment using the Cloud SQL Admin API, send a GET request to the blueGreenDeployments.get method:

GET https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/
  locations/REGION/
  blueGreenDeployments/DEPLOYMENT_NAME

Replace the following:

  • PROJECT_ID: the ID of your Google Cloud project.
  • REGION: the Google Cloud region where the deployment was created.
  • DEPLOYMENT_NAME: the name of your blue-green deployment.

In the command output or the Google Cloud console, monitor the state field to track the deployment lifecycle:

  • PROVISIONING: the green instance is being created, upgraded (if requested), and connected to continuous logical replication.
  • SWITCHOVER_READY: initial replication has completed and continuous logical replication is active. The deployment is ready for validation, testing, and switchover.
  • SWITCHOVER_NOT_READY: the deployment is provisioned, but switchover can't be initiated (for example, if replication is broken or an issue is reported in the errorDetail field).

For more information about all deployment lifecycle phases, see Deployment lifecycle and states.

Wait until the deployment state changes to SWITCHOVER_READY before you proceed to describe the deployment, validate your application workloads, or start a switchover.

What's next