Introduction
Kubernetes is a powerful tool for container orchestration, but it can be daunting for newcomers. Learning kubectl, the command-line tool for interacting with Kubernetes clusters, is a critical step in mastering Kubernetes. This blog post aims to provide beginners with a comprehensive list of kubectl commands that are essential for daily operations. By the end of this post, you will have a solid foundation to start managing your Kubernetes clusters effectively.
What is Kubectl?
kubectl is the command-line tool used to interact with Kubernetes clusters. It allows you to deploy applications, inspect and manage cluster resources, and view logs. Understanding how to use kubectl is essential for anyone working with Kubernetes.

Photo by admingeek from Infotechys
25 Essential Kubectl Commands
1. Configure Access to Your Cluster |
Before you can use kubectl, you need to configure access to your cluster. Use the following command to set the context for your cluster:
kubectl config set-context <context-name> --cluster=<cluster-name> --user=<user-name>To switch between contexts, use:
kubectl config use-context <context-name>2. Get Cluster Information |
To get a summary of your cluster’s information, use:
kubectl cluster-infoThis command provides details about the Kubernetes master and services running in the cluster.
3. List Nodes |
To see all nodes in your cluster, execute:
kubectl get nodesThis command lists all the nodes along with their status, roles, age, and version.
4. Describe a Node |
To get detailed information about a specific node, use:
kubectl describe node <node-name>Viewing and Describing Resources
5. List All Pods |
To list all pods in a specific namespace, use:
kubectl get pods -n <namespace>For all namespaces, simply add the --all-namespaces flag:
kubectl get pods --all-namespaces6. Describe a Pod |
To get detailed information about a specific pod, use:
kubectl describe pod <pod-name> -n <namespace>This command provides information about the pod’s status, events, and resource usage.
7. Get Services |
To list all services in a namespace, execute:
kubectl get svc -n <namespace>8. Describe a Service |
To describe a specific service, use:
kubectl describe svc <service-name> -n <namespace>9. Get Deployments |
To list all deployments in a namespace, use:
kubectl get deployments -n <namespace>10. Describe a Deployment |
To get detailed information about a deployment, execute:
kubectl describe deployment <deployment-name> -n <namespace>Creating and Updating Resources
11. Create a Resource |
To create a resource from a YAML or JSON file, use:
kubectl apply -f <file-name>.yamlThe same command applies for updating a resource or changes made to the YAML file.
12. Scale a Deployment |
To scale a deployment up or down, use:
kubectl scale deployment <deployment-name> --replicas=<number-of-replicas> -n <namespace>13. Edit a Resource |
To edit a resource directly, use:
kubectl edit <resource-type> <resource-name> -n <namespace>This command opens the resource configuration in your default text editor.
Deleting Resources
14. Delete a Resource |
To delete a resource, use:
kubectl delete <resource-type> <resource-name> -n <namespace>15. Delete Resources from a File |
To delete resources specified in a file, use:
kubectl delete -f <file-name>.yaml16. Delete All Pods in a Namespace |
To delete all pods in a specific namespace, execute:
kubectl delete pods --all -n <namespace>Namespace Management
17. List Namespaces |
To list all namespaces in the cluster, use:
kubectl get namespaces18. Create a Namespace |
To create a new namespace, use:
kubectl create namespace <namespace-name>19. Delete a Namespace |
To delete a namespace and all resources within it, use:
kubectl delete namespace <namespace-name>Logs and Debugging
20. View Pod Logs |
To view the logs of a specific pod, use:
kubectl logs <pod-name> -n <namespace>To view logs from a specific container within a pod, use:
kubectl logs <pod-name> -c <container-name> -n <namespace>21. Stream Pod Logs |
To stream the logs of a pod, use:
kubectl logs -f <pod-name> -n <namespace>22. Execute a Command in a Pod |
To execute a command inside a pod, use:
kubectl exec -it <pod-name> -n <namespace> -- <command>23. Port Forwarding |
To forward a local port to a port on a pod, use:
kubectl port-forward <pod-name> <local-port>:<pod-port> -n <namespace>24. Debug a Node |
To start a debugging session on a node, use:
kubectl debug node/<node-name> -it --image=busyboxThis command starts a pod with the specified image on the node for debugging purposes.
25. Apply a Label to a Resource |
To add a label to a resource, use:
kubectl label <resource-type> <resource-name> <label-key>=<label-value> -n <namespace>For example, to label a pod with the key environment and value production, use:
kubectl label pod <pod-name> environment=production -n <namespace>This command is useful for categorizing resources and managing them more efficiently through selectors.
No comments:
Post a Comment