How to Set Up High Availability Cloud SQL on GCP
To set up high availability in
Cloud SQL on GCP, create a Cloud SQL instance with the High Availability (HA) option enabled, which provisions a primary and standby instance in different zones. This setup automatically handles failover to the standby if the primary fails, ensuring minimal downtime.Syntax
Use the gcloud sql instances create command with the --availability-type=REGIONAL flag to enable high availability. This creates a primary instance and a standby instance in different zones within the same region.
Key parts:
--availability-type=REGIONAL: Enables HA with automatic failover.--region: Specifies the region for the instances.--tier: Defines the machine type for the instance.--database-version: Sets the database engine and version.
bash
gcloud sql instances create [INSTANCE_NAME] \ --database-version=POSTGRES_15 \ --tier=db-custom-2-7680 \ --region=us-central1 \ --availability-type=REGIONAL
Example
This example creates a high availability Cloud SQL instance named my-ha-instance running PostgreSQL 15 in the us-central1 region. It uses a custom machine type and enables automatic failover with the regional availability type.
bash
gcloud sql instances create my-ha-instance \ --database-version=POSTGRES_15 \ --tier=db-custom-2-7680 \ --region=us-central1 \ --availability-type=REGIONAL
Output
Created [https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/instances/my-ha-instance].
Instance 'my-ha-instance' is configured with high availability in region 'us-central1'.
Common Pitfalls
Common mistakes when setting up high availability Cloud SQL include:
- Not specifying
--availability-type=REGIONAL, which creates a single-zone instance without failover. - Choosing a region without multiple zones, which prevents HA setup.
- Ignoring maintenance windows, which can cause unexpected downtime.
- Not configuring backups and failover replicas properly.
Always verify your instance's HA status in the Cloud Console or with gcloud sql instances describe [INSTANCE_NAME].
bash
## Wrong: Single zone instance (no HA) gcloud sql instances create my-instance \ --database-version=POSTGRES_15 \ --tier=db-custom-2-7680 \ --region=us-central1 ## Right: Enable HA with regional availability gcloud sql instances create my-instance \ --database-version=POSTGRES_15 \ --tier=db-custom-2-7680 \ --region=us-central1 \ --availability-type=REGIONAL
Quick Reference
Summary tips for setting up high availability Cloud SQL:
- Use
--availability-type=REGIONALto enable HA. - Choose a multi-zone region for failover support.
- Configure automated backups and maintenance windows.
- Monitor instance status regularly for failover events.
Key Takeaways
Enable high availability by setting --availability-type=REGIONAL when creating your Cloud SQL instance.
Select a region with multiple zones to support automatic failover.
Regularly configure backups and maintenance windows to avoid downtime.
Verify HA status using gcloud commands or the Cloud Console.
Avoid creating single-zone instances if you need high availability.