Deploying Flask app to raspberry kubernetes cluster Part - 1 (Environment Setup)
Tags: kubernetes, flask, docker, raspberrypi
Published: 2023-08-19 06:17:23

recently i have bought two raspberry pi's and taught of converting it into a server to host backend code for free 😉
yaa you heard it right you can host you website or the backend api for free on raspberry pi

i will walk you through the steps required in this process

first you need a raspberry pi i have two but you can even use one for this guide and it is completely fine, and you need to install the raspberry pi debian OS i would suggest you to install the **raspberry pi light 64bit** os we dont need desktop verison it could create unessary load

you can find [this](https://www.youtube.com/watch?v=nZyyfJYOhbM) video helpful to install raspberrypi os

now lets get into the main part !!

## 1. Setting up K3s in RaspberryPi

k3s is a lightweight kubernetes which can run on low end devices
with two raspberry pi's we can configure one raspberry pi as master node and other as worker node

**Configuring the Master Node**

1. SSH into raspberry pi and edit the **/boot/cmdline.txt** and add the following line at the end of the file

```
# /boot/cmdline.txt
# (append this to the end of file don't replace)
cgroup_memory=1 cgroup_enable=memory
```

2. Reboot your raspberry pi using

```
sudo reboot
```

3. once the raspberry pi is rebooted SSH into it again now its time to install k3s

start installing k3s using the following commands we will be using docker backend for k3s

Install docker

```
curl https://releases.rancher.com/install-docker/20.10.sh | sh
```

Install k3s with `--docker` option

```
curl -sfL https://get.k3s.io | sh -s - --docker
```

you can verify the k3s installation using `sudo docker ps`
you will get the following output

```
CONTAINER ID        IMAGE                     COMMAND                  CREATED              STATUS              PORTS               NAMES
3e4d34729602        897ce3c5fc8f              "entry"                  About a minute ago   Up About a minute                       k8s_lb-port-443_svclb-traefik-jbmvl_kube-system_d46f10c6-073f-4c7e-8d7a-8e7ac18f9cb0_0
bffdc9d7a65f        rancher/klipper-lb        "entry"                  About a minute ago   Up About a minute                       k8s_lb-port-80_svclb-traefik-jbmvl_kube-system_d46f10c6-073f-4c7e-8d7a-8e7ac18f9cb0_0
436b85c5e38d        rancher/library-traefik   "/traefik --configfi…"   About a minute ago   Up About a minute                       k8s_traefik_traefik-758cd5fc85-2wz97_kube-system_07abe831-ffd6-4206-bfa1-7c9ca4fb39e7_0
de8fded06188        rancher/pause:3.1         "/pause"                 About a minute ago   Up About a minute                       k8s_POD_svclb-traefik-jbmvl_kube-system_d46f10c6-073f-4c7e-8d7a-8e7ac18f9cb0_0
7c6a30aeeb2f        rancher/pause:3.1         "/pause"                 About a minute ago   Up About a minute                       k8s_POD_traefik-758cd5fc85-2wz97_kube-system_07abe831-ffd6-4206-bfa1-7c9ca4fb39e7_0
ae6c58cab4a7        9d12f9848b99              "local-path-provisio…"   About a minute ago   Up About a minute                       k8s_local-path-provisioner_local-path-provisioner-6d59f47c7-lncxn_kube-system_2dbd22bf-6ad9-4bea-a73d-620c90a6c1c1_0
be1450e1a11e        9dd718864ce6              "/metrics-server"        About a minute ago   Up About a minute                       k8s_metrics-server_metrics-server-7566d596c8-9tnck_kube-system_031e74b5-e9ef-47ef-a88d-fbf3f726cbc6_0
4454d14e4d3f        c4d3d16fe508              "/coredns -conf /etc…"   About a minute ago   Up About a minute                       k8s_coredns_coredns-8655855d6-rtbnb_kube-system_d05725df-4fb1-410a-8e82-2b1c8278a6a1_0
c3675b87f96c        rancher/pause:3.1         "/pause"                 About a minute ago   Up About a minute                       k8s_POD_coredns-8655855d6-rtbnb_kube-system_d05725df-4fb1-410a-8e82-2b1c8278a6a1_0
4b1fddbe6ca6        rancher/pause:3.1         "/pause"                 About a minute ago   Up About a minute                       k8s_POD_local-path-provisioner-6d59f47c7-lncxn_kube-system_2dbd22bf-6ad9-4bea-a73d-620c90a6c1c1_0
64d3517d4a95        rancher/pause:3.1         "/pause"
```

now type the following command to get the master node's token we will need this to configure the other raspberry pi as worker node

```
sudo cat /var/lib/rancher/k3s/server/node-token

# [out]: K106e6f015b9399c74bab39bf6be3b53bf9661fbc64d6da8ab713544843399418cb::server:04cf766c678cf022a6f80d296b4125c8
```

**
4. now lets SSH into our other raspberry pi and configure it as the worker node

**you need to follow the above step of editing the /boot/cmdline.txt even for this node and reboot the Pi**

once you have done editing the `/boot/cmdline.txt` just install the docker with the command 

```
curl https://releases.rancher.com/install-docker/20.10.sh | sh
```
**2. Worker node setup**

after installing the docker we will configure the worker node

```
curl -sfL https://get.k3s.io | K3S_URL=http://<IP_OF_MASTER_NODE>:6443 sh -s - agent --token <masternodes token> --docker
```

you are done with the setup process of the master and worker nodes


you can verify the setup using `sudo kubectl get nodes` inside the master node

now we will copy the maters node kubectl config file so that we can access it from our local machine

type the following command

`sudo cat /etc/rancher/k3s/k3s.yaml`

```
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: ...
    server: https://127.0.0.1:6443  # <-- replace this with local ip
  name: default
contexts:
- context:
    cluster: default
    user: default
  name: default
current-context: default
kind: Config
preferences: {}
users:
- name: default
  user:
    client-certificate-data: ...
    client-key-data: ...

```

replace `127.0.0.1` with the local ip of the pi in my case it is 192.168.1.10 you can get it by typing following command `ip a`

now copy the config into `~/.kube/config` in your local system

**note: you need to install kubectl in you local system**

you can follow this guide to install kubectl
https://kubernetes.io/docs/tasks/tools/install-kubectl-linux/

**we are done with the setup now in part 2 we will deploy a flask application to kubernetes**

**part 2**
https://dev.to/rohit20001221/deploying-flask-app-to-raspberry-kubernetes-cluster-part-2-3bld