Index

kubectl

administration

using the proxy
kubectl proxy
curl localhost:8001
curl localhost:8001/v1

getting help / logs / debugging

help
kubectl --help
kubectl [arguments] --help
deployment logs
kubectl get events --sort-by=.metadata.creationTimestamp
kubectl get events --sort-by=.metadata.creationTimestamp -n [namespace]
kubectl get events --watch
pod logs
kubectl logs [pod]
kubectl logs -f [pod]
kubectl logs --follow [pod]
describe yaml fields
kubectl explain
kubectl explain pod
kubectl explain pod.spec
dynamically edit yaml (some fields)
kubectl edit pod idne

command structure irrespective of resource type

getting info on a resource
kubectl get [resource] [id]?
kubectl get [resource] [id]? -o wide
kubectl get [resource] [id]? -o yaml
kubectl get [resource] --all
kubectl get all --all
getting info on multiple resources at once
kubectl get [res1],[res2]...
getting resources in another namespace than the default one
kubectl get [resource] -n=[namespace]
kubectl get [resource] --namespace=[namespace]
kubectl get [resource] --all-namespaces

kubectl describe [resource] [id]?
create resources from a yaml file
kubectl create -f [yaml-file]
apply changes
kubectl create -f [yaml-file] --save-config
kubectl apply  -f [yaml-file]
deleting resources
kubectl delete [resource] [id]
kubectl delete [resource] --all
kubectl delete [resource] --all --force

namespaces

get info
kubectl get namespaces
kubectl describe namespaces
configure default namespace
kubectl config set-context --current --namespace=[namespace]
view currently configured default namespace
kubectl config view --minify | grep namespace:
creating a namespace
kubectl create namespace [id]
deleting a namespace and everything in it
kubectl delete namespace [id]
deploying a resource from a yaml file into a namespace
kubectl create -f [yaml-file] -n=[namespace]
kubectl create -f [yaml-file] --namespace=[namespace]

nodes

get info
kubectl get nodes
kubectl get node
kubectl get no
kubectl get nodes -o wide
kubectl get nodes -o yaml
kubectl describe node [id]
list node names only
kubectl get nodes -o jsonpath='{range.items[*].metadata}{.name} {end}'
tainting a node
kubectl taint nodes [worker] [key]=[value]:PreferNoSchedule
kubectl taint nodes [worker] [key]=[value]:NoSchedule
kubectl taint nodes [worker] [key]=[value]:NoExecute
removing a taint
kubectl taint nodes [worker] [key]:PreferNoSchedule-
kubectl taint nodes [worker] [key]:NoSchedule-
kubectl taint nodes [worker] [key]:NoExecute-

labels

showing labels
kubectl get [resource] --show-labels
kubectl get [resource] -L [label]
kubectl get [resource] -L [label1],[label2]...
assigning a label to a resource
kubectl label [resource] [resource-id] [key]=[value]
kubectl label [resource] [resource-id] [key]=[value] --overwrite
example
kubectl label pod multi multipod=true
deleting a label
kubectl label [resource] [resource-id] [label]-
listing resources that have a specific label key assigned
kubectl get [resource] -o wide -l [key]
inverse
kubectl get [resource] -o wide -l '![key]'
listing resources that have a specific label key & value assigned
kubectl get [resource] -o wide -l [key]=[value]
logical and of labels
kubectl get [resource] -o wide -l [label1],[label2]
deleting resources selected with labels
kubectl delete [resource] -l [label]

pods

basics

get info
kubectl get pods
kubectl get pod
kubectl get po
kubectl describe pod [id]?
delete pods
kubectl delete pod [id]
kubectl delete pods --all
kubectl delete pods --all --force

kubectl run - running a pod

running a docker image as pod
kubectl run --image=[docker-image] [pod-id]
kubectl run --image=[docker-image] [pod-id]	-- [command]
auto delete
kubectl run --rm -ti --image=[docker-image] [pod-id]
kubectl run --rm -ti --image=[docker-image] [pod-id] -- [command]
specifying an image pull policy
--image-pull-policy IfNotPresent
--image-pull-policy Always
--image-pull-policy :latest
don't restart on process exit
--restart=OnFailure
--restart=Never
specifying a namespace to run the pod
--namespace [namespace]
-n [namespace]
examples:
kubectl run --image httpd apache
kubectl run --image alpine alp -- sleep infinity
example: throwaway container that runs a shell and auto-deletes on exit
kubectl run --image alpine alp -ti --rm --restart=never
example: throwaway container that runs a single command, then auto-deletes
kubectl run --image ubuntu ubn --rm -ti --restart=Never -- date

kubectl exec - running a command on a container

create shell in interactive tty
kubectl exec -it [pod] sh
run a single command on a pod
kubectl exec [pod] -- [cmd]
to run a command on any container other than the first in a a pod
kubectl exec [pod] -c [container] -- [cmd]
example: workaround for shell interpretation of variables: '$'
kubectl exec [pod] -- sh -c 'echo $KUBERNETES_SERVICE_HOST'

kubectl cp - copying files between pods and host

copy file between host and pod
kubectl cp [file] [pod]:[path]

port-forwarding

kubectl port-forward [pod] [port]

replication controllers

get info
kubectl get replicationcontroller
kubectl get rc
set the number of replicas
kubectl scale rc [id] --replicas=[n]
delete a replicationcontroller with all of its pods
kubectl delete rc myrc
delete a replicationcontroller, keep the pods
kubectl delete rc myrc --cascade=false

deployment

get info
kubectl get deployments
kubectl describe deployments
create a deployment
kubectl create deployment [id] --image=[image]
set the number of replicas
kubectl scale deployment [id] --replicas=[n]
upgrade image
kubectl set image deployment/[id] nginx=nginx:1.7.8 --all=true --record
deployment status
kubectl rollout status deployment nix
kubectl rollout history deployment [id]
kubectl rollout undo deployment/[id]
kubectl rollout undo deployment/[id] --to-revision=[n]
kubectl rollout pause deployment nix
kubectl rollout resume deployment nix
delete deployment
kubectl delete deployment [id]

config maps

getting info
kubectl get configmap
kubectl describe configmap [map-id]
create configmap from the command line
kubectl create configmap [map-id] --from-literal=[key]=[value]
kubectl create configmap [map-id] --from-literal=[key1]=[value1] --from-literal=[key2]=[value2]
create configmap with file name as key and file contents as value
kubectl create configmap [map-id] --from-file=[file]
create configmap with custom key and file contents as value
kubectl create configmap [map-id] --from-file=[key]=[file]
create configmap with a key for every file in a directory; values will be file content
kubectl create configmap [map-id] --from-file=[dir]

secrets

getting info
kubectl get secrets
kubectl get secret [id] -o yaml
kubectl describe secret [id]
create simple key-value secret
kubectl create secret generic [id] --from-literal=[key]=[value]

services

getting info
kubectl get services
kubectl get svc
kubectl get svc -o wide
kubectl get svc -o yaml
kubectl get endpoints

kubectl describe service [id]?
create a service backed by a pod
kubectl run [id] --image=[image] --expose --port=[container-port]
expose existing pods as service
kubectl expose (pod|rc|rs|deployment) [id] --port=[container-port] --type=(ClusterIP|NodePort|LoadBalancer)
show service environment variables
kubectl exec -ti pod1 env | grep -i service
service dns records
[svc].[namespace].svc.cluster.local
[svc].[namespace].svc
[svc].[namespace]
[svc]
delete service
kubectl delete svc [id]

documentation

https://kubernetes.io/docs/reference/kubectl/cheatsheet/
https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands