Table of Contents
kubectl version kubectl cluster-info kubectl config view kubectl config get-contexts … # shows and configures Contexts and Clusters (-> kubectx)
kubectl api-resources / api-versions # shows all API objects and their current versions kubectl explain pod.spec # shows the reference manual for the object "Pod" (or others) kubectl explain pod --recusive # shows tree of all things under "Pod" kubectl describe deployment hello-node # freetext information for deployment kubectl describe pods # freetext information about pod
kubectl get all --all-namespaces -o wide # the big overview ("all" is not really all and no longer documented!)
kubectl get nodes
kubectl get deployments
kubectl get service -o wide
kubectl get replicasets -o wide
kubectl get pods
kubectl get svc # alias for "kubectl get services"
kubectl get services # shows external port like 8080:32065/TCP
kubectl get endpoints # shows endpoint ip addressses; managed by services
kubectl get events
kubectl get ingress # shows Ingress objects; not included in "get all"
kubectl get service,ingress # shows both categories
kubectl get networkpolices # shows network policies that limit access to Pods by e.g. labels
kubectl get pod foo -o yaml --export # shows Yaml without Container specific details
kubectl get pod -L project,app # shows only the Labels "project" and "app"
kubectl get pod -l app=foo # selects Pod not by name but by Label
kubectl get pod -l 'app in (foo,bar)' # selects Pods with label "app" being either "foo" or "bar"
# Also accepts "notin" and "," for AND. There is no OR nor NOT.
kubectl logs hello-server-6f5bdf948c-7ttsv # Logs of this Pod kubectl logs tmp -c b2 # Logs from Pod "tmp" Container "b2" (for multi-container Pods) kubectl exec -ti hello-server-6f5bdf948c-7ttsv bash # Shell into this Pod
# Show information using custom template
kubectl get pod nginx1 -o jsonpath --template='{.metadata.name},{.metadata.annotations.description}{"\n"}'
kubectl get pods -o jsonpath --template="{range .items[*]}{.metadata.namespace}{'/'}{.metadata.name}{'\n'}{end}"
kubectl get pods -o jsonpath='{.items[*].spec.containers[*].image}'
# Show information (node names for alle running pods) using JSON-Template with "\n" as separator
kubectl get pods -o go-template='{range .items[*]}{.spec.nodeName}{"\n"}{end}'
# Sort output kubectl get jobs --sort-by=.metadata.creationTimestamp
# Show really everything in the current namespace
kubectl api-resources --namespaced=true -o name | paste -s -d, | xargs -I{} kubectl get {} --ignore-not-found --show-kind
kubectl rollout status deployment nginx # shows rollout status
# declarative kubectl apply -f foo.yaml
# imperative kubectl create deployment hello-node --image=gcr.io/hello-minikube-zero-install/hello-node kubectl expose deployment hello-node --type=LoadBalancer --port=8080 [--target-port=18080] [--external-ip=...] kubectl create job --from=cronjob/foo foo-manual-001
curl http://192.168.99.100:32065/
kubectl autoscale deployment hello-node --min=2 --max=10
# Run a single command in a Pod (--restart=Never creates a Pod without a Deployment; --rm deletes if afterwards)
kubectl run tmp1 -it --rm --image=busybox --restart=Never --command -- env
# Run a single command to access another Container on its internal IP
ubectl run tmp7 -it --rm --restart=Never --image=busybox --command -- wget -qO- http://172.17.0.18/
# Generates Yaml for an action without executing it
kubectl create namespace mynamespace --dry-run -o yaml
# Updates image for container with name "nginx" in Pod or Deployment with name "nginx3"
kubectl set image pod/nginx3 nginx=nginx:1.8.1
kubectl set image deployment/nginx nginx=nginx:1.7.9
# Edit object using JQ and reapply
kubectl get pod nginx2 -o json | jq '.metadata.labels.app = "v2"' | kubectl apply -f -
# Edit and remove labels
kubectl label pods nginx2 app=v3 --overwrite
kubectl label pods nginx2 app-
# Add or remote annotation metadata from objects
kubectl annotate pod nginx1 nginx2 nginx3 description="my descr" --overwrite
kubectl annotate pod nginx{1..3} description-
# Show revision history and undo last rollout (e.g. after changing the image version)
kubectl rollout history deployment nginx
kubectl rollout undo deployment nginx --to-revision=3
# Delete all Deployments
kubectl delete deployment --all
# Immediately delete a pod
kubectl delete pod --force --grace-period=0 foo