🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

kubectl Command

Intermediate Containers man(1)

Kubernetes cluster management CLI

📅 Updated: Mar 16, 2026
SYNTAX
kubectl [COMMAND] [TYPE] [NAME] [FLAGS]

What Does kubectl Do?

The kubectl command is the official command-line tool for interacting with Kubernetes clusters. It communicates with the Kubernetes API server to deploy applications, inspect and manage cluster resources, view logs, and execute commands inside containers running on the cluster.

kubectl is the Swiss Army knife of Kubernetes — virtually every Kubernetes operation can be performed through kubectl. It supports both imperative commands (kubectl run, kubectl create) and declarative management (kubectl apply -f manifest.yaml), with the declarative approach being the recommended practice for production workloads.

The tool uses a kubeconfig file (~/.kube/config) to connect to one or more clusters. You can switch between clusters and namespaces using contexts, making it easy to manage development, staging, and production environments from a single workstation.

kubectl supports output in multiple formats (JSON, YAML, table, custom-columns, jsonpath) and can be extended with plugins through the krew plugin manager. It is essential for anyone working with Kubernetes — from developers deploying their applications to platform engineers managing cluster infrastructure.

Options & Flags

OptionDescriptionExample
get List resources (pods, services, deployments, etc.) kubectl get pods -n production
describe Show detailed information about a resource kubectl describe pod nginx-abc123
apply -f Apply a configuration file (declarative) kubectl apply -f deployment.yaml
delete Delete resources by name or from file kubectl delete pod nginx-abc123
logs View container logs (supports -f for follow) kubectl logs -f deployment/myapp
exec Execute a command in a container kubectl exec -it myapp-pod -- bash
scale Scale a deployment to N replicas kubectl scale deployment myapp --replicas=5
rollout Manage deployment rollouts (status, history, undo) kubectl rollout undo deployment/myapp
port-forward Forward a local port to a pod kubectl port-forward svc/myapp 8080:80
config Manage kubeconfig (contexts, clusters, users) kubectl config use-context production

Practical Examples

#1 List pods in all namespaces

Show all pods across all namespaces with node assignment and IP addresses.
$ kubectl get pods --all-namespaces -o wide

#2 Deploy from YAML manifest

Declaratively apply multiple Kubernetes manifests. Creates or updates resources to match the desired state.
$ kubectl apply -f deployment.yaml -f service.yaml -f ingress.yaml

#3 Debug a failing pod

Show pod events and conditions, then view logs from the previous (crashed) container instance.
$ kubectl describe pod myapp-abc123 -n production && kubectl logs myapp-abc123 -n production --previous

#4 Shell into a running pod

Open an interactive shell inside a running container for debugging. Use -- to separate kubectl args from the command.
$ kubectl exec -it myapp-abc123 -- /bin/sh

#5 Scale a deployment

Scale to 5 replicas and wait for the rollout to complete.
$ kubectl scale deployment myapp --replicas=5 && kubectl rollout status deployment/myapp

#6 Rollback a deployment

Revert a deployment to the previous revision. Kubernetes keeps revision history for rollbacks.
$ kubectl rollout undo deployment/myapp && kubectl rollout status deployment/myapp

#7 Port-forward to a service

Forward local port 5432 to the PostgreSQL service in the cluster. Access the database at localhost:5432.
$ kubectl port-forward svc/postgres 5432:5432 -n database

#8 Get resources in JSON/custom format

Extract pod names and phases using jsonpath for scripting and reporting.
$ kubectl get pods -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.phase}{"\n"}{end}'

Tips & Best Practices

Use aliases: Set alias k=kubectl and enable kubectl completion in bash/zsh for dramatically faster command entry. Add to .bashrc: source <(kubectl completion bash)
Check context before destructive ops: Always verify your current context before delete/apply: kubectl config current-context. Accidentally deleting resources in production is a real risk.
Declarative > Imperative: Use kubectl apply -f manifest.yaml (declarative) in production, not kubectl run/create (imperative). Store manifests in git for versioning and reproducibility.
Use --dry-run for testing: Generate YAML without applying: kubectl create deployment myapp --image=nginx --dry-run=client -o yaml > deployment.yaml
Install krew for plugins: Krew is the plugin manager for kubectl. Popular plugins: ctx (context switching), ns (namespace switching), neat (clean YAML output), tree (resource hierarchy).

Frequently Asked Questions

How do I switch between Kubernetes clusters?
Use kubectl config use-context CONTEXT_NAME. List available contexts with kubectl config get-contexts. Each context defines a cluster, user, and default namespace.
How do I view logs from a crashed pod?
Use kubectl logs POD_NAME --previous to see logs from the previous container instance. Use kubectl describe pod POD_NAME to see crash reasons and events.
What is the difference between kubectl apply and kubectl create?
kubectl create is imperative — it creates a resource and fails if it already exists. kubectl apply is declarative — it creates or updates resources to match the YAML, and is idempotent (safe to run multiple times).
How do I delete all resources in a namespace?
kubectl delete all --all -n NAMESPACE removes most resources. For a complete cleanup, delete the entire namespace: kubectl delete namespace NAMESPACE.
How do I check why a pod is not starting?
Run kubectl describe pod POD_NAME to see events (image pull errors, resource limits, scheduling failures). Check kubectl get events --sort-by=.lastTimestamp for cluster-wide issues.

Master Linux with Professional eBooks

Curated IT eBooks covering Linux, DevOps, Cloud, and more

Browse Books →