prefabim / cheatsheet-kubernetes-A4

:book: Kubernetes CheatSheets In A4

Home Page:https://cheatsheet.dennyzhang.com/cheatsheet-kubernetes-A4

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

1 Kubernets CheatSheet

linkedin
github
slack


PRs Welcome

File me Issues or star this repo.

See more CheatSheets from Denny: #denny-cheatsheets

1.0.1 Common Commands

NameCommand
List everythingkubectl get all --all-namespaces
Validate yaml file with dry runkubectl create --dry-run --validate -f pod-dummy.yaml
Start a temporary pod for testingkubectl run --rm -i -t --image=alpine test-$RANDOM -- sh
Run wget test temporarilykubectl run --rm mytest --image=busybox -it
Run curl test temporarilykubectl run --rm mytest --image=yauritux/busybox-curl -it
Get system conf via configmapkubectl -n kube-system get cm kubeadm-config -o yaml
Explain resourcekubectl explain pods, kubectl explain svc
Get all serviceskubectl get service --all-namespaces
Get services sorted by namekubectl get services –sort-by=.metadata.name
Get pods sorted by restart countkubectl get pods –sort-by=’.status.containerStatuses[0].restartCount’
Query healthcheck endpointcurl -L http://127.0.0.1:10250/healthz
Open a bash terminal in a podkubectl exec -it storage sh
Check pod environment variableskubectl exec redis-master-ft9ex env
Enabling shell autocompletion for kubectlecho "source <(kubectl completion bash)" >> ~/.bashrc, then reconnect
In mac desktop, use minikube dockerdeval $(minikube docker-env), No need to docker push any more

1.1 Components & Services

  • Services on Master Nodes
NameSummary
kube-apiserverexposes the Kubernetes API from master nodes
etcdreliable data store for all k8s cluster data
kube-schedulerschedule pods to run on selected nodes
kube-controller-managernode controller, replication controller, endpoints controller, and service account & token controllers
  • Services on Worker Nodes
NameSummary
kubeletmakes sure that containers are running in a pod
kube-proxyperform connection forwarding
Container RuntimeKubernetes supported runtimes: Docker, rkt, runc and any OCI runtime-spec implementation.
  • Addons: pods and services that implement cluster features
NameSummary
DNSserves DNS records for Kubernetes services
Web UIa general purpose, web-based UI for Kubernetes clusters
Container Resource Monitoringcollect, store and serve container metrics
Cluster-level Loggingsave container logs to a central log store with search/browsing interface
  • Tools
NameSummary
kubectlthe command line util to talk to k8s cluster
kubeadmthe command to bootstrap the cluster
kubefedthe command line to control a Kubernetes Cluster Federation
Kubernetes Componentslink: Kubernetes Components

1.2 Check Performance

NameCommand
Get node resource usagekubectl top node
Get pod resource usagekubectl top pod
Get resource usage for a given podkubectl top <podname> --containers
List resource utilization for all containerskubectl top pod --all-namespaces --containers=true

1.3 Resources Deletion

NameCommand
Delete podkubectl delete pod hello-node-95913-n63qs -n $my-namespace
Delete pods by labelskubectl delete pod -l env=test
Delete deployments by labelskubectl delete deployment -l app=wordpress
Delete persist volumes by labelskubectl delete pvc -l app=wordpress
Delete statefulset only (not pods)kubectl delete sts <stateful_set_name> --cascade=false

1.4 Pod

NameCommand
List all podskubectl get pods
List pods for all namespacekubectl get pods -all-namespaces
List all critical podskubectl get -n kube-system pods -a
List pods with more infokubectl get pod -o wide, kubectl get pod -o yaml
Get pod infokubectl describe pod srv-mysql-server
List all pods with labelskubectl get pods --show-labels
Get Pod initContainer statuskubectl get pod --template '{{.status.initContainerStatuses}}' <pod-name>
kubectl run commandkubectl exec -it -n “$ns” “$podname” – sh -c “echo $msg >>/dev/err.log”
Get pod by selectorpodname=$(kubectl get pods -n $namespace –selector=”app=syslog” -o jsonpath=’{.items[*].metadata.name}’)
List pods with docker imageskubectl get pods -o=jsonpath=’{range .items[*]}{.metadata.name}:{.spec.containers[0].name}{“\t”}{.spec.containers[0].image}{“\n”}{end}’
Kubernetes Yaml Exampleslink: kubernetes yaml templates

