Skip to content

Instantly share code, notes, and snippets.

@pratheeshrussell
Last active September 21, 2025 03:53
Show Gist options
  • Select an option

  • Save pratheeshrussell/47595f356383dd099431f8bb0115b85d to your computer and use it in GitHub Desktop.

Select an option

Save pratheeshrussell/47595f356383dd099431f8bb0115b85d to your computer and use it in GitHub Desktop.

kubectl and kind commands

This doc is a quick reference for kind + kubectl

Manage local Clusters with kind

Create

This uses the kindest/node image. Which will be pulled when you run the command

kind create cluster --name test-cluster

you 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.2

List

kind get clusters

get kubectl context

kubectl cluster-info --context test-cluster

Delete

kind delete cluster --name test-cluster

Load images in the Cluster

kind load image-1 image-2 --name test-cluster

Export Kind Cluster Kubeconfig

kind get kubeconfig --name my-cluster > kubeconfig.yaml

Kind Ingress

TBD

Kubectl Commands

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

List Contexts

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-namespace

Get Resources

kubectl get pods
kubectl get services
kubectl get deployments
kubectl get namespaces
kubectl get pvc
kubectl get pv

....

to get all resources

kubectl get all

to get resources from all namespaces pass the parameter --all-namespaces

kubectl get pods --all-namespaces
kubectl get all --all-namespaces

Describe Resources

kubectl describe pod <pod-name>
kubectl describe deployment <deployment-name>

Create Resources

kubectl apply -f my-manifest.yaml
kubectl create deployment my-app --image=my-image:tag

To add all resources from a folder

kubectl apply -f ./k8s/

kustomize commands

To build resources from kustomize overlays

kubectl kustomize ./k8s/overlays/dev 

# or
kubectl apply -k ./overlays/dev --dry-run=client -o yaml

To apply and delete resources from kustomize overlays

# create or update
kubectl apply -k ./k8s/overlays/dev

# delete
kubectl delete -k ./k8s/overlays/dev

Check diff

kubectl diff -k ./k8s/overlays/dev

Edit Resources

kubectl edit pod <name>

Patch Resources

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 json

Deployment Rollout

Tip

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=1

it is also possible to pause and resume a deployment

kubectl rollout pause deployment/my-app
kubectl rollout resume deployment/my-app

Create temp pods for debugging

kubectl run -i --tty --rm debug-pgsql --image=postgres:latest --restart=Never --command -- bin/sh

debug command

# 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-processes

To debug a node

kubectl debug node/<node-name> -it --image=nicolaka/netshoot

Delete Resources

kubectl delete pod <pod-name>
kubectl delete deployment <deployment-name>
kubectl delete -f my-manifest.yaml

To delete all resources from a folder

kubectl delete -f ./k8s/

Copy Files

kubectl cp <pod-name>:<src-path> <destination-path>

Logs and Debugging

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> --previous

To exec into a pod

kubectl exec -it <pod-name> -- /bin/sh  # Open shell inside pod

To specify cluster

kubectl exec -it <pod-name> --context test-cluster -- /bin/sh

Port Forwarding

kubectl port-forward <pod-name> 8080:80

Expose a Service

kubectl expose deployment my-app --type=NodePort --port=80

Scale a Deployment

kubectl scale deployment my-app --replicas=3

Namespace Operations

kubectl create namespace my-namespace
kubectl get namespaces
kubectl delete namespace my-namespace

Explain Structure of Resources

kubectl explain pods
kubectl explain services

Get Cluster Information

kubectl cluster-info
kubectl get nodes

Get Events

kubectl get events --sort-by=.metadata.creationTimestamp
kubectl get events --field-selector involvedObject.kind=pod,involvedObject.name=mypod

Metrics

If metrics is enabled. To get resource usage (CPU/Memory) of pods and nodes

kubectl top pod
kubectl top node

View raw metrics

This is the raw metrics API, that is usually scraped by prometheus. You can use kubectl proxy to access it locally

kubectl proxy --port=12500

Then 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

CRD

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"

ArgoCD related commands

kubectl get applications -n argocd
kubectl get appprojects -n argocd

patch finalizers to force delete an application stuck in terminating state

kubectl patch application <app-name> -n argocd -p '{"metadata":{"finalizers":[]}}' --type=merge
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment