Introduction
Before we discuss a list of kubectl commands, let's first understand-What is Kubectl ?
The kubectl is a command-line tool used to interact with Kubernetes clusters. It allows you to manage and control various aspects of your Kubernetes environment. Every Kubernetes command has an API endpoint, and the primary function of kubectl is to make HTTP requests to the API.
While it is feasible to issue HTTP requests manually (for example, with curl), kubectl is designed to make this process more convenient and simple.
In this tutorial, you will understand a list of kubectl commands. We will also address a few FAQs on kubectl commands.
List of kubectl Commands
While working with Kubernetes, use the kubectl commands mentioned below as a quick reference.
Listing Resources
Use the kubectl get
command to list one or more pods, replication controllers, services, or daemon sets.
Create a plain-text list of all namespaces:
kubectl get namespaces
Display a plain-text list of all pods:
kubectl get pods
Create a thorough plain-text list of all pods that includes details like node names:
kubectl get pods -o wide
See a list of all active pods on a specific node server:
kubectl get pods --field-selector=spec.nodeName=[server-name]
In plain text, specify a replication controller:
kubectl get replicationcontroller [replication-controller-name]
Create a list in plain text of each replication controller and service:
kubectl get replicationcontroller,services
Display a list in plain text of all daemon sets:
kubectl get daemonset
Creating a Resource
The kubectl create
command can be used to create a resource such as a service, deployment, job, or namespace.
Type the following, for instance, to make a new namespace:
kubectl create namespace [namespace-name]
A JSON or YAML file can be used to create a resource:
kubectl create -f [filename]
Applying and Updating a Resource
The kubectl apply
command can be used to apply or update a resource. A file or standard input (stdin) can be used as the source for this procedure.
Build a new service and add the [service-name].yaml file definition to it.
kubectl apply -f [service-name].yaml
Build a new replication controller using the definition given in a [controller-name].yaml file:
kubectl apply -f [controller-name].yaml
Create the objects specified in any .yaml, .yml, or .json file in a directory:
kubectl apply -f [directory-name]
The kubectl edit
command can be used to modify a resource once it has been set up in a text editor. This command combines the kubectl get
and kubectl apply
commands.
For instance, enter the following to edit a service:
kubectl edit svc/[service-name]
Using this command, your default editor opens the file. To use a different editor, add the editor's name before the command:
KUBE_EDITOR=”[editor-name]” kubectl edit svc/[service-name]
Displaying the State of Resources
Use the kubectl describe
command to display the state of any number of resources in detail. The output by default includes a list of uninitialized resources.
See information regarding a specific node:
kubectl describe nodes [node-name]
See information regarding a specific pod:
kubectl describe pods [pod-name]
Show information about a pod whose name and type are provided in pod.json
:
kubectl describe -f pod.json
See information on all pods managed by a certain replication controller:
kubectl describe pods [replication-controller-name]
Display information for each pod:
kubectl describe pods
Deleting Resources
The kubectl delete
command can be used to remove resources from a file or stdin.
Use the name and type specified in pod.yaml to remove a pod:
kubectl delete -f pod.yaml
Remove all pods and services with a particular label:
kubectl delete pods,services -l [label-key]=[label-value]
Remove each and every pod, including uninitialized pods:
kubectl delete pods --all
Executing a Command
To run commands or launch a shell inside a container, use kubectl exec
.
Get the following output from a command executed on the pod's first container:
kubectl exec [pod-name] -- [command]
Receive output from a command run on a specific container in a pod:
kubectl exec [pod-name] -c [container-name] -- [command]
Execute /bin/bash from a specific pod. The first container is where the output is received:
kubectl exec -ti [pod-name] -- /bin/bash
Modifying kubeconfig Files
You may view and edit kubeconfig files with kubectl config
. Normally, this command is followed by another sub-command.
Show the current context:
kubectl config current-context
Create a cluster entry in kubeconfig:
kubectl config set-cluster [cluster-name] --server=[server-name]
Unset a kubeconfig entry:
kubectl config unset [property-name]
Printing Container Logs
Use the kubectl logs
command to print the logs from the containers in a pod.
Print logs:
kubectl logs [pod-name]
Logs from a pod can be streamed using:
kubectl logs -f [pod-name]
Short Names for Resource Types
Given their length, several of the kubectl
commands above could appear cumbersome. Because of this, the names of popular kubectl resource types also have shortened versions.
Take the above command into consideration:
kubectl create namespace [namespace-name]
This command may also be executed as:
kubectl create ns [namespace-name]
The complete list of kubectl short names is given below:
Short Name | Long Name |
---|---|
csr | certificatesigningrequests |
cs | componentstatuses |
cm | configmaps |
ds | daemonsets |
deploy | deployments |
ep | endpoints |
ev | events |
hpa | horizontalpodautoscalers |
ing | ingresses |
limits | limitranges |
ns | namespaces |
no | nodes |
pvc | persistentvolumeclaims |
pv | persistentvolumes |
po | pods |
pdb | poddisruptionbudgets |
psp | podsecuritypolicies |
rs | replicasets |
rc | replicationcontrollers |
quota | resourcequotas |
sa | serviceaccounts |
svc | services |
FAQs on A List of Kubectl Commands
How do I install kubectl?
The installation process for kubectl depends on your operating system. It can be installed as part of the Kubernetes command-line tools bundle or separately. Detailed installation instructions can be found in the Kubernetes documentation.
How do I authenticate with a Kubernetes cluster using kubectl?
kubectl uses several methods for authentication, including kubeconfig files, environment variables, and user credentials. Typically, you provide your authentication details during the cluster configuration.
What is a kubeconfig file, and how do I use it with kubectl?
A kubeconfig file holds cluster information, such as the cluster address, authentication details, and user preferences. You can specify a kubeconfig file using the --kubeconfig
flag with kubectl commands.
How can I list all the pods in a Kubernetes cluster using kubectl?
The command kubectl get pods
lists all the running pods in the current cluster, including information like the pod name, status, and node location.
How do I create a new deployment in Kubernetes using kubectl?
To create a deployment, use the kubectl create deployment
command along with the desired deployment name and image details. For example: kubectl create deployment my-app --image=my-image:tag
.
Can kubectl scale resources in Kubernetes?
Yes, kubectl can scale resources such as deployments. Use the kubectl scale
command, specifying the desired number of replicas. For example: kubectl scale deployment my-app --replicas=3
.
How do I update the image of a running deployment using kubectl?
To update the image of a deployment, use the kubectl set image
command, specifying the deployment name and the new image. For example: kubectl set image deployment/my-app my-app=my-new-image:tag
.
Conclusion
This tutorial covered the most common kubectl
commands for managing your Kubernetes API. The accompanying cheat sheet gives you access to all the commands in one place, easily accessible for quick reference.
If you have any queries, please leave a comment below, and we’ll be happy to respond to them for sure.