kubectl-helper

Welcome to Kubectl cheatsheet GitHub

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!

Table of Contents

  1. Cluster โŽˆ
  2. Switching Between Contexts โ‡ข
  3. Rollout ๐Ÿ”„
  4. Labels ๐Ÿท๏ธ
  5. Pod ๐Ÿ›ก๏ธ
  6. Nodes ๐Ÿ’ป
  7. Troubleshooting With Logs ๐Ÿ“Š
  8. Secrets Encode & Decode ๐Ÿ•ต๏ธ
  9. Taints & Tolerations ๐Ÿ”ญ
  10. Patch ๐Ÿ› ๏ธ
  11. Set โš™๏ธ
  12. Port Forward โ†”๏ธ
  13. Create Resources ๐Ÿ—๏ธ
  14. Delete Resources ๐Ÿšฎ
  15. Kubernetes Blogs ๐Ÿ“
  16. Contact Information ๐Ÿ“ง
  17. Feedback Welcome ๐ŸŒŸ

1. Cluster

a. Listing all the resources in cluster

  kubectl api-resources

b. Listing all the api versions in cluster

  kubectl api-versions

c. Get the configurations of saved clusters

  kubectl config view

d. Get the Kubernetes version running on the client and server

  kubectl version

e. Get everything from the cluster

  kubectl get all --all-namespaces

f. to Check Access

kubectl auth can-i create deployments
kubectl auth can-i create pods

g. to check IP range for pods within the namespaces

kubectl cluster-info dump | grep -m 1 cluster-cidr

Go to Top โ–ฒ

2. Switching between contexts

a. Get all preconfigured contexts and see which one is active:

  kubectl config get-contexts

b. Get the current config name

  kubectl config current-context

c. Get the current config with more details

  kubectl config view --minify

d. Switch between the predefined contexts(Switch to a context/cluster)

  kubectl config use-context <context-name>

e. Setting default namespace

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

Go to Top โ–ฒ

3. Rollout

The kubectl rollout command is primarily used with Kubernetes Deployments, Statefulsets and DaemonSets.

a. kubectl rollout syntax

  kubectl rollout <COMMAND> 

COMMAND can be one of the following:

  1. status
  2. history
  3. restart
  4. pause
  5. resume
  6. undo

b. To check the rollout status

  kubectl rollout status <resource-type>/<resource-name>

c. To get the rollout history

  kubectl rollout status <resource-type>/<resource-name>

d. To restart the deployment

  kubectl restart status <resource-type>/<resource-name> 

e. To pause the deployment updates

  kubectl rollout pause <resource-type>/<resource-name>

f. To resume the deployment updates

  kubectl rollout resume <resource-type>/<resource-name>

g. To undo the deployment updates to previous revision

  kubectl rollout undo <resource-type>/<resource-name>

[!TIP]
Here you can use Deployments, Statefulsets and DaemonSets in place of .


Go to Top โ–ฒ

4. Labels

a. Adding a label to a specific resource

  kubectl label <resource-type> <resource-name> <label-key>=<label-value>

b. Removing a label to a specific resource

  kubectl label <resource-type> <resource-name> <label-key>-

c. List all the labels from a resource

  kubectl get <resource-type> <resource-name> --show-labels 

d. Overwrite the resource label

  kubectl label --overwrite <resource-type> <resource-name> <label-key>=<label-new-value>  

Go to Top โ–ฒ

5. Pod

a. To list the pods in specific namespace

  kubectl get pods -n <namespace> 

b. To list all pods in all namespaces

  kubectl get pods -A

c. Exec into a pod

bash
  kubectl -n <namespace> exec -it <pod-name> //bin//bash
sh
  kubectl -n <namespace> exec -it <pod-name> //bin//sh

In windows

bash
  winpty kubectl -n <namespace> exec -it <pod-name> //bin//bash
sh
  winpty kubectl -n <namespace> exec -it <pod-name> //bin//sh

d. Watch a pod status

  kubectl -n <namespace> get <pods/deployments>  -w
  kubectl -n <namespace> get <pods/deployments> --watch

e. Print env variables of a pod

  kubectl -n <namespace> exec <pod-name> -- printenv

f. To make a curl to a pod

sh
  kubectl -n <namespace> exec -it <pod-name> -- /bin/sh -c "curl http://example.com"
bash
  kubectl -n <namespace> exec -it <pod-name> -- /bin/bash -c "curl http://example.com"

g. Delete all pods which are evicted with namespace wise

  kubectl get pod -n <namespace> | grep Evicted | awk '{print $1}' | xargs kubectl delete pod -n <namespace>

h. To get the pod definition in YAML format

  kubectl -n <namespace> get pod pod-name -o yaml > pod.yaml

i. To get pod definition possible options

  kubectl explain pods --recursive | less

j. Get the pod deatils with wide options

  kubectl get pods -o wide

k. View detailed information about a pod

  kubectl -n <namespace> describe pod <podname>

l. Create or apply a pod configuration

  kubectl -n <namespace> apply -f <pod-definition.yaml>

m. Delete a pod

Delete a specific pod

  kubectl -n <namespace> delete pod <pod-name>

Delete all pods

  kubectl -n <namespace> delete pods --all

n. Execute a command in a running pod

  kubectl exec -it <pod-name> -- <command> 

o. Copy files to/from a pod

Copy a file from your local machine to a pod

  kubectl -n <namespace> cp <local-file> <pod-name>:<destination-path>

