$linuxjunkies
>

kubectl(1)

kubectl is the command-line interface for controlling Kubernetes clusters and managing containerized applications.

UbuntuDebianFedoraArch

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

FlagWhat it does
-n, --namespacespecify the Kubernetes namespace to operate in (default: default)
-A, --all-namespaceslist resources across all namespaces
-o, --outputoutput format: json, yaml, wide, name, custom-columns (default: wide for get)
--kubeconfigpath to the kubeconfig file (default: ~/.kube/config)
-l, --selectorfilter resources by label selector (e.g., app=nginx)
--dry-runpreview changes without applying them (client or server)
-f, --filenamespecify file or directory containing Kubernetes manifests
--contextspecify the kubeconfig context to use
-v, --verboseincrease verbosity level for debugging
--recordrecord the command in resource annotation for rollback history

Examples

list all pods in the default namespace

kubectl get pods -n default

create or update resources defined in a YAML manifest file

kubectl apply -f deployment.yaml

show detailed information about a specific pod including events and conditions

kubectl describe pod nginx-abc123 -n production

stream the last 100 lines of logs from a deployment (follow mode)

kubectl logs deployment/myapp --tail=100 -f

open an interactive bash shell inside a running pod

kubectl exec -it pod/myapp -- /bin/bash

scale a deployment to 5 replicas

kubectl scale deployment nginx --replicas=5

forward local port 8080 to port 3000 on a service

kubectl port-forward svc/myapp 8080:3000

list all cluster nodes with additional details like IP and kernel version

kubectl get nodes -o wide

Related commands