Skip to content

kubectl

125 commands

The Kubernetes command line: inspect and manage clusters, workloads, RBAC and secrets — and check what an identity is allowed to do.

  • Kubernetes
  • containers
  • RBAC

Install kubectl

sudo snap install kubectl --classic

Basic Syntax & Help2

kubectl --help

Displays the root level help menu for kubectl commands and global flags.

kubectl options

Lists all global command-line options available for every kubectl command.

Cluster Information4

kubectl version

Prints the client and server version information in JSON format.

Link
kubectl cluster-info

Displays addresses of the master and services, or dumps detailed cluster debugging info.

Link
kubectl api-resources

Lists all supported API resources available on the server, filtered by namespaced scope.

Link
kubectl api-versions

Lists the API versions that are supported by the current Kubernetes cluster.

Context & Configuration5

kubectl config view

Displays the current kubeconfig settings, optionally minimizing output to the current context.

Link
kubectl config get-contexts

Lists all contexts defined in the kubeconfig file and highlights the active one.

kubectl config use-context

Switches the current active context to the specified context name.

Link
kubectl config set-context

Modifies the current context to set a default namespace for future commands.

Link
kubectl config delete-context

Removes the specified context entry from the kubeconfig file.

Link

Namespace Management2

kubectl get namespaces

Lists all namespaces in the cluster along with their associated labels.

Link
kubectl create namespace

Generates the YAML manifest for a new namespace without actually creating it.

Link

Resource Inspection (Basic)3

kubectl get all

Lists most resources (pods, services, deployments) across every namespace in the cluster.

Link
kubectl get pods

Lists pods with extended information such as IP address and Node assignment.

Link
kubectl get services

Lists services sorted alphabetically by their name.

Link

Resource Inspection (Detail)2

kubectl describe pod

Shows detailed configuration, status, events, and conditions for a specific pod.

Link
kubectl describe node

Displays detailed node status, allocated resources, and system information.

Link

Imperative Creation5

kubectl run

Creates a single pod instance imperatively using the specified image.

Link
kubectl create deployment

Creates a deployment with a specified replica count and image.

Link
kubectl expose

Exposes a resource as a new Kubernetes Service.

Link
kubectl create job

Creates a Job resource that runs a specific command to completion.

Link
kubectl create cronjob

Creates a CronJob that runs on a specific schedule.

Link

Debugging & Interaction10

kubectl logs

Print the logs for a container in a pod or specified resource.

Link
kubectl logs (follow)

Streams the logs for a container in real-time, similar to tail -f.

Link
kubectl logs (multi-container)

Retrieves logs from a specific container within a multi-container pod.

Link
kubectl logs (previous)

Retrieves logs from a previous instantiation of a container (e.g., after a crash).

Link
kubectl exec

Executes a command inside a container with an interactive TTY.

Link
kubectl cp

Copies files and directories to and from containers.

Link
kubectl port-forward

Forwards one or more local ports to a pod.

Link
kubectl attach

Attaches to a running process in a container to view output or interact.

Link
kubectl top pod

Displays current CPU and memory usage for pods and their containers.

Link
kubectl top node

Displays current CPU and memory usage for nodes.

Link

Declarative Management7

kubectl apply

Apply a configuration to a resource by filename or directory.

Link
kubectl apply (server-side)

Uses server-side apply to manage field ownership and merge conflicts.

Link
kubectl delete

Delete resources defined in a configuration file.

Link
kubectl diff

Displays differences between the current live cluster state and the local manifest.

Link
kubectl replace

Replaces a resource by filename, optionally forcing deletion and recreation.

Link
kubectl kustomize

Builds a kustomization target and prints the resulting YAML to stdout.

Link
kubectl explain

Describes the fields and structure of a Kubernetes resource type.

Link

Scaling & Rollouts8

kubectl scale

Updates the replica count of a deployment, replicaset, or statefulset.

Link
kubectl autoscale

Creates a HorizontalPodAutoscaler to automatically manage scale.

Link
kubectl rollout status

Watches the status of a rollout until it is complete.

Link
kubectl rollout history

Views the revision history of a rollout.

Link
kubectl rollout undo

Rolls back a deployment to a previous revision.

Link
kubectl rollout restart

Restarts a resource, triggering a new rollout without configuration changes.

Link
kubectl rollout pause

Pauses a rollout to perform multiple changes before resuming.

Link
kubectl rollout resume

Resumes a paused rollout.

Link

Config & Secrets4

kubectl create configmap

Creates a ConfigMap from a file, directory, or literal value.

Link
kubectl create secret generic

Creates a generic secret from literal values or files.

Link
kubectl create secret docker-registry

Creates a secret for authenticating with a Docker registry.

Link
kubectl create secret tls

Creates a TLS secret from a certificate and key file.

Link

Modification & Patching8

kubectl label

Updates the labels on a resource, optionally overwriting existing ones.

Link
kubectl annotate

Updates the annotations on a resource.

Link
kubectl set image

Updates the image of a container in a pod template.

Link
kubectl set env

Updates environment variables on a pod template.

Link
kubectl set resources

Specifies compute resource requirements for a container.

Link
kubectl edit

Edit a resource on the server using the default text editor.

Link
kubectl patch (merge)

Update field(s) of a resource using strategic merge patch.

Link
kubectl patch (json)

Update resource using RFC 6902 JSON patch.

Link

Node Maintenance4

kubectl cordon

Mark node as unschedulable to prevent new pods from arriving.

Link
kubectl uncordon

Mark node as schedulable, allowing new pods to start.

Link
kubectl drain

Prepare node for maintenance by evicting all pods.

Link
kubectl taint

Update the taints on one or more nodes.

Link

