LinuxCommandLibrary

kubectl

Manage Kubernetes clusters

TLDR

List information about a resource with more details

$ kubectl get [pods|service|deployment|ingress|...] [[-o|--output]] wide
copy

Update specified pod with the label unhealthy and the value true
$ kubectl label pods [name] unhealthy=true
copy

List all resources with different types
$ kubectl get all
copy

Display resource (CPU/Memory/Storage) usage of nodes or pods
$ kubectl top [pods|nodes]
copy

Print the address of the master and cluster services
$ kubectl cluster-info
copy

Display an explanation of a specific field
$ kubectl explain [pods.spec.containers]
copy

Print the logs for a container in a pod or specified resource
$ kubectl logs [pod_name]
copy

Run command in an existing pod
$ kubectl exec [pod_name] -- [ls /]
copy

SYNOPSIS

kubectl [flags] COMMAND [args]

PARAMETERS

-n, --namespace string
    Namespace scope for the request

--context string
    Kubeconfig context to use

--kubeconfig string
    Path to kubeconfig file

-o, --output string
    Output format (json|yaml|wide|name|jsonpath|...)

--dry-run string
    "none|server|client" to simulate without applying

-v, --v level
    Logging verbosity (0-9)

--help
    Display help

--as string
    Username to impersonate

-f, --filename file
    Filename, directory, or URL for config

-R, --recursive
    Process directory recursively

-l, --selector string
    Selector (label query) to filter

-A, --all-namespaces
    All namespaces

DESCRIPTION

Kubectl is the official command-line tool for Kubernetes, enabling users to interact with clusters via the Kubernetes API. It supports deploying applications, inspecting resources, scaling workloads, and troubleshooting.

Key uses include applying YAML manifests (kubectl apply -f file.yaml), listing pods (kubectl get pods), viewing logs (kubectl logs pod-name), port-forwarding (kubectl port-forward), and executing shells in containers (kubectl exec -it pod -- /bin/sh). Kubectl authenticates using kubeconfig files storing clusters, users, and contexts.

Output formats include YAML, JSON, tables; dry-run simulates changes. Global flags control namespace, verbosity, and config. Subcommands like create, delete, describe, edit cover most operations. Essential for developers, operators, and CI/CD in containerized environments.

Requires kubectl binary installed, kubeconfig (~/.kube/config), and cluster access. Supports aliases like k for quick use.

CAVEATS

Requires kubeconfig and cluster access; some flags subcommand-specific; high verbosity floods output; dry-run=client doesn't validate server-side.

COMMON SUBCOMMANDS

apply - Apply config
create - Create resource
delete - Delete resource
describe - Show details
exec - Exec in pod
get - List resources
logs - Pod logs
port-forward - Port forward

CONFIG SOURCES

Kubeconfig from kubectl config; clusters via kubeadm, minikube, kind; auth via tokens, certs, OIDC.

HISTORY

Introduced in Kubernetes v0.1 (2014) by Google; matured with Kubernetes 1.0 (2015). Now CNCF project, versioned with Kubernetes (e.g., v1.28). Widely adopted for container orchestration.

SEE ALSO

helm(1), kustomize(1), crictl(1)

Copied to clipboard