Kubernetes Cheat Sheet Shortlist

June 1, 2022 | DevOps | No Comments

Context & Namespaces


List All Contexts

Returns the list of contexts from the local kubectl config file

kubectl config get-contexts

Show Current Context

Returns the context currently being used by kubectl

kubectl config current-context


Change Current Context
kubectl config use-context CLUSTER_IDENTIFIER

Set “Sticky” Namespace

use a “sticky” namespace so you won’t have to specify the namespace in every command

kubectl config set-context --current -n NS_NAME


General Commands


Port Forwarding to Pod

Port-forwarding using the same port on the local machine and the target pod

kubectl port-forward -n NS_NAME POD_NAME PORT

Port-forwarding using a different port on the local machine and the target pod

kubectl port-forward -n NS_NAME POD_NAME LOCAL_PORT:TARGET_PORT

Port-forwarding to a service
kubectl port-forward -n NS_NAME svc/SERVICE_NAME PORT

Scaling
kubectl scale OBJECT OBJECT_NAME -n NS_NAME --replicas=DESIRED_COUNT
kubectl scale deployment DEPLOYMENT_NAME -n NS_NAME --replicas=5


Wait For…

Wait for Pod to become ready

kubectl wait -n NS_NAME --for=delete pod/POD_NAME --timeout=90s

Wait for Pod to become deleted

kubectl wait -n NS_NAME --for=condition=Ready pod/POD_NAME --timeout=90s

Run BASH Command On Pod
kubectl exec -n NS_NAME -it POD_NAME -- bash -c "YOUR_COMMANDS;"

SSH to Pod
kubectl exec -n NS_NAME -it POD_NAME-- bash

Force Pod Deletion
kubectl delete pod POD_NAME -n NS_NAME --grace-period=0 --force

Get Pods With Name-Only
kubectl get pods -n NS_NAME --no-headers -o custom-columns=":metadata.name"

Suspend a Cronjob
kubectl patch cronjob CRONJOB_NAME -n NS_NAME -p '{"spec" : {"suspend" : true }}'

About Author

Leave a Reply

Your email address will not be published.