How to deploy on GKE

k8s

Google Kubernetes Engine (GKE) is a highly scalable and fully automated Kubernetes service.

GKE web interface: console.cloud.google.com/compute

Prerequisites

  • A physical or virtual machine running Ubuntu 24.04+

  • Juju 3.6+ installed via snap


Install GKE tooling

Install the Google Cloud command-line tools via snap:

user@host:~$
sudo snap install google-cloud-cli --classic

Install the kubectl CLI tools via snap:

user@host:~$
sudo snap install kubectl --classic

To check they are correctly installed, run

user@host:~$
gcloud --version
Google Cloud SDK 474.0.0
...
user@host:~$
kubectl version --client
Client Version: v1.28.2
Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3

Authenticate

Log in to Google Cloud:

user@host:~$
gcloud auth login

This should open a page in your browser starting with https://accounts.google.com/o/oauth2/... where you can complete the login.

If successful, the command prompt will show:

You are now logged in as [<account>@gmail.com].

Configure project ID

Next, you must associate this installation with GCloud project using “Project ID” from resource-management :

user@host:~$
gcloud config set project <PROJECT_ID>
Updated property [core/project].

Install additional auth plugin

As a last step, install the Debian package google-cloud-sdk-gke-gcloud-auth-plugin using this official Google Cloud documentation .

Create a new GKE cluster

The following command will start three compute engines on Google Cloud and deploy a K8s cluster. You can imagine the compute engines as three physical servers in clouds.

user@host:~$
gcloud container clusters create --zone <region-name>-c $USER-$RANDOM --cluster-version 1.25 --machine-type <compute-engine> --num-nodes=3 --no-enable-autoupgrade

Next, assign your account as an admin of the newly created K8s cluster:

user@host:~$
kubectl create clusterrolebinding cluster-admin-binding-$USER --clusterrole=cluster-admin --user=$(gcloud config get-value core/account)

Bootstrap Juju on GKE

Add a Juju K8s cloud:

user@host:~$
/snap/juju/current/bin/juju add-k8s <k8s-cloud-name> --storage=standard --client

Bootstrap a Juju controller:

user@host:~$
juju bootstrap <controller-name>

See also: Juju | Google GKE and Juju

Deploy charms

Create a Juju model (K8s namespace):

user@host:~$
juju add-model <model-name>

At this stage, Juju is ready to use GKE. Check the list of currently running K8s pods with:

user@host:~$
kubectl get pods -n <model-name>

The following commands deploy PostgreSQL and PgBouncer:

user@host:~$
juju deploy postgresql-k8s --channel 16/stable --trust
user@host:~$
juju deploy pgbouncer-k8s --trust

Display deployment information

To list GKE clusters:

user@host:~$
gcloud container clusters list
>NAME          LOCATION         MASTER_VERSION   MASTER_IP      MACHINE_TYPE      NODE_VERSION     >NUM_NODES  STATUS
>mykola-18187  <region-name>-c  1.25.9-gke.2300  31.210.22.127  <compute-engine>  1.25.9-gke.2300  3          >RUNNING
>taurus-7485   <region-name>-c  1.25.9-gke.2300  142.142.21.25  <compute-engine>  1.25.9-gke.2300  3          >RUNNING

Juju can handle multiple clouds simultaneously. To see a list of clouds with registered credentials on Juju, run:

user@host:~$
juju clouds
>Clouds available on the controller:
>Cloud      Regions  Default       Type
><k8s-cloud-name>  1        <region-name>  k8s
>
>Clouds available on the client:
>Cloud           Regions  Default       Type  Credentials  Source    Description
><k8s-cloud-name>       1        <region-name>  k8s   1            local     A Kubernetes Cluster
>localhost       1        localhost     lxd   1            built-in  LXD Container Hypervisor
>microk8s        0                      k8s   1            built-in  A local Kubernetes context
>

Clean up

Always clean cloud resources that are no longer necessary; they could be costly!

See all controllers in your machine with

user@host:~$
juju controllers
Controller         Model         User   Access     Cloud/Region                Models  Nodes    HA  Version
<controller-name>  <model-name>  admin  superuser  <cloud-name>/<region-name>  1       1      none  3.6.1

The following command will destroy the Juju controller and remove the cloud instance - meaning all your data will be permanently removed:

user@host:~$
juju destroy-controller <controller-name> --destroy-all-models --destroy-storage --force

To delete the Juju cloud, run

user@host:~$
juju remove-cloud <k8s-cloud-name>

To delete GKE clusters, run

user@host:~$
gcloud container clusters list
user@host:~$
gcloud container clusters delete <cluster_name> --zone <region-name>-c

Revoke the Google Cloud user credentials (you should see a confirmation output):

user@host:~$
gcloud auth revoke <account>@gmail.com
>Revoked credentials:
 >- <account>@gmail.com