DEV Community

Cover image for Kubernetes cheat sheet
Charles Motaroki
Charles Motaroki

Posted on

Kubernetes cheat sheet

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

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

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
Enter fullscreen mode Exit fullscreen mode

To print out some details for all the cluster contexts in the kubeconfig file

kubectl config get-contexts
Enter fullscreen mode Exit fullscreen mode

To print out the cluster information for the active context

kubectl cluster-info
Enter fullscreen mode Exit fullscreen mode

To print out the active context

kubectl config current-context
Enter fullscreen mode Exit fullscreen mode

To change the active context

kubectl config use-context <new_context>
Enter fullscreen mode Exit fullscreen mode

To get running nodes

kubectl get ns
Enter fullscreen mode Exit fullscreen mode

To view resource utilization across the nodes of the cluster

kubectl top nodes
Enter fullscreen mode Exit fullscreen mode

To create a deployment from a YAML file

kubectl apply -f ./<filename>.yml
Enter fullscreen mode Exit fullscreen mode

To stop a running container

kubectl scale --replicas=0 deployment/<name of deployment>
Enter fullscreen mode Exit fullscreen mode

To deploy an nginx pod and rename it to nginx-1

kubectl create deployment --image nginx nginx-1
Enter fullscreen mode Exit fullscreen mode

To check the running deployments

kubectl get deploy
Enter fullscreen mode Exit fullscreen mode

To see running pods

kubectl get po
Enter fullscreen mode Exit fullscreen mode

To get a wide view of the pods deployed

kubectl get pods -o wide
Enter fullscreen mode Exit fullscreen mode

To view the complete details of the pod you just created

kubectl describe pod [pod_name]
Enter fullscreen mode Exit fullscreen mode

Copying files into a pod

kubectl cp ~/test.html [pod_name]:/usr/share/nginx/html/<filename>.html
Enter fullscreen mode Exit fullscreen mode

Creating a load balancing service to expose pods externally

kubectl expose pod $my_nginx_pod --port 80 --type LoadBalancer
Enter fullscreen mode Exit fullscreen mode

To view replicasets

kubectl get replicasets
Enter fullscreen mode Exit fullscreen mode

To see the IP addresses of nodes

kubectl get nodes -o jsonpath='{.items[*].spec.podCIDR}'
Enter fullscreen mode Exit fullscreen mode

To get into a pod

kubectl exec -it [pod-name] bash
Enter fullscreen mode Exit fullscreen mode

To check running services

kubectl get svc
Enter fullscreen mode Exit fullscreen mode

Generating a password for MYSQL when working with Kubernetes

kubectl create secret generic mysql-pass --from-literal=password=Password123
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

To retrieve the log file from the pod that ran a job

kubectl logs [POD-NAME]
Enter fullscreen mode Exit fullscreen mode

Top comments (0)