1.5 Label & Annontation

NameCommand
Filter pods by labelkubectl get pods -l owner=denny
Manually add label to a podkubectl label pods dummy-input owner=denny
Remove labelkubectl label pods dummy-input owner-
Manually add annonation to a podkubectl annotate pods dummy-input my-url=https://www.dennyzhang.com

1.6 Deployment & Scale

link: Pausing and Resuming a Deployment

NameCommand
Scale outkubectl scale --replicas=3 deployment/nginx-app
online rolling upgradekubectl rollout app-v1 app-v2 --image=img:v2
Roll backupkubectl rollout app-v1 app-v2 --rollback
List rolloutkubectl get rs
Check update statuskubectl rollout status deployment/nginx-app
Check update historykubectl rollout history deployment/nginx-app
Pause/Resumekubectl rollout pause deployment/nginx-deployment, resume
Rollback to previous versionkubectl rollout undo deployment/nginx-deployment
Kubernetes Yaml Exampleslink: kubernetes yaml templates

1.7 Quota & Limits

NameCommand
List Resource Quotakubectl get resourcequota
List Limit Rangekubectl get limitrange
Kubernetes Yaml Exampleslink: kubernetes yaml templates

1.8 Service

NameCommand
List all serviceskubectl get services
List service endpointskubectl get endpoints
Get service detailkubectl get service nginx-service -o yaml
Get service cluster ipkubectl get service nginx-service -o go-template=’{{.spec.clusterIP}}’
Get service cluster portkubectl get service nginx-service -o go-template=’{{(index .spec.ports 0).port}}’
Kubernetes Yaml Exampleslink: kubernetes yaml templates

1.9 StatefulSet

NameCommand
List statefulsetkubectl get sts
Scale statefulsetkubectl scale sts <stateful_set_name> --replicas=5
Delete statefulset only (not pods)kubectl delete sts <stateful_set_name> --cascade=false
Kubernetes Yaml Exampleslink: kubernetes yaml templates

1.10 Volumes & Volume Claims

NameCommand
Check the mounted volumeskubectl exec storage ls /data
Check persist volumekubectl describe pv pv0001
List storage classkubectl get storageclass
Kubernetes Yaml Exampleslink: kubernetes yaml templates

1.11 Security

NameCommand
List certificateskubectl get csr
Kubernetes Yaml Exampleslink: kubernetes yaml templates

1.12 Resources

NameCommand
Customize resource definitionkubectl set resources deployment nginx -c=nginx --limits=cpu=200m,memory=512Mi

1.13 Other Components

1.13.1 Log files

NameCommand
API Server.log= in master node/var.log=/kube-apiserver.log
Scheduler.log= in master node/var.log=/kube-scheduler.log
Controller.log= in master node/var.log=/kube-controller-manager.log
Kubelet.log= in worker node/var.log=/kubelet.log
Kube Proxy.log= in worker node/var.log=/kubelet-proxy.log

1.13.2 Events & Metrics

NameCommand
View all eventskubectl get events --all-namespaces

1.13.3 Namespace & Security

NameCommand
List authenticated contextskubectl config get-contexts
List contextskubectl config get-contexts
Switch contextkubectl config use-context <cluster-name>
List all namespaces definedkubectl get namespaces
kubectl config file~/.kube/config
Kubernetes Yaml Exampleslink: kubernetes yaml templates

1.13.4 Network

NameCommand
Temporarily add a port-forwardingkubectl port-forward redis-izl09 6379
Add port-forwaring for deploymentkubectl port-forward deployment/redis-master 6379:6379
Add port-forwaring for replicasetkubectl port-forward rs/redis-master 6379:6379
Add port-forwaring for servicekubectl port-forward svc/redis-master 6379:6379
Get network policykubectl get NetworkPolicy

