K8S Cheatsheet
Terminologies:
- k8s can use internally not just docker but also cri-o, container-d etc.,
- Each pod must be located in the same sever, i.e, it is not possible to spread containers of a pod across multiple servers
- Node: Is a server / computing unit, pod always runs inside a node
- Cluster: Contains a bunch of nodes (can be across differnt data centers), but nodes are recommended to stay closer to each other, you need to setup this cluster, it is not automatic
- Node types: Master node and worker node, master - does load balancing of resources.
- Master node(AKA control plane) has runtime of k8s, all your code runs on the worker node.
K8s Core components:
Services that run common across nodes: (They are present on each node on k8s cluster)
- Kubelet: The kubelet is the primary “node agent” that runs on each node
- Kubeproxy: Communication inside of node and between nodes
- Container runtime: api server of the master node
- Scheduler:
- responsible for planning and scheduling load across nodes in the cluster
- Kube controller manager:
- controls everything on the k8s cluster
- Cloud controller manager:
- Interaction with cloud service provider
- etcd:
- storing cluster logs as kv-pair
- Kubectl:
- CLI to control kubernets cluster by interacting with API service
- Service that are prese
- API Server:(communicator of worker nodes)
- API server located only in the master node
- Kubelet from each worker node communicates wint on only in Master node
Running k8s locally using Minikube:
- Minicube is a single node cluster, which is designed to test K8S locally
- Requires 2 CPU cores, but can be configured to run with 1, useful to try out in digitalocean basic droplets
- Starting: (it requires a virtual machine to run inside)
minikube start --vm-driver=virtualbox
- Minikube uses docker as default container environment
- to get the cluster ip
minikube ip
- ssh to minikube
ssh docker@<ip-address>
, default password: tcuser - upon starting minikube, kubectl in the host machine will be automatically linked
- Check kubectl cluster info:
kubectl cluster-info
- Get all nodes in kubectl
kubectl get nodes
Pods:
- Each pod has its own IP addresss
- Each pod has one or more containers, but usually 1
- listing all pods in a namespace,
kubectl get pods --namespace=kube-system
- get details about a single pod, eg, to know the namespace
kubects describe pod podname
- get more details of pod all pods
kubectl get pods -o wide
this gives info like, IP address, node in which this pod is running and its status.
Launching a new service like redis inside kubernets cluster
- Create a pod with existing docker image from public repo
kubectl run rediskv --image=redis
- After this, k8s first finds a suitable worker node and creates a new pod and launches this container in side that.
- Once launched, you can use desribe command to see, the namespace,
- NOTE: if there several containers in a pod, they all share the same IP Address
Deleting a pod:
- When deleting a pod, it will remove all its volumes and stuffs
kubectl delete pod nginx
K8s deployment:
- Deployment is name of an object in k8s
- pods are enclosed and managed by deployment object, we don’t directly interact with pod or containers
- we can use
kubectl run
command to create pods, but we can’t scale it. i.e we can’t increase its replicas - Most used approach to create pods is via deployment.
- All pods inside the deployment are same, but you can create multiple replicas of the same pod and distribute it across different nodes across k8s cluster, and once deployment is created, we can even increase the quantity of the pods.
- Command to create deployment:
kubectl create deployment redis-depl --image=redis
- Once deployment is created, relevant pod will also be created automatically
- in k8s, pods and deployments are seperate objects, to connect them, we need to use another k8s object
selector
the string in selector and string in label should match inorder for the connection to happen. - replica set: manages all pods related to deployment. Replica set can be identified as a string in this format
nginx-deployment-8364Vusd4-hd834hd
here the8364Vusd4
is the unique ID for deployment and thehd834hd
referres to ID of the first replica, each replica has its own unique ID - how to increase the replica size
kubectl scale deployment redis-depl --replicas=3
- Deployment Flow:
- when you want to deploy new code to existing deployment (via docker images):
kubectl set image deployment <depl-name> <depl_name>=<docker_image_name>:img_version_tag
- Default deployment stratergy is: RollingUpdate. It applies the update to replica on rolling basis to aviod downtime
- Deployment rollout logs can be seen using:
kubectl rollout status deploy <deploy_name>
- Some useful deployment strategies: https://github.com/ContainerSolutions/k8s-deployment-strategies
Services:
- services are k8s objects that helps assign IP address to k8s componentts
- servies by default creates an IP address at cluster level for the default k8s system, they usually start from 10*..
- IP address of pods are hidden inside in a different network, and they usually start from 172*…
- Setting IP address for deployment: We do this through deploy object and don’t interact directly with pod
- Method 1 [ClusterIP]:
- Expose the Pod ports (You should know target port in advance, ex: redis 6379)
kubectl expose deployment redis-depl --port=8090 --target-port=6379
- By running this above command k8s creates a new cluster IP address (1 IP address for all the pods inside the deployment)
for the redis-depl deployment, It can be found by this command:
kubectl get services
- All deployments inside the K8s cluster can access each other, but this cluster IP address of the deployment is hidden from external connections / internet.
- You can access cluster IP from any Node in a K8s cluster
- When you access that cluster IP, one amoung the replica from the pod will serve the request
- Expose the Pod ports (You should know target port in advance, ex: redis 6379)
- Method 2 [NodePort]:
- Creating service with type NodePort, exposes cluster IP to internet
kubectl expose deployment redis-depl --type=NodePort --port=8090 --target-port=6379
- service will assign a random PORT as well
- Method 3 [LoadBalancer]:
- Creating service with type LoadBalancer, Also exposes cluster IP to internet
kubectl expose deployment redis-depl --type=LoadBalancer --port=8090 --target-port=6379
- this method is preferred with running in cloud and it provides External-IP