This is a mono repository for my homelab infrastructure. I try to adhere to Infrastructure as Code (IaC) and GitOps practices using the tools like Ansible, Terraform, Kubernetes, Flux, Renovate and GitHub Actions.
There is a template over at onedr0p/flux-cluster-template if you wanted to try and follow along with some of the practices I used here.
TODO
It is advisable to install pre-commit and the pre-commit hooks that come with this repository.
-
Enable Pre-Commit
task precommit:init
-
Update Pre-Commit, though it will occasionally make mistakes, so verify its results.
task precommit:update
The Git repository contains the following directories under kubernetes
and are ordered below by how Flux will apply them.
📁 kubernetes # Kubernetes cluster defined as code
├─📁 bootstrap # Flux installation
├─📁 flux # Main Flux configuration of repository
└─📁 apps # Apps deployed into the cluster grouped by namespace
Here we will create a Age Private and Public key. Using SOPS with Age allows us to encrypt secrets and use them in Ansible and Flux.
- Create a Age Private / Public Key
age-keygen -o age.agekey
- Set up the directory for the Age key and move the Age file to it
mkdir -p ~/.config/sops/age
mv age.agekey ~/.config/sops/age/keys.txt
- Export the
SOPS_AGE_KEY_FILE
variable in yourbashrc
,zshrc
orconfig.fish
and source it, e.g.
export SOPS_AGE_KEY_FILE=~/.config/sops/age/keys.txt
source ~/.bashrc
- Fill out the Age public key in the
.config.env
underBOOTSTRAP_AGE_PUBLIC_KEY
, note the public key should start withage
...
📍 The .config.env
file contains necessary configuration that is needed by Ansible, Terraform and Flux.
-
Copy the
.config.sample.env
to.config.env
and start filling out all the environment variables.All are required unless otherwise noted in the comments.
cp .config.sample.env .config.env
-
Once that is done, verify the configuration is correct by running:
task verify
-
If you do not encounter any errors run start having the script wire up the templated files and place them where they need to be.
task configure
📍 Here we will be running a Ansible Playbook to prepare Ubuntu Server for running a Kubernetes cluster.
-
Ensure you are able to SSH into your nodes from your workstation using a private SSH key without a passphrase. This is how Ansible is able to connect to your remote nodes.
-
Install the Ansible deps
task ansible:init
-
Verify Ansible can view your config
task ansible:list
-
Verify Ansible can ping your nodes
task ansible:ping
-
Run the Fedora Server Ansible prepare playbook
task ansible:prepare
-
Reboot the nodes
task ansible:reboot
📍 Here we will be running a Ansible Playbook to install k3s with this wonderful k3s Ansible galaxy role. After completion, Ansible will drop a kubeconfig
in ./kubeconfig
for use with interacting with your cluster with kubectl
.
☢️ If you run into problems, you can run task ansible:nuke
to destroy the k3s cluster and start over.
-
Verify Ansible can view your config
task ansible:list
-
Verify Ansible can ping your nodes
task ansible:ping
-
Install k3s with Ansible
task ansible:install
-
Verify the nodes are online
task cluster:nodes # NAME STATUS ROLES AGE VERSION # k8s-0 Ready control-plane,master 4d20h v1.21.5+k3s1 # k8s-1 Ready worker 4d20h v1.21.5+k3s1