By Anvesh Muppeda & Sai Manasa Kota
๐ Kubectl Helper: Your Ultimate Kubernetes Command Companion! ๐
Master the art of Kubernetes with our feature-packed Kubectl Cheatsheet! This comprehensive guide empowers both beginners and seasoned professionals to navigate the Kubernetes landscape effortlessly. From cluster management and context switching to intricate pod manipulations, weโve got you covered. Say goodbye to command memorization and hello to streamlined operations!
kubectl api-resources
kubectl api-versions
kubectl config view
kubectl version
kubectl get all --all-namespaces
kubectl auth can-i create deployments
kubectl auth can-i create pods
kubectl cluster-info dump | grep -m 1 cluster-cidr
kubectl config get-contexts
kubectl config current-context
kubectl config view --minify
kubectl config use-context <context-name>
The default namespace default is configured in your clusterโs context. To change the default namespace, use the below command. Specify the desired namespace name that you want to set as the default.
kubectl config set-context --current --namespace=<NAMESPACE-NAME>
For example, to set the namespace kube-system as your default, you would run the following command
kubectl config set-context --current --namespace=kube-system
The kubectl rollout
command is primarily used with Kubernetes Deployments, Statefulsets and DaemonSets.
kubectl rollout <COMMAND>
COMMAND can be one of the following:
kubectl rollout status <resource-type>/<resource-name>
kubectl rollout status <resource-type>/<resource-name>
kubectl restart status <resource-type>/<resource-name>
kubectl rollout pause <resource-type>/<resource-name>
kubectl rollout resume <resource-type>/<resource-name>
kubectl rollout undo <resource-type>/<resource-name>
[!TIP]
Here you can use Deployments, Statefulsets and DaemonSets in place of.
kubectl label <resource-type> <resource-name> <label-key>=<label-value>
kubectl label <resource-type> <resource-name> <label-key>-
kubectl get <resource-type> <resource-name> --show-labels
kubectl label --overwrite <resource-type> <resource-name> <label-key>=<label-new-value>
kubectl get pods -n <namespace>
kubectl get pods -A
kubectl -n <namespace> exec -it <pod-name> //bin//bash
kubectl -n <namespace> exec -it <pod-name> //bin//sh
winpty kubectl -n <namespace> exec -it <pod-name> //bin//bash
winpty kubectl -n <namespace> exec -it <pod-name> //bin//sh
kubectl -n <namespace> get <pods/deployments> -w
kubectl -n <namespace> get <pods/deployments> --watch
kubectl -n <namespace> exec <pod-name> -- printenv
kubectl -n <namespace> exec -it <pod-name> -- /bin/sh -c "curl http://example.com"
kubectl -n <namespace> exec -it <pod-name> -- /bin/bash -c "curl http://example.com"
kubectl get pod -n <namespace> | grep Evicted | awk '{print $1}' | xargs kubectl delete pod -n <namespace>
kubectl -n <namespace> get pod pod-name -o yaml > pod.yaml
kubectl explain pods --recursive | less
kubectl get pods -o wide
kubectl -n <namespace> describe pod <podname>
kubectl -n <namespace> apply -f <pod-definition.yaml>
kubectl -n <namespace> delete pod <pod-name>
kubectl -n <namespace> delete pods --all
kubectl exec -it <pod-name> -- <command>
kubectl -n <namespace> cp <local-file> <pod-name>:<destination-path>
kubectl -n <namespace> cp <pod-name>:<source-path> <local-destination>
kubectl get events
kubectl -n <namespace> top pod
kubectl get pods
kubectl top node
kubectl describe node <nodename>
Mark a node as unschedulable, preventing new pods from being scheduled.
kubectl cordon node <nodename>
Mark a node as schedulable, allowing new pods to be scheduled.
kubectl uncordon node <nodename>
Evict pods from a node, moving them to other nodes. The --ignore-daemonsets
flag is used to ignore DaemonSet managed pods.
kubectl drain <nodename> --ignore-daemonsets
kubectl get node <nodename> -o jsonpath='{.status.nodeInfo.kubeletVersion}'
kubectl -n <namespace> logs <podname>
kubectl -n <namespace> -f logs <podname>
kubectl -n <namespace> logs <podname> -c <containerName>
kubectl -n <namespace> logs --previous <pod-name>
kubectl logs <pod-name> --tail=<lines>
kubectl logs --since=<time-period> <pod-name>
[!TIP]
Here you can mention 10s, 10m, 10h, and 10d in place of(Just an exmaple).
kubectl -n <namespace> logs <pod-name> --timestamps
kubectl -n <namespace> logs <resource-type>/<resource-name>
[!TIP]
Here you can use Deployments, Statefulsets, DaemonSets, and Jobs in place of. ---
echo -n 'your-secret' | base64
echo -n 'your-string' | base64 --decode
kubectl describe node <node-name> | grep Taints
kubectl taint nodes <nodename> <key>=<value>:<effect>
[!TIP]
Here you can use NoSchedule, NoExecute, and NoSchedule in place of.
kubectl taint nodes <nodename> <key>-
Add the following section to your pod YAML
tolerations:
- key: "<key>"
operator: "Equal"
value: "<value>"
effect: "<effect>"
kubectl get pod <pod-name> -o=jsonpath='{.spec.tolerations}'
kubectl patch <resource <resource-name> --patch-file patch-file.yaml
spec:
template:
spec:
containers:
- name: patch-demo-ctr-3
image: gcr.io/google-samples/node-hello:1.0
kubectl patch <resource> <resource-name> -p '<pathcing-string>'
kubectl patch deployment sampledeploy -p '{"spec": {"replicas": 2}}'
kubectl set <resource-type> <resource-name> [options]
kubectl set deployment sampledeploy --replicas=3
kubectl port-forward forwards connections to a local port to a port on a pod. Compared to kubectl proxy, kubectl port-forward is more generic as it can forward TCP traffic while kubectl proxy can only forward HTTP traffic.
kubectl port-forward is useful for testing/debugging purposes so you can access your service locally without exposing it.
kubectl port-forward -n <namespace> <resource-type>/<resource-name> <localhost-port>:<pod-port>
kubectl port-forward -n default deploy/sampledeploy 8080:80
Once the connection is succesfull from local port to target resource port, then we can test local connection using curl to the end point or we can access the end point using localhost:8080
curl -X GET -s http://localhost:80/
curl -X GET -s http://localhost:80/_cluster/health
kubectl create <resource-type> <resource-name> --PARAMETER1=VALUE1
kubectl create deployment sampledeploy --image=sampleimage
kubectl run samplepod --image=sampleimage
kubectl create -f manifest-file.yaml
kubectl apply -f manifest-file.yaml
kubectl create -f ./directory
kubectl create -f 'URL'
kubectl edit <resource-type> <resource-name>
kubectl -n <namespace> delete <resource-type> <resource-name>
kubectl delete -f manifest-file.yaml
kubectl delete <resource-type> --selector=<key>=<value>
kubectl delete <resource-type> --selector=<key>
[!CAUTION] PLEASE DOUBLE CHECK BEFORE RUNNING THESE COMMANDS!!
e. Deleting all resources in current namespace
kubectl delete all --all
f. Deleting all resources in specific namespace
kubectl -n <namespace> delete all --all
g. Deleting all resources matching a label selector
kubectl delete all --selector=<key>=<value> kubectl delete all --selector=<key>
If you have any questions or feedback, feel free to reach out:
We welcome your feedback and suggestions! If you encounter any issues or have ideas for improvements, please open an issue on our GitHub repository. ๐