Install Percona Distribution for PostgreSQL on Google Kubernetes Engine (GKE)¶
Following steps help you install the Operator and use it to manage Percona Distribution for PostgreSQL with the Google Kubernetes Engine. The document assumes some experience with Google Kubernetes Engine (GKE). For more information on GKE, see the Kubernetes Engine Quickstart .
Prerequisites¶
All commands from this installation guide can be run either in the Google Cloud shell or in your local shell.
To use Google Cloud shell, you need nothing but a modern web browser.
If you would like to use your local shell, install the following:
-
gcloud . This tool is part of the Google Cloud SDK. To install it, select your operating system on the official Google Cloud SDK documentation page and then follow the instructions.
-
kubectl . This is the Kubernetes command-line tool you will use to manage and deploy applications. To install the tool, run the following command:
gcloud auth login gcloud components install kubectl
Create and configure the GKE cluster¶
You can configure the settings using the gcloud tool. You can run it either in the Cloud Shell or in your local shell (if you have installed Google Cloud SDK locally on the previous step). The following command creates a cluster named cluster-1:
gcloud container clusters create cluster-1 --project <project ID> --zone us-central1-a --cluster-version 1.35 --machine-type n1-standard-4 --num-nodes=3
Note
You must edit the above command and other command-line statements to replace the <project ID> placeholder with your project ID (see available projects with gcloud projects list command). You may also be required to edit the zone location, which is set to us-central1 in the above example. Other parameters specify that we are creating a cluster with 3 nodes and with machine type of 4 vCPUs.
You may wait a few minutes for the cluster to be generated.
When the process is over, you can see it listed in the Google Cloud console
Select Kubernetes Engine → Clusters in the left menu panel:
Now you should configure the command-line access to your newly created cluster
to make kubectl be able to use it.
In the Google Cloud Console, select your cluster and then click the Connect shown on the above image. You will see the connect statement which configures the command-line access. After you have edited the statement, you may run the command in your local shell:
gcloud container clusters get-credentials cluster-1 --zone us-central1-a --project <project name>
kubectl create clusterrolebinding cluster-admin-binding --clusterrole cluster-admin --user $(gcloud config get-value core/account)
Expected output
clusterrolebinding.rbac.authorization.k8s.io/cluster-admin-binding created
Install the Operator and deploy your PostgreSQL cluster¶
-
First of all, use the following
git clonecommand to download the correct branch of the percona-postgresql-operator repository:git clone -b v3.1.0 https://github.com/percona/percona-postgresql-operator cd percona-postgresql-operator -
Create the Kubernetes namespace for your cluster if needed (for example, let’s name it
postgres-operator):kubectl create namespace postgres-operatorExpected output
namespace/postgres-operator was createdNote
To use different namespace, specify other name instead of
postgres-operatorin the above command, and modify the-n postgres-operatorparameter with it in the following steps. You can also omit this parameter completely to deploy everything in thedefaultnamespace. -
Deploy the Operator using the following command:
kubectl apply --server-side -f deploy/bundle.yaml -n postgres-operatorExpected output
customresourcedefinition.apiextensions.k8s.io/crunchybridgeclusters.postgres-operator.crunchydata.com serverside-applied customresourcedefinition.apiextensions.k8s.io/perconapgbackups.pgv2.percona.com serverside-applied customresourcedefinition.apiextensions.k8s.io/perconapgclusters.pgv2.percona.com serverside-applied customresourcedefinition.apiextensions.k8s.io/perconapgrestores.pgv2.percona.com serverside-applied customresourcedefinition.apiextensions.k8s.io/perconapgupgrades.pgv2.percona.com serverside-applied customresourcedefinition.apiextensions.k8s.io/pgadmins.postgres-operator.crunchydata.com serverside-applied customresourcedefinition.apiextensions.k8s.io/pgupgrades.postgres-operator.crunchydata.com serverside-applied customresourcedefinition.apiextensions.k8s.io/postgresclusters.postgres-operator.crunchydata.com serverside-applied serviceaccount/percona-postgresql-operator serverside-applied role.rbac.authorization.k8s.io/percona-postgresql-operator serverside-applied rolebinding.rbac.authorization.k8s.io/service-account-percona-postgresql-operator serverside-applied deployment.apps/percona-postgresql-operator serverside-appliedAs the result you will have the Operator Pod up and running.
-
Deploy Percona Distribution for PostgreSQL:
kubectl apply -f deploy/cr.yaml -n postgres-operatorExpected output
perconapgcluster.pgv2.percona.com/cluster1 createdThe creation process may take some time. When the process is over your cluster will obtain the
readystatus. You can check it with the following command:kubectl get pg -n postgres-operatorExpected output
NAME ENDPOINT STATUS POSTGRES PGBOUNCER AGE cluster1 cluster1-pgbouncer.default.svc ready 3 3 30m
Verifying the cluster operation¶
When creation process is over, kubectl get pg -n <namespace> command will show you the
cluster status as ready, and you can try to connect to the cluster.
When the Operator deploys a database cluster, it generates several Secrets . Among them there is the Secret with the credentials of the default PostgreSQL user. This default user has the same username as the cluster name.
-
Use
kubectl get secrets -n <namespace>command to see the list of Secrets objects. The Secrets object you are interested in is named in the format<cluster_name>-pguser-<cluster_name>(where the<cluster_name>is the name of your Percona Distribution for PostgreSQL Cluster). For example, if your cluster name iscluster1, the Secret name will becluster1-pguser-cluster1. -
Use the following command to get the password of this user. Replace the
<cluster_name>and<namespace>placeholders with your values:kubectl get secret <cluster_name>-<user_name>-<cluster_name> -n <namespace> --template='{{.data.password | base64decode}}{{"\n"}}' -
To connect to PostgreSQL, you will use the
pgbouncerservice as the entry point to your cluster. To find this service, use the following command:kubectl get svc -n <namespace>Look for the service named
<cluster-name>-pgbouncer.Sample output
cluster1-ha ClusterIP 34.118.234.155 <none> 5432/TCP 51m cluster1-ha-config ClusterIP None <none> <none> 51m cluster1-pgbouncer ClusterIP 34.118.239.5 <none> 5432/TCP 51m cluster1-pods ClusterIP None <none> <none> 51m cluster1-primary ClusterIP None <none> 5432/TCP 51m cluster1-replicas ClusterIP 34.118.234.19 <none> 5432/TCP 51m -
Create a pod and start Percona Distribution for PostgreSQL inside. The following command will do this, naming the new Pod
pg-client:kubectl run -n <namespace> -i --rm --tty pg-client --image=percona/percona-distribution-postgresql:18.6.1-3 --restart=Never -- bash -ilIt may require some time to execute the command and deploy the corresponding Pod.
-
Run a container with
psqltool and connect its console output to your terminal. Substitute the<namespace>placeholder with your value in the following command to connect as acluster1user to thecluster1database via the PostgreSQL interactive terminal.[postgres@pg-client /]$ PGPASSWORD='pguser_password' psql -h cluster1-pgbouncer.<namespace>.svc.cluster.local -p 5432 -U cluster1 cluster1Sample output
psql (18.6.1-3) SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off) Type "help" for help. pgdb=>
Removing the cluster¶
If you need to delete the Operator and PostgreSQL cluster (for example, to clean up the testing deployment before adopting it for production use), check this HowTo.
Also, there are several ways that you can delete your Kubernetes cluster in GKE.
You can clean up the cluster with the gcloud command as follows:
gcloud container clusters delete <cluster name> --zone us-central1-a --project <project ID>
The return statement requests your confirmation of the deletion. Type y to confirm.
Also, you can delete your cluster via the Google Cloud console
Just click the Delete popup menu item in the clusters list:
The cluster deletion may take time.
Warning
After deleting the cluster, all data stored in it will be lost!
Created: July 12, 2022