So you would like to have a homelab? And keep infrastructure as code? You are a madman… for not trying it earlier!
Also, I needed to come up with some catchy abbreviation for the initiative. Please be introduced to “Homelab As Code, Kid” a.k.a. HACK, because the only more important IT thing than TLA (Three Letter Abbreviations) are TLAE (Three Letter Abbreviations Extended).
First things first. What is a homelab after all?
Homelab
What is it?
Homelab is a piece of your digital kingdom. A laboratory in your home, your own server. The popular trend is now to create one to oppose the constant violation of privacy conducted by BigTech. Some people treat it as a counter-culture movement embracing self-hosting as a way to own your data.
“You’ll own nothing and be happy.” - Ida Auken
Others, like me for example, like to tinker with technology without restrictions on what and how they can use. At work we have segregation of duty, no admin privileges or simply a predefined technology stack. Managing your own part of the digital world is very refreshing in these circumstances and allows me to embrace the learning fully. Preparing for a certificate? Sure - I will spin up my own kuberenetes cluster. Working on some website and want to experience the full process? Let’s do this. One Virtual Machine and you have it independent of your machine. Want to try out different CI/CD tools? The world is your oyster. And on, and on, and on… You get the drill.
There is also an added benefit to setting up your own network infrastructure - with custom internal DNS I will not let the [REDACTED] projector call home with telemetry. :)
Your homelab could be as simple as Raspberry Pi Zero set as the aforementioned DNS or some docker containers running in the background on your PC or as complex as an in-house data center with enterprise-grade hardware making you (or your parents) cry after receiving the electric bill. As always, I believe that extremes are not the way to go. While the Pi mentioned before is a nice start, the data center belongs in a data center in my honest opinion. If the homelab grows to the point where it starts to bring you money then of course it is another story but at the same time I wouldn’t call it a homelab anymore in that case.
How to start?
Alright. We covered what the homelab is. But how do we get there?
Get yourself whatever piece of technology you can. Dip the toe. Find an old laptop in the closet and flash a Linux Server distribution on it - Debian, Ubuntu, Fedora - it really doesn’t matter. Maybe you have an old gaming rig? That’s good. Take out the CD/DVD bay and GPU to minimize the power consumption and go! Nothing on hand? You can try Virtual Machines on your computer or get something used on a local platform - Raspberry Pis and mini PCs are a common start.
Where did I start? With the laptop - HP Elitebook 840 G2 with i5-5300U and 8GB DDR3 RAM to be precise. I installed Debian there, setup an SSH server, turned off the lock on lid-close and gone to town. My first attempt at homelab was a bunch of docker containers run via docker compose on the bare metal Debian.
Good next step is to use a proper hypervisor like Proxmox.
Infrastructure As Code (IAC)
Infrastructure As Code or IAC for short is a DevOps practice to, you guessed it, configure infrastructure as code. This comes with several benefits like self-documentation, idempotency and increased control over the configuration drift of your devices. The drawback? You need to spend more time upfront to generate the configuration files …and maybe there is a need to do some standardization because it is much easier to manage all servers as Debian / Ubuntu. Linux distributions have their own quirks and configurations, for example the package manager which is apt on Debian-based, yum / dnf on Red Hat-based, apk on Alpine or pacman on Arch-based.
I really like the IAC approach. The drawbacks are fun and challenging tasks to tackle too!
Today I will not show you how to start though. We need to get an overview of the tools that we will be using to setup our home server first.
Toolset
Probably you could have a fun time using only scripting languages like Bash, but I think that utilizing the proper toolset could get you a headstart. They are nicely documented and widely utilized in the industry. That means that you can put those on your CV while looking for a job or get some proper help from forums, reddit and AI chatbots.
Ansible
Ansible is maintained by Red Hat Inc. but luckily for us there is also a community-maintained version. Meet Ansible Community.
Ansible provides open-source automation that reduces complexity and runs everywhere. Using Ansible lets you automate virtually any task. Here are some common use cases for Ansible:
- Eliminate repetition and simplify workflows
- Manage and maintain system configuration
- Continuously deploy complex software
- Perform zero-downtime rolling updates
Ansible uses simple, human-readable scripts called playbooks to automate your tasks. You declare the desired state of a local or remote system in your playbook. Ansible ensures that the system remains in that state.
In our case we will be using it to provision the configuration of the node and by “node” I mean the existing Linux system. It could be anything ranging from bare metal servers (Raspberry Pi or old laptop), LXCs (Linux Containers) or VMs (Virtual Machines). We will also use it to roll out upgrades with some instructions (in Ansible known as Playbooks) so you will not have to log in to each system just to keep the packages up to date.
Bash
Good ol’ Bash. BASH stands for Bourne Again SHell and let’s stop there. If you have a need to read about it more here is a nice Wikipedia article.
For us it will serve as a scripting language. You will see it everywhere in Linux so it’s good to at least understand basic syntax. In IAC it will be helpful to define some procedures or scripts so in later stages we will have less to type while invoking Ansible or Terraform commands.
Git / GitLab
I have talked a bit about git and GitLab in a previous post. Feel free to check it out!
If you haven’t yet setup an account in one of the services (doesn’t have to be GitLab, but that’s what I am using) it is a good time to do it now. Take a look at the interface, get accustomed with the buttons. This will be our source of truth for IAC configuration, so whenever you will destroy (sic!) your computer or the control node goes missing you can get the whole config up and running in no time!
Make
It is another software that will help us write less in the long run. Make is using something called Makefile that serves as a predefined set of commands that you could use to build packages or run some commands. It is very much used in regular programming but for us it will serve a simpler purpose. We will define the execution commands there so this:
ansible-playbook ansible/playbooks/bootstrap-server.yml \
-i ansible/inventory/hosts.yml \
-u root \
--private-key ~/.ssh/id_ed25519 \
-e ansible_user=root \
-e ansible_ssh_private_key_file=~/.ssh/id_ed25519 \
--vault-password-file ~/.vault_pass \
--limit server
can become this:
make bootstrap-server
Terraform
HasiCorp Terraform. Very powerful tool dedicated for IAC.
What is Terraform?
Terraform is an infrastructure as code tool that lets you build, change, and version infrastructure safely and efficiently. This includes low-level components like compute instances, storage, and networking; and high-level components like DNS entries and SaaS features.
I am using that to provision new LXCs and VMs on the Proxmox server. You can use it to provision cloud infrastructure. It can even be combined with Ansible to run as one script.
This tool, similarly to (or I would even say more than) Ansible is stateful. What you see in the configuration is what you will get and the tool will make sure that it stays like that eliminating the configuration drift or your manual interventions - so always make sure to add fixes to the IAC configuration instead of just messing with the server manually if you can help it.
For now - it’s good to know it’s there but if you plan to continue setting up the basic homelab with me in the next post then we will not be using it (yet).
Summary
Summing up, homelab is what you want it to be and IAC is a coded way to get there.
Ansible and Terraform are THE tools that are used widely. In short words - Terraform will get you the server / instance and Ansible will configure it as you want.
The rest I would consider helpers. Git will store the configuration and allow you to version control it in case something gets bonked, bash and make will help you in the long run with commands.
Stay tuned for more as I want to help bootstrap you with a simple server!