Files
blog-ahmza-com/content/posts/building-a-docker-swarm-with-pis.md
T
2021-12-12 23:57:03 +00:00

134 lines
5.6 KiB
Markdown

---
title: Building a Docker Swarm With Pi's
date: 2021-12-12T17:23:32Z
author: "ahmza"
draft: true
weight:
tags: ["Homelab","Docker","Docker Swarm","Raspberry Pi","Pi4"]
description: "Pis are very capable computers, when banded together they're almost unstoppable"
canonicalURL: "https://blog.ahmza.com/Page%28/posts/building-a-docker-swarm-with-pis.md%29"
cover:
image: "/img/pi-docker-swarm/pi-swarm.gif" # image path/url
alt: "Pi Docker Swarm" # alt text
caption: "<text>" # display caption under cover
relative: false # when using page bundles set this to true
showtoc: true
searchHidden: false
---
## **Introduction**
The _Raspberry Pi_ from it's first iteration has been a great hit.
Adopted by many as the computer of choice for projects, and for
very good reason. The Pi is a fully fledged computer that fits in
your pocket and the most recent edition has blown fans away with
the flagship model boasting an ARM64 CPU and 8GB of RAM.
Now one _Raspberry Pi 4 8GB_ is more than powerful enough for most
workloads and in some cases totally unnecessary, **so you may even
get away with a single 4GB model**. However taking into consideration
their relatively low price and power consumption why not go all out
and build a cluster.
> **You can never have too many _Raspberry Pis_, just
not enough Ideas**
My excuse to build a Pi cluster is, I would like to migrate the
services currently running on a **bare metal server that sucks
a lot of power to a low power solution.** What better way to do that
than a Pi cluster.
### **My Setup**
* 4 x _Raspberry Pi 4 8GB_
* 4 x 32GB SD cards
* 4 x PoE hats
* 1 x 5 port PoE Switch
![Insert Image of cluster here]()
### **Goal of this post**
Hopefully after following this post you will have setup _Docker
swarm_ on a cluster of Pis **- stay even if you currently only have
one -** and understand how to bend it to your will 🦾.
### **Why** ***Docker Swarm***
While we can manage each docker node - _Raspberry Pi_ - individually
, **_Docker swarm_ enables us to manage all the devices from a single
main node** making managing the cluster a lot easier.
Think of _Docker swarm_ as a set of 🤖 hands which will run tasks on
each individual node for us. These tasks can consist of:
* Cluster Management
* Deployment and Scaling across the cluster
* Multi-host networking
* load balancing
* etc...
### **Well why not** ***Kubernetes***
Although _Kubernetes_ is awesome, **it adds a bunch of complexity that
is simply not needed in a home lab environment** where we only have
a small cluster. As well as that, from my experience creating
```docker-compose``` files is a lot easier than writing manifests.
**_Docker swarm_ can do everything I want it to and requires less
effort** which is great if you're like me 🥱 😂
### **Required Stuff**
> The links below are reccomendations/stuff I used
* [_Raspberry Pi_ 4B](https://thepihut.com/products/raspberry-pi-4-model-b?variant=31994565689406) (Reccomend 4-8 @ 8GB)
* [_Raspberry Pi_ PoE Hats](https://thepihut.com/products/power-over-ethernet-poe-hat-d-for-raspberry-pi) (If you can find the [PoE+](https://thepihut.com/products/raspberry-pi-poe-plus-hat) version get them)
* [Cluster Case](https://thepihut.com/products/cluster-case-for-raspberry-pi) (Get any case you want)
* [PoE switch](https://www.amazon.co.uk/gp/product/B076982FVC/) (Get one that has enough ports for the nuber of pis you want)
* [RJ45 Ethernet Cable](https://www.amazon.co.uk/gp/product/B00C4U030G) (Reccomend short cables as it will look neater)
* [Micro SD cards](https://www.amazon.co.uk/gp/product/B08J4HJ98L) (Get enough cards for the number of Pis, Get the biggest cards you can afford)
**This is without a doubt an expensive project**, however in my eyes
it's the cost of another server 😂 and the best part about this
project is **you can add to the cluster over time** spreding the cost
which is how I managed to purchase all the hardware. A suggestion
would be to **buy equipment that allows you room to grow** so an 8 port
PoE switch instead of a 4 port if you know you will scale to 8
nodes
If you build this then you can safely say another server is not
needed. This cluster should be more than capable to serve all your
home needs even if you only have 2 nodes. Put it this way, if you
have 4 8GB nodes then you have a combined total of:
* 32GB of RAM
* 16 physical CPU cores @ 1.5GHz
#### **Money Saving Tips**
* Just get 1-2 nodes to start
* cut PoE and use normal power cables
* Use Raspberry Pis with less memmory eg 4GB
* Grab a really cheap case or make one using [these](https://www.amazon.co.uk/dp/B091KJNZLB/)
* Use smaller SD Cards try not to go below 32GB (Reccomended 64GB If money saved elsewhere)
## **BUILD TIME !!**
### **Image the SD Cards**
So interesting fact the CPUs used in the raspberry pi 4 use an ARM64
archetecture meaning they're 64bit. When looking for images of
Raspbian you will only find 32bit variants which will work however
will not make use of all the hardware which is just a waste. 64bit
images exist however are well hidden, Dont fear 😨 **you can download
the latest images from the [raspberrypi.org index](https://downloads.raspberrypi.org/raspios_arm64/images/)**
To write the cards I use the Raspberry Pi imager [available here](https://www.raspberrypi.com/software/).
once that is installed and you have the 64bit raspbian image
downloaded do the following:
1. Click CHOOSE OS
1. Click Use custom
2. Select the 64bit rasbian image and open
2. Click CHOOSE STORAGE
1. Select storage device
3. Click WRITE
We dont need to unzip the file as the tool is clever enough to
handle that for us
**repeat steps for number of cards you have**
### Assemble the cluster