DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 967,611 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for A First look at kubectl in Google Kubernetes Engine
Azeez Lukman
Azeez Lukman

Posted on

A First look at kubectl in Google Kubernetes Engine

kubectl is a command-line utility used by administrators to control Kubernetes clusters. Behind the scenes, it transforms the command line entries into API calls.

Kubectl can be thought of as a command-line tool for administering the internal state of an existing cluster. But one thing to note is that kubectl can't create new clusters or change existing ones. For this, you use the gcloud command-line utility.

Another way to describe kubectl is as a client for the Kubernetes API. The main job of kubectl is to carry out HTTP requests to the Kubernetes API

Prerequisites

The following steps assume that you have some experience architecting with Kubernetes Engine

Configure Google Kubernetes Engine Cluster

You need to have your cluster configured with some tool. We would be using gcloud as I mentioned earlier

The gcloud command-line utility and kubectl are both installed in the cloud shell by default

Firstly, create a GKE cluster by running the following command

gcloud container clusters create my-cluster
Creating my-cluster...done
Fetching cluster endpoint and auth data.
kubeconfig entry generated for my-cluster
Enter fullscreen mode Exit fullscreen mode

This command creates a cluster using the gcloud utility, and names the cluster my-cluster. It also generates a kubeconfig entry generated for my-cluster.

After the cluster is configured and created, the next step is to connect kubectl to the Kubernetes engine cluster and authorize kubectl on the cluster.

kubectl requires credentials before it can be used at all, so it must be configured on the cluster with the proper credentials.

When you access a cluster using kubectl, GKE uses the kubeconfig.json file generated on the cluster for authentication. The file contains user information, based on which GKE determines which Kubernetes resources can be accessed by kubectl. The permissions recorded in a kubeconfig.json file vary from user to user.

Generate aΒ kubeconfigΒ entry by running the following command:

gcloud container clusters get-credentials my-app
Enter fullscreen mode Exit fullscreen mode

This command writes the configuration info into a config file and is placed in the .kube dir in the home dir.

Using kubectl on google cloud shell

Kubectl is no ready for use on your cluster. kubectl is installed on google cloud shell out of the box.

Cloud Shell is a personal hosted Virtual Machine that comes pre-loaded with developer tools for Google Cloud products. It usually comes with a built-in code editor, persistent disk storage, and web preview functionality.

You can also install kubectl on your local machine, learn more about it here. This article focuses on utilizing the cloud shell to use kubectl with GKE.

The could shell can be accessed from the top right in the cloud console, open it.

sCC_F1_launch_Cloud_Shell_desktop.png

Sending instructions to kubectl follows a declarative approach where you simply tell it the type of command you want to perform and where to perform that task on followed by some optional flags to enhance some commands.

The kubectl syntax typically follows a form of

kubectl [command] [TYPE] [NAME] [flags]
Enter fullscreen mode Exit fullscreen mode

To illustrate this, let's see an example

kubectl get pods my-pod -o yaml
Enter fullscreen mode Exit fullscreen mode

which will make a call to the Kubernetes API to with get as the command, my-pod is the name of the pod I want to get, my-pod is a pod and output the configuration of a pod named my-pod in YAML format.

Thank you for reading, I'm Azeez Lukman and here's a developer's journey building something awesome every day. Please let's meet on Twitter, LinkedIn and GitHub and anywhere else @robogeeek95

Learn More

Top comments (0)

🌚 Friends don't let friends browse without dark mode.

Sorry, it's true.