This doc is a quick reference for kind + kubectl
This uses the kindest/node image. Which will be pulled when you run the command
kind create cluster --name test-clusteryou can also specify the image if you have it downloaded already or using a custom image
kind create cluster --name test-cluster --image kindest/node:v1.31.2kind get clustersget kubectl context
kubectl cluster-info --context test-clusterkind delete cluster --name test-clusterkind load image-1 image-2 --name test-clusterkind get kubeconfig --name my-cluster > kubeconfig.yamlTBD
You can specify namespace by adding the parameter -n test-name to commands dealing with resources that are namespaced
You can specify cluster by adding the parameter --context test-cluster to most of the below commands
kubectl config get-contexts
kubectl config current-context
# to set a default context
kubectl config use-context <context-name>
# to set a default namespace for a specific context
kubectl config set-context <context-name> --namespace=<namespace>
# to set a default namespace for the current context
kubectl config set-context --current --namespace=my-namespacekubectl get pods
kubectl get services
kubectl get deployments
kubectl get namespaces
kubectl get pvc
kubectl get pv
....to get all resources
kubectl get allto get resources from all namespaces pass the parameter --all-namespaces
kubectl get pods --all-namespaces
kubectl get all --all-namespaceskubectl describe pod <pod-name>
kubectl describe deployment <deployment-name>kubectl apply -f my-manifest.yaml
kubectl create deployment my-app --image=my-image:tagTo add all resources from a folder
kubectl apply -f ./k8s/To build resources from kustomize overlays
kubectl kustomize ./k8s/overlays/dev
# or
kubectl apply -k ./overlays/dev --dry-run=client -o yamlTo apply and delete resources from kustomize overlays
# create or update
kubectl apply -k ./k8s/overlays/dev
# delete
kubectl delete -k ./k8s/overlays/devCheck diff
kubectl diff -k ./k8s/overlays/devkubectl edit pod <name>kubectl patch deployment my-app -p '{"spec":{"replicas":3}}'NOTE: You can get the current spec in json format using
kubectl get deployment my-app -o jsonTip
use the annotation kubernetes.io/change-cause
kubectl rollout status deployment/my-app
kubectl rollout history deployment/my-app
kubectl rollout undo deployment/my-app
kubectl rollout undo deployment/my-app --to-revision=1it is also possible to pause and resume a deployment
kubectl rollout pause deployment/my-app
kubectl rollout resume deployment/my-appkubectl run -i --tty --rm debug-pgsql --image=postgres:latest --restart=Never --command -- bin/sh# to create a debugging container in an existing pod
kubectl debug -it mypod --image=nicolaka/netshoot
# to create a copy of an existing pod with debugging tools
kubectl debug mypod -it --image=nicolaka/netshoot --copy-to=debug-mypod --share-processesTo debug a node
kubectl debug node/<node-name> -it --image=nicolaka/netshootkubectl delete pod <pod-name>
kubectl delete deployment <deployment-name>
kubectl delete -f my-manifest.yamlTo delete all resources from a folder
kubectl delete -f ./k8s/kubectl cp <pod-name>:<src-path> <destination-path>kubectl logs <pod-name>
kubectl logs <pod-name> -c <container-name> # in case of multiple containers in pod
kubectl logs <pod-name> -f # Follow logs
# If pod had crashed and restarted
kubectl logs <pod-name> --previousTo exec into a pod
kubectl exec -it <pod-name> -- /bin/sh # Open shell inside podTo specify cluster
kubectl exec -it <pod-name> --context test-cluster -- /bin/shkubectl port-forward <pod-name> 8080:80kubectl expose deployment my-app --type=NodePort --port=80kubectl scale deployment my-app --replicas=3kubectl create namespace my-namespace
kubectl get namespaces
kubectl delete namespace my-namespacekubectl explain pods
kubectl explain serviceskubectl cluster-info
kubectl get nodeskubectl get events --sort-by=.metadata.creationTimestamp
kubectl get events --field-selector involvedObject.kind=pod,involvedObject.name=mypodIf metrics is enabled. To get resource usage (CPU/Memory) of pods and nodes
kubectl top pod
kubectl top nodeThis is the raw metrics API, that is usually scraped by prometheus. You can use kubectl proxy to access it locally
kubectl proxy --port=12500Then access the metrics endpoint in your browser or using curl
http://localhost:12500/metrics
http://127.0.0.1:12500/api/v1/nodes/<node-name>/proxy/metrics/cadvisor
To list CRDs
kubectl get crds
# better option to list CRDs with more details
kubectl get crds -o custom-columns="NAME:.spec.names.plural,SHORTNAMES:.spec.names.shortNames,SCOPE:.spec.scope"kubectl get applications -n argocd
kubectl get appprojects -n argocdpatch finalizers to force delete an application stuck in terminating state
kubectl patch application <app-name> -n argocd -p '{"metadata":{"finalizers":[]}}' --type=merge