Giter Site home page Giter Site logo

cheatsheet-kubernetes-a4's Introduction

1 Kubernets CheatSheet

Blog URL: https://cheatsheet.dennyzhang.com/cheatsheet-kubernetes, Category: kubernetes

linkedin
github
slack


PRs Welcome

File me Issues or star this repo.

See more CheatSheets from Denny: #denny-cheatsheets

1.1 Common Usage

1.1.1 yaml templates

NameSummary
Pod yaml examplespod-dummy.yaml, pod-nginx.yaml, pod-volume-redis.yaml
Pod yaml examplespod-healthcheck-nginx.yaml, pod-gitclone.yaml, pod-handlers.yaml
Deployment yaml examplesdeployment-nginx.yaml
Service yaml examplesservice-clusterip-nginx.yaml
Statefulset yaml examplesstatefulset-nginx.yaml, statefulset-single-mysql, statefulset-replicated-mysql
Serviceaccount yaml examplesserviceaccount-default.yaml
Volume yaml examplespod-volume-empty-redis.yaml

1.1.2 Common Commands

NameCommand
Validate yaml file with dry runkubectl create --dry-run --validate -f pod-dummy.yaml
Start a temporary pod to run wget testkubectl run --rm wget-test --image=busybox --restart=Never --tty -i
Start a temporary pod to run curl testkubectl run --rm curl-test --image=yauritux/busybox-curl --tty -i
Open a bash terminal in a podkubectl exec -it storage sh
Check pod environment variableskubectl exec redis-master-ft9ex env
Get all services for all namespacekubectl get service --all-namespaces
Get system conf via configmapkubectl -n kube-system get cm kubeadm-config -o yaml
Query healthcheck endpointcurl -L http://127.0.0.1:10250/healthz

1.1.3 Check Performance

NameCommand
Get node resource usagekubectl top node
Get node resource usagekubectl top pod
List resource utilization for all containerskubectl top pod --all-namespaces --containers=true

1.2 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 dummy –template ‘{{.status.initContainerStatuses}}’
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}’
kubectl run commandkubectl exec -it -n “$namespace” “$podname” – sh -c “echo $msg >>/dev/termination-log”
  • Delete Pod
NameCommand
Delete podkubectl delete pod hello-node-95913-n63qs
Delete pod by labelkubectl delete pod -l env=test

1.3 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.4 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

1.5 Service

NameCommand
List all serviceskubectl get services
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}}’

1.6 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

1.7 Volumes & Volume Claims

NameCommand
Check the mounted volumeskubectl exec storage ls /data
Check persist volumekubectl describe pv pv0001

1.8 Other Components

1.8.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.8.2 Events & Metrics

NameCommand
View all eventskubectl get events --all-namespaces

1.8.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

1.8.4 Network

NameCommand
Temporarily add a port-forwardingkubectl port-forward redis-izl09 6379

1.8.5 Endpoint

NameCommand
List endpointskubectl get endpoints

1.9 Basic

1.9.1 Key Concepts

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

1.9.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.9.3 Check status

NameSummary
Start a servicekubectl run hello --image=my_img --port=80
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 deployment infokubectl get deploy
Delete servicekubectl delete service nginxservice
Get kubectl versionkubectl version
Get cluster infokubectl cluster-info
Get configurationkubectl config view
Get component statuskubectl get componentstatus
Get node statuskubectl describe node $node_name
Get services for current namespacekubectl get svc

1.9.4 Kubernetes Developer Resources

NameSummary
API Conventionshttps://github.com/kubernetes/community/blob/master/contributors/devel/api-conventions.md

1.10 Minikube

link: minikube in GitHub

NameCommand
Start minikube envminikube start
minikube docker-enveval $(minikube docker-env)
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

TODO: rolling-update command is imperative, better use Deployments rollout. It’s declarative.

1.11 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.12 More Resources

License: Code is licensed under MIT License.

  • Useful links
- 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/

cheatsheet-kubernetes-a4's People

Contributors

dennyzhang avatar riteshpuj2013 avatar

Watchers

James Cloos avatar Sathish MR avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.