1.14 Basic

1.14.1 Key Concepts

NameSummary
CNCFCloud Native Computing Foundation
CRIContainer Runtime Interface
CNIContainer Network Interface
CSIContainer Storage Interface

1.14.2 Kubernets Critical Files

NameComment
Config folder/etc/kubernetes/
Certificate files/etc/kubernetes/pki/
Credentials to API server/etc/kubernetes/kubelet.conf
Superuser credentials/etc/kubernetes/admin.conf
Kubernets working dir/var/lib/kubelet/
Docker working dir/var/lib/docker/
Etcd working dir/var/lib/etcd/
Network cni/etc/cni/net.d/
Docker container log/var/log/containers/
Log files/var/log/pods/
Envexport KUBECONFIG=/etc/kubernetes/admin.conf
Env/etc/systemd/system/kubelet.service.d/10-kubeadm.conf

1.14.3 Check status

NameSummary
List everythingkubectl get all --all-namespaces
Get cluster infokubectl cluster-info
Get configurationkubectl config view
Get kubectl versionkubectl version
Get component statuskubectl get componentstatus
Similar to docker pskubectl get nodes
Similar to docker inspectkubectl describe pod nginx-app-413181-cn
Similar to docker logskubectl logs
Similar to docker execkubectl exec
Get services for current namespacekubectl get svc
Get node statuskubectl describe node $node_name

1.14.4 Kubernetes Developer Resources

NameSummary
API Conventionslink: API Conventions

1.15 Minikube

NameCommand
Get minikube versionminikube version, link: all minikube releases
Start minikube with a specific k8s versionminikube start --kubernetes-version v1.10.0
Start minikube env with a bigger machine flavorminikube start --memory 5120 --cpus=4
Gets all available Kubernetes versions for minikubeminikube get-k8s-versions
Mount host OS’s folder to minikube VMminikube mount /host-mount-path:/vm-mount-path
Check minikube config in your host OS desktop~/.minikube/machines/minikube/config.json
folder of k8s.io/minikube-hostpath provisioner/tmp/hostpath-provisioner, /tmp/hostpath_pv
Critical minikube folder/var/lib/localkube, /var/lib/docker, /data
minikube docker-enveval $(minikube docker-env)
Get minikube logminikube logs
Get dashboardminikube dashboard
SSH to minikube vmminikube ssh
Get ipminikube ip
Get cluster infokubectl cluster-info
List addonsminikube addons list
Get service infominikube service $srv_name

1.16 Misc scripts

  • Tail pod log by label
namespace="mynamespace"
mylabel="app=mylabel"
kubectl get pod -l "$mylabel" -n "$namespace" | tail -n1 \
    | awk -F' ' '{print $1}' | xargs -I{} \
      kubectl logs -n "$namespace" -f {}
  • Get node hardware resource utilization
kubectl get nodes --no-headers \
     | awk '{print $1}' | xargs -I {} \
     sh -c 'echo {}; kubectl describe node {} | grep Allocated -A 5'

kubectl get nodes --no-headers | awk '{print $1}' | xargs -I {} \
    sh -c 'echo {}; kubectl describe node {} | grep Allocated -A 5 \
     | grep -ve Event -ve Allocated -ve percent -ve -- ; echo'
  • Apply the configuration in manifest.yaml and delete all the other configmaps that are not in the file.
kaubectl apply --prune -f manifest.yaml --all --prune-whitelist=core/v1/ConfigMap

1.17 More Resources

License: Code is licensed under MIT License.

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

https://github.com/kubecamp/kubernetes_in_2_days

https://marc.xn–wckerlin-0za.ch/computer/kubernetes-on-ubuntu-16-04

https://codefresh.io/kubernetes-guides/kubernetes-cheat-sheet/

About

:book: Kubernetes CheatSheets In A4

https://cheatsheet.dennyzhang.com/cheatsheet-kubernetes-A4