Cloud Shell and its gcloud set of commands can be used to manage GKE together with Cloud Console. The gcloud command can be handy when you want to script and automate GKE-related tasks.
Cloud SDK provides Cloud Client Libraries, allowing you to interact with GKE resources. SDK libraries are available in the following programming languages: Java, Go, Python, Ruby, PHP, C#, C++, and Node.js.
The primary tool for managing Kubernetes clusters is a tool called kubectl. It can be used on Windows, Linux, or macOS operating systems.
To install it on those operating systems, follow this guide: https://kubernetes.io/docs/tasks/tools/.
We are a big fan of Cloud Shell, which can be used directly from the browser, so we will show you how to use kubectl from Cloud Shell.
Here is the command we need to run to install it via the Google Cloud CLI component manager:
gcloud components install kubectl
sudo apt-get install kubectl
Once installed, we can interact with Kubernetes clusters.
Cluster management access configuration
As described in the Kubernetes components section, we interact with clusters using a YAML file called kubeconfig in the $HOME/.kube/config directory.
You can create a cluster using the following command:
gcloud container clusters create my-cluster
Then, kubeconfig is added automatically to the kubeconfig file. Similar to the gcloud commands in Chapter 4, about GCE, gcloud might require a zone parameter —for example, –zone=us-central1-a.
If you operate more than one Kubernetes cluster, you can check which cluster is currently in context and switch between them, like so:
kubectl config current-context
In this case, the current context is empty, so we need to configure it, as follows:
Figure 5.9 – Current kubectl context is not configured
To manage the Kubernetes cluster, we need to retrieve credentials. To receive them, we need to run the following gcloud command to list existing clusters:
gcloud container clusters list
The following screenshot shows us the output of the previous command:
Figure 5.10 – List of existing GKE clusters
We need to retrieve the credentials if we want to manage the cluster.
Run the following command:
gcloud container clusters get-credentials –zone=europe-west4-a CLUSTER_NAME
Once executed, we can finally list cluster components, as follows:
Figure 5.11 – kubectl command output with a listing of cluster resources
To view the current kubectl context, we run the command again:
kubectl config current-context
In this case, the current context was as follows:
gke_wmarusiak-book_europe-west6-a_gke-cluster-2
To switch context to another cluster, we need to run the following command:
kubectl config set-context CONTEXT_NAME
In our case, we ran the following:
kubectl config set-context gke-cluster-1
If we would like to rename the context, we can do this by executing the following command:
kubectl config rename-context OLD_CONTEXT_NAME NEW_CONTEXT_NAME
We used the following command:
kubectl config rename-context gke_wmarusiak-book_europe-west4-a gke-cluster-1 gke-cluster-1
In the next section, we will learn how to create our first GKE cluster using Cloud Shell and the command line.