kubectl

Kubernetes cluster management with kubectl — pods, deployments, services, secrets, and debugging.

Context and Configuration

Switch contexts and set default namespace
kubectl config get-contexts
kubectl config use-context k3s-local
kubectl config set-context --current --namespace=monitoring

Pod Operations

List pods with detail
kubectl get pods -A                                 # all namespaces
kubectl get pods -o wide                            # node, IP, status
kubectl get pods -l app=nginx                       # label selector
kubectl get pods --field-selector=status.phase=Running
Describe and inspect pods
kubectl describe pod nginx-7bf8c77b5b-x2k9j
kubectl get pod nginx-7bf8c77b5b-x2k9j -o yaml     # full YAML spec
Custom output with jsonpath
kubectl get pods -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.phase}{"\n"}{end}'

Logs and Debugging

Read pod logs
kubectl logs nginx-7bf8c77b5b-x2k9j
kubectl logs -f nginx-7bf8c77b5b-x2k9j             # follow (stream)
kubectl logs nginx-7bf8c77b5b-x2k9j -c sidecar     # specific container
kubectl logs -l app=api --all-containers            # all matching pods
kubectl logs --previous nginx-7bf8c77b5b-x2k9j     # previous crash
Exec into a running pod
kubectl exec -it nginx-7bf8c77b5b-x2k9j -- sh
kubectl exec nginx-7bf8c77b5b-x2k9j -- cat /etc/resolv.conf
Cluster events sorted by time
kubectl get events --sort-by='.lastTimestamp'
kubectl get events -n kube-system --field-selector reason=Failed

Apply and Delete

Declarative resource management
kubectl apply -f manifest.yaml
kubectl apply -k overlays/prod/                     # Kustomize overlay
kubectl diff -f manifest.yaml                       # preview before applying
kubectl delete -f manifest.yaml
kubectl delete pod stuck-pod --grace-period=0 --force   # force-delete

Deployments

Rollout management
kubectl rollout status deploy/nginx
kubectl rollout restart deploy/nginx                # rolling restart
kubectl rollout undo deploy/nginx                   # rollback to previous
kubectl rollout history deploy/nginx                # revision history
Scale replicas
kubectl scale deploy/nginx --replicas=3

Services and Port Forwarding

Inspect services
kubectl get svc -o wide
kubectl get endpoints nginx-svc
Forward local port to cluster service
kubectl port-forward svc/grafana 3000:80
kubectl port-forward pod/debug-pod 8080:8080

Secrets and ConfigMaps

Decode a secret value
kubectl get secret db-creds -o jsonpath='{.data.password}' | base64 -d
Create secrets
kubectl create secret generic db-creds --from-literal=password=changeme
kubectl create secret tls my-tls --cert=tls.crt --key=tls.key

Resource Usage

CPU and memory consumption
kubectl top pods --sort-by=memory
kubectl top nodes
kubectl top pods -A --sort-by=cpu | head -20

Node Management

Drain and cordon for maintenance
kubectl cordon worker-01                            # no new pods scheduled
kubectl drain worker-01 --ignore-daemonsets --delete-emptydir-data
kubectl uncordon worker-01                          # return to service