Mastering kubectl: The Guide to Kubernetes CLI Commands

Whether you’re just stepping into Kubernetes or you're managing large-scale production clusters, kubectl is your essential Swiss Army knife. It’s the command-line tool used to interact with your Kubernetes cluster, allowing you to deploy apps, inspect resources, troubleshoot issues, and automate workflows.
In this comprehensive guide, we’ll walk through kubectl command, grouped by purpose, with clear explanations of what each one does.
1. Cluster Configuration and Context Management
These commands help you connect to, configure, and switch between Kubernetes clusters.
kubectl version
Shows the version ofkubectland the connected Kubernetes API server.kubectl config view
Displays the kubeconfig file details (clusters, contexts, users).kubectl config use-context <context-name>
Switches the current context to a different cluster.kubectl config get-contexts
Lists all available contexts in your kubeconfig.kubectl config current-context
Prints the name of the current context.kubectl cluster-info
Displays addresses of the Kubernetes control plane and services.kubectl config set-context <name>
Sets a named context in your kubeconfig.
2. Listing and Describing Resources
Used for inspecting the current state of your cluster’s resources.
kubectl get <resource>
Lists one or more resources (e.g.,pods,services,deployments).kubectl get <resource> -o wide
Adds more detail to the output (IP, node name, etc.).kubectl get <resource> -A
Shows resources across all namespaces.kubectl get all
Lists all standard resources (pods, services, deployments, etc.) in the current namespace.kubectl describe <resource> <name>
Shows detailed information about a resource, including events and configuration.kubectl explain <resource>
Displays the API documentation for the resource type and fields.kubectl get events
Lists cluster events in chronological order.kubectl get componentstatuses(deprecated)
Shows the health of cluster components (controller manager, scheduler, etc.).
3. Creating, Applying, and Deleting Resources
For managing resource lifecycles from YAML or JSON files.
kubectl create -f <file>.yaml
Creates resources defined in a manifest file.kubectl apply -f <file>.yaml
Creates or updates resources declaratively.kubectl delete -f <file>.yaml
Deletes resources defined in the file.kubectl replace -f <file>.yaml
Replaces the live resource with a new one from a file.kubectl create deployment <name> --image=<image>
Imperatively creates a deployment.kubectl delete <resource> <name>
Deletes a specific resource by name.kubectl delete <resource> --all
Deletes all resources of a type in the current namespace.
4. Editing and Patching Resources
For quick changes to live resources.
kubectl edit <resource> <name>
Opens the resource definition in your editor for inline editing.kubectl patch <resource> <name> -p '<patch-data>'
Applies a strategic merge patch to a resource.
5. Working with Pods
Manage and troubleshoot pods with these commands.
kubectl get pods
Lists pods in the current namespace.kubectl logs <pod>
Displays logs from a pod's main container.kubectl logs <pod> -c <container>
Shows logs from a specific container in the pod.kubectl exec <pod> -- <command>
Runs a command inside the pod (e.g.,kubectl exec pod-name -- ls /).kubectl exec -it <pod> -- bash
Opens an interactive terminal session inside the container.kubectl port-forward <pod> 8080:80
Forwards a local port to a port on the pod.kubectl cp <pod>:<path> <local-path>
Copies files from a pod to the local machine or vice versa.kubectl delete pod <name>
Deletes a specific pod.kubectl delete pod --all
Deletes all pods in the current namespace.
6. Deployments and Rollouts
Commands for managing application rollouts.
kubectl rollout status deployment/<name>
Monitors the rollout status of a deployment.kubectl rollout history deployment/<name>
Shows the history of a deployment.kubectl rollout undo deployment/<name>
Rolls back to the previous deployment.kubectl rollout restart deployment/<name>
Triggers a new rollout by restarting the deployment.kubectl scale deployment <name> --replicas=<n>
Changes the number of pod replicas in a deployment.
7. Services and Exposing Pods
Use these to expose pods and create services.
kubectl expose pod <name> --port=80 --target-port=8080
Exposes a pod as a service.kubectl expose deployment <name> --port=80 --type=NodePort
Exposes a deployment through a service.
8. Namespaces
Commands for managing namespaces.
kubectl get namespaces
Lists all namespaces.kubectl create namespace <name>
Creates a new namespace.kubectl delete namespace <name>
Deletes the specified namespace.kubectl get pods --namespace <name>
Lists pods in a specific namespace.
9. Labels and Annotations
Used to organize and identify Kubernetes resources.
kubectl label <resource> <name> key=value
Adds or updates a label on a resource.kubectl annotate <resource> <name> key=value
Adds or updates an annotation.
10. Resource Usage
Check CPU and memory consumption.
kubectl top pod
Shows CPU and memory usage of pods.kubectl top node
Shows CPU and memory usage of nodes.
(Requires metrics server to be installed.)
11. Access Control and Security (RBAC)
Useful for debugging permissions and managing roles.
kubectl auth can-i <verb> <resource>
Checks whether the current user is allowed to perform an action.kubectl describe role <name>
Describes a Role's permissions.kubectl describe clusterrole <name>
Describes a ClusterRole's permissions.kubectl get roles/kubectl get clusterroles
Lists roles and cluster-wide roles.
12. Output Customization and Sorting
Get more detailed or structured output.
kubectl get <resource> -o yaml
Outputs the resource definition in YAML format.kubectl get <resource> -o json
Outputs the resource definition in JSON format.kubectl get <resource> --sort-by=.metadata.name
Sorts the resource list by a field.kubectl get <resource> -l key=value
Filters by label selector.
kubectl is powerful because it abstracts Kubernetes operations into a unified CLI. From basic pod management to advanced rollout strategies and RBAC validation, mastering kubectl is essential for efficient Kubernetes operations. As Kubernetes evolves, keep an eye on updates and new flags, but this foundation will always serve you well.