Networking2

kubectl get endpoints

Displays the endpoints (IP:Port) backing a service.

Link
kubectl get ingress

Lists Ingress resources to view external access rules.

Link

Output Formatting8

kubectl get -o wide

Output in plain text with additional columns (e.g., Node, IP).

Link
kubectl get -o yaml

Output the full resource definition in YAML format.

Link
kubectl get -o json

Output the full resource definition in JSON format.

Link
kubectl get --show-labels

Include a column showing labels attached to resources.

Link
kubectl get --sort-by

Sort list output by a specific JSONPath expression.

Link
kubectl get -o jsonpath (names)

Extract and print specific fields using a JSONPath expression.

Link
kubectl get -o custom-columns

Define custom table columns based on field paths.

Link
kubectl get --no-headers

Suppress the header row in output, useful for scripts.

Link

Filtering2

kubectl get -l (label selector)

Filter resources by label selector equality or set-based requirements.

Link
kubectl get --field-selector

Filter resources based on the value of resource fields.

Link

Wait & Automation2

kubectl wait

Wait for a specific condition on one or more resources.

Link
kubectl delete (wait)

Delete resources without waiting for finalizer cleanup (returns immediately).

Link

RBAC & Security10

kubectl create serviceaccount

Creates a new ServiceAccount in the current namespace.

Link
kubectl create role

Creates a Role with specific permissions within a namespace.

Link
kubectl create rolebinding

Binds a Role to a user or group in a specific namespace.

Link
kubectl create clusterrole

Creates a ClusterRole with permissions across the entire cluster.

Link
kubectl create clusterrolebinding

Binds a ClusterRole to a user or group globally.

Link
kubectl auth can-i

Checks if the current user has permission to perform an action.

Link
kubectl auth can-i (impersonate)

Checks permissions impersonating another user or group.

Link
kubectl auth reconcile

Patches RBAC roles and bindings to match the input file if safe.

Link
kubectl certificate approve

Approves a CertificateSigningRequest (CSR).

Link
kubectl certificate deny

Denies a CertificateSigningRequest (CSR).

Link

Events & Diagnostics2

kubectl get events

Lists recent events in the namespace, sorted by time.

Link
kubectl get events (watch)

Streams events as they happen without listing history.

Link

Advanced Debugging3

kubectl debug (node)

Creates a debugging pod on a node to troubleshoot host issues.

Link
kubectl debug (pod)

Attaches a new container to a running pod for debugging purposes.

Link
kubectl alpha events

Experimental command to view events filtered for a specific object.

Link

Raw API Access2

kubectl get --raw

Sends a raw GET request to the Kubernetes API server.

Link
kubectl proxy

Creates a proxy server between localhost and the Kubernetes API server.

Link

Plugin Management1

kubectl plugin list

Lists all available kubectl plugins installed in the path.

Link

Cluster Management1

kubectl delete node

Removes a node object from the cluster (does not terminate VM).

Link

Cleanup2

kubectl delete pods (force)

Forcefully deletes a pod, bypassing graceful termination.

Link
kubectl delete evictions

Deletes all failed pods across all namespaces (common cleanup task).

Link

Blue Team/Defense4

kubectl get pod (security context)

Audits the security context of running pods to find misconfigurations.

Link
kubectl get psp

Lists PodSecurityPolicies (if enabled) to verify admission controls.

Link
kubectl get networkpolicies

Audits network segmentation rules across the cluster.

Link
kubectl get clusterrolebindings (audit)

Audits global permissions to identify privileged users.

Link

Incident Response3

kubectl get pods (by time)

Lists pods ordered by creation time to identify recently launched rogue containers.

Link
kubectl get secrets (audit)

Lists secrets and their types to audit for exposed credentials.

Link
kubectl delete (cascade false)

Deletes the deployment controller but leaves the pods running for investigation.

Link

Advanced Workflow8

kubectl cp (snapshot)

Extracts logs or files from a compromised pod for forensic analysis.

Link
kubectl patch (finalizers)

Removes finalizers to force deletion of stuck resources.

Link
kubectl diff (server-side)

Previews changes using server-side apply logic.

Link
kubectl replace (force)

Forces resource replacement by deleting and recreating it immediately.

Link
kubectl get (watch)

Watches for changes to the list of pods in real-time.

Link
kubectl completion bash

Generates and sources the bash auto-completion code for the current shell.

Link
kubectl completion zsh

Generates and sources the zsh auto-completion code.

Link
kubectl convert

Converts config files between different API versions (requires plugin).

Link

Metrics & Discovery1

kubectl get --raw (nodes)

Fetches raw metrics directly from a specific node.

Link

Complex Filtering2

kubectl get (jsonpath conditional)

Lists only nodes that are currently cordon/unschedulable.

Link
kubectl get (custom columns images)

Audits all images running across the entire cluster.

Link

Service Mesh / Sidecar1

kubectl describe (sidecar)

Inspects sidecar container details often used in service meshes.

Link

API Discovery1

kubectl explain (recursive)

Recursively prints all fields and sub-fields for a resource spec.

Link

Lifecycle Management1

kubectl drain (force)

Forces a node drain even if there are pods not managed by a controller.

Link

Security Auditing1

kubectl auth can-i (list)

Lists all allowed actions for a specific service account.

Link

Resource Tuning1

kubectl set serviceaccount

Updates the service account used by a workload resource.

Link

Connectivity Check2

kubectl run (curl)

Launches a temporary curl pod to test internal cluster connectivity.

Link
kubectl run (dns)

Launches a temporary pod to verify internal DNS resolution.

Link

Final Cleanup1

kubectl delete all

Deletes all resources in a namespace (use with extreme caution).

Link