Kubernetes cheat sheet
Published: 2022-08-21 14:36:10

Learning Kubernetes can be a daunting task at first for any programmer. And like any other valuable skill worth learning, mastery of Kubernetes will test both your grit and resolve. The name itself is a mouthful with a little bit of jargon thrown around like pods and containers. Hopefully this quick read will help to demystify the mystery that is Kubernetes.

## So what is Kubernetes?

Kubernetes is is a container orchestration tool that was originally developed at Google for internal use within the company. They later open sourced it and now everyone including you and me can use it to manage containers. Most cloud providers not to mention Microsoft, Amazon and even Google, offer a managed Kubernetes service.

## Do we even need containers?

To answer this question first allow me to give you a brief history lesson on the evolution of how we deployed applications in the past. In the very beginning applications were designed to run on specific hardware. This meant that applications designed for Linux machines could not run on windows servers. Also, there was no way of allocating compute resources to multiple applications running on the same server. Organizations used to have specific servers for running databases, serving application content and storing backups. As you can imagine this was very expensive. Not to mention that once resources were provisioned for your application, once the quota was exceeded, your application crashed. Getting it back up would involve liaising with your server admin to restore functions. Managing dependencies was a nightmare as upgrading one application's dependencies would often break other applications using the same dependencies. There was need for a logical abstraction layer between the operating system layer and the application layer. Fast forward to the dawn of virtualization and hypervisors made it easier to not only share resources between multiple applications hosted on the same server, but also the underlying infrastructure was obfuscated. This made it possible to deploy applications to the same server without necessarily designing for multiple operating systems. 
One problem remained. Starting up virtual machine instances took time and they were bulky. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ao49zb7c30aq7yud1x6t.PNG)

> Enter containers📦

Containers make it possible to package applications and the dependencies they require at runtime. This made running applications more predictable since now very little emphasis was placed on the underlying operating system. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n76wbe0h8zufackdxf16.PNG)

Containers make use of container runtimes with some of the more popular ones being Docker, Rocket and containerd. Containers alleviate most of the problems of virtualization since they are scalable, lightweight, more efficient and portable.

> So why Kubernetes?
Containers come with their fair share of challenges. How do you scale containers, how do you make sure they know how to communicate with each other and how do you detect failing instances. These are some of the challenges Kubernetes seeks to solve. According to their site Kubernetes describes itself as an open-source system for automating deployment, scaling, and management of containerized applications.

## The cheat sheet
Common kubectl commands:

To view the configuration and print out the contents of the kubeconfig file
```
kubectl config view
```
To print out some details for all the cluster contexts in the kubeconfig file

```
kubectl config get-contexts
```
To print out the cluster information for the active context

```
kubectl cluster-info
```
To print out the active context

```
kubectl config current-context
```
To change the active context

```
kubectl config use-context <new_context>
```
To get running nodes

```
kubectl get ns
```
To view resource utilization across the nodes of the cluster

```
kubectl top nodes
```
To create a deployment from a YAML file

```
kubectl apply -f ./<filename>.yml
```
To stop a running container

```
kubectl scale --replicas=0 deployment/<name of deployment>
```
To deploy an nginx pod and rename it to nginx-1

```
kubectl create deployment --image nginx nginx-1
```
To check the running deployments

```
kubectl get deploy
```
To see running pods

```
kubectl get po
```
To get a wide view of the pods deployed

```
kubectl get pods -o wide
```
To view the complete details of the pod you just created

```
kubectl describe pod [pod_name]
```
Copying files into a pod

```
kubectl cp ~/test.html [pod_name]:/usr/share/nginx/html/<filename>.html
```
Creating a load balancing service to expose pods externally

```
kubectl expose pod $my_nginx_pod --port 80 --type LoadBalancer
```
To view replicasets

```
kubectl get replicasets
```
To see the IP addresses of nodes

```
kubectl get nodes -o jsonpath='{.items[*].spec.podCIDR}'
```
To get into a pod

```
kubectl exec -it [pod-name] bash
```
To check running services

```
kubectl get svc
```
Generating a password for MYSQL when working with Kubernetes

```
kubectl create secret generic mysql-pass --from-literal=password=Password123
```
To display the logs and to stream new logs as they arrive (and also include timestamps for the running pod

```
kubectl logs [pod_name] -f --timestamps
```
To retrieve the log file from the pod that ran a job

```
kubectl logs [POD-NAME]
```