Copy a file from a pod to your local machine

  kubectl -n <namespace> cp <pod-name>:<source-path> <local-destination>

p. Get pod events

  kubectl get events 

q. Get resource usage

  kubectl -n <namespace> top pod

Go to Top โ–ฒ

6. Nodes

a. List nodes

  kubectl get pods 

b. List nodes with the resource usage

  kubectl top node

c. Get node details

  kubectl describe node <nodename>

d. Cordon a node

Mark a node as unschedulable, preventing new pods from being scheduled.

  kubectl cordon node <nodename> 

e. Uncordon a node

Mark a node as schedulable, allowing new pods to be scheduled.

  kubectl uncordon node <nodename>

f. Drain Nodes

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

g. Get the kubelet version for a specific node.

  kubectl get node <nodename> -o jsonpath='{.status.nodeInfo.kubeletVersion}

Go to Top โ–ฒ

7. Streaming With Logs

a. Get logs from a pod

  kubectl -n <namespace> logs <podname>

b. Stream Logs in Real-time

  kubectl -n <namespace> -f logs <podname> 

c. Specify Container in Multi-container Pods

  kubectl -n <namespace> logs <podname> -c <containerName>  

d. Retrieve Previous Container Logs

  kubectl -n <namespace> logs --previous <pod-name>

e. Tail the Logs with a Specific Number of Lines

  kubectl logs <pod-name> --tail=<lines>

f. Filter the logs based on a time window

  kubectl logs --since=<time-period> <pod-name>

[!TIP]
Here you can mention 10s, 10m, 10h, and 10d in place of (Just an exmaple).

g. Add timestamps in the Logs

  kubectl -n <namespace> logs <pod-name> --timestamps  

h. Deployment, Statefulset, Daemonset, and Job logs

  kubectl -n <namespace> logs <resource-type>/<resource-name> 

[!TIP]
Here you can use Deployments, Statefulsets, DaemonSets, and Jobs in place of . ---

Go to Top โ–ฒ

8. certs

a. Encode your secret

  echo -n 'your-secret' | base64

b. Decode your secret

  echo -n 'your-string' | base64 --decode

Go to Top โ–ฒ

9. Taints & Tolerations

a. View Taints on a Node

  kubectl describe node <node-name> | grep Taints

b. Add a Taint to a node

  kubectl taint nodes <nodename> <key>=<value>:<effect>  

[!TIP]
Here you can use NoSchedule, NoExecute, and NoSchedule in place of .

c. Remove a Taint from a Node

  kubectl taint nodes <nodename> <key>-

d. Adding tolerations to a pod YAML

Add the following section to your pod YAML

  tolerations:
  - key: "<key>"
    operator: "Equal"
    value: "<value>"
    effect: "<effect>"

e. Get Toleration in a Running Pod

  kubectl get pod <pod-name> -o=jsonpath='{.spec.tolerations}'

Go to Top โ–ฒ

10 Patch

a. Patch using a file

  kubectl patch <resource <resource-name> --patch-file patch-file.yaml  

patch file looks like:

  spec:
    template:
      spec:
        containers:
        - name: patch-demo-ctr-3
          image: gcr.io/google-samples/node-hello:1.0

c. Patch using a string

  kubectl patch <resource> <resource-name> -p '<pathcing-string>'

Example

  kubectl patch deployment sampledeploy -p '{"spec": {"replicas": 2}}'

Go to Top โ–ฒ

11. Set Command

a. Set a resource with specific option

  kubectl set <resource-type> <resource-name> [options]

Example

  kubectl set deployment sampledeploy --replicas=3  

Go to Top โ–ฒ

12. Port Forward

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.

a. Syntax

kubectl port-forward -n <namespace> <resource-type>/<resource-name> <localhost-port>:<pod-port>

Example

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

Testing

curl -X GET -s http://localhost:80/
curl -X GET -s http://localhost:80/_cluster/health  

Go to Top โ–ฒ

13. Create a Resource

a. Create a resource in imperative way.

kubectl create <resource-type> <resource-name> --PARAMETER1=VALUE1

Example

kubectl create deployment sampledeploy --image=sampleimage

For Pod

kubectl run samplepod --image=sampleimage

b. Create a resource in declarative way

kubectl create -f manifest-file.yaml 

c. Create/update resource

kubectl apply -f manifest-file.yaml

d. Create resources from all manifest files in a directory

kubectl create -f ./directory
kubectl create -f 'URL'

f. Edit and update the resources

kubectl edit <resource-type> <resource-name>

Go to Top โ–ฒ

14. Delete a Resource

a. Delete a resource by name

kubectl -n <namespace> delete <resource-type> <resource-name>

b. Delete a resource using a manifest file

kubectl delete -f manifest-file.yaml

d. Deleting resources with a label selector

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>

Go to Top โ–ฒ

Kubernetes Blogs

1. Exploring Container Types in Kubernetes: Beyond Init and Sidecar Containers.

2. Deploying Nginx on Kubernetes: Exploring Various Methods.

3. Setting Up an Amazon EKS Cluster and Node Group Using eksctl.

4. Jump box setup on eks cluster


Go to Top โ–ฒ

Contact Information

If you have any questions or feedback, feel free to reach out:


Go to Top โ–ฒ

Feedback Welcome!

We welcome your feedback and suggestions! If you encounter any issues or have ideas for improvements, please open an issue on our GitHub repository. ๐Ÿš€