kubectl Commands Reference

54 commands with practical examples and interview context.

kubectl annotate

Update annotations on a resource. Annotations store non-identifying metadata for tools, libraries, and operational information.

kubectl api-resources --namespaced

Filter API resources by scope — namespaced or cluster-wide. Essential for understanding resource boundaries and RBAC planning.

kubectl api-resources

Print the supported API resources on the server, including their short names, API group, and whether they are namespaced.

kubectl api-versions

Print the supported API versions on the server in the form group/version. Useful for checking API availability and migration planning.

kubectl apply

Apply a configuration to a resource by file, stdin, or URL. Creates the resource if it does not exist, updates it if it does.

kubectl attach

Attach to a process running inside an existing container. Similar to docker attach, it connects to the main process's stdin/stdout/stderr.

kubectl auth can-i

Check whether an action is allowed by the current user or a specified user. Essential for RBAC debugging and verification.

kubectl autoscale

Automatically scale the number of pods in a deployment, ReplicaSet, or StatefulSet based on observed CPU utilization or other metrics.

kubectl certificate approve

Approve a CertificateSigningRequest (CSR). Used when manually managing TLS certificates for nodes, users, or webhooks.

kubectl cluster-info

Display addresses of the control plane and cluster services. A quick health-check entry point for any Kubernetes cluster.

kubectl completion

Generate autocompletion scripts for bash, zsh, fish, or PowerShell. Dramatically improves kubectl productivity.

kubectl config current-context

Display the name of the current context from the kubeconfig. A quick check to confirm which cluster you are targeting.

kubectl config get-contexts

Display all contexts defined in the kubeconfig, showing the current context, cluster, user, and namespace for each.

kubectl config set-context

Set a context entry in the kubeconfig file, or modify an existing one. Used to bind a cluster, user, and namespace together.

kubectl config use-context

Set the current context in the kubeconfig file. This determines which cluster and user kubectl communicates with.

kubectl config view

Display merged kubeconfig settings or a specified kubeconfig file. Essential for understanding cluster access configuration.

kubectl cordon

Mark a node as unschedulable, preventing new pods from being scheduled onto it. Existing pods continue running.

kubectl cp

Copy files and directories to and from containers in pods. Uses tar internally to transfer data.

kubectl create

Create a resource from a file or stdin. Supports imperative creation of many resource types.

kubectl debug

Create debugging sessions for troubleshooting workloads and nodes. Supports ephemeral containers, pod copies, and node debugging.

kubectl delete

Delete resources by filenames, stdin, resource type and name, or by label selector.

kubectl describe

Show detailed information about a specific resource or group of resources, including events.

kubectl diff

Diff the live cluster state against the configuration that would be applied. A dry-run comparison tool for safe deployments.

kubectl drain

Drain a node by evicting all pods gracefully. The node is cordoned first, then pods are evicted respecting PodDisruptionBudgets.

kubectl edit

Edit a resource directly in your default editor. The resource is updated when you save and close the editor.

kubectl events

Display events in the cluster. Events provide a timeline of what happened to resources — scheduling, pulling images, errors, and more.

kubectl exec

Execute a command in a container. Provides interactive shell access for debugging running containers.

kubectl explain

Get documentation for resource types and their fields directly from the API server schema.

kubectl expose

Expose a resource as a new Kubernetes Service. Supports pods, deployments, replica sets, and replication controllers.

kubectl get --field-selector

Reference for field selectors that filter resources by their spec and status fields, complementing label selectors for precise queries.

kubectl get

Display one or many resources. The most frequently used kubectl command for inspecting cluster state.

kubectl kustomize

Build a set of KRM resources from a kustomization directory. Renders overlays and patches without a separate tool installation.

kubectl get -l

Reference for label selector syntax used to filter resources across kubectl commands. Covers equality-based and set-based selectors.

kubectl label

Update the labels on a resource. Labels are key-value pairs used for organizing, selecting, and managing Kubernetes resources.

kubectl logs

Print the logs for a container in a pod. Supports streaming, multi-container pods, and previous container instances.

kubectl get -o

Reference for all kubectl output formats including json, yaml, wide, jsonpath, custom-columns, and go-template.

kubectl patch

Update fields of a resource using strategic merge patch, JSON merge patch, or JSON patch.

kubectl port-forward

Forward one or more local ports to a pod or service. Creates a tunnel for accessing cluster resources from your local machine.

kubectl proxy

Run a proxy to the Kubernetes API server on localhost. Handles authentication and allows direct access to the REST API.

kubectl replace

Replace a resource by filename or stdin. The resource must already exist, and the entire object is replaced with the new definition.

kubectl rollout history

View the rollout history of a resource, including revision numbers and change causes.

kubectl rollout pause

Pause the rollout of a resource. Prevents new ReplicaSets from being created while you make multiple changes to the pod template.

kubectl rollout restart

Trigger a rolling restart of a deployment, DaemonSet, or StatefulSet without changing the pod template.

kubectl rollout resume

Resume a paused rollout for a deployment, DaemonSet, or StatefulSet. Triggers the rollout of any pending changes.

kubectl rollout status

Show the status of a rollout. Watches the deployment progress and reports whether it completed successfully.

kubectl rollout undo

Roll back to a previous revision of a deployment, DaemonSet, or StatefulSet.

kubectl run

Create and run a particular image in a pod. Useful for quick testing and debugging in the cluster.

kubectl scale

Set a new size for a deployment, ReplicaSet, or StatefulSet by updating the replica count.

kubectl set

Set specific features on objects. Provides subcommands to update images, environment variables, resources, selectors, service accounts, and subjects.

kubectl taint

Update the taints on one or more nodes. Taints work with tolerations to control which pods can be scheduled on specific nodes.

kubectl top

Display resource usage (CPU/memory) for pods and nodes. Requires the Metrics Server to be installed in the cluster.

kubectl uncordon

Mark a node as schedulable again, allowing the scheduler to place new pods on it. Used after maintenance or troubleshooting.

kubectl version

Print the client and server version information for the current context. Essential for compatibility checks and troubleshooting.

kubectl wait

Wait for a specific condition on one or more resources. Blocks until the condition is met or a timeout is reached.