Skip to content

Latest commit

 

History

History
266 lines (209 loc) · 4.99 KB

File metadata and controls

266 lines (209 loc) · 4.99 KB

Kubernetes kubectl Cheat Sheet

1. Basic Cluster and Node Operations

View Cluster Information

kubectl cluster-info
kubectl get nodes
kubectl describe node <node-name>
kubectl get nodes -o wide

Check Node Conditions (Resource Pressure)

kubectl describe node <node-name> | grep -i pressure
  • MemoryPressure: High memory usage
  • DiskPressure: Low disk space
  • PIDPressure: Too many processes running

Cordon & Uncordon Nodes

kubectl cordon <node-name>         # Mark node as unschedulable
kubectl uncordon <node-name>       # Allow scheduling on the node again

Drain a Node (Evict Pods Before Maintenance)

kubectl drain <node-name> --ignore-daemonsets --delete-local-data

2. Pod Management & Evictions

View Running Pods

kubectl get pods -A              # List all pods in all namespaces
kubectl get pods -n <namespace>  # List pods in a specific namespace
kubectl get pods -o wide         # Show detailed pod information
kubectl describe pod <pod-name>  # Show pod details, including events

Check Pod Events & Eviction History

kubectl get events --sort-by=.metadata.creationTimestamp
kubectl describe pod <pod-name> | grep -i eviction

Manually Evict a Pod

kubectl delete pod <pod-name>

Check QoS of a Pod

kubectl get pod <pod-name> -o jsonpath="{.status.qosClass}"
  • Guaranteed: Requests = Limits
  • Burstable: Requests < Limits
  • BestEffort: No requests or limits set

3. Scheduling: Taints, Tolerations, and Affinity

View Taints on a Node

kubectl describe node <node-name> | grep -i taint

Add a Taint to a Node

kubectl taint nodes <node-name> key=value:NoSchedule

Remove a Taint from a Node

kubectl taint nodes <node-name> key=value:NoSchedule-

Deploy a Pod with a Toleration

tolerations:
  - key: "dedicated"
    operator: "Equal"
    value: "database"
    effect: "NoSchedule"

Deploy a Pod with Node Affinity

affinity:
  nodeAffinity:
    requiredDuringSchedulingIgnoredDuringExecution:
      nodeSelectorTerms:
      - matchExpressions:
        - key: "region"
          operator: "In"
          values:
          - "us-east"

Deploy a Pod with Pod Anti-Affinity

affinity:
  podAntiAffinity:
    requiredDuringSchedulingIgnoredDuringExecution:
    - labelSelector:
        matchExpressions:
        - key: "tier"
          operator: "In"
          values:
          - "db"
      topologyKey: "kubernetes.io/hostname"

4. Helm Commands

Add a Helm Repository

helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo update

Install a Helm Chart

helm install my-nginx bitnami/nginx

List Installed Helm Releases

helm list

Upgrade a Helm Release

helm upgrade my-nginx bitnami/nginx -f values.yaml

Rollback a Helm Release

helm rollback my-nginx 1

Uninstall a Helm Release

helm uninstall my-nginx

5. Resource Monitoring

Check Node Resource Usage

kubectl top nodes

Check Pod Resource Usage

kubectl top pods --containers

Describe Resource Requests & Limits of a Pod

kubectl describe pod <pod-name> | grep -i "requests\|limits"

6. Debugging & Logs

View Pod Logs

kubectl logs <pod-name>
kubectl logs <pod-name> -f         # Follow logs in real-time
kubectl logs <pod-name> -c <container-name> # Logs for a specific container

Execute a Command in a Running Pod

kubectl exec -it <pod-name> -- /bin/sh

Ping from One Pod to Another

kubectl exec -it <pod-name> -- ping <target-pod-name>

Get Detailed YAML Output for Any Resource

kubectl get pod <pod-name> -o yaml
kubectl get node <node-name> -o yaml

7. Namespace Management

View Namespaces

kubectl get namespaces

Create a New Namespace

kubectl create namespace my-namespace

Delete a Namespace

kubectl delete namespace my-namespace

8. Cleanup & Deleting Resources

Delete All Pods in a Namespace

kubectl delete pods --all -n <namespace>

Delete a Specific Deployment

kubectl delete deployment <deployment-name>

Delete a Specific Service

kubectl delete service <service-name>

Delete a Persistent Volume Claim (PVC)

kubectl delete pvc <pvc-name>

9. Useful One-Liners

Get All Running Pods with Labels

kubectl get pods --show-labels

Check Where Each Pod is Running

kubectl get pods -o wide

Find All Pods in Pending State

kubectl get pods --field-selector=status.phase=Pending

Get Nodes & Their Labels

kubectl get nodes --show-labels