Connect to your cluster and access Kubernetes pods

This is the easy, no-frills guide to access your k8s clusters and pods in GCP - GKE.

Google Cloud provides three methods to access and interact with Kubernetes clusters. The first two of them require no previous preparation since they are browser-based, the third one requires installing a set of tooling on your machine:

  • browser-based, visual, click-based interface called Console, available from https://console.cloud.google.com/
  • browser-based Cloud Shell, a turn-key equivalent of a command-line terminal, to save you from meters of mouse-hovering on the Console, with an entry point still from the above mentioned Console, or direct link https://shell.cloud.google.com/?show=terminal .
  • local machine-installed Google SDK, which is used by most DevOps and GCP pros to manage nodes and pods is by installing the Google SDK on the local machine.

Connect to the cluster via Console

On the Google Cloud Console here , go to your cluster(eg the cluster where you installed the Bitpoke App) in the Kubernetes Engine -> Clusters left menu and make sure you have the right project selected:

Connect to cluster button

Click on the three vertical dots, then on the Connect button corresponding to the cluster you want to connect to and choose Open Workloads Dashboard to connect and stay in the Console.

Connect to cluster via console button

Connect to the cluster via Cloud Shell

Follow the same first step as above, but click on the Run in Cloud Shell button instead and you’ll connect to your cluster by using the Google Cloud Shell. This will open the terminal in your browser and automatically fill in the command line displayed below. Make sure to run it afterwards by pressing enter inside the terminal.

Connect to cluster via Cloud Shell

Connect to the cluster from your own computer

You can easily connect to your cluster and run Kubernetes commands from your own terminal. Before using the same command line access displayed above, you need to have installed:

After these are installed and you have entered your Google Cloud credentials, just copy and paste in your terminal the command line above for connecting to your cluster, for example:

gcloud container clusters get-credentials cluster-1 --zone us-central1-c --project mumbai-341413

Access the Kubernetes pods

Once you are connected to the cluster through one of the two command-line options mentioned above, you can access the pods of your clusters.

For example, when you run the Bitpoke App in your GKE cluster, each site runs in its own namespace, which you can see in the Bitpoke App, under Sites - Runtime . In the example presented here the project namespace is proj-f3y745.

To see all the pods that run in a certain namespace run:

kubectl get pod -n <your_namespace>

You will see something like:

NAME                                       READY   STATUS    RESTARTS   AGE
default-mysql-0                            4/4     Running   0          5h22m
prometheus-prom-proj-0                     3/3     Running   0          5h22m
prometheus-prometheus-0                    3/3     Running   1          20h
mysite-bitpoke-ajj0r-6f44fd564d-7wld2    3/3     Running   0          5h22m
mysite-bitpoke-ajj0r-memcached-0         2/2     Running   0          20h

WP-CLI and any files of the site can be accessed by using the WordPress container from the instance’s pod, which in this case is mysite-presslabs-ajj0r-6f44fd564d-7wld2.

To access a pod, simply run:

kubectl exec -it -n <your_namespace> <pod_name> -- bash

Don’t forget to replace <your_namespace> and <pod_name>. For the example above, if you want to access the WordPress pod you’ll run:

kubectl exec -it -n proj-f3y745 mysite-bitpoke-ajj0r-6f44fd564d-7wld2 -- bash

Now you can use WP-CLI commands, e.g. list all installed plugins:

wp plugin list

As a shortcut version, you can also run WP-CLI using the commands from Commands - Run WP-CLI , which already contain your project namespace, so all you have to do is connect to your cluster and run these commands.

If you need to access the database pod, see the commands from Commands - connect to the database .

Read more about kubectl here .