Kubernetes (1.7) commands cheatsheet

Version: 1.6

Contexts

kubectl config get-contexts
kubectl config delete-context CONTEXT_NAME
kubectl config set-context --current --namespace=NAMESPACE 
kubectl config use-context CONTEXT_NAME

Run pod from image

kubectl run NAME --image=image [--env="key=value"]

Get info

kubectl get pods [-o wide]
kubectl describe service SERVICE_NAME
kubectl get pods --all-namespaces -o wide --field-selector spec.nodeName=NODE_NAME

Delete all pods with a label

kubectl delete pods -l label=value

Delete all pods in a specific state pods, in Linux

E.g., STATE=Evicted

kubectl get pod | grep Evicted | awk '{print $1}' | xargs kubectl delete pod

Set namespace for kubectl console session

kubectl config set-context --current -n=<NAMESPACE>

Validate deployment before actually applying it

kubectl apply --dry-run=client -f deployment.yaml

Deploy a service and watch process

kubectl apply -f deployment.yaml
kubectl get service <SERVICE-NAME> [--watch]

Bash into k8s existing pod

kubectl exec -it <POD-NAME> -- /bin/bash

Bash into k8s existing pod to troubleshoot

Create an ephemeral container in the existing pod

kubectl debug <POD_NAME> -it --image=<IMAGE_NAME>

# e.g. with simplest alpine busybox
kubectl debug <POD_NAME> -it --image=busybox

Run pod with curl, to access the network.

Then wget a url and print result in terminal (curl is not included in alpine)

kubectl run --rm -it --image=alpine disposable-curl-pod
# wget -qO- 10.0.1.34/health

To install curl, just

apk --no-cache add curl

Or just

kubectl run disposable-curl-pod --image=radial/busyboxplus:curl -it --rm

Forward ClusterIP service port in k8s cluster to local port

kubectl port-forward service/[SERVICE_NAME] [HOST_PORT]:[POD_PORT]
kubectl port-forward pod/[POD_NAME] [HOST_PORT]:[POD_PORT]

DNS service info

kubectl get services kube-dns --namespace=kube-system

Troubleshoot crash on startup

kubectl get pod [POD_NAME] --output=[yaml/json]
kubectl logs [POD_NAME]
kubectl describe [POD_NAME]

If it already exited, add -p

kubectl logs [POD_NAME] -p

Logs of specific container inside the pod, e.g., from the initContainer

kubectl logs [POD_NAME] -c [CONTAINER_NAME]

List all resources

kubectl api-resources --verbs=list --namespaced -o wide

Get info about resources to deploy

This is quite relevant, as with new versions there are important changes

kubectl explain <RESOURCE>.<PATH>.<TO>.<ITEM>
// e.g:
kubectl explain ingress.spec.rules.http

Cleanup k8s resources

Just delete the namespace(s) and everything under it will be deleted. Deletion is async, so namespaces appears as Terminating for some time.

kubectl delete namespace NAMESPACE_NAME

A service url

Given cluster.local is the cluster domain (the default), a service can be resolved at: service-name.namespace.svc.cluster.local. If the client is in the same namespace, only service-name is enough. Details here https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/.

AWS

Set EKS connection in profile

aws eks update-kubeconfig --name <EKS_CLUSTER> --alias <CLUSTER_LOCAL_ALIAS> --profile <AWS_PROFILE>