Important Command in kubectl
The create command in Kubernetes is used to create a resource from a file or stdin. For example, kubectl create -f pod.yaml will create a pod using the configuration specified in pod.yaml. The expose command takes a replication controller, service, deployment, or pod and exposes it as a new Kubernetes service. For instance, kubectl expose deployment my-deployment --type=LoadBalancer --name=my-service will expose a deployment as a LoadBalancer service.
The run command lets you run a specific image on the cluster. By executing kubectl run nginx --image=nginx, you create a deployment running the NGINX image. The set command is used to set specific features on objects. For example, kubectl set image deployment/my-deployment my-container=my-image:1.5.0 updates the image of a container in a deployment.
For intermediate commands, explain provides documentation for a resource. For instance, kubectl explain pods details the pods resource. The get command displays one or many resources. For example, kubectl get pods lists all pods in the current namespace. The edit command allows you to edit a resource on the server. For example, kubectl edit deployment/my-deployment opens the deployment configuration in an editor. The delete command removes resources. For example, kubectl delete pod my-pod deletes a specific pod.
Deployment-related commands include rollout, which manages the rollout of a resource. Using kubectl rollout status deployment/my-deployment shows the status of a deployment rollout. The scale command sets a new size for a deployment, replica set, or replication controller. For example, kubectl scale deployment/my-deployment --replicas=3 scales a deployment to three replicas. The autoscale command automatically scales a deployment. For example, kubectl autoscale deployment/my-deployment --min=2 --max=5 --cpu-percent=80 sets autoscaling parameters based on CPU usage.
Cluster management commands help manage and retrieve cluster information. The certificate command modifies certificate resources, such as with kubectl certificate approve my-cert. The cluster-info command displays cluster information, simply using kubectl cluster-info. To display resource usage, kubectl top nodes shows CPU and memory usage for nodes. The cordon command marks a node as unschedulable with kubectl cordon my-node, while kubectl uncordon my-node makes it schedulable again. The drain command prepares a node for maintenance, like kubectl drain my-node --ignore-daemonsets. The taint command updates the taints on nodes, as in kubectl taint nodes my-node key=value:NoSchedule.
For troubleshooting and debugging, describe shows details of specific resources, such as kubectl describe pod my-pod. The logs command prints the logs for a container in a pod; for example, kubectl logs my-pod retrieves logs from a pod. The attach command attaches to a running container, with kubectl attach my-pod -c my-container for a specific container. The exec command executes a command in a container, like kubectl exec my-pod -- ls /app to list directory contents. The port-forward command forwards local ports to a pod, such as kubectl port-forward my-pod 8080:80. The proxy command runs a proxy to the Kubernetes API server, simply using kubectl proxy. The cp command copies files and directories to and from containers, for example, kubectl cp /local/path my-pod:/container/path. The auth command inspects authorization, like kubectl auth can-i create pods. The debug command creates debugging sessions for troubleshooting workloads and nodes, for instance, kubectl debug node/mynode -it --image=busybox. The events command lists events, using kubectl get events to show recent events in the cluster.
Advanced commands include diff, which compares the live version against a would-be applied version, such as kubectl diff -f my-deployment.yaml. The apply command applies a configuration to a resource, with kubectl apply -f my-deployment.yaml. The patch command updates specific fields of a resource, like kubectl patch deployment my-deployment -p '{"spec":{"replicas":2}}'. The replace command replaces a resource, for example, kubectl replace -f my-deployment.yaml. The wait command waits for a specific condition, such as kubectl wait --for=condition=available --timeout=60s deployment/my-deployment. The kustomize command builds a kustomization target, using kubectl kustomize ./my-directory.
Settings commands include label, which updates the labels on a resource, like kubectl label pods my-pod new-label=label-value. The annotate command updates annotations, with kubectl annotate pods my-pod description="my description". The completion command outputs shell completion code for the specified shell, for example, kubectl completion bash.
Other commands include api-resources, which prints supported API resources on the server with kubectl api-resources, and api-versions, which prints supported API versions using kubectl api-versions. The config command modifies kubeconfig files, such as with kubectl config view. The plugin command interacts with plugins, using kubectl plugin list to list available plugins. Lastly, the version command prints the client and server version information, like kubectl version --short.