- Published on
Day 01: Exploring the K8s Architecture
- Authors
- Name
- Surya Harahap
- @suryaharahap18
Welcome to Day 1 of my 100 Days of Cloud Journey ✍️
Afer class learning AWS Cloud Architecting from Kominfo. Today, I learn about Kubernetes/K8s. What Is Kubernetes?
What is Kubernetes
Kubernetes is a tool for automated management of containerized applications, also known as a container orchestration tool. What this means is it's Kubernetes job to start and stop container‑based applications based on the requirements of the systems administrator or developer. Now, one of the key facets of Kubernetes is workload placement. If I need to deploy a container‑based application into a cluster, how do I deploy it? On which servers does it physically live on? Does it need to be co‑resident with other services or containers inside of the cluster? If that's the case, we can define that in our code that describes our application, and Kubernetes can manage that for us.
Kubernetes also provides an infrastructure abstraction. As a developer, if I need to deploy an application into production, I really don't want to have to care about which server it lives on or have to go configure a load balancer to send traffic to my application. That's all going to be handled for me under the hood by Kubernetes. One of the other core ideas behind Kubernetes is this concept called desired state.
You can find plenty of information and documentation on Kubernetes at the official Kubernetes site: kubernetes.io
The End Goal: Automate your application infrastructure and make it easy to manage!
Benefits of Using Kubernetes
Let's look at some of the key benefits of using Kubernetes
- Speed of Deployment
Kubernetes gives us the ability to deploy container‑based applications very, very quickly. And what this enables us actually to do is to get code from a developer's workstation into production fast, and that gives us the ability to absorb change quickly. The speed of deployment really allows you to iterate quickly and get new versions of code out, enabling new key capabilities for your organization's applications.
- Ability to absorb change & recover quickly
Next up is Kubernetes' ability to recover very quickly. When we define our system and code and we define that desired state, so perhaps a collection of web app containers, and something causes our system to no longer be in that desired state, so perhaps a container crashed or a server failed, well, it's Kubernetes' job to ensure that our application comes back to that defined desired state by deploying new containers supporting our applications, making sure that we have a collection of web app containers up or really whatever resource it is that defines our application or our system's desired state.
- Hide complexity in the cluster
And then finally, Kubernetes allows us to hide infrastructure complexity in the cluster. And so things like storage, networking configuration, and workload placement are core functions of Kubernetes, and developers don't have to worry about these things when deploying container‑based applications into Kubernetes.
Kubernetes Principles
And so, for example. If we've defined that we want three web app containers online, it's Kubernetes job, more specifically, a controller's job, to ensure that three web app containers are online. A controller will start the three replicas up, and later, if one of those fails or goes offline, a controller will create a new web app container, replacing the one that failed.
The key concept here is controllers are what make changes to the system to ensure the desired state. Another core principle is the Kubernetes API. The Kubernetes API provides a collection of objects that we can use to build and define the systems that we want to deploy in code.
The objects defined in our code define the desired state of our applications or the systems that we want to deploy in Kubernetes. The Kubernetes' API is implemented and available via the API Server. The API Server is the central communication hub for information in a Kubernetes cluster.
Understanding API Objects - Pods
Pods is one or more containers inside of a Kubernetes cluster.
Understanding API Objects - Storage
Kubernetes stores data for persistent storage in the cluster using the concept of persistent volumes (PVs) and persistent volume claims (PVCs). PVs are storage resources defined by the cluster administrator, independent of any specific pod. When a pod needs access to storage, it claims a PV by creating a PVC specifying its storage requirements. This decouples the pod from the underlying storage, allowing for greater flexibility in administration. Initially, Kubernetes used volumes, which tied storage directly to pods, limiting flexibility. PVs and PVCs provide a more flexible and scalable approach to managing storage in Kubernetes clusters.
What are Containers?
Kubernetes is all about managing containers.
Containers wrap software in independent, portale packages, making it easy to quickly run software in a variety of environments.
When you wrap your software in a container, you can quickly and easily run it (almost) anywhere. That makes containers great for automation!
Containers are a bit like virtual machines, but they are smaller and start up faster. This means that automation can move quickly and efficiently with containers.
more detail check sites about containers
Orchestration
With containers, you can run a variety of software components across a cluster of genertic servers.
This can help ensure high availability and make it easier to scale resources.
This reises some questions:
- How can i ensure that multiple instances of a piece of software are spread across multiple servers for high availability?
- How can i deploy new code changes and roll them out across the entire cluster?
- How can i create new containers to handle additional load (scale up)?
Of course, these kinds of tasks can all be done manually, but that is a lot of work!
Detail sites about more topics orchestration
Cluster Architecture
In this section, I am going to show you how to build your own Kubernetes cluster.
Here is the architecture of the cluster that we will be building 3 node Kubernetes cluster, 1 will function as the control plane server, and 2 will server as worker nodes. I have a Kubernetes master server, kube node 1 and kube node 2.
Kube Master | Kube Node 1 | Kube Node 2 |
---|---|---|
Docker | Docker | Docker |
Kubeadm | Kubeadm | Kubeadm |
Kubelet | Kubelet | Kubelet |
Kubectl | Kubectl | Kubectl |
Control Plane | Control Plane | Control Plane |
Note:
Docker:
Docker is just going to be our container runtime. Kubernetes requires a container runtime to actually execute the containers on each node, so we're going to need to install Docker on all 3 nodes.
Kubeadm:
This is a tool which automates a large portion of the process of setting up a cluster. It will make out job much easier!
Kubelet:
The essential component of Kubernetes that handles running containers on a node. Every server that will be running containers needs kubelet.
Kubectl:
Command-line tool for interacting with the cluster once it is up. We will use this to manage the cluster.
Kube Master:
We're going to have the control plane. The control plane is just a series of different services that form that Kubernetes master structure that allows the Kubernetes master to control the cluster.
Installing Docker
Docker is the container runtime that we will be using.
A container runtime is the software that actually runs the containers. Kubernetes supports several other container runtimes (such as rkt and containerd), but Docker is the most popular.
Te first step in building our cluster is to install Docker on all three servers:
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
# Add the repository to Apt sources:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
Install Docker Engine, containerd, and Docker Compose
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Check Docker:
suryaharahap835@k8s-control:~$ sudo docker version
Client: Docker Engine - Community
Version: 26.0.0
API version: 1.45
Go version: go1.21.8
Git commit: 2ae903e
Built: Wed Mar 20 15:17:51 2024
OS/Arch: linux/amd64
Context: default
Server: Docker Engine - Community
Engine:
Version: 26.0.0
API version: 1.45 (minimum version 1.24)
Go version: go1.21.8
Git commit: 8b79278
Built: Wed Mar 20 15:17:51 2024
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.6.28
GitCommit: ae07eda36dd25f8a1b98dfbf587313bsds9c0190bb
runc:
Version: 1.1.12
GitCommit: v1.1.12-0-g51d5e94
docker-init:
Version: 0.19.0
GitCommit: de40ad0
Check Documentation install docker ubuntu detail here
Install Kubernetes packages - kubeadm, kubelet and kubectl
Let's install kubernetes packages, kubeadm, kubelet, and kubectl on all three servers.
# Install Packages
# containerd prerequisites, and load two modules and configure them to load on boot
# https://kubernetes.io/docs/setup/production-environment/container-runtimes/
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF
sudo modprobe overlay
sudo modprobe br_netfilter
# sysctl params required by setup, params persist across reboots
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
EOF
# Apply sysctl params without reboot
sudo sysctl --system
# Now install the Kubernetes packages and Kubernetes does require swap to be disabled.
# On all nodes, disable swap
sudo swapoff -a
# Install Kubernetes packages - kubeadm, kubelet and kubectl
# Add k8s.io's apt repository gpg key, this will likely change for each version of kubernetes release.
sudo apt-get install -y apt-transport-https ca-certificates curl gpg
sudo curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.29/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
# Add the Kubernetes apt repository
echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.29/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list
# Update the package list and use apt-cache policy to inspect versions available in the repository
sudo apt-get update
apt-cache policy kubelet | head -n 20
# Install the required packages, if needed we can request a specific version.
# Use this version because in a later course we will upgrade the cluster to a newer version.
# Try to pick one version back because later in this series, we'll run an upgrade
VERSION=1.29.1-1.1
sudo apt-get install -y kubelet=$VERSION kubeadm=$VERSION kubectl=$VERSION
sudo apt-mark hold kubelet kubeadm kubectl containerd
# To install the latest, omit the version parameters. I have tested all demos with the version above, if you use the latest it may impact other demos in this course and upcoming courses in the series
# sudo apt-get install kubelet kubeadm kubectl
# sudo apt-mark hold kubelet kubeadm kubectl containerd
# systemd Units
# Check the status of our kubelet and our container runtime, containerd.
# The kubelet will enter a inactive (dead) state until a cluster is created or the node is joined to an existing cluster.
sudo systemctl status kubelet.service
sudo systemctl status containerd.service
So, now that i have kubelet, kubeadm and kubectl installed on all three of my servers. I'm just gonna do a little bit of quick verification.
kubeadm version