Command-line tool for communicating with the Kubernetes API server to manage cluster resources.

Table of Contents#

  1. Overview
  2. Installation and Configuration
  3. Basic Commands
  4. Common Resource Short Names
  5. Output Formatting
  6. Troubleshooting
  7. See Also
  8. Sources

1. Overview#

kubectl is the official command-line interface for Kubernetes. It communicates with the Kubernetes API server to deploy applications, inspect and manage cluster resources, and view logs. Almost every Kubernetes operation can be performed through kubectl.

The tool reads cluster connection details from a kubeconfig file (default: ~/.kube/config), which defines clusters, users, and contexts.

2. Installation and Configuration#

2.1 Prerequisites#

  • Network access and appropriate RBAC permissions to a Kubernetes cluster
  • A valid kubeconfig file with cluster credentials

2.2 Installation via Package Managers#

On Windows#

Using Chocolatey:

choco install kubernetes-cli

Using winget:

winget install Kubernetes.kubectl

On macOS#

Using Homebrew:

brew install kubectl

On Linux#

Using apt (Debian/Ubuntu):

# Add the Kubernetes apt repository
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.31/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
echo "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.31/deb/ /" | sudo tee /etc/apt/sources.list.d/kubernetes.list

sudo apt-get update && sudo apt-get install -y kubectl

Using dnf (Fedora/RHEL/CentOS):

sudo dnf install -y kubectl

Using pacman (Arch Linux):

sudo pacman -S kubectl

2.3 Configuring kubectl#

Set the KUBECONFIG environment variable or place the config file at the default path:

# Use default location
cp <kubeconfig-file> ~/.kube/config

# Or set the environment variable to use a custom path
export KUBECONFIG=/path/to/kubeconfig.yaml

# Merge multiple kubeconfig files
export KUBECONFIG=~/.kube/config:/path/to/other-config

# Verify connectivity
kubectl cluster-info

3. Basic Commands#

3.1 Viewing Cluster and Resource Information#

ActionCommand
View cluster infokubectl cluster-info
List nodeskubectl get nodes
List all namespaceskubectl get namespaces
List all API resourceskubectl api-resources
View cluster component statuskubectl get componentstatuses

3.2 Resource Management Commands#

ActionCommand
Create a deploymentkubectl create deployment <name> --image=<image>
List podskubectl get pods
List pods (all namespaces)kubectl get pods -A
List serviceskubectl get svc
Expose a deploymentkubectl expose deployment <name> --type=LoadBalancer --port=<port>
Edit a deploymentkubectl edit deployment <name>
Scale a deploymentkubectl scale deployment <name> --replicas=<count>
Delete a deploymentkubectl delete deployment <name>
Apply a manifestkubectl apply -f <file.yaml>
Delete from a manifestkubectl delete -f <file.yaml>

3.3 Cluster Management Commands#

ActionCommand
Apply configuration from filekubectl apply -f <file.yaml>
Drain a node for maintenancekubectl drain <node-name> --ignore-daemonsets --delete-emptydir-data
Uncordon a node after maintenancekubectl uncordon <node-name>
Cordon a node (mark unschedulable)kubectl cordon <node-name>
Taint a nodekubectl taint nodes <node-name> <key>=<value>:<effect>

3.4 Debugging Commands#

ActionCommand
View pod logskubectl logs <pod-name>
View previous container logskubectl logs <pod-name> --previous
Follow logs in real timekubectl logs -f <pod-name>
Exec into a podkubectl exec -it <pod-name> -- /bin/bash
Describe a resourcekubectl describe <resource-type> <name>
Port-forward to a podkubectl port-forward <pod-name> <local-port>:<pod-port>
Run a temporary debug podkubectl run debug --rm -it --image=busybox -- sh

4. Common Resource Short Names#

Resource TypeShort Name
Certificate Signing Requestscsr
Config Mapscm
DaemonSetsds
Deploymentsdeploy
Endpointsep
Eventsev
Horizontal Pod Autoscalershpa
Ingressesing
Limit Rangeslimits
Namespacesns
Nodesno
Persistent Volume Claimspvc
Persistent Volumespv
Podspo
Pod Disruption Budgetspdb
ReplicaSetsrs
Replication Controllersrc
Resource Quotasquota
Service Accountssa
Servicessvc
StatefulSetssts
Storage Classessc

Note: Pod Security Policies (psp) were removed in Kubernetes 1.25 (August 2022). Use Pod Security Standards with the built-in Pod Security Admission controller instead.

5. Output Formatting#

kubectl supports multiple output formats for scripting and readability:

# Wide output with additional columns
kubectl get pods -o wide

# JSON output
kubectl get pods -o json

# YAML output
kubectl get pods -o yaml

# Custom columns
kubectl get pods -o custom-columns=NAME:.metadata.name,STATUS:.status.phase

# JSONPath for extracting specific fields
kubectl get pods -o jsonpath='{.items[*].metadata.name}'

# Sort output by a field
kubectl get pods --sort-by=.metadata.creationTimestamp

Troubleshooting#

IssueCauseSolution
"The connection to the server was refused"Cluster is unreachable or kubeconfig is wrongVerify KUBECONFIG path; run kubectl config view to check the server URL
"error: You must be logged in to the server (Unauthorized)"Expired or invalid credentials in kubeconfigRefresh credentials; check token expiry; re-export kubeconfig from the cluster
"No resources found in default namespace"Resources exist in a different namespaceAdd -A for all namespaces or -n <namespace> to target the correct one
kubectl is slow or hangsNetwork timeout reaching the API serverCheck network connectivity; try kubectl cluster-info to confirm reachability
"unable to recognize file: no matches for kind"API version in the manifest does not match clusterRun kubectl api-versions to list supported versions; update the manifest accordingly

See Also#

Sources#