12 Kubectl Commands to Master Kubernetes Deployments
Master the Art of Kubernetes Deployment with These 12 Essential kubectl Commands
The kubectl command-line tool is the main interface for interacting with Kubernetes, a robust platform for managing containerized applications. You may create, update, and manage resources, such as pods, services, and deployments, in a Kubernetes cluster using kubectl.
In this article, we'll look at the 12 kubectl commands that any Kubernetes administrator needs to be familiar with. These commands will help you master Kubernetes, from managing and debugging apps to creating and updating resources. This article will provide you a thorough overview of the most important kubectl commands, whether you're an experienced Kubernetes user or you're just getting started.
1. kubectl cluster-info
This command provides information about the current state of your Kubernetes cluster, including the API server address, the cluster state, and the versions of the components that make up your cluster.
kubectl cluster-info
2. kubectl version
This command displays the version of kubectl
that is currently installed on your system, as well as the version of the Kubernetes cluster that it is connected to.
kubectl version
3. kubectl get
This command will provide a list of resources available in your Kubernetes cluster. There are several types of resources:
Namespace
Deployment
Pod
Node
Service
ReplicaSets.
kubectl get all
This command will give the list of all of those available resources in your cluster. To pull a list of a specific resource, you need to use the following command in your terminal.
kubectl get deployment
For a specific namespace:
kubectl get deployment -n namespace
Here, -n is the short form of the flag -namespace. If you don't use any specific namespace, then kubectl get deployment
command will return the deployment list from the "default"/current namespace. A namespace is a Kubernetes object that separates a single physical Kubernetes cluster into numerous virtual clusters.
Similarly, If you need to fetch the list of all the pods in a namespace, you can use this command:
kubectl get pods -o wide
here, "-o wide" flag is providing more details about the pod.
So, with this kubectl get command, we can also fetch the list of Node, Namespace, ReplicaSets and Service
4. kubectl Create
The Kubernetes command kubectl create
is used to add new resources to a cluster. Users can build resources like pods, services, and deployments using this commands. Here's an illustration of how to build a new deployment using kubectl create:
kubectl create deployment my-nginx --image=nginx
With this command, a new deployment named will my-nginx
will be created using the nginx
image.
Here is another example of how to create a new cronjob with this command:
kubectl create job my-cronjob --schedule="*/5 * * * *" --image=busybox -- command -- args="echo This is a cron job!"
With this command:
A new CornJob will be created named my-cornjob which will be running in busybox container
An echo command will be repeated every 5 minutes
--schedule
flag specifies the schedule for the job in cron syntax"*/5 " means to run the job every 5 minutes
--image
flag specifies which container it should run--command
flag is used to specify which command it should run inside the container.
With Kubernetes' CronJobs functionality, you can automate recurring processes like database backups, log rotations, and more.
5. kubectl edit
With this kubectl edit
command, you can edit an existing resource in a cluster. You can modify a resource's configuration in a text editor using kubectl edit
, which eliminates the need for you to manually generate a new YAML file.
Here's an illustration of how to change a deployment using kubectl edit:
kubectl edit deployment my-nginx
This command starts a text editor and opens the my-nginx deployment. When you make changes to the deployment's configuration and save the file, the cluster's deployment will be updated.
6. kubectl delete
kubectl delete
command will help you to delete any resources such as pod, deployment, service, cornjob in your Kubernetes cluster. So, never just remove anything without knowing everything you need to know about it. Think carefully before executing this command since once the resource is deleted, it cannot be recovered; you must reconstruct it.
kubectl delete deployment my-nginx
7. kubectl apply
The Kubernetes command kubectl apply
enables you to create or modify resources in a cluster. Kubectl apply creates or modifies the resource in the cluster to match the configuration by reading the resource's configuration from a file or from standard input.
kubectl apply -f deployment.yaml
Here, kubectl apply
command reads a deployment configuration from a file named deployment.yaml
and creates a new deployment in the cluster depending on that configuration file. In case, if there will be a file existed on that name, then this command will update that configuration file.
8. kubectl config
In Kubernetes, the command kubectl config
allows you to manage the configuration for a kubectl client. The config command can be used to view, edit, or switch between multiple cluster configurations, as well as to manage user credentials and context settings.
Set a Current Namespace
If you are working on a specific namespace, then every time typing a namespace in each command is a hassle. To overcome this, you can set that namespace as a current namespace.
Here is an example of how to set up a current namespace:
kubectl config set-context --current --namespace=NAMESPACE
kubectl config set-context
is a command in Kubernetes that allows you to modify the context of a kubectl configuration. The context defines the cluster, user, and namespace that kubectl commands operate. In this example, this command will set the current namespace as "NAMESPACE".
Set Current Context
A context is defined as a named group of clusters and user credentials in the Kubernetes cluster. To set a context, you need to execute the following command:
kubectl config use-context docker-desktop
In this example, this command is used to switch the current context in the kubectl
configuration to the docker-desktop
context. So, kubectl commands can interact with the cluster running on docker-desktop. You can also use minikube instead of docker-desktop if you want to interact with the cluster running on minikube.
You can check the current context by running this command:
kubectl config current-context
9. kubectl describe
kubectl describe
is a useful tool for monitoring and debugging your Kubernetes cluster. It offers a quick approach to obtaining comprehensive information about a resource, making it simpler to understand the resource's current state and spot any problems. It shows details about a resource's status, events, and metadata.
Here is an example:
kubectl describe deployment my-nginx
With this command, you can get details information on my-nginx deployment which includes the status of the deployment, the events that have occurred, and the metadata associated with the deployment.
10. kubectl logs
You may get the logs of a container in a pod using the Kubernetes command kubectl logs
. The logs can be used to track down problems with a container or to troubleshoot problems with it.
Here's an example of how you can use kubectl logs
kubectl logs my-pod
If there will be multiple containers in a pod, we can get the log of a specific container this way:
kubectl logs my-pod -c my-container
Here, -c
is an option that specifies the name of the container from where you want to retrieve the logs.
11. kubectl exec
You can execute a command in a running container of a pod using the Kubernetes command kubectl exec
. It is helpful for debugging, troubleshooting, and monitoring the status of an application.
Here's an illustration of how kubectl exec might be used:
kubectl exec -it pod-name -- bash
In this example, kubectl exec
is used to launch a bash
shell in the container of the specified pod. The -it
flag is used to attach to the shell and interact with it.
If you have multiple containers in a pod, you can use the following command:
kubectl exec -it pod_name -c container_name bash
In this example, -c
is used to specify the container name where the shell command will be executed.
12. kubectl cp
In Kubernetes, the command kubectl cp
allows you to copy files and directories between a local file system and a container in a pod, or between two containers in the same pod. This can be useful for transferring files between the host and containers, or for copying files between containers within a pod.
kubectl cp <local-file-path> <pod-name>:<container-destination-path>
In this example:
kubectl cp
is used to copy a local file to a container in a pod.local-file-path
specifies the path to the file on the local file system.pod-name
andcontainer-destination-path
specify the destination of the file within the container.
Conclusion
With that, we have come to the end of this discussion on the 12 kubectl commands for mastering Kubernetes deployments. Please keep in mind that these are not the only kubectl command and there are many more Kubernetes concepts and kubectl commands to investigate.
I appreciate you taking the time to read this. Your support is much appreciated! If you found this article valuable, please consider clicking the ๐ Follow button and giving it a few claps by clicking the โค๏ธ like button to help me create more informative content like this. Thank you for your time! ๐ค
Also, follow me on Medium, Twitter & LinkedIn.