Kubectl Essentials: Must-Know Commands for Kubernetes

 

Kubectl Command Cheat Sheet

Syntax

Basic syntax: kubectl [command] [TYPE] [NAME] [flags]

COMMANDDESCRIPTION
kubectl --helpDisplay the command usage and list most common options
kubectl <command> --helpDisplay the command usage and list all available options
kubectl explain <resource>Get documentation of resources

Cluster & Context Management

COMMANDDESCRIPTION
kubectl config viewDisplay merged kubeconfig settings
kubectl config current-contextDisplay the current context
kubectl config use-context <context>Set the default context
kubectl config set-context <context>Set a context entry in kubeconfig
kubectl config get-contextsList all available contexts
kubectl cluster-infoDisplay cluster information

Resource Management

COMMANDDESCRIPTION
kubectl get <resource>List resources
kubectl get <resource> -o wideList resources with more details
kubectl get <resource> -o yamlList resources in YAML format
kubectl get <resource> -o jsonList resources in JSON format
kubectl describe <resource> <name>Display detailed information about a resource
kubectl create -f <file.yaml>Create a resource from a file
kubectl apply -f <file.yaml>Create or update a resource from a file
kubectl delete <resource> <name>Delete resources
kubectl delete -f <file.yaml>Delete resources using a file

Workload Resources

COMMANDDESCRIPTION
kubectl run <name> --image=<image>Create and run a pod from an image
kubectl create deployment <name> --image=<image>Create a deployment
kubectl scale deployment <name> --replicas=<n>Scale a deployment
kubectl set image deployment/<name> <container>=<image>Update container image
kubectl rollout status deployment/<name>Check the status of a rollout
kubectl rollout history deployment/<name>View rollout history
kubectl rollout undo deployment/<name>Rollback to the previous version
kubectl rollout restart deployment/<name>Restart a deployment

Pod Operations

COMMANDDESCRIPTION
kubectl get podsList all pods
kubectl get pods -n <namespace>List pods in a namespace
kubectl get pods --show-labelsList pods with their labels
kubectl get pods -l key=valueList pods with specific labels
kubectl describe pod <pod-name>Show detailed pod information
kubectl logs <pod-name>Print pod logs
kubectl logs -f <pod-name>Stream pod logs
kubectl logs <pod-name> -c <container>Print container logs from a pod
kubectl exec -it <pod-name> -- <command>Execute a command in a pod
kubectl port-forward <pod-name> <local-port>:<pod-port>Forward a port to a pod

Service & Networking

COMMANDDESCRIPTION
kubectl get servicesList all services
kubectl expose deployment <name> --port=<port> --target-port=<target-port>Expose a deployment as a service
kubectl get endpointsList all endpoints
kubectl get ingressList all ingresses
kubectl describe ingress <name>Show detailed ingress information
kubectl get networkpoliciesList all network policies

Configuration & Storage

COMMANDDESCRIPTION
kubectl get configmapsList all configmaps
kubectl create configmap <name> --from-file=<path>Create configmap from a file
kubectl create configmap <name> --from-literal=key=valueCreate configmap from literal values
kubectl get secretsList all secrets
kubectl create secret generic <name> --from-file=<path>Create a secret from a file
kubectl create secret generic <name> --from-literal=key=valueCreate a secret from literal values
kubectl get pvList all persistent volumes
kubectl get pvcList all persistent volume claims
kubectl get storageclassesList all storage classes

RBAC & Security

COMMANDDESCRIPTION
kubectl get serviceaccountsList all service accounts
kubectl create serviceaccount <name>Create a service account
kubectl get rolesList all roles
kubectl get rolebindingsList all role bindings
kubectl get clusterrolesList all cluster roles
kubectl get clusterrolebindingsList all cluster role bindings
kubectl auth can-i <verb> <resource>Check if you can perform an action

Namespaces & Resource Quotas

COMMANDDESCRIPTION
kubectl get namespacesList all namespaces
kubectl create namespace <name>Create a namespace
kubectl get resourcequotasList all resource quotas
kubectl get limitrangesList all limit ranges
kubectl -n <namespace> get <resource>Get resources in specific namespace

Debugging & Troubleshooting

COMMANDDESCRIPTION
kubectl get eventsShow events
kubectl get events --sort-by=.metadata.creationTimestampShow events sorted by timestamp
kubectl describe node <node-name>Show detailed node information
kubectl top nodeShow node resource usage
kubectl top podShow pod resource usage
kubectl get componentstatusesCheck control plane components status
kubectl get podsecuritypoliciesList pod security policies
kubectl debug <pod-name> -it --image=<debugger-image>Debug a pod with a debug container

Advanced Operations

COMMANDDESCRIPTION
kubectl diff -f <file.yaml>Preview apply changes
kubectl patch <resource> <name> -p '{}'Update field(s) of a resource
kubectl cp <pod-name>:/path /local/pathCopy files from pod to local
kubectl cp /local/path <pod-name>:/pathCopy files from local to pod
kubectl taint nodes <node-name> key=value:effectAdd a taint to a node
kubectl cordon <node-name>Mark node as unschedulable
kubectl drain <node-name>Drain node in preparation for maintenance
kubectl uncordon <node-name>Mark node as schedulable
kubectl api-resourcesList all supported resources and their API groups
kubectl api-versionsList all supported API versions

Common Options

OPTIONDESCRIPTION
-n, --namespace <namespace>Specify namespace
-A, --all-namespacesList resources across all namespaces
-l, --selector <key=value>Filter resources by label
-o, --output <format>Output format (json, yaml, wide, etc.)
--dry-run=clientSimulate an operation without making actual changes
--field-selector <field=value>Filter resources by fields
--forceForce immediate removal of resources
--grace-period=<seconds>Period of time to wait before force termination
--waitWait for completion before returning
--timeout=<duration>Set timeout for operation

Working with Manifests

COMMANDDESCRIPTION
kubectl kustomize <dir>Build a kustomization target
kubectl apply -k <dir>Apply a kustomization directory
kubectl delete -k <dir>Delete resources using a kustomization directory
kubectl create -f <url>Create resources from a URL
kubectl replace -f <file>Replace a resource from a file

Comments

Popular posts from this blog

Free Courses - Git & GitHub (DevOps)

6 FREE courses to learn AWS & AWS DevOps (Concepts + Hands-on + Interview)