Loading...
Loading…
Loading…
Categories
Popular tags
Pods, deployments, services, debugging, and cluster management
kubectl config get-contexts # list all contexts
kubectl config current-context # current context
kubectl config use-context my-cluster # switch context
kubectl config set-context --current --namespace=dev # set default namespace
kubectl cluster-info # cluster info
kubectl get nodes # list nodes
kubectl get nodes -o wide # with IPs and OS infokubectl get pods # list pods (current namespace)
kubectl get pods -n kube-system # list in namespace
kubectl get pods -A # list all namespaces
kubectl get pods -o wide # with node and IP
kubectl get pod mypod -o yaml # full pod spec
kubectl describe pod mypod # events and details
kubectl logs mypod # pod logs
kubectl logs mypod -c container-name # specific container
kubectl logs mypod -f # follow (tail -f)
kubectl logs mypod --previous # logs from crashed container
kubectl exec -it mypod -- bash # shell into pod
kubectl exec mypod -- env # run command in pod
kubectl delete pod mypod # delete pod
kubectl delete pod mypod --force # force delete (stuck pods)kubectl get deployments
kubectl describe deployment myapp
kubectl create deployment myapp --image=myimage:1.0
kubectl scale deployment myapp --replicas=3
kubectl set image deployment/myapp app=myimage:2.0 # update image
kubectl rollout status deployment/myapp # watch rollout
kubectl rollout history deployment/myapp # revision history
kubectl rollout undo deployment/myapp # rollback
kubectl rollout undo deployment/myapp --to-revision=2
kubectl edit deployment myapp # live editkubectl get services
kubectl describe service myapp
kubectl expose deployment myapp --port=80 --target-port=8080
kubectl port-forward svc/myapp 8080:80 # local port forward
kubectl port-forward pod/mypod 8080:8080 # forward to pod
kubectl delete service myapp# ConfigMap
kubectl create configmap myconfig --from-literal=KEY=value
kubectl create configmap myconfig --from-file=config.yaml
kubectl get configmap myconfig -o yaml
kubectl delete configmap myconfig
# Secret
kubectl create secret generic mysecret --from-literal=password=s3cr3t
kubectl create secret generic mysecret --from-file=.dockerconfigjson
kubectl get secret mysecret -o jsonpath='{.data.password}' | base64 -d
kubectl delete secret mysecretkubectl get namespaces
kubectl create namespace staging
kubectl delete namespace staging
# Run command in specific namespace
kubectl get pods -n staging
# Set default namespace for session
kubectl config set-context --current --namespace=staging# Events (most useful first stop for debugging)
kubectl get events --sort-by='.lastTimestamp'
kubectl get events -n mynamespace
# Resource usage
kubectl top pods
kubectl top nodes
# Debug with ephemeral container (k8s 1.23+)
kubectl debug -it mypod --image=busybox --target=mycontainer
# Copy files to/from pod
kubectl cp mypod:/app/log.txt ./log.txt
kubectl cp ./config.yaml mypod:/app/config.yaml
# Apply and dry-run
kubectl apply -f manifest.yaml --dry-run=client
kubectl diff -f manifest.yamlkubectl apply -f deployment.yaml # apply manifest
kubectl apply -f ./k8s/ # apply directory
kubectl apply -k ./kustomize/ # apply kustomize dir
kubectl delete -f deployment.yaml # delete from manifest
kubectl replace -f deployment.yaml # replace (not patch)
kubectl patch deployment myapp -p '{"spec":{"replicas":5}}'kubectl get pods -l app=myapp # filter by label
kubectl get pods -l 'env in (prod,staging)'
kubectl label pod mypod tier=frontend # add label
kubectl label pod mypod tier- # remove label
kubectl annotate pod mypod note="debug" # add annotation