25 Essential Kubectl Commands for Beginners

25 essential kubectl commands

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.

Table of Contents

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.

25 Essential Kubectl Commands

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-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 <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-namespaces
				
			

6. 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>.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 <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>.yaml
				
			

16. 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 namespaces
				
			

18. 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=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 <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.

Conclusion

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.

Cheat Sheet

CommandDescription
kubectl config set-contextSet context for your cluster
kubectl config use-contextSwitch between contexts
kubectl cluster-infoGet cluster information
kubectl get nodesList 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>.yamlCreate 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>.yamlDelete resources from a file
kubectl delete pods --all -n <namespace>Delete all pods in a namespace
kubectl get namespacesList 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=busyboxDebug a node using a specified image

Additional Resources

For more detailed information, consider exploring the Kubernetes Documentation and the Kubectl Reference.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *