DEV Community

Cover image for Kubernetes cheatsheet
Mohammad Jawad (Kasir) Barati
Mohammad Jawad (Kasir) Barati

Posted on Edited on

Kubernetes cheatsheet

Manage Pods

In Kubernetes we can have clusters, if we run these commands they will target the default cluster. For more info about clusters you can look at cluster section

kubectl get pods -A                 # all namespaces
kubectl get pods -n <namespace>     # find the pods by their namespace, e.g. it can be smart-novel
kubectl get pods -A -o wide         # with node and IP
kubectl get pods -A -w              # watch for changes
Enter fullscreen mode Exit fullscreen mode

Cluster

A Kubernetes cluster is a set of machines/nodes that run your containerized workloads, managed as one unit. It has two parts:

  • Control plane: the API server, scheduler, and etcd. It stores the desired state and decides which pod runs where.
  • Worker nodes: the machines that run the pods, each with a kubelet and a container runtime.

You describe what you want (deployments, services), and the cluster keeps reality matching it.

kind ("Kubernetes IN Docker") creates a real cluster on your machine in which each node is a Docker container instead of a VM or physical server.

kind get clusters                                # Lists clusters
kubectl config get-contexts                      # Return the current cluster which receive your kubectl commands
kubectl config use-context kind-<cluster-name>   # Select the desired cluster
Enter fullscreen mode Exit fullscreen mode

Note

The cluster name comes from kind get clusters.

And in case you do not wanna change your context:

kubectl --context kind-<cluster-name> get pods -A
Enter fullscreen mode Exit fullscreen mode

Important

kubectl reads contexts from ~/.kube/config, and if you have minikube context it will be there too. But e.g. in smart-novel when you have a Terraform which start the app and project's start script writes the kind cluster's kubeconfig to its own file, so the kind-smart-novel context never reached your default config. kind get clusters still lists the cluster because kind finds clusters through Docker, not through kubeconfig.

The fix is to point kubectl at that file with --kubeconfig infra/terraform/envs/local/smart-novel-config or export KUBECONFIG=.... The kind- prefix added to the EKS context name was a separate mistake, since only kind-created contexts have it:

kubectl --kubeconfig infra/terraform/envs/local/smart-novel-config get pods -A

Look at the Logs

export KUBECONFIG=infra/terraform/envs/local/smart-novel-config  # you can have this if you have a kind cluster

kubectl logs <pod> -n <namespace>                                # print logs of a
kubectl logs -f <pod> -n <namespace>                             # follow (stream)
kubectl logs --tail=100 <pod> -n <namespace>                     # last 100 lines
kubectl logs --since=10m <pod> -n <namespace>                    # last 10 minutes
kubectl logs -p <pod> -n <namespace>                             # previous container (after a crash)
kubectl logs <pod> -c <container> -n <namespace>                 # specific container in a multi-container pod
Enter fullscreen mode Exit fullscreen mode

Important

If you have a namespace you must pass it.


Top comments (1)

Collapse
 
maame-codes profile image
Maame Afua A. P. Fordjour •

This is really useful for someone learning Kubernetes. I like that you explain what a cluster is and not just the commands. Saving this one!