Kubectl is a powerful command-line tool for managing Kubernetes clusters. With kubectl, you can deploy, inspect, and manage applications running on a Kubernetes cluster. Whether you are a developer or a system administrator, you will find kubectl to be an indispensable tool in your toolkit. In this blog post, we will discuss 10 popular kubectl commands that you should know.

  1. kubectl apply

The kubectl apply command is used to apply configuration changes to a Kubernetes cluster. You can use it to create new resources, update existing resources, or delete resources. The apply command works by sending a patch request to the Kubernetes API server, which updates the cluster’s state accordingly. Here’s an example of using kubectl apply to create a new deployment:

kubectl apply -f deployment.yaml

This command reads the configuration in the deployment.yaml file and creates a new deployment in the cluster based on that configuration.

  1. kubectl get

The kubectl get command is used to retrieve information about resources in a Kubernetes cluster. You can use it to get a list of all the resources of a specific type, such as pods, deployments, or services. Here’s an example of using kubectl get to get a list of all the pods running in a cluster:

kubectl get pods

This command returns a list of all the pods running in the cluster, along with their current status, age, and other metadata.

  1. kubectl logs

The kubectl logs command is used to retrieve the logs for a specific container running in a pod. You can use it to debug applications running in a Kubernetes cluster. Here’s an example of using kubectl logs to retrieve the logs for a container running in a pod:

kubectl logs <pod-name> <container-name>

This command retrieves the logs for the specified container running in the specified pod.

  1. kubectl exec

The kubectl exec command is used to execute a command inside a container running in a pod. You can use it to debug or troubleshoot applications running in a Kubernetes cluster. Here’s an example of using kubectl exec to execute a command inside a container running in a pod:

kubectl exec <pod-name> <command>

This command executes the specified command inside the specified container running in the specified pod.

  1. kubectl describe

The kubectl describe command is used to get detailed information about a specific resource in a Kubernetes cluster. You can use it to get information about pods, services, deployments, and other resources. Here’s an example of using kubectl describe to get information about a specific pod:

kubectl describe pod <pod-name>

This command retrieves detailed information about the specified pod, including its current status, labels, annotations, and other metadata.

  1. kubectl delete

The kubectl delete command is used to delete a resource from a Kubernetes cluster. You can use it to delete pods, services, deployments, and other resources. Here’s an example of using kubectl delete to delete a deployment:

kubectl delete deployment <deployment-name>

This command deletes the specified deployment from the cluster.

  1. kubectl scale

The kubectl scale command is used to scale the number of replicas for a deployment or a stateful set. You can use it to increase or decrease the number of pods running in a cluster. Here’s an example of using kubectl scale to scale a deployment:

kubectl scale deployment <deployment-name> --replicas=<number-of-replicas>

This command scales the specified deployment to the specified number of replicas.

  1. kubectl rollout

The kubectl rollout command is used to manage roll

outs for deployments in a Kubernetes cluster. You can use it to deploy new versions of an application and roll back to a previous version if necessary. Here are some examples of using kubectl rollout:

To deploy a new version of an application:

kubectl set image deployment/<deployment-name> <container-name>=<new-image>
kubectl rollout status deployment/<deployment-name>

These commands update the specified container in the specified deployment with a new image and then monitor the rollout status until it is complete.

To roll back to a previous version of an application:

kubectl rollout undo deployment/<deployment-name>
kubectl rollout status deployment/<deployment-name>

These commands roll back the specified deployment to the previous revision and then monitor the rollout status until it is complete.

  1. kubectl port-forward

The kubectl port-forward command is used to forward traffic from a local port to a port on a pod running in a Kubernetes cluster. You can use it to access services running in a cluster from your local machine. Here’s an example of using kubectl port-forward to forward traffic to a pod:

kubectl port-forward <pod-name> <local-port>:<pod-port>

This command forwards traffic from the specified local port to the specified pod port running in the specified pod.

  1. kubectl label

The kubectl label command is used to add, modify, or remove labels from resources in a Kubernetes cluster. Labels are key-value pairs that can be used to organize and manage resources in a cluster. Here’s an example of using kubectl label to add a label to a pod:

kubectl label pod <pod-name> <key>=<value>

This command adds the specified label with the specified key and value to the specified pod.

Conclusion

In this blog post, we discussed 10 popular kubectl commands that you should know. With these commands, you can deploy, inspect, and manage applications running in a Kubernetes cluster. Whether you are a developer or a system administrator, kubectl is an indispensable tool for working with Kubernetes. By mastering these commands, you can become more efficient and productive when working with Kubernetes.