Kubectl Cheatsheet

Kubectl

Kubectl is a command line tool for communicating with a Kubernetes ([[kubernetes]]) cluster's control pane, using the Kubernetes API.

Documentation: Kubectl Reference


Installation

On Windows (PowerShell)

Install Kubectl with Chocolatey ([[chocolatey]]):

choco install kubernetes-cli

On Linux

[!INFO] Installing on WSL2 On WSL2 it's recommended to install Docker Desktop [[docker-desktop]], which automatically comes with kubectl.

  1. Download the latest release
1curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"  
  1. Install Kubectl
1sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl

On mac OS

Install Kubectl with Homebrew ([[homebrew]]):

1brew install kubernetes-cli

Config Management

Multiple Config Files

On Windows (PowerShell

1$env:KUBECONFIG = "$HOME/.kube/prod-k8s-clcreative-kubeconfig.yaml;$HOME/.kube/infra-home-kube-prod-1.yml;$HOME/.kube/infra-home-kube-demo-1.yml;$HOME/.kube/infra-cloud-kube-prod-1.yml"

On Linux

1export KUBECONFIG=~/.kube/kube-config-1.yml:~/.kube/ube-config-2.yml

Commands

Networking

Connect containers using Kubernetes internal DNS system: <service-name>.<namespace>.svc.cluster.local

Troubleshoot Networking with a netshoot toolkit Container: kubectl run tmp-shell --rm -i --tty --image nicolaka/netshoot -- /bin/bash

Containers

Restart Deployments (Stops and Restarts all Pods): kubectl scale deploy <deployment> --replicas=0 kubectl scale deploy <deployment> --replicas=1

Executing Commands on Pods: kubectl exec -it <PODNAME> -- <COMMAND> kubectl exec -it generic-pod -- /bin/bash

Config and Cluster Management

COMMANDDESCRIPTION
kubectl cluster-infoDisplay endpoint information about the master and services in the cluster
kubectl config viewGet the configuration of the cluster

Resource Management

COMMANDDESCRIPTION
kubectl get all --all-namespacesList all resources in the entire Cluster
kubectl delete <RESOURCE> <RESOURCENAME> --grace-period=0 --forceTry to force the deletion of the resource

List of Kubernetes Resources "Short Names"

Short NameLong Name
csrcertificatesigningrequests
cscomponentstatuses
cmconfigmaps
dsdaemonsets
deploydeployments
ependpoints
evevents
hpahorizontalpodautoscalers
ingingresses
limitslimitranges
nsnamespaces
nonodes
pvcpersistentvolumeclaims
pvpersistentvolumes
popods
pdbpoddisruptionbudgets
psppodsecuritypolicies
rsreplicasets
rcreplicationcontrollers
quotaresourcequotas
saserviceaccounts
svcservices

陼 Logs and Troubleshooting

...

Logs

...

MySQL

kubectl run -it --rm --image=mysql:5.7 --restart=Never mysql-client -- mysql -u USERNAME -h HOSTNAME -p

Networking

kubectl run -it --rm --image=nicolaka/netshoot netshoot -- /bin/bash


Resources stuck in Terminating state

...

Namespaces

  1. Save the namespace in a YOUR-NAMESPACE.json file.
kubectl get ns YOUR-NAMESPACE -o json > YOUR-NAMESPACE.json
  1. Edit the YOUR-NAMESPACE.json and remove the section in Finalizers.

  2. Update the NAMESPACE.

kubectl replace --raw "/api/v1/namespaces/YOUR-NAMESPACE/finalize" -f .\YOUR-NAMESACE.json