In this tutorial, you integrate an AlloyDB instance and a Compute Engine VM instance on a Virtual Private Cloud (VPC) network. You then verify the integration by installing pgbench on the Compute Engine VM instance, and use this benchmarking tool to conduct performance tests against the AlloyDB instance.
Objectives
In this tutorial, you learn how to:
- Create a VPC network for your AlloyDB instance and your Compute Engine VM instance so that communication can pass between the instance and the VM securely.
- Create an AlloyDB instance and a Compute Engine VM instance, and assign and configure your VPC network for both instances.
- Install
pgbenchon the Compute Engine VM instance, run it against the AlloyDB instance, and use it to conduct performance tests against the AlloyDB instance.
Costs
This tutorial uses billable components of Google Cloud, including VPC network, AlloyDB, and Compute Engine. Use the pricing calculator to generate a cost estimate based on your projected usage.
Before you begin
In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.
Make sure that billing is enabled for your Google Cloud project. Learn how to confirm that billing is enabled for your project.
Enable the VPC network, AlloyDB, and Compute Engine APIs.
Make sure that you have the AlloyDB Admin role assigned to your user account.
Create a VPC network
In this section, you create a VPC network for your AlloyDB instance and your Compute Engine VM instance so that communication can pass between the instance and the VM securely.
Go to the VPC networks page in the Google Cloud Console.
Click Create VPC network. The Create a VPC network page appears.
Enter
my-vpc-networkfor the Name of your VPC network.In the Subnet creation mode section, select the Custom option.
Click ADD SUBNET.
Enter
my-subnetfor the Name of your subnet.Select a Region and enter an IP address range for your subnet.
Click DONE.
Click CREATE.
Create an AlloyDB instance
In this section, you create an AlloyDB instance, and assign and configure your VPC network for the instance.
In the Google Cloud console, go to the Clusters page.
Click Create cluster.
Configure your cluster as follows:
In the Cluster ID field of the Basic info section, enter
my-cluster.In the Password field, enter any password you'd like. Take note of this password—you use it again later in this tutorial.
In the Region field of the Location section, select
us-central1 (Iowa).In the Database version field, keep the default value.
From the Network menu, select my-vpc-network. You created this VPC network in Create a VPC network.
Because your VPC network hasn't yet initialized private service access, click Set up connection.
In the Create a private service connection pane that appears:
Select the Use an automatically allocated IP range option to have Google Cloud select an automatically allocated IP range in your network.
Click Continue.
Click Create connection.
Configure your primary instance as follows:
In the Instance ID field, enter an ID for your primary instance.
Under Zonal availability, select one of the following options:
To create a highly available production instance with automated failover, select Multiple zones (Highly available).
To create a basic instance that does not need to be highly available, select Single zone.
Select a machine type.
Click Create cluster to create the cluster and the primary instance together.
Make a note of the private IP address of your AlloyDB instance that appears on the AlloyDB Instances page. You'll need this address to verify the integration between the AlloyDB instance and the Compute Engine VM instance.
Create a Compute Engine VM instance
In this section, you create a Compute Engine VM instance, and assign and configure your VPC network for the instance.
Go to the VM instances page in the Google Cloud Console.
Click Create instance.
In the Name field of the Create an instance page, enter a unique identifier for the Compute Engine VM instance that you're creating. For this tutorial, enter
my-compute-engine-vm-instancein the field.In the Access scopes section of the page, select the Allow full access to all Cloud APIs option.
Expand Advanced options, and then complete the following steps:
Click the Networking subtab.
Click Add a network interface.
From the Network menu, select my-vpc-network. You created this VPC network in Create a VPC network.
Click Done. Two VPC networks appear in the Network interfaces section of the page: default and my-vpc-network
Click delete Delete to delete the default VPC network.
By deleting the default VPC network, you force your Compute Engine VM instance to use the VPC network that you created in this tutorial.
Click Create.
It may take a few seconds for your Compute Engine VM instance to be created. After this happens, the instance appears in the VM instances page, and the status of this instance is Running (as indicated by the check mark).
Verify the integration
In this section, you install pgbench on the Compute Engine VM instance, run it against the AlloyDB instance, and use it to conduct performance tests against the AlloyDB instance.
Connect to your Compute Engine VM instance. To do so, click the SSH menu to the right of your VM instance, and then select Open in browser window.
Install the Contributed Extensions and Additions to PostgreSQL package on your Compute Engine VM instance.
sudo apt-get -y install postgresql-contrib
Initialize a database for
pgbenchon your AlloyDB for PostgreSQL instance. For example, use the defaultpostgresdatabase:pgbench -i --host=INSTANCE_PRIVATE_IP --port=PORT_NUMBER --username=USERNAME
Replace the following:
INSTANCE_PRIVATE_IP: The private IP address of your AlloyDB instance. You made a note of this IP address when you created the instance.PORT_NUMBER: The port number reserved for your AlloyDB instance. For this tutorial, the port number is5432.USERNAME: The administrator username for your AlloyDB instance. For this tutorial, the username ispostgres.
At the
Passwordprompt, enter the password for your AlloyDB instance. When you created the instance, you either supplied a password for it or had AlloyDB generate a password for you.Run
pgbenchwith no arguments to verify basic features.pgbench --host=INSTANCE_PRIVATE_IP --port=PORT_NUMBER --username=USERNAME
At the
Passwordprompt, enter the password for your AlloyDB instance.You should see output similar to the following:
starting vacuum...end. transaction type: <builtin: TPC-B (sort of)> scaling factor: 1 query mode: simple number of clients: 1 number of threads: 1 number of transactions per client: 10 number of transactions actually processed: 10/10 latency average = 3.794 ms tps = 263.560171 (including connections establishing) tps = 345.375423 (excluding connections establishing)
Use some options to make a useful test. Some important options are
-c(the number of clients) and--transactions(the number of transactions per client).For this tutorial, conduct a performance test against
10clients that are accessing your AlloyDB for PostgreSQL database and100transactions for each client.pgbench --host=INSTANCE_PRIVATE_IP --port=PORT_NUMBER --username=USERNAME -c 10 --transactions=100
At the
Passwordprompt, enter the password for your AlloyDB instance.You should see output similar to the following:
starting vacuum...end. transaction type: <builtin: TPC-B (sort of)> scaling factor: 1 query mode: simple number of clients: 10 number of threads: 1 number of transactions per client: 100 number of transactions actually processed: 1000/1000 latency average = 36.421 ms tps = 274.563444 (including connections establishing) tps = 275.733225 (excluding connections establishing)
Clean up
After you've finished this tutorial, you can clean up the resources that you created so they won't take up quota and you won't be billed for them in the future.
To avoid incurring charges to your Google Cloud account for the resources used in this tutorial, follow these steps.
- Use the Google Cloud console to delete your project, AlloyDB instance, Compute Engine VM instance, and VPC network.
The following sections describe how to delete or turn off these resources.
Delete your project
The easiest way to eliminate billing is to delete the project that you created for this tutorial.
Go to the Manage resources page in the Google Cloud Console.
In the project list, select the project that you want to delete, and then click Delete.
In the dialog, type the project ID, and then click Shut down to delete the project.
Delete the AlloyDB instance
Go to the AlloyDB Clusters page in the Google Cloud Console.
Click the name of your AlloyDB instance. For this tutorial, click my-instance.
Click DELETE.
In the dialog, enter the name of your AlloyDB instance in the text field, and then click DELETE.
Delete the Compute Engine VM instance
Go to the VM instances page in the Google Cloud Console.
Click the name of your Compute Engine VM instance. For this tutorial, click my-compute-engine-vm-instance.
Click the Delete icon. This icon resembles a trashcan.
In the dialog, click DELETE.
Delete the VPC network
Go to the VPC networks page in the Google Cloud Console.
Click the name of your VPC network. For this tutorial, click my-vpc-network.
Click DELETE VPC NETWORK.
In the dialog, click DELETE.