Kubectl command cheat sheet

Kubernetes is an open-source platform designed to automate deploying, scaling, and operating application containers. To interact with Kubernetes, you’ll need to use kubectl, the command-line interface that allows you to run commands against Kubernetes clusters. This article will explain some of the basic kubectl commands essential for managing your cluster.

Checking the Status of Pods

To check the status of all running pods in your cluster, you use the kubectl get pods command. This command retrieves information about all pods and their status.

kubectl get pods

This will output a list of all pods, along with details like their status, number of restarts, and age.

Applying Configuration Changes

When you want to change the configuration of your cluster, you can use the kubectl apply command with the -f flag followed by the filename of your configuration file.

kubectl apply -f <filename>

This command tells Kubernetes to apply the changes specified in your configuration file to the cluster.

Getting Detailed Information About Resources

For more detailed information about a specific resource, use kubectl describe. This command can be used for any object type, such as pods, nodes, services, etc.

kubectl describe <object type> <object name>

Replace <object type> with the type of resource, and <object name> with the name of the specific resource you want to inspect.

Updating a Container’s Image

If you need to update the image of a running container, you can use the kubectl set image command. This is an imperative command to update the image property of a resource.

kubectl set image <object type>/<object name> <container name>=<new image>

Here, <object type> could be deployment, <object name> is the name of the deployment, <container name> is the name of the container in the pod specified in the deployment, and <new image> is the new image to use.

Deleting Resources

To remove objects from your cluster, such as pods, services, or deployments, use the kubectl delete command with the -f flag followed by the path to the configuration file that created the object.

kubectl delete -f <config file>

This will remove the specified resources from your cluster.

Conclusion

These kubectl commands are just the beginning of what you can do with Kubernetes, but they are some of the most frequently used and essential for day-to-day operations. By understanding and mastering these commands, you’ll be well on your way to efficiently managing your Kubernetes clusters.

,