Kubernetes Commands Cheat Sheet - Essential kubectl Commands

Master Kubernetes with our comprehensive kubectl commands cheat sheet. Find essential commands for client configuration, resource management, viewing, and monitoring Kubernetes clusters.

Kubernetes Commands Cheat Sheet

Kubernetes Commands Cheat Sheet

This cheat sheet provides essential kubectl commands for managing and interacting with Kubernetes clusters. Whether you're configuring clients, managing resources, or monitoring your applications, these commands will help streamline your workflow.

Client Configuration

Configure your local environment to interact with your Kubernetes cluster effectively.

  • Setup autocomplete in bash (requires bash-completion package)
source <(kubectl completion bash)
  • View Kubernetes configuration
kubectl config view
  • View specific config items by JSON path
kubectl config view -o jsonpath='{.users[?(@.name == "k8s")].user.password}'
  • Set credentials for foo.kubernetes.com
kubectl config set-credentials kubeuser/foo.kubernetes.com --username=kubeuser --password=kubepassword
  • Set the active namespace for the current context
kubectl config set-context --current --namespace=namespace_name

Viewing and Finding Resources

Discover and inspect resources within your Kubernetes cluster.

  • List all services in the current namespace
kubectl get services
  • List all pods in all namespaces in wide format
kubectl get pods -o wide --all-namespaces
  • List all pods in JSON format
kubectl get pods -o json
  • Describe resource details (e.g., node, pod, service)
kubectl describe nodes my-node
  • List services sorted by name
kubectl get services --sort-by=.metadata.name
  • List pods sorted by restart count
kubectl get pods --sort-by='.status.containerStatuses[0].restartCount'

Managing Resources

Create, update, scale, and delete Kubernetes resources.

  • Rolling update pods for frontend-v1 to frontend-v2
kubectl rolling-update frontend-v1 -f frontend-v2.json
  • Scale a replicaset named 'foo' to 3 replicas
kubectl scale --replicas=3 rs/foo
  • Scale a resource defined in "foo.yaml" to 3 replicas
kubectl scale --replicas=3 -f foo.yaml
  • Execute a command in every pod/replica of a deployment
for i in 0 1; do kubectl exec foo-$i -- sh -c 'echo $(hostname) > /usr/share/nginx/html/index.html'; done
  • Get documentation for resources like pods or services
kubectl explain pods,svc
  • Create resource(s) from a manifest file (e.g., pods, services, daemonsets)
kubectl create -f ./my-manifest.yaml
  • Apply a configuration to a resource, creating or updating it
kubectl apply -f ./my-manifest.yaml
  • Start a single instance of an Nginx pod
kubectl run nginx --image=nginx
  • Create a secret with multiple keys
cat <<EOF | kubectl create -f -
apiVersion: v1
kind: Secret
metadata:
  name: mysecret
type: Opaque
data:
  password: $(echo "s33msi4" | base64)
  username: $(echo "jane" | base64)
EOF
  • Delete a resource defined in a manifest file
kubectl delete -f ./my-manifest.yaml

Monitoring & Logging

Gain insights into your cluster's performance and troubleshoot issues.

  • Deploy Heapster from a GitHub repository (example)
kubectl create -f deploy/kube-config/standalone/
  • Show resource usage metrics for nodes
kubectl top node
  • Show resource usage metrics for pods
kubectl top pod
  • Show resource usage metrics for a given pod and its containers
kubectl top pod pod_name --containers
  • Dump pod logs (stdout)
kubectl logs pod_name
  • Stream pod container logs (stdout, for multi-container pods)
kubectl logs -f pod_name -c my-container

Interacting with Running Pods

Execute commands and access terminals within your running pods.

  • Run a command in a pod
kubectl exec pod_name -- command_name
  • Run a command in a specific container of a multi-container pod
kubectl exec pod_name -c container_name -- command_name
  • Get an interactive terminal session for a pod
kubectl exec -it pod_name /bin/sh
  • Get an interactive terminal session for a specific container in a multi-container pod
kubectl exec -it pod_name -c container_name /bin/sh

External Resources