Kubernetes Cheatsheet
Tags: kubernetes, devops, productivity, discuss
Published: 2021-07-22 09:29:29

Kubernetes is a portable, extensible, open-source platform for managing containerized workloads and services, that facilitates both declarative configuration and automation. It has a large, rapidly growing ecosystem. Kubernetes services, support, and tools are widely available. Kubernetes provides you with a framework to run distributed systems resiliently. It takes care of scaling and failover for your application, provides deployment patterns, canary deployments, and more. In this blog post, I will mention Kubernetes commands which we need for most of the use-cases.

I will list down `kubectl` commands in the sections below as a quick reference to work with Kubernetes.

* [List Resources](#list-resources)
* [Create Resources](#create-resources)
* [Update Resources](#update-resources)
* [Display the State of Resources](#display-the-state-of-resources)
* [Delete Resources](#delete-resources)
* [Execute Command](#execute-command)
* [Print Container Logs](#print-container-logs)
* [Modify Kubeconfig Files](#modify-kubeconfig-files)

## List Resources

- Get list of all namespaces
```kubernetes
kubectl get namespaces
```

- Get list of all pods
```kubernetes
kubectl get pods
```

- Get list of all pods with detailed information like IP, Node Name etc...
```kubernetes
kubectl get pods -o wide
```

- Get list of all pods running on a particular node server
```kubernetes
kubectl get pods --field-selector=spec.nodeName=[server-name]
```

- Get list of all replication controllers and services
```kubernetes
kubectl get replicationcontroller,services
```

- Get list of daemonsets
```kubernetes
kubectl get daemonset
```

## Create Resources

- Create a new namespace
```kubernetes
kubectl create namespace [namespace-name]
```

- Create a new namespace from JSON or YAML file.
```kubernetes
kubectl create –f [filename]
```

## Update Resources

To apply or update a resource use the `kubectl apply` command.

- Create a new service with the definition contained in [service-config].yaml
```kubernetes
kubectl apply -f [service-config].yaml
```

- Create a new replication controller with the definition contained in [controller-config].yaml
```kubernetes
kubectl apply -f [controller-config].yaml
```

- Create the objects defined in any .yaml, .yml, or .json file in a directory
```kubernetes
kubectl apply -f [yaml-file/directory-name]
```

- Edit a service config
```kubernetes
kubectl edit svc/[service-name]
```
Above command opens the file in your default editor. To choose another editor, specify it in front of the command:
```kubernetes
KUBE_EDITOR=”[editor-name]” kubectl edit svc/[service-name]
```

## Display the State of Resources

- Get Details about Particular Node
```kubernetes
kubectl describe nodes [node-name]
```

- Get Details about a Particular pod
```kubernetes
kubectl describe pods [pod-name]
```

- Get Details about a Particular pod whose name and type are listed in `pod.json`
```kubernetes
kubectl describe –f pod.json
```

- Get Details about a Particular pod managed by a specific replication controller
```kubernetes
kubectl describe pods [replication-controller-name]
```

- Get Details about all pods
```kubernetes
kubectl describe pods
```

## Delete Resources

- Delete a pod using the name and type mentioned in pod.yaml
```kubernetes
kubectl delete -f pod.yaml
```

- Delete all pods and services with a specific label
```kubernetes
kubectl delete pods,services -l [label-key]=[label-value]
```

- Delete all pods
```kubernetes
kubectl delete pods --all
```

## Execute Command

- Get output from a command run on the first container in a pod
```kubernetes
kubectl exec [pod-name] -- [command]
```

- Get output from a command run on a specific container in a pod
```kubernetes
kubectl exec [pod-name] -c [container-name] -- [command]
```

- Run /bin/bash from a specific pod. The received output comes from the first container
```kubernetes
kubectl exec -ti [pod-name] -- /bin/bash
```

## Print Container Logs

- Print Logs from Pod
```kubernetes
kubectl logs [pod-name]
```

- Stream Logs from Pod
```kubernetes
kubectl logs -f [pod-name]
```

- Tail Logs from Pod (Print last 200 logs from pod)
```kubernetes
kubectl logs --tail=200 [pod-name]
```

## Modify Kubeconfig Files

`kubectl config` command lets you view and modify kubeconfig files.

- Get Current Context
```kubernetes
kubectl config current-context
```

- Set cluster entry in kubeconfig
```kubernetes
kubectl config set-cluster [cluster-name] --server=[server-name]
```

- Unset an entry in kubeconfig
```kubernetes
kubectl config unset [property-name]
```

<hr>

## Thank you for reading
Hope you find these resources useful. If you like what you read and want to see more about system design, microservices, and other technology-related stuff... You can follow me on 

- Twitter - <a href="https://twitter.com/vishnuchi?ref_src=twsrc%5Etfw" class="twitter-follow-button" data-size="large" data-show-count="false">Follow @vishnuchi</a><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
- Subscribe to my weekly newsletter [here](https://www.getrevue.co/profile/vishnuch).