Main kubectl commands
Last updated
Last updated
CLI Tool to interact with our K8s cluster
In order for kubectl to access a K8s cluster, it needs a kubeconfig file, which is created automatically when deploying our minikube cluster
By default, config file is located at ~/.kube/config
kubectl api-versions − It prints the supported versions of API on the cluster.
kubectl apply − It has the capability to configure a resource by file or stdin.
kubectl attach − This attaches things to the running container.
kubectl autoscale − This is used to auto scale pods which are defined such as Deployment, replica set, Replication Controller.
kubectl cluster-info − It displays the cluster Info.
kubectl cluster-info dump − It dumps relevant information regarding cluster for debugging and diagnosis.
kubectl config − Modifies the kubeconfig file.
kubectl config current-context − It displays the current context.
kubectl config delete-cluster − Deletes the specified cluster from kubeconfig.
kubectl config delete-context − Deletes a specified context from kubeconfig.
kubectl config get-clusters − Displays cluster defined in the kubeconfig.
kubectl config get-contexts − Describes one or many contexts.
kubectl config set-cluster − Sets the cluster entry in Kubernetes.
kubectl config set-context − Sets a context entry in kubernetes entrypoint.
kubectl config set-credentials − Sets a user entry in kubeconfig.
kubectl config set − Sets an individual value in kubeconfig file.
kubectl config unset − It unsets a specific component in kubectl.
kubectl config use-context − Sets the current context in kubectl file.
kubectl config view
kubectl cp − Copy files and directories to and from containers.
kubectl create − To create resource by filename of or stdin. To do this, JSON or YAML formats are accepted.
In the same way, we can create multiple things as listed using the create command along with kubectl.
deployment
namespace
quota
secret docker-registry
secret
secret generic
secret tls
serviceaccount
service clusterip
service loadbalancer
service nodeport
kubectl delete − Deletes resources by file name, stdin, resource and names.
kubectl describe − Describes any particular resource in kubernetes. Shows details of resource or a group of resources.
kubectl drain − This is used to drain a node for maintenance purpose. It prepares the node for maintenance. This will mark the node as unavailable so that it should not be assigned with a new container which will be created.
kubectl edit − It is used to end the resources on the server. This allows to directly edit a resource which one can receive via the command line tool.
kubectl exec − This helps to execute a command in the container.
kubectl expose − This is used to expose the Kubernetes objects such as pod, replication controller, and service as a new Kubernetes service. This has the capability to expose it via a running container or from a yaml file.
kubectl get − This command is capable of fetching data on the cluster about the Kubernetes resources.
For example,
kubectl logs − They are used to get the logs of the container in a pod. Printing the logs can be defining the container name in the pod. If the POD has only one container there is no need to define its name.
kubectl port-forward − They are used to forward one or more local port to pods.
kubectl replace − Capable of replacing a resource by file name or stdin.
kubectl rolling-update − Performs a rolling update on a replication controller. Replaces the specified replication controller with a new replication controller by updating a POD at a time.
kubectl rollout − It is capable of managing the rollout of deployment.
Apart from the above, we can perform multiple tasks using the rollout such as −
rollout history
rollout pause
rollout resume
rollout status
rollout undo
kubectl run − Run command has the capability to run an image on the Kubernetes cluster.
kubectl scale − It will scale the size of Kubernetes Deployments, ReplicaSet, Replication Controller, or job.
kubectl set image − It updates the image of a pod template.
kubectl set resources − It is used to set the content of the resource. It updates resource/limits on object with pod template.
kubectl top node − It displays CPU/Memory/Storage usage. The top command allows you to see the resource consumption for nodes.
The same command can be used with a pod as well.