K8S Cheatsheet

Posted on Jun 3, 2020

Terminologies:

  • k8s can use internally not just docker but also cri-o, container-d etc.,
  • Each pod must be located on the same server, i.e. it is not possible to spread containers of a pod across multiple servers
  • Node: Is a server / computing unit; a pod always runs inside a node
  • Cluster: Contains a bunch of nodes (can be across different data centers), but nodes are recommended to stay closer to each other; you need to set up this cluster, it is not automatic
  • Node types: Master node and worker node; master handles control-plane duties (scheduling, API, etc.)
  • Master node (AKA control plane) runs the Kubernetes control plane; your workloads run on worker nodes

K8s Core components:

Services that run on each node in a k8s cluster:

  • Kubelet: The kubelet is the primary “node agent” that runs on each node
  • Kube-proxy: Communication inside a node and between nodes
  • Container runtime: Runs containers on the node (Docker, containerd, CRI-O, etc.)

Control-plane components (typically on the master node):

  • Scheduler:
    • responsible for planning and scheduling load across nodes in the cluster
  • Kube controller manager:
    • controls controllers that reconcile cluster state
  • Cloud controller manager:
    • Interaction with cloud service provider
  • etcd:
    • storing cluster state as key-value pairs
  • API Server:
    • API server is located on the master / control plane
    • Kubelet from each worker node communicates with the API server on the master node
  • Kubectl:
    • CLI to control a Kubernetes cluster by interacting with the API server

Running k8s locally using Minikube:

  • Minikube is a single-node cluster designed to test Kubernetes 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 address
  • 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 kubectl describe pod podname
  • get more details of 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 a Kubernetes 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 inside that.
  • Once launched, you can use describe command to see the namespace
  • NOTE: if there are 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 related resources
  • kubectl delete pod nginx

K8s deployment:

  • Deployment is the name of an object in k8s
  • pods are enclosed and managed by a deployment object; we don’t directly interact with pods 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 the same, but you can create multiple replicas of the same pod and distribute it across different nodes in the k8s cluster; once a 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 separate objects; to connect them, we need to use another k8s object selector the string in selector and string in label should match in order 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 the 8364Vusd4 is the unique ID for deployment and the hd834hd refers 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 strategy is: RollingUpdate. It applies the update to replicas on a rolling basis to avoid 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 help assign an IP address to k8s components
  • services by default create an IP address at cluster level for the default k8s system; they usually start from 10*..
  • IP address of pods are hidden inside a different network, and they usually start from 172*…
  • Setting IP address for deployment: We do this through the deploy object and don’t interact directly with the 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 among the replicas from the pod will serve the request
  • 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 when running in cloud and it provides External-IP