Skip to content

Instantly share code, notes, and snippets.

@jonashackt
Last active March 13, 2024 13:36
Show Gist options
  • Save jonashackt/df283eb3f5ed587ee1f93cbcfc23be08 to your computer and use it in GitHub Desktop.
Save jonashackt/df283eb3f5ed587ee1f93cbcfc23be08 to your computer and use it in GitHub Desktop.
Kubernetes cheat sheet

k9s cheatsheet: https://www.hackingnote.com/en/cheatsheets/k9s/

CLI enhancements

https://discuss.kubernetes.io/t/kubectl-tips-and-tricks/192

krew as the Kubernetes CLI (kubectl) plugin manager helps to install all the tools: https://krew.sigs.k8s.io/

kubectl autocompletion

Install kubectl

then configure autocompletion as stated in the docs: https://kubernetes.io/docs/tasks/tools/install-kubectl-macos/#optional-kubectl-configurations-and-plugins with adding the following to your ~/.zshrc or ~/.bashrc:

### kubectl autocompletion & alias k
source <(kubectl completion zsh)
alias k=kubectl
complete -F __start_kubectl k

kubectx & kubens

https://github.com/ahmetb/kubectx: Install via brew: brew install kubectx

Now run it and switch with ease:

# list all contexts
kubectx
# switch
kubectx k3d

kubectx interactive mode

If you want a fancy interactive mode, you need to install https://github.com/junegunn/fzf#using-homebrew

brew install fzf

# To install useful key bindings and fuzzy completion:
$(brew --prefix)/opt/fzf/install

Now your kubectx and kubens are super powered as interactive mode :)

kube-ps1

https://github.com/jonmosco/kube-ps1:

install via:

brew install kube-ps1

# now source kube-ps1.sh in your ~/.zshrc or ~/.bashrc. Therefore add the following 2 lines to them:
source "/usr/local/opt/kube-ps1/share/kube-ps1.sh"
PS1='$(kube_ps1)'$PS1

Now you should already see the current k8s context & namespace inside your prompt:

# switch on and off
kubeon
kubeoff

Now this could all look like this:

asciicast

For evaluation tools ee https://learnk8s.io/validating-kubernetes-yaml

kubeval

Install via homebrew (see https://www.kubeval.com/installation/):

brew tap instrumenta/instrumenta
brew install kubeval

Now use kubeval your-yaml-to-check.yaml

kubescore

Install via homebrew (see https://github.com/zegl/kube-score#installation): brew install kube-score

Run with

kube-score score your-yaml-to-check.yaml

K8s cluster

See logs of K8s cluster

https://kubernetes.io/docs/tasks/debug-application-cluster/debug-cluster/#looking-at-logs

show k8s api-server logging

sudo journalctl -u kube-apiserver --follow

see https://kubernetes.io/docs/reference/kubectl/cheatsheet/

Retrieve cluster info

kubectl cluster-info dump

every component with namespace kube-system

kubectl get all --namespace kube-system

every component

kubectl get all --all-namespaces

etcd

get all etcd entries

ETCDCTL_API=3 etcdctl --endpoints=https://127.0.0.1:2379 --cacert=/etc/etcd/ca.pem --cert=/etc/etcd/kube-apiserver.pem --key=/etc/etcd/kube-apiserver-key.pem get / --prefix --keys-only

Networking

kubectl get nodes --output json

Flannel logging

kubectl logs -n kube-system kube-flannel-ds-cw7kf -c kube-flannel

kubectl get pod kube-flannel-ds-cw7kf --namespace kube-system -o yaml

Kube-DNS

kubectl describe pods -l k8s-app=kube-dns -n kube-system

kubectl get nodes --output=jsonpath='{range .items[*]}{.status.addresses[?(@.type=="InternalIP")].address} {.spec.podCIDR} {"\n"}{end}'

Debug kube-dns

see https://kubernetes.io/docs/tasks/administer-cluster/dns-debugging-resolution/

kubectl logs --namespace=kube-system $(kubectl get pods --namespace=kube-system -l k8s-app=kube-dns -o name | head -1) -c kubedns kubectl logs --namespace=kube-system $(kubectl get pods --namespace=kube-system -l k8s-app=kube-dns -o name | head -1) -c dnsmasq kubectl logs --namespace=kube-system $(kubectl get pods --namespace=kube-system -l k8s-app=kube-dns -o name | head -1) -c sidecar

CoreDNS

kubectl describe pod/coredns-65db874f4f-lz5hl --namespace kube-system

DNS

see https://kubernetes.io/docs/tasks/debug-application-cluster/debug-service/#does-the-service-work-by-ip

kubectl run hostnames --image=k8s.gcr.io/serve_hostname \
                        --labels=app=hostnames \
                        --port=9376 \
                        --replicas=3

kubectl expose deployment hostnames --port=80 --target-port=9376

Worker

K8s nodes in State NotReady

kubectl describe nodes

Awesome tools

Cluster in cluster: https://www.vcluster.com/ & https://github.com/loft-sh/vcluster

https://github.com/ahmetb/kubectl-tree

Deployment abstraction above Helm: https://www.acorn.io/

https://microk8s.io/

Telepresence alternative: https://gefyra.dev/

Docker Desktop alternatives: https://podman-desktop.io/

Bare metal Kubernetes: https://www.siderolabs.com/platform/bare-metal-kubernetes-sidero/

K8s simple deployment tooling (simpler than Argo and Flux): https://werf.io/

KubeCon 2022 Recordings: https://www.youtube.com/playlist?list=PLj6h78yzYM2MCEgkd8zH0vJWF7jdQ-GRR

VMWare Tanzu on a Laptop: https://tanzu.vmware.com/developer/guides/tanzu-application-platform-local-devloper-install/ (and in GitHub Actions etc.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment