DEV Community

Cover image for Drilling down kubernetes cheat sheet
Sunil Vijay
Sunil Vijay

Posted on

Drilling down kubernetes cheat sheet

Hey dev people out there, it's been a longtime since writing a blog so thought of starting this 2021 with some useful resources on kubernetes. Hope you benefit out of it

What is Kubernetes 🎺

Kubernetes is a platform for managing containerised workloads. kubernetes orchestrates computing, networking, and storage to provide a seamless portability across infrastructure providers.

This particular blog is based on noobs for kube commands and operations. Also this can be used as a quick handbook for k8s.

Alt Text

Deployment

  • I'm putting deployment in a simple sentence to understand. Basically deployment is something will specify how many replicas should we run inside the Kubernetes cluster. Also that's a way to handle HA clusters [ High Availability ]. By just writing a yaml file you can deploy a deployment. Will show you some sample.

Alt Text

Image source: matthewpalmer
kubectl get deploy
kubectl get deploy -o wide 
kubectl get deploy -o yaml 
kubectl describe deploy
kubectl edit deploy [ deployment.name ] -n [ namespace.name ]
kubectl run redis-app --image=redis --replicas=4 --port=6943
kubectl delete deployment [ deployment.name ] -n [ namespace.name ]
kubectl scale --replicas=5 deployment/redis-app
kubectl rollout status deployment/redis-app
kubectl rollout history deployment/redis-app
kubectl rollout pause deployment/redis-deployment, resume
kubectl rollout undo deployment/redis-deployment
kubectl expose deployment/redis --type=NodePort -n [ namespace.name ]
Enter fullscreen mode Exit fullscreen mode

Service

  • Kubernetes Service is an abstraction which defines a logical set of Pods and a policy by which to access them – sometimes called a micro-service. Something like DNS where you can expose your application to outside world. This divides into 2, so called NodePort / Cluster IP.

Alt Text

Image source: matthewpalmer
kubectl get svc 
kubectl get svc -o wide 
kubectl get svc -o yaml 
kubectl get svc -show-labels
kubectl describe svc
kubectl get services –sort-by=.metadata.name
kubectl delete pods,services -n [ namespace.name ]
kubectl get endpoints -A 
kubectl get service redis-service -o go-template='{{.spec.clusterIP}}’
kubectl get service nginx-service -o go-template='{{(index .spec.ports 0).port}}’
kubectl patch svc redis-service -p '{"spec": {"type": "LoadBalancer"}}'
kubectl expose service/redis-svc --type=LoadBalancer -n [ namespace.name ]
kubectl port-forward svc/redis-master 6379:6379
Enter fullscreen mode Exit fullscreen mode

Nodes

  • Node is nothing but the host machine where your pod runs !
kubectl get nodes
kubectl get nodes -o wide 
kubectl get nodes -o yaml 
kubectl get nodes --selector=[label_name] 
kubectl get nodes -o json 
kubectl describe nodes 
kubectl top nodes
Enter fullscreen mode Exit fullscreen mode

Pods

  • Pods in Kubernetes are the cube box which comprised with the docker containers. When a pod gets deployed you'll have a unique IP address for that pod. Also as like all, pod can also be defined in yaml file.
kubectl get pods 
kubectl get pods -o wide 
kubectl get pods -o yaml 
kubectl get pods --show-labels 
kubectl top pod --all-namespaces
kubectl edit pod [pod.name] -n [namespace.name]
kubectl get pods --field-selector status.phase=Running
kubectl get pods | grep -e ContainerCannotRun -e Pending -e Unknown -e Error
kubectl get pods –sort-by=’.status.containerStatuses[0].restartCount’
kubectl get pods -o=’custom-columns=PODS:.metadata.name,Images:.spec.containers[*].image’
kubectl get pods nginx -o yaml --export > nginx_pod_info.yaml 
kubectl get pods –sort-by=’.status.containerStatuses[0].restartCount’
Enter fullscreen mode Exit fullscreen mode

Namespace

  • Namespaces in kube is like separate blocks where your deployment is isolated for easier to deploy and delete. By default kubernetes will create 3 namespaces: Kube-system, Kube-public, default.
kubectl get namespace 
kubectl get namespace [ namespace-name ] -o yaml 
kubectl describe namespace [ namespace-name ] 
kubectl edit namespace [ namespace-name ]
kubectl get events --all-namespace 
kubectl get events -sort-by=.metadata.creationTimestamp
kubectl -n [ namespace-name ] delete po,svc,deploy --all
Enter fullscreen mode Exit fullscreen mode

Logs

  • Sometimes we might need to check why the pods are getting failed, so we need to check the logs of the pods deployed with the below commands.
kubectl get events
kubectl get events -n default 
kubectl get events -w 
kubectl exec -it -n “$ns” “$podname” – sh -c “echo $msg >>/logs/poderror.log”
Enter fullscreen mode Exit fullscreen mode

Labels

  • Labels can be used to organize and select subsets of objects. They are often used for example to identify releases (beta, stable), environments (dev, prod), or tiers (frontend, backend).
kubectl label pods redis-master owner=sunil
kubectl label pods redis-master owner-   #this command is to remove label
kubectl label [node.name] disktype=ssd
kubectl label [pod.name] env=prod
Enter fullscreen mode Exit fullscreen mode

Service Account

kubectl edit sa [service.account.name]
kubectl delete sa [service.account.name]
Enter fullscreen mode Exit fullscreen mode

Some kube maintenance command

kubectl cordon [node.name]
kubectl uncordon [node.name]
kubectl drain [node.name]
Enter fullscreen mode Exit fullscreen mode

Taint

kubectl taint [node.name] [taint.name]
Enter fullscreen mode Exit fullscreen mode

Kube-shell terminal for faster insight

  • These days kube-shell terminal made me more comfy on resolving k8s conflicts and faster retrieval of cluster information. Not sure this can be used on client premises [Security Approval Needed]. But make use of it on your local system. Hope this would save your time in typing commands and not to view my blog again xD

Alt Text

Download kube-shell click here

Some Hacks

To list all the images which is present inside the cluster

Alt Text

Also I'm including this video, trust me it'll be fun to watch and easy to understand 😅
Thanks Matt Butcher & Bailey Beougher for this video.

Top comments (0)