2 min read

Kubectl Cheat Sheet

Kubectl Cheat Sheet

Kubectl is the command line interface (CLI) tool for interacting with Kubernetes, the popular open-source container orchestration system. In this cheat sheet, we'll cover the most commonly used commands for managing Kubernetes clusters using kubectl.

Preliminaries

To start using kubectl, you must install it and set up access to your Kubernetes cluster.

1. Installation

Install kubectl using the following command (for MacOS and Linux):

brew install kubernetes-cli

For other operating systems, you can follow the instructions on the official Kubernetes documentation.

2. Bash or ZSH configuration (optional)

Bash

source <(kubectl completion bash) # set up autocomplete in bash into the current shell, bash-completion package should be installed first.

echo "source <(kubectl completion bash)" >> ~/.bashrc # add autocomplete permanently to your bash shell.

Also you can add :

alias k=kubectl
complete -o default -F __start_kubectl k

ZSH

source <(kubectl completion zsh)  # set up autocomplete in zsh into the current shell

echo '[[ $commands[kubectl] ]] && source <(kubectl completion zsh)' >> ~/.zshrc # add autocomplete permanently to your zsh shell

3. Configuring access to your cluster

To configure kubectl to access your cluster, you need to obtain a kubeconfig file which contains the information about your clusters, users, namespaces, and authentication mechanisms. The default location for the file is ~/.kube/config.

Basic kubectl commands

1. Getting information

  • Get information about all pods:
kubectl get pods
  • Get detailed information about a pod:
kubectl describe pod <pod-name>
  • Get information about all services:
kubectl get services
  • Get information about a deployment:
kubectl get deployment <deployment-name>

2. Creating and deleting resources

You can create or delete resources either directly from command line or from a configuration file (usually a .yaml or .json file).

  • Create resource:
kubectl apply -f <file.yaml>
  • Expose deployment to NodePort:
kubectl expose deployment nginx --port=80 --type=NodePort
  • Delete resource:
kubectl delete -f <file.yaml>
  • Dry run:
kubectl run test --image busybox --dry-run=client -o yaml

This last command is pretty useful to generate a yaml skeleton.

3. Updating resources

  • Update a deployment with a new image:
kubectl set image deployment/<deployment-name> <container-name>=<new-image>

4. Interacting with the cluster

  • Execute a command in a specific pod:
kubectl exec -it <pod-name> -- <command>
  • Attach to a running container:
kubectl attach <pod-name> -i
  • Open a shell to a running container:
kubectl exec -it <pod-name> -- /bin/bash
  • Start a temporary pod that dies on exit:
kubectl exec -it <pod-name> -- /bin/bash

5. Debugging

  • Check logs of a pod:
kubectl logs <pod-name>
  • Check logs of a specific container in a pod:
kubectl logs <pod-name> -c <container-name>
  • Show metrics for a given node:
kubectl top node <node-name>
  • Show metrics for a given pod:
kubectl top pod <pod-name>

This cheat sheet is a great starting point for kubectl usage. However, Kubernetes and kubectl offer a lot more features and commands that might be useful for more specific tasks. For more detailed information, be sure to check the official kubectl documentation.