Brief introduction of Kubernetes(k8s)

What is Kubernetes?

Kubernetes is an open-source container orchestration platform. Kubernetes is used to automate the deployment, scaling, and management of containerized applications. It was originally developed by Google and is now maintained by the Cloud Native Computing Foundation (CNCF).

How does Kubernetes work?

When you deploy Kubernetes, you get a cluster.

A Kubernetes cluster consists of a set of worker machines, called nodes, that run containerized applications. Every cluster has at least one worker node.

The worker node(s) host the Pods that are the components of the application workload. The control plane manages the worker nodes and the Pods in the cluster.

In production environments, the control plane usually runs across multiple computers and a cluster usually runs multiple nodes, providing fault-tolerance and high availability.

Why need Kubernetes?

You need to manage the containers that run the applications and ensure that there is no downtime. For example, if a container goes down, another container needs to start.

Kubernetes provides you with a framework to run distributed systems resiliently.

Iterations of the deployment process

The iterations of the deployment process can be summarized as follows: At first, the programmer directly hosted the application on the operating system, then on a virtual machine. Nowadays, the major way of deployment has become containerized.

Containers are similar to virtual machines, but they have relaxed isolation properties to share the operating system (OS) among applications. As they are decoupled from the underlying infrastructure, they are portable across clouds and OS distributions.

Most common terminology in Kurbernetes

Term Definition
Node A worker machine in a Kubernetes cluster that runs containerized applications.
Pod The smallest deployable unit in Kubernetes. It represents one or more containers that are deployed together on a single node and share the same network namespace.
Container A lightweight, standalone executable package of software that includes everything needed to run an application.
Deployment A higher-level concept that represents a set of replicas of a pod running the same application.
Service An abstract way to expose an application running on a set of pods as a network service.
Namespace A way to divide the resources of a Kubernetes cluster between multiple users or teams.
ReplicaSet Ensures that a specified number of replicas of a pod are running at any given time.
Volume A directory containing data that is accessible to containers in a pod.
ConfigMap A Kubernetes object used to store configuration data that can be used by a pod or container.
Secret A Kubernetes object used to store sensitive data, such as passwords or API keys, that should not be exposed in a pod's configuration.

Dive deeper in Kubernetes architecture

There are two main part in Kubernetes, Control Plane and Node, Both of them accompanied with serval components for different functionality purpose.

Control Plane

The control plane acts like the brain of a Kubernetes cluster, helping to make decisions for the cluster.

The control plane's components make global decisions about the cluster, such as scheduling, as well as detect and respond to cluster events.

There are four major components in each control plane.

  • API Server
  • etcd
  • Scheduler
  • Control Manager

Node

A node is a worker machine in a Kubernetes cluster that runs containerized applications.

There are three major components in each node.

  • kubelet
  • kube-proxy
  • Container Runtime

In short, the control plane is responsible for managing the lifecycle of Kubernetes objects.

Worker nodes are responsible for running containers, and they use the Kubernetes runtime environment to manage container execution.

Kubernetes objects

Kubernetes objects are persistent entities in the Kubernetes system. Kubernetes uses these entities to represent the state of your cluster.

Kubernetes objects can be created in two ways

  • Use Kubernetes API
  • Use kubectl

Even when using kubectl, there are still necessary API calls behind the scenes. Most often, you provide the information to kubectl in a .yaml file. kubectl then converts the information to JSON format when making the API request to the Kubernetes API server.

Pod

A Pod is similar to a set of containers with shared namespaces and shared filesystem volumes. A pod is the smallest deployable unit in Kubernetes. It represents one or more containers that are deployed together on a single node and share the same network namespace.

The relationship between them

Developers write the .yaml file and use kubectl to generate Kubernetes object.

The control plane manage nodes based on the description in Kubernetes object.

The control plane ensures that the current state of the cluster matches the desired state specified by Kubernetes objects.

Demo

Install minikube

At first, we need to install minikube, minikube is local Kubernetes, focusing on making it easy to learn and develop for Kubernetes.

brew install minikube

This is my environment, select the one you are using.

After installation done, check if it works.

peter.yang@ycy-mac demo % minikube version
minikube version: v1.29.0
commit: ddac20b4b34a9c8c857fc602203b6ba2679794d3

Start it.

peter.yang@ycy-mac demo % minikube start
😄  minikube v1.29.0 on Darwin 12.5 (arm64)
✨  Using the docker driver based on existing profile
👍  Starting control plane node minikube in cluster minikube
🚜  Pulling base image ...
🔄  Restarting existing docker container for "minikube" ...
🐳  Preparing Kubernetes v1.26.1 on Docker 20.10.23 ...
🔗  Configuring bridge CNI (Container Networking Interface) ...
🔎  Verifying Kubernetes components...
    ▪ Using image gcr.io/k8s-minikube/storage-provisioner:v5
🌟  Enabled addons: storage-provisioner, default-storageclass
🏄  Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default

Show all nodes that can be used to host our applications.

peter.yang@ycy-mac demo % kubectl get nodes
NAME       STATUS   ROLES    AGE   VERSION
minikube   Ready    <none>   34s   v1.26.1

If you prefer UI for manipulating

minikube dashboard

Create Deployment

A Kubernetes Deployment checks on the health of your Pod and restarts the Pod's Container if it terminates. Let's create a .yaml file for creating Kubernetes object.

vim web-deployment.yaml

The following example creates a ReplicaSet to bring up three nginx Pods

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

Create Service

If we want Pod accessible from outside the Kubernetes virtual network, we have to expose the Pod as a Kubernetes Service.

vim web-service.yaml

Under the spec section, we specified app: nginx correspond to the metadata in web-deployment.yaml.

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80

Apply Configuration

Run kubectl apply -f <file-name> to apply the configuration we just created.

kubectl apply -f web-deployment.yaml
kubectl apply -f web-service.yaml

Access App

Check Pod, there should be three due to replicas: 3 in web-deployment.yaml

peter.yang@ycy-mac demo % kubectl get pod
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-85996f8dbd-lwvcd   1/1     Running   0          106s
nginx-deployment-85996f8dbd-s858r   1/1     Running   0          106s
nginx-deployment-85996f8dbd-x5wp5   1/1     Running   0          106s

Check Service, there should exist nginx-service .

peter.yang@ycy-mac demo % kubectl get svc
NAME            TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
kubernetes      ClusterIP   10.96.0.1      <none>        443/TCP   46m
nginx-service   ClusterIP   10.108.30.45   <none>        80/TCP    3m45s

Finally, run the following command to access our app.

minikube service nginx-service

References

https://kubernetes.io/docs/home/

Tags:
# k8s
# container