DevOps 06 - Kubernetes basics

Posted by Lanzhou on February 16, 2022

Research summary

  • Basic objects
  • Basic concepts

1. Why?

  • Why we need Container orchestration?

Issues related to Container Deployment

  • When one container is not working, how to start a new container automatically to replace the existing container.
  • When workload increases, how to scale containers horizontally & automatically.

These issues are container orchestration issues.

Container orchestration tools

  • Docker Swarm
  • Mesos
  • Kubernetes …

Why choose Kubernetes?

why k8s k8s market share

2. What?

  • What is K8s? what k8s

  • “Kubernetes” → ancient Greek word for “helmsman”. → ship wheel logo
  • Kubernetes → a group of servers → cluster
    • It can run specific program in different nodes, in order to manage containers inside these nodes. The purpose is to automate resource management.
  • What can K8s do?
  • Kubernetes functions:
    • Self-healing: restarts containers that fail.(within 1 second)
    • Automatic bin packing:
      • You tell Kubernetes how much CPU and memory (RAM) each container needs. Kubernetes can fit containers onto your nodes to make the best use of your resources.
    • Service discovery: One service can automatically discover the services that it depends on.
      • Service discovery → a technique for getting traffic from one container to another using the containers direct IP address
      • Kubernetes can expose a container using the DNS name or using their own IP address.
      • Service discovery takes advantage of the labels and selectors to associate a service with a set of pods.
      • A single pod or a ReplicaSet may be exposed to internal or external clients via services, which associate a set of pods with a specific criterion. Any pod whose labels match the selector defined in the service manifest will automatically be discovered by the service. This architecture provides a flexible, loosely-coupled mechanism for service discovery.
    • Load balancing
      • If traffic to a container is high, Kubernetes is able to load balance and distribute the network traffic so that the deployment is stable.
    • Automated rollouts and rollbacks
      • If the newly released version is not working, can instantly roll back to the previous version.
    • Storage Orchestration
      • Kubernetes allows you to automatically mount a storage system of your choice, such as local storages, public cloud providers, and more.
    • Elastic stretching

3. How?

  • How does K8s work?
    • Basic Components:
      • A Kubernetes cluster consists of a set of worker machines, called nodes, that run containerized applications. Every cluster has at least one worker node.
      • A kubernetes cluster is composed of master node(Control Plane) and worker nodes.
      • Master node → Control & Management (Control plane)
        • ApiServer: The only entrypoint for managing resources, accept user commands…
        • Scheduler: Responsible for resource schedule, schedule pods to corresponding nodes following designed strategies. (Focus on using algorithm to calculate & decide who is going to do the job)
        • ControllerManager: Responsible for maintaining the state of the cluster, such as create pos, program deployment arrangements, fault detection, automatic scaling, and rolling updates
        • Etcd: Responsible for storage of the info of resource objects. (e.g. Master asked Node1 to run a job nginx)
      • Worker node → work (data plane, provides a running env for containers)
        • Kubelet: Responsible for container life cycle, control docker to create or destroy containers. (Contact of master, manage docker)
        • KubeProxy: responsible to provide service discovery inside cluster & load balancing. (User can visit app inside container via kube-proxy)
        • Docker: responsible for managing containers
      • the API which sits in front of services, the worker nodes & the Kubelet process that the nodes run, all together make up the Kubernetes Cluster.

    k8s structure k8s api

    Nginx service Example:

    1. Firstly, once k8s environment is running, master and worker node would store their info inside etcd database.
    2. A nginx service installation request would be send to ApiServer inside master node.
    3. ApiServer will use Scheduler component to “think” and determine which node is responsible for service installation task. At the same time. it would read nodes info from etcd, then use algorithm to calculate and choose between different worker nodes and give the result to ApiServer.
    4. ApiServer would use Controller-Manager to arrange a worker node to install nginx service.
    5. Once kubelet receives command, it will notify docker, and let docker to start a nginx pod. (minimum unit in k8s, containers running inside pod)
    6. So nginx service is running successfully now, if user wants to visit nginx, kube-proxy (k8s network proxy) can create proxy of pod, so that outside users can visit nginx service inside the cluster.

4. Basic Objects

  • NameSpace

    • used to isolate pods running environments, logical pods group.
    • namespaces provides a mechanism for isolating groups of resources within a single cluster.
  • Pod: Smallest unit in k8s, containers are running in pods, 1 or more containers inside one pod.

  • Deployment: pod controller

  • Controller: k8s manage pods via controller, like start pod, stop pod, scaling pod numbers etc. (Multiple controllers)
  • Service: a Service is an abstraction which defines a logical set of Pods; unified entrance, multiple pods link to one service. → load balancing etc. Service is the bridge in between pods and outside.

  • Ingress:
    • An API object that manages external access to the services in a cluster, typically HTTP. Ingress may provide load balancing, SSL termination and name-based virtual hosting.
  • Label & Selector: used to characterise pods for example, similar pods have the same label, service map to pods based on label (using selector) (Label is not only for pods!)

  • Basic Concepts
    • Master: Cluster control node, a cluster has at least one master node.
    • Node: Workload node, master arrange containers to the nodes, worker node is responsible for running the containers.

Useful Resources: