Kubernetes is a powerful and flexible container orchestration system that is widely used in production environments. One of the strengths of Kubernetes is its command-line interface (CLI), which allows users to perform a wide range of operations on their clusters and deployments. In this article, we will take a closer look at a few Kubernetes commands and provide examples of how to use them.
kubectl exec
: This command allows you to execute commands inside a running container. For example, to execute a shell command inside a container named “my-container” in the “my-namespace” namespace, you would run the following command:
kubectl exec -it -n my-namespace my-container -- /bin/sh
kubectl logs
: This command allows you to view the logs of a running container. For example, to view the logs of a container named “my-container” in the “my-namespace” namespace, you would run the following command:
kubectl logs -n my-namespace my-container
kubectl cp
: This command allows you to copy files and directories between a local machine and a running container. For example, to copy a file named “my-file” from your local machine to the “/data” directory inside a container named “my-container” in the “my-namespace” namespace, you would run the following command:
kubectl cp my-file -n my-namespace my-container:/data/
kubectl patch
: This command allows you to update a specific field of a Kubernetes resource. For example, to update the “image” field of a deployment named “my-deployment” in the “my-namespace” namespace, you would run the following command:
kubectl patch deployment my-deployment -n my-namespace -p '{"spec":{"template":{"spec":{"containers":[{"name":"my-container","image":"my-new-image"}]}}}}'
kubectl scale
: This command allows you to scale the number of replicas of a deployment. For example, to scale a deployment named “my-deployment” in the “my-namespace” namespace to 3 replicas, you would run the following command:
kubectl scale deployment my-deployment -n my-namespace --replicas=3
kubectl run
: This command allows you to run a command in a new container. For example, to run a command in a new container named “my-container” in the “my-namespace” namespace, you would run the following command:
kubectl run my-container -n my-namespace --image=my-image --command -- sleep 3600