After you test and validate your green environment, you start a switchover to convert the green environment to become your new production read and write environment.
Before you begin
Before you initiate a switchover, ensure that you have the required permissions and that your deployment is ready for switchover.
Required roles and permissions
To get the permissions that you need to execute a switchover, ask your administrator to grant you the following IAM role on your project:
- Cloud SQL Admin (
roles/cloudsql.admin)
For custom roles, ensure that you have the following permissions:
cloudsql.blueGreenDeployments.switchovercloudsql.blueGreenDeployments.getcloudsql.instances.switchovercloudsql.operations.get
For more information about IAM roles and permissions in Cloud SQL, see Roles and permissions.
Pre-switchover verification
Before initiating a switchover, verify the following conditions:
- Deployment state: the deployment must be in the
SWITCHOVER_READYstate. - Configuration settings: confirm that the green instance has your
expected configuration settings by viewing the setting differences in
the Cloud de Confiance console, running the
gcloud sql blue-green-deployments describecommand with the--show-config-diffflag, or calling theblueGreenDeployments.getmethod with theview=DETAILEDparameter. For more information, see View configuration differences. - Workload validation: complete all testing and validation on the green staging instance.
- Low replication lag: ensure that replication lag between blue and green is minimal to reduce switchover time.
How switchover works
When you start a switchover, Cloud SQL performs the following automated sequence:
- Pre-switchover validation: before the switchover operation, Cloud SQL validates that replication is not broken and performs a series of configuration checks to ensure that the deployment is ready.
- Switchover workflow execution: during workflow execution, Cloud SQL prepares the instances for switchover. If replication lag is too high or if an active transaction blocks switchover, the switchover operation fails and your production blue environment remains online.
- Traffic redirection: Cloud SQL swaps the connection endpoints between the blue and green instances.
- Role conversion: the green instance becomes the active production read and write instance, and the blue instance becomes a standalone read and write instance.
During switchover, application connections experience a brief disruption (typically in seconds) before reconnecting automatically to the upgraded production instance. Switchover downtime varies based on the Cloud SQL edition:
- Cloud SQL Enterprise Plus edition: switchover downtime is typically sub-second.
- Cloud SQL Enterprise edition: switchover downtime is typically less than 60 seconds, depending on workload and replication lag. No application configuration or connection string changes are required.
Switchover lifecycle states
Before, during, and after a switchover, the deployment transitions through the
following states in the state response field:
SWITCHOVER_READY: logical replication from blue to green is healthy and the deployment is ready for switchover. TheSWITCHOVER_READYstatus is contingent only on replication being healthy. Cloud SQL doesn't evaluate replication lag when setting this status.SWITCHOVER_NOT_READY: the deployment is provisioned, but switchover can't be initiated. This occurs if logical replication is broken, paused, or stopped, or if an underlying node error occurred. Cloud SQL doesn't evaluate replication lag when setting this status. Inspect theerrorDetailfield and resolve the error before attempting switchover.SWITCHOVER_IN_PROGRESS: the switchover command is actively executing. Cloud SQL is swapping connection endpoints and converting green to the production read and write instance.SWITCHOVER_COMPLETED: the switchover finished successfully. Green is now your active production database instance serving read and write traffic, and the blue instance is retained as a standalone instance until you delete the deployment.
Best practices before switchover
- Schedule during low-traffic windows: although downtime is minimal (typically in seconds), start the switchover during periods of low write activity (for example, off-peak hours or maintenance windows when write queries per second [QPS] are at their lowest). This minimizes replication lag and reduces the risk of canceled transactions.
- Verify replication lag: make sure that replication lag is minimal
before you start the switchover. Describing a blue-green deployment doesn't
show replication lag, and Cloud SQL doesn't check replication lag
before the switchover operation. However, during workflow
execution, switchover fails if replication lag is too high. To monitor
replication lag, check Cloud Monitoring metrics (such as
replica_lag) or inspect replication status directly on the green instance. For more information, see Replication lag. - Check active transactions: make sure that long-running DDL operations or batch writes are completed before you start the switchover.
Start a switchover
Start the switchover operation by using the Cloud de Confiance console, the gcloud CLI, Terraform, or the Cloud SQL Admin API:
Console
-
In the Cloud de Confiance console, go to the Cloud SQL Instances page.
- To open the Overview page of an instance, click the instance name.
- In the Blue Green Deployment Status card, click Details to open the Deployment overview page.
- Click Switchover deployment.
- In the Instance Setting Differences dialog, review the setting differences between the Source and Target instances, and then click Continue to start the switchover.
gcloud
Run the gcloud sql blue-green-deployments switchover command:
gcloud sql blue-green-deployments switchover DEPLOYMENT_NAME \ --region=REGION \ --async
Switchover operations can take several minutes to complete. 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
To check the status of the switchover operation, run the
gcloud sql
operations describe command:
gcloud sql operations describe OPERATION_ID
Replace the following:
- DEPLOYMENT_NAME: the name of your blue-green deployment.
- REGION: the Cloud de Confiance by S3NS region where the deployment was created.
- OPERATION_ID: the ID of the switchover operation.
Terraform
To start a switchover using Terraform, update your primary
google_sql_database_instance
resource configuration and apply the changes in a single operation:
- In the
blue_green_deploymentcode block, change theactive_environmentfield from"SOURCE"to"TARGET". - If you created the deployment with a
target_database_versionfield that differs from the source instance, update the top-leveldatabase_versionfield to match thetarget_database_versionfield in the same configuration change. If you modified any other settings on the green instance, update those fields in yourgoogle_sql_database_instanceresource at the same time so that your Terraform configuration matches the green instance. - Optional: To automatically delete the former blue instance when you
later remove the
blue_green_deploymentcode block during cleanup, set thedelete_old_source_on_destroyfield totruein theblue_green_deploymentcode block.
resource "google_sql_database_instance" "primary" { name = "SOURCE_INSTANCE_ID" region = "REGION" database_version = "TARGET_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 = "TARGET" delete_old_source_on_destroy = true } }
Replace the following:
- SOURCE_INSTANCE_ID: the name of your primary instance.
- REGION: the Cloud de Confiance by S3NS region where the deployment was created.
- TARGET_DATABASE_VERSION: the database version of the green
instance (for example,
MYSQL_8_4). - MACHINE_TIER: the machine type of the green instance (for
example,
db-custom-2-7680). - EDITION: the Cloud SQL edition of the green
instance (
ENTERPRISEorENTERPRISE_PLUS). - DEPLOYMENT_NAME: the name of your blue-green deployment.
To apply your Terraform configuration in a Cloud de Confiance project, complete the steps in the following sections.
Prepare Cloud Shell
- Launch Cloud Shell.
-
Set the default Cloud de Confiance 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).
-
In Cloud Shell, create a directory and a new
file within that directory. The filename must have the
.tfextension—for examplemain.tf. In this tutorial, the file is referred to asmain.tf.mkdir DIRECTORY && cd DIRECTORY && touch main.tf
-
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.
- Review and modify the sample parameters to apply to your environment.
- Save your changes.
-
Initialize Terraform. You only need to do this once per directory.
terraform init
Optionally, to use the latest Google provider version, include the
-upgradeoption:terraform init -upgrade
Apply the changes
-
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.
-
Apply the Terraform configuration by running the following command and entering
yesat the prompt:terraform apply
Wait until Terraform displays the "Apply complete!" message.
- Open your Cloud de Confiance project to view the results. In the Cloud de Confiance console, navigate to your resources in the UI to make sure that Terraform has created or updated them.
REST v1
Send a POST request to the blueGreenDeployments.switchover
method:
POST https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/ locations/REGION/ blueGreenDeployments/DEPLOYMENT_NAME:switchover
Replace the following:
- PROJECT_ID: the ID of your Cloud de Confiance by S3NS project.
- REGION: the Cloud de Confiance by S3NS region where the deployment was created.
- DEPLOYMENT_NAME: the name of your blue-green deployment.
REST v1beta4
Send a POST request to the blueGreenDeployments.switchover
method:
POST https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/ locations/REGION/ blueGreenDeployments/DEPLOYMENT_NAME:switchover
Replace the following:
- PROJECT_ID: the ID of your Cloud de Confiance by S3NS project.
- REGION: the Cloud de Confiance by S3NS region where the deployment was created.
- DEPLOYMENT_NAME: the name of your blue-green deployment.
Post-switchover monitoring
After the switchover completes:
- Verify that your application reconnects successfully and production database operations resume.
- Monitor query throughput, error logs, and replication status on the new production instance.
- Keep the blue standalone instance intact during your initial post-upgrade verification window. After you confirm stability, delete the deployment. For more information, see Delete a blue-green deployment.
What's next
- Delete a blue-green deployment to remove deployment metadata and delete the former blue instance.
- Learn how to monitor instances.
- Learn about backup and recovery.