Discover the 25 basic Docker commands every beginner needs to know. This comprehensive guide covers everything from pulling images to managing containers, complete with examples
Discover 25 essential kubectl commands for beginners in Kubernetes. Learn how to manage clusters, view logs, scale deployments, and more with easy-to-follow examples and explanations.
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.
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
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 --cluster= --user=
To switch between contexts, use:
kubectl config use-context
2. Get Cluster Information |
To get a summary of your cluster’s information, use:
kubectl cluster-info
This 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 nodes
This 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
5. List All Pods |
To list all pods in a specific namespace, use:
kubectl get pods -n
For all namespaces, simply add the --all-namespaces
flag:
kubectl get pods --all-namespaces
6. Describe a Pod |
To get detailed information about a specific pod, use:
kubectl describe pod -n
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
8. Describe a Service |
To describe a specific service, use:
kubectl describe svc -n
9. Get Deployments |
To list all deployments in a namespace, use:
kubectl get deployments -n
10. Describe a Deployment |
To get detailed information about a deployment, execute:
kubectl describe deployment -n
11. Create a Resource |
To create a resource from a YAML or JSON file, use:
kubectl apply -f .yaml
The 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 --replicas= -n
13. Edit a Resource |
To edit a resource directly, use:
kubectl edit -n
This command opens the resource configuration in your default text editor.
14. Delete a Resource |
To delete a resource, use:
kubectl delete -n
15. Delete Resources from a File |
To delete resources specified in a file, use:
kubectl delete -f .yaml
16. Delete All Pods in a Namespace |
To delete all pods in a specific namespace, execute:
kubectl delete pods --all -n
17. List Namespaces |
To list all namespaces in the cluster, use:
kubectl get namespaces
18. Create a Namespace |
To create a new namespace, use:
kubectl create namespace
19. Delete a Namespace |
To delete a namespace and all resources within it, use:
kubectl delete namespace
20. View Pod Logs |
To view the logs of a specific pod, use:
kubectl logs -n
To view logs from a specific container within a pod, use:
kubectl logs -c -n
21. Stream Pod Logs |
To stream the logs of a pod, use:
kubectl logs -f -n
22. Execute a Command in a Pod |
To execute a command inside a pod, use:
kubectl exec -it -n --
23. Port Forwarding |
To forward a local port to a port on a pod, use:
kubectl port-forward : -n
24. Debug a Node |
To start a debugging session on a node, use:
kubectl debug node/ -it --image=busybox
This 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 = -n
For example, to label a pod with the key environment
and value production
, use:
kubectl label pod environment=production -n
This command is useful for categorizing resources and managing them more efficiently through selectors.
Mastering kubectl
commands is crucial for efficiently managing Kubernetes clusters. This list of 25 commands provides a solid foundation for beginners to start exploring and managing their clusters. By understanding and practicing these commands, you’ll be well on your way to becoming proficient in Kubernetes management.
We hope this guide has been helpful. Feel free to bookmark this page and refer back to it as you continue your Kubernetes journey.
Command | Description |
---|---|
kubectl config set-context | Set context for your cluster |
kubectl config use-context | Switch between contexts |
kubectl cluster-info | Get cluster information |
kubectl get nodes | List all nodes |
kubectl describe node <node-name> | Describe a specific node |
kubectl get pods -n <namespace> | List all pods in a namespace |
kubectl describe pod <pod-name> -n <namespace> | Describe a specific pod |
kubectl get svc -n <namespace> | List all services in a namespace |
kubectl describe svc <service-name> -n <namespace> | Describe a specific service |
kubectl get deployments -n <namespace> | List all deployments in a namespace |
kubectl describe deployment <deployment-name> -n <namespace> | Describe a deployment |
kubectl apply -f <file-name>.yaml | Create or update a resource from a file |
kubectl scale deployment <deployment-name> --replicas=<number-of-replicas> -n <namespace> | Scale a deployment |
kubectl edit <resource-type> <resource-name> -n <namespace> | Edit a resource directly |
kubectl delete <resource-type> <resource-name> -n <namespace> | Delete a resource |
kubectl delete -f <file-name>.yaml | Delete resources from a file |
kubectl delete pods --all -n <namespace> | Delete all pods in a namespace |
kubectl get namespaces | List all namespaces |
kubectl create namespace <namespace-name> | Create a new namespace |
kubectl delete namespace <namespace-name> | Delete a namespace |
kubectl logs <pod-name> -n <namespace> | View logs of a specific pod |
kubectl logs -f <pod-name> -n <namespace> | Stream logs of a specific pod |
kubectl exec -it <pod-name> -n <namespace> -- <command> | Execute a command in a pod |
kubectl port-forward <pod-name> <local-port>:<pod-port> -n <namespace> | Port forward local port to a pod |
kubectl debug node/<node-name> -it --image=busybox | Debug a node using a specified image |
For more detailed information, consider exploring the Kubernetes Documentation and the Kubectl Reference. |
Related Posts
Discover the 25 basic Docker commands every beginner needs to know. This comprehensive guide covers everything from pulling images to managing containers, complete with examples
This blog post covers setting up a Kubernetes cluster on Ubuntu 23.10. Follow our step-by-step guide to set up a robust and scalable Kubernetes environment
Learn how to install Kubernetes using Ansible on Ubuntu 24.04. This comprehensive guide covers prerequisites, setup, and configuration, ensuring a streamlined and consistent deployment process.