---
# System prepended metadata

title: 11. Kubernetes

---

# 11. Kubernetes

[toc]


### Keywords
:::success
- Software Application architectures
    - Monolithic
    - SOA (Service Oriented Architecture)
    - Microservices
- Stateless vs Stateful applications
- k8s (Kubernetes)
- With great **flexibility** comes great **complexity**
- Kubernetes ~= Datacentres
    - Storage (CSI)
    - Networking (CNI)
        - Flannel
        - Weave
        - Calico
    - compute (Pods)
    - Security (Authentication/RBAC)
- namespaces ~= applications
:::

**Software Evolution**
:::warning
Monolithic --> SOA --> Microservices
:::

### Topics

:::info
- Introduction
- Monolithic vs Microservices
- Kubernetes Architecture
- Installation and Configuration (via k3s)
- Kubernetes Objects
- Kubernetes Object Definitions (YAML)
:::

### Introduction

#### Monolithic vs Microservices

![](https://i.imgur.com/vU8ri8V.png)


#### Benefits of Microservices
:::warning
- One component failing does not result in failing of app as a whole
- Easier testing and deployment
- API based communication
- Services can be Scaled individually
- Loosely coupled
- Different services can be developed using different languages
:::

#### Kubernetes Architecture

![](https://i.imgur.com/pRSbekE.png)


### Installation and Configuration

#### k3s installation
````yaml=
curl -sfL https://get.k3s.io | sh - 

##### Check for Ready node, takes ~30 seconds 

root@sl-controller:~# kubectl get nodes
NAME            STATUS   ROLES                  AGE   VERSION
sl-controller   Ready    control-plane,master   18s   v1.25.6+k3s1
root@sl-controller:~# 
````

#### Creating command alias for shortcuts (Optional)
````yaml=
Configure the alias/shortcut command:
alias kc='k3s kubectl'
or
alias kc='sudo k3s kubectl'

check/test the alias:
kc get node
````

#### Add a worker node to cluster
````yaml=!
Make sure that Docker (or any other runtime) is installed and run the following command on the worker node:

Syntax:
curl -sfL https://get.k3s.io | K3S_URL=https://myserver:6443 K3S_TOKEN=mynodetoken sh -

Example:
curl -sfL https://get.k3s.io | K3S_URL=https://<worker-node-ip>:6443 K3S_TOKEN=K10089aed2050d7c7336f87961059c6fa56a5cc69e886cbc2c2e0c2ed4c5685a3ed::server:0b3ab3945d645d4c60b70bf29497ec64 sh -
````
:mag: `K3S_TOKEN` is stored at `/var/lib/rancher/k3s/server/node-token` on the Manager/controller node

### Working with Kubernetes Objects

#### Explore your Kubernetes cluster
````yaml=
kubectl version
kubectl cluster-info
kubectl get nodes (nodes/node/no)
kubectl get pods (pods/pod/po)
kubectl get namespaces (namespaces/namespace/ns)
kubectl get pods --all-namespaces
kubectl get pods -n kube-system
kubectl get pods [pod name]
kubectl get pods -o wide --all-namespaces #Show pods along with nodes
kubectl get pods -o wide --all-namespaces | grep <node> #Show pods on specified node
kubectl describe node <node> #Show all of the non-terminated pods running on that node
````

#### Pods

Pods are the smallest deployable units of computing that you can create and manage in Kubernetes.

![](https://i.imgur.com/p3kCYDq.png)
:arrow_right: *Source: https://kubernetes.io/docs/*

#### Pods inside a worker node

![](https://i.imgur.com/KxMDI9l.png)
:arrow_right: *Source: https://kubernetes.io/docs/*


#### K8s pod definition
Example 1:
````yaml=
apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - name: nginx
    image: nginx:latest
    ports:
    - containerPort: 80
````
Create the pod from above definition
````
kubectl create -f <file-name.yaml>
````


### References
:::success
- https://medium.com/@sajithswa/soa-vs-microservices-with-docker-and-kubernetes-1-291686200a0f
- https://12factor.net/
- https://www.rancher.com/
- https://k3s.io/
- https://www.cncf.io/phippy/the-childrens-illustrated-guide-to-kubernetes/
- https://www.youtube.com/watch?v=BE77h7dmoQU&t=1s

**Kubernetes workshops**
- https://kubernetes.io/docs/tutorials/kubernetes-basics/

EKS
- https://www.eksworkshop.com/
- https://labs.play-with-k8s.com/

AKS
- https://www.microsoft.com/azure/partners/news/article/azure-kubernetes-service-workshop
- https://github.com/carlosalexei/aks-workshop
:::
