Skip to content

Kubectl

Pass Config

Option Description
~/.kube/config file with the Kubernetes API address and the path to our TLS certificates used to authenticate
--kubeconfig flag to pass a config file
--server,
--user
directly

Commands (kubectl ...)

Cluster wide

Command Description
cluster-info Main installation
config view Location and credentials that kubectl knows about
config get-contexts List contexts
get componentstatus Get component status
get secrets -o yaml List all secrets (-o yaml is essential to get the actual values!)
get node Look at the composition of our cluster
get node -o wide More information
get node -o json
  • Build custom reports
  • kubectl get nodes -o json | jq ".items[] | {name:.metadata.name} + .status.capacity"

Get

Command Description
get all List resources
get nodes List nodes
get namespaces List the namespaces on our cluster
get pods List pods on our cluster
get deployments List deployments
get services
  • List services on our cluster
  • IP=$(kubectl get svc <name> -o go-template --template '{{ .spec.clusterIP }}')
get service <servicename> -o go-template='{{.spec.clusterIP}}'

get service <servicename> -o go-template='{{(index .spec.ports 0).port}}'
Get service cluster IP and port
-n kube-system get pods Switch to a different namespace (default otherwise)

Describe/Explain

Command Description
describe <type> View details about a resource
explain <type> View the definition for a resource type
diff -f deployment.yaml Check what would change before applying any updates
rollout history deployment/<name> List deployment version history

Logs

Command Description
logs <podname>
logs <type/name>
logs deploy/<name>
View container output (1 single pod)
  • --follow = stream logs in real time (à la tail -f)
  • --tail = indicate #lines you want to see (from the end)
  • --since = get logs only after a given timestamp
logs -l run=<deployment_name> View logs of multiple pods

Run/Scale

Command Description
run <deployment_name> --image <alpine> <ping goog.gl> Create a deployment
scale <type/name> --replicas <num> Create additional copies of the pod
exec <pod-name> env Check pod environment variables
exec -it <container-name> sh Run command in a running pod
run --rm -it mytest --image=busybox --sh Start a temporary pod

Services

Command Description
expose deploy/<name> --port 9200 Create a ClusterIP service
port-forward svc/kubernetes-dashboard <LOCAL PORT>:<PORT IN K8S> Temporarily add port forwarding