Kubernetes Learning Part II - Understanding of Kubernetes Architecture and Hands-on Lab Setup
Tags: kubernetes, beginners, tutorial
Published: 2022-04-18 10:43:35

## Kubernetes Learning Series - Part II
## Table of contents
  - [Introduction](#introduction)
  - [Kubernetes High Level Architecture](#kubernetes-high-level-architecture)
  - [Control Plane](#control-plane)
  - [Node](#node)
  - [Kubernetes Tools](#kubernetes-tools)
  - [What is Minikube](#what-is-minikube)
  - [Minikube Installation Steps](#minikube-installation-steps)
  - [Install Curl](#install-curl)
  - [Install docker](#install-docker)
  - [Install Minikube](#install-minikube)
  - [Minikube Run](#minikube-run)
  - [Sample Output of minikube start and status](#sample-output-of-minikube-start-and-status)
  - [Install Kubectl](#install-kubectl)
  - [Verify and Run Minikube Cluster](#verify-and-run-minikube-cluster)
  - [Conclusion](#conclusion)
  - [Bibliography](#bibliography)

## Introduction
- In this article we will discuss the Kubernetes architecture and how to set up the lab environment to run the Kubernetes commands.
- This blog is part of the Kubernetes [Learning series](https://dev.to/chefgs/series/17756)
- In the previous [blog](https://dev.to/kcdchennai/kubernetes-learning-part-i-application-architecture-decision-and-purpose-of-k8s-38kk), we discussed about architecture decision that provides when to choose k8s for our application deployments

---

## Kubernetes High Level Architecture
- Let us now discuss Kubernetes Architecture and components
- Kubernetes has two major building blocks,
    1. Kubernetes `Control Plane` (or `Master Node` as called previously)
    2. `Nodes` (or `Worker Node` as called previously)

## Control Plane
Kubernetes Control Plane has four major components,
    1. *Kube API Server* : This component exposes Kubernetes API and acts as a front end of k8s control plane. `kubectl` is used to interact with API servers.
    2. *ETCD* : Secure key-value store
    3. *Kube Scheduler* : Watches newly created Pod with and selects node for those pods to run
    4. *Controller Manager* : This component manages the four controller processes of Kubernetes
  - Node Controller 
  - Job Controller
  - End-point Controller
  - Service account & Token Controller

![Kubernetes Architecture](https://d33wubrfki0l68.cloudfront.net/2475489eaf20163ec0f54ddc1d92aa8d4c87c96b/e7c81/images/docs/components-of-kubernetes.svg?raw=true)

## Node
A node consists of,
    1. Kubelet : An agent that runs on each node in the cluster. It makes sure that containers are running in a Pod.
    2. Kube-proxy : kube-proxy is a network proxy that runs on each node in your cluster, implementing part of the Kubernetes Service concept.
    3. Container Runtime : The container runtime is the software that is responsible for running containers.
Kubernetes supports several container runtimes: Docker, containerd, CRI-O, and any implementation of the Kubernetes CRI (Container Runtime Interface).
    4. Pods : Pods are the smallest deploy-able units of computing that we can create and manage in Kubernetes.
A Pod (as in a pod of whales or pea pod) is a group of one or more containers, with shared storage and network resources, and a specification for how to run the containers.

[Refer Kubernetes Component Documentation](https://kubernetes.io/docs/concepts/overview/components/) for more details.

---

## Kubernetes Tools
- There are various tools available to getting started with Kubernetes by doing the setup in User Workstation
- Tools that provide local cluster,
  - Minikube
  - Kind
- Tools to interact with Kubernetes
  - Kubectl
  - Kubeadm

As part of learning and to practice Kubernetes, we will pick-up **`minikube`** installation steps.

[Refer documentation for more details](https://kubernetes.io/docs/tasks/tools/)

## What is Minikube
- `minikube` is a CLI tool that lets you run Kubernetes cluster locally on any workstation. 
- It runs a single-node Kubernetes cluster on our personal computer (including Windows, macOS and Linux OS) so we can try out Kubernetes learning, and for any daily development work.
- Minikube cluster packs both the master and worker components for us to get started with managing the container.
- Below diagram depicts the minikube single node cluster,
![Minikube Cluster](https://github.com/chefgs/repo_images/blob/master/minikube_single_node_cluster.jpg?raw=true)

## Version information
While writing this article, Kubernetes version is `1.23.0` and minikube version is `1.25.2`

## Minikube Installation Steps
- In order to getting started with `minikube`, we need to install below components in our workstation
  - Install `curl` command
  - Install `docker`
  - Install `minikube`
  - Install `kubectl` 

- I have carried out the steps in `Ubuntu 21.04` based operating system
- Refer `minikube` [documentation](https://minikube.sigs.k8s.io/docs/start/) for installation steps for other operating systems

## Install curl
```
sudo apt update
sudo apt install curl -y
```
## Install docker
```
# Update the apt package index, and install the latest version of Docker Engine and containerd, 
# or go to the next step to install a specific version:
sudo apt update -y
sudo apt install -y docker-ce docker-ce-cli containerd.io

# Add current user to 'docker' user group to invoke minikube (otherwise, the .sock permission denied error will occur)
sudo usermod -aG docker $USER
newgrp docker

# Verify that Docker Engine is installed correctly by running the hello-world image.
sudo docker run hello-world
```
  
## Install Minikube  
```
cd /tmp
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube_latest_amd64.deb
sudo dpkg -i minikube_latest_amd64.deb
```

## Minikube Run
- Start `minikube` cluster and provide docker as the *container runtime* engine
```
$ minikube start --driver=docker
```
- If we want to run `minikube` with specific version of kubernetes, it can done using the option `--kubernetes-version`,
```
minikube start --kubernetes-version=v1.11.10
```

## Sample Output of minikube start and status
- It will show up the below output in console
```
😄  minikube v1.22.0 on Ubuntu 21.04
✨  Using the docker driver based on user configuration
👍  Starting control plane node minikube in cluster minikube
🚜  Pulling base image ...
💾  Downloading Kubernetes v1.21.2 preload ...
    > preloaded-images-k8s-v11-v1...: 502.14 MiB / 502.14 MiB  100.00% 7.75 MiB
    > gcr.io/k8s-minikube/kicbase...: 361.09 MiB / 361.09 MiB  100.00% 5.08 MiB
🔥  Creating docker container (CPUs=2, Memory=3900MB) ...
🐳  Preparing Kubernetes v1.21.2 on Docker 20.10.7 ...
    ▪ Generating certificates and keys ...
    ▪ Booting up control plane ...
    ▪ Configuring RBAC rules ...
🔎  Verifying Kubernetes components...
    ▪ Using image gcr.io/k8s-minikube/storage-provisioner:v5
🌟  Enabled addons: storage-provisioner, default-storageclass
💡  kubectl not found. If you need it, try: 'minikube kubectl -- get pods -A'
🏄  Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default
```
- Minikube status command,
```
$ minikube status
minikube
type: Control Plane
host: Running
kubelet: Running
apiserver: Running
kubeconfig: Configured
```
## Install Kubectl
- `minikube` comes with the desired version of `kubectl`
so, if we don't install `kubectl` explicitly, then we can invoke `kubectl` commands as below,
```
minikube kubectl -- <expected kubectl command>
```
- Otherwise, follow the `kubectl` [installation doc](https://kubernetes.io/docs/tasks/tools/) to install it
- If we installed `kubectl`, then we can invoke `kubectl` commands as below,
```
kubectl <expected kubectl command>
```
Kubectl installation steps for Ubuntu,
Run below commands to install kubectl
```
sudo snap install kubectl --classic
kubectl version
```

---

## Verify and Run Minikube Cluster
- We can use `kubectl` commands to verify the installed cluster and other details.
- We can use it to access our new cluster created using `minikube`
```
kubectl get po -A
```
or
```
minikube kubectl -- get po -A
```
- It shows the below output, which shows the deployed single node cluster created using `minikube`
- Please note components from control plane and worker node available as part of minikube deployment
```
$ kubectl get po -A
NAMESPACE              NAME                                         READY   STATUS    RESTARTS   AGE
kube-system            coredns-558bd4d5db-4tncm                     1/1     Running   5          9d
kube-system            etcd-minikube                                1/1     Running   5          9d
kube-system            kube-apiserver-minikube                      1/1     Running   5          9d
kube-system            kube-controller-manager-minikube             1/1     Running   5          9d
kube-system            kube-proxy-wtzn9                             1/1     Running   5          9d
kube-system            kube-scheduler-minikube                      1/1     Running   5          9d
kube-system            storage-provisioner                          1/1     Running   10         9d
```
- Then, we can try performing the creation of new k8s deployments,
```
minikube kubectl -- create deployment hello-minikube --image=k8s.gcr.io/echoserver:1.4
minikube kubectl -- expose deployment hello-minikube --type=NodePort --port=8080

minikube kubectl -- get services hello-minikube
minikube service hello-minikube
```
- The entire Minikube cluster can be visualised using the below command,
```
minikube dashboard
```

---

## Conclusion
- We have covered kubernetes architecture understanding and gone through the setup of lab for k8s hands-on
- Try out and start hands-on with Kubernetes and let me know your feedback
- This article was previously published on my dev community personal profile, re-publishing for the benefit of a wider audience.
- Hope this article is helpful to people getting started with kubernetes.

Thanks for reading!

### Heads up for Next Blog
We will further explore topics like deployment of pods and services in next blog

### Follow me and share your thoughts,
- [GitHub](https://github.com/chefgs)
- [LinkedIn](https://www.linkedin.com/in/saravanan-gnanaguru-1941a919/)
- [Twitter](https://twitter.com/saransid)
- [gsaravanan.dev](https://gsaravanan.dev)

## Bibliography
[Kubernetes official documentation](https://kubernetes.io/docs/home/)
[Minikube documentation](https://minikube.sigs.k8s.io/docs/start/)