All Articles

Basic Tutorial of Kubernetes

What is Kubernetes (k8s) ?

flight-instrument
Photographer: Rafael Cosquiere (https://www.pexels.com/zh-tw/photo/2064123/)
  • Kubernetes is just like the control panel (flight instrucment) of an aircraft.
  • For aviators, they have lots of component to control and manage, so there is a control panel for them.
  • For developers, when we have lots of computer to manage, so there is Kubernetes for us!

In short, Kubernetes is a tool for developers to manage a cluster of computers.


kubernetes icon
If you see the shape of Kubernetes logo, it's a rudder!

Think of you have lots of service to manage…

twitter system design
Non-offical Twitter System Architecture
  • You have Kafka, Cassandra, MySQL, Redis, Elasticsearch and many services. For each service, you need:

    • Deploy newer version to certain machines, rollback if deployment failed
    • Restart the service once it failed
    • Scale up when heavy loading

Why should we use Kubernetes ?

  • self-healing: auto restart services once if failed
  • automated rollouts and rollbacks
  • built-in load balancing
  • easier service discovery
  • secret and configuration management
  • it abstracts away the hardware infrastructure and exposes your whole cluster as a single enormous computational resource
  • better resource allocation: you can specify the CPU & memory for each service, k8s will automatically allocate them for you.
  • storage orchestration: you don’t need to think where to store your data*

Who invents Kubernetes ?

google logo
  • 15 years of experience of building production workloads at Google: Borg, Omega
standing on huge man

Before we dive into details

  • Let’s have a very simple practice today.

    practice

Core concepts of Kubernetes

Fundaments

  • Container: a technique to package our application, and run it in an isolated environment. You can think of that we packed our our application, and run it in a virtualized computer.
kubernetes icon

Architecture

  • Node: a real computer or a virtualized machine (e.g. ec2, gce) who has computing resource (CPU, memory)
  • Cluster: a bunch of real computers or VMs managed by Kubernetes
  • Pod: basic working unit of kubernetes
  • Container: a container instance by containerized technology, e.g. docker
kubernetes simplified architecture

A cluster can contain many nodes, a node can contain many pods, and a pod can contain many containers


Important resource types

  • Pod
  • Service (NodePort, LoadBalancer) & Ingress
  • ReplicaSet
  • Deployment
  • Persistent Volume

Pod

  • Purpose: the basic working unit for scaling/deployment
  • each container in a pod share same host and port space
  • each pod inside cluster has its own unique IP address
kubernetes pod
every pod has a unique IP iddress inside cluster (Cluster IP)
  • we can specify multiple containers you would like to use in a single pod

When we use multiple containers in a pod ?

  • Sidecar pattern: log collecting

    when to separate pod
  • Ambassador/Adapter pattern: proxy / adaptor

    when to separate pod

When we should NOT use multiple containers in a pod ?

when we want to scale up different applications inside a pod in different scale, we should separate it as different pods

when to separate pod

Service

  • Purpose: make cluster’s internal and external networking more convenient and feasible
  • Use cases:

    • Pod-to-Pod communication
    • Pod to connect external service
    • External client to connect internal pod

Pod-to-Pod communication

  • Pod can fail / be deleted / be created at any time, we are not possible to setup our application by IP address mannually
  • By using service, we can refer to one or more pods by a selector
  • When a service can to muliple pods, the service can load balancing the requests
service usage example

Pod to connect external service

service usage example

External client to connect internal pod

service usage example

service usage example
NodePort

service usage example
LoadBalancer

service usage example
Ingress

ReplicaSet

  • Purpose: to maintain a stable number of replica Pods running
  • Use case:

    • High Availability (HA): to ensure service always being available
    • Load Balance
    • For example: Login service, API service, …
service usage example
ReplicaSet

Deployment

  • Purpose: to deploy our application more gracefully
  • Use case: almost every application
  • Before introducting Deployment, let’s see what kind of strategies we can use for deploying new version of application:
service usage example
Delete all old pods, and then create all new pods

service usage example
Create all new pods, and then delete all old pods

service usage example
Delete one, and create one, and so on

service usage example
Create several and delete several, and so on (rolling update)

And actually, Deployment works by ReplicaSet:

service usage example

Persistent Volume

Pods can be deleted/removed at any time, and all the files inside pod will disappear by default. However, for some of service, persistent storage is crucial. (e.g. DB service)

service usage example
We need a persistent storage even pod was deleted and re-created

service usage example
Inside kubernetes, the concept was splitted into PersistentVolume and PersistentVolumeClaim

service usage example
The work flow of using persistent storage in kubernetes

Important resource types recap

  • Pod: basic working unit
  • Service (NodePort, LoadBalancer) & Ingress: for networking
  • ReplicaSet: to keep stable number of replica pods
  • Deployment: to deploy application more gracefully
  • Persistent Volume: to save data persistently

Tips for inspecting issues in Kubernetes cluster while on duty

  • Common tasks for onduty person:

    • ensure applications running correctly
    • scale up/down service if needed

How to monitor whether application is running properly ?

  • Pod: status, age
  • Monitoring service like Grafana & Prometheus

    • worker: understand the source of jobs, usually a queue.

      • how many tasks are there in a queue ?
      • what is the producing / consuming velocity ?
      • what is the oldest task in the queue ?
    • server: request per second / latency (response time)
    • cronjob: log

Reference