Wednesday, February 28, 2024

Kubernetes Essentials: A Handy Reference for DevOps Engineers

 This Kubernetes cheatsheet provides a comprehensive reference for managing Kubernetes clusters and applications efficiently. It includes essential commands for beginners and experienced users. From basic cluster information to advanced deployment strategies and observability tools, this cheatsheet covers everything you need to navigate Kubernetes effectively. Whether you're deploying applications, troubleshooting issues, or optimizing resource usage, this cheatsheet is your go-to resource for streamlining Kubernetes operations.

 Cluster Management:

Contexts:

  • kubectl config get-contexts: Lists available contexts.
  • kubectl config current-context: Displays the current context.
  • kubectl config use-context context-name: Sets the current context to the specified one.
  • kubectl config set-context --current --namespace=<namespace-name>: Sets the default namespace for the current context.

Node Information:

  • kubectl get nodes: Lists all nodes in the cluster.
  • kubectl describe node <node-ip> | grep MemoryPressure: Displays memory pressure for a specific node.
  • kubectl top nodes: Shows current resource usage for all nodes in the cluster.
  • kubectl drain <node-name>: Drains a node for maintenance, evicting pods gracefully.

Namespaces and Resources:

  • kubectl create namespace <namespace-name>: Creates a new namespace.
  • kubectl get pods --all-namespaces: Lists pods in all namespaces.
  • kubectl describe namespace <namespace-name>: Describes details of a namespace.
  • kubectl delete namespace <namespace-name>: Deletes a namespace and all its resources.


Pod Management:

Pods:

  • kubectl get pods --namespace=<namespace-name> -o wide|json|yaml: Lists pods in a specific namespace with additional details.
  • kubectl get pods | grep <POD-NAME>: Filters pods by name.
  • kubectl describe pod: Describes details of a pod.
  • kubectl delete pod <POD-NAME> --grace-period=0 --force --namespace <NAMESPACE>: Deletes a pod forcibly.
  • kubectl logs <pod-name> --tail=<lines>: Retrieves the last few lines of logs for a pod.


Deployment Management:

Deployments:

  • kubectl get deployments: Lists all deployments in the cluster.
  • kubectl describe deployment <deployment-name>: Describes details of a deployment.
  • kubectl scale --replicas=5 deployment/<deployment-name>: Scales a deployment to a specific number of replicas.
  • kubectl edit deployment <deployment-name>: Edits a deployment's configuration.
  • kubectl rollout pause deployment/<deployment-name>: Pauses a deployment rollout.
  • kubectl rollout resume deployment/<deployment-name>: Resumes a paused deployment rollout.

Rollout and Rollback:

  • kubectl rollout status deployment/<deployment-name>: Monitors deployment rollout status.
  • kubectl rollout history deployment/<deployment-name>: Displays revision history of a deployment.
  • kubectl rollout undo deployment/<deployment-name>: Rolls back a deployment to the previous version.
  • kubectl rollout undo deployment/<deployment-name> --to-revision=<revision>: Rolls back to a specific revision.


Troubleshooting:

Logs and Events:

  • kubectl logs <pod-name> --namespace <namespace-name>: Retrieves logs for a pod in a specific namespace.
  • kubectl logs <pod-name> --container <container-name> : Retrieves logs for a pod  from a specific container.
  • kubectl describe pod <pod-name> --namespace <namespace-name>: Describes details of a pod in a specific namespace.
  • kubectl get events --namespace <namespace-name> --sort-by='{.lastTimestamp}': Displays events sorted by timestamp in a specific namespace.

Executing Commands:

  • kubectl exec -it <pod-name> -- /bin/bash: Opens a shell in a running pod. (use /bin/sh instead of /bin/bash if running through powershell window)
  • kubectl exec -it <pod-name> -- powershell: Opens a powershell shell in a running pod.
  • kubectl exec -it <pod-name> --container <container-name> -- /bin/bash: Opens a shell in a specific container of a pod.


Miscellaneous:

Exit Code and Restart:

  • echo $?: Displays the exit code of the last application.
  • kubectl rollout restart deployment <deployment-name>: Restarts a deployment.

Scaling and Deleting Pods:

  • kubectl scale deployment <deployment-name> --replicas=0: Scales a deployment to zero replicas.
  • kubectl delete pods --all --namespace <namespace-name>: Deletes all pods in a specific namespace.

Automated Actions:

  • for each in $(kubectl get pods --namespace <namespace-name> | grep Evicted | awk '{print $1}'); do kubectl delete pods $each --namespace <namespace-name>; done: Deletes all evicted pods in a specific namespace.
  • kubectl get pods | grep <pod-name> | awk '{print $1}' | xargs kubectl describe pod : Describe the pods