kubectl(1)
kubectl is the command-line interface for controlling Kubernetes clusters and managing containerized applications.
Synopsis
kubectl [command] [TYPE] [NAME] [flags]Description
kubectl is the primary tool for interacting with Kubernetes clusters. It allows you to deploy applications, inspect and manage cluster resources, view logs, and execute commands inside containers. kubectl communicates with the Kubernetes API server to perform operations on clusters.
Commands are organized into categories: basic (create, get, delete), running workloads (run, exec, logs), configuration (apply, set, patch), and cluster management (cluster-info, top, drain). Most commands operate on resource types like pods, services, deployments, and configmaps.
Common options
| Flag | What it does |
|---|---|
-n, --namespace | specify the Kubernetes namespace to operate in (default: default) |
-A, --all-namespaces | list resources across all namespaces |
-o, --output | output format: json, yaml, wide, name, custom-columns (default: wide for get) |
--kubeconfig | path to the kubeconfig file (default: ~/.kube/config) |
-l, --selector | filter resources by label selector (e.g., app=nginx) |
--dry-run | preview changes without applying them (client or server) |
-f, --filename | specify file or directory containing Kubernetes manifests |
--context | specify the kubeconfig context to use |
-v, --verbose | increase verbosity level for debugging |
--record | record the command in resource annotation for rollback history |
Examples
list all pods in the default namespace
kubectl get pods -n defaultcreate or update resources defined in a YAML manifest file
kubectl apply -f deployment.yamlshow detailed information about a specific pod including events and conditions
kubectl describe pod nginx-abc123 -n productionstream the last 100 lines of logs from a deployment (follow mode)
kubectl logs deployment/myapp --tail=100 -fopen an interactive bash shell inside a running pod
kubectl exec -it pod/myapp -- /bin/bashscale a deployment to 5 replicas
kubectl scale deployment nginx --replicas=5forward local port 8080 to port 3000 on a service
kubectl port-forward svc/myapp 8080:3000list all cluster nodes with additional details like IP and kernel version
kubectl get nodes -o wide