liam.pm

kubectl cheat sheet

Context and namespaces

# Change context to a namespace
kubectl config set-context --current --namespace=my-k8s-namespace

# View current context
kubectl config current-context

# List all contexts
kubectl config get-contexts

Pod operations

# Get a shell to a running container
kubectl exec --stdin --tty my-pod -- /bin/bash
# Shorthand
kubectl exec -it my-pod -- /bin/bash

# Get logs
kubectl logs my-pod
kubectl logs -f my-pod  # follow logs

Common commands

# List resources
kubectl get pods
kubectl get services
kubectl get deployments

# Describe resource
kubectl describe pod my-pod

# Delete resource
kubectl delete pod my-pod