jjbuchan / docs

0 stars 0 forks source link

Kubernetes Onboard #21

Open jjbuchan opened 3 years ago

jjbuchan commented 3 years ago

This onboarding guide provides information about Kubernetes. Depending on your role or task you may find that your need for Kubernetes will fluctuate.

The official Kubernetes documentation is quite helpful; however, it can be overwhelming. We recommend glancing through the Concepts and Tasks sections. Furthermore, some useful sub-sections are:

Docker Desktop

Docker Desktop includes the ability to start a single-node Kubernetes cluster and provides the kubectl client. (The provided kubectl can also be used with other Kubernetes clusters.) This Docker Desktop deployment gives you an isolated sandbox to try out Kubernetes concepts and test most aspects of deployments, so it is highly recommended as a first step before interacting with other clusters.

Docker Desktop's Kubernetes includes some convenient features, such as

The following screenshot shows how Kubernetes can be enabled from the Docker preferences:

image

If you are using an Ubuntu workstation, then MicroK8s is recommended as a similar solution.

Managed Kubernetes in GCP

Similar to other managed Kubernetes types, GKE takes care of mapping Kubernetes concepts to native Google Cloud constructs. Separate GKE clusters have been configured for development, staging, and production deployments.

The following diagram conveys general GKE concepts with annotations highlighting how these correspond to usage in Salus:

image

Kubernetes client config

kubectl is the command-line client that you run on your workstation to interact with Kubernetes clusters. If you don't already have kubectl, such as the one provided by Docker Desktop, you can use this documentation to install kubectl.

WARNING kubectl makes it surprisingly easy to access both a local Docker Desktop cluster and any number of remote clusters in GKE. Please double check your current context using kubectl config current-context before executing deployment-altering commands.

To connect kubectl to an existing GKE cluster, you will first need to install the gcloud tool. After installation, you may need to run gcloud init to perform initial authentication.

With gcloud installed, it is recommended to use the Google Cloud web UI momentarily to locate the gcloud command to use for accessing an existing cluster.

Access the Kubernetes clusters as shown here:

image

Locate the desired cluster and click the "Connect" button shown here:

image

From the pop-up window, click on the copy icon to grab the gcloud command to use:

image

Run the copied command on your workstation to configure a new "kubeconfig" context for that cluster. The default context name is fairly long and ugly, so you can always rename it using kubectl config rename-context.

The Google Cloud web UI is also quite powerful. It includes the ability to inspect deployments, view logs, scale replicas, and much more. Give it a try since you might find some tasks are easier in the web UI rather than with kubectl.

Common tasks

In general, the official kubectl cheatsheet is a great resource to keep handy; however, the following describes a few specific tasks that we have needed.

kubectl is fairly self-documenting with the option to add -h at any level. The documentation of sub-commands even includes a few examples.

To get the current list of pods:

kubectl get pod

To watch the state of pods, such as after scaling or restart, add -w:

kubectl get pod -w

To view and follow the logs of a pod's container:

kubectl logs -f POD_NAME

If a pod has been running for a while, you can start the logs from a relative time offset by adding --since=DURATION, such as --since=1m. If the pod has more than one container, then you will need to add -c CONTAINER_NAME.

To scale (up or down) the replicas of a workload, where TYPE is usually "deployment" or "statefulset"

kubectl scale --replicas=COUNT TYPE/WORKLOAD_NAME

Restart all replicas of a workload:

kubectl rollout restart TYPE/WORKLOAD_NAME

Execute a bash shell inside of a container (if bash is installed):

kubectl exec -it POD_NAME CONTAINER_NAME -- bash

Similarly, any executable installed inside the container can be executed with kubectl exec.

To view the current kubeconfig context:

kubectl config current-context

To switch contexts:

kubectl config use-context CONTEXT_NAME

Speaking of switching contexts, krew can be used to easily install plugins into kubectl. With that you can install the ctx plugin. That tool is also available to use stand-alone. With either approach install fzf to access an interactive mode.