Cluster Details/Original Text
Highly opinionated template for deploying a single k3s cluster with Ansible and Terraform backed by Flux and SOPS.
The purpose here is to showcase how you can deploy an entire Kubernetes cluster and show it off to the world using the GitOps tool Flux. When completed, your Git repository will be driving the state of your Kubernetes cluster. In addition with the help of the Ansible, Terraform and Flux SOPS integrations you'll be able to commit Age encrypted secrets to your public repo.
The following components will be installed in your k3s cluster by default. They are only included to get a minimum viable cluster up and running. You are free to add / remove components to your liking but anything outside the scope of the below components are not supported by this template.
Feel free to read up on any of these technologies before you get started to be more familiar with them.
- cert-manager - SSL certificates - with Cloudflare DNS challenge
- calico - CNI (container network interface)
- flux - GitOps tool for deploying manifests from the
cluster
directory - hajimari - start page with ingress discovery
- kube-vip - layer 2 load balancer for the Kubernetes control plane
- local-path-provisioner - default storage class provided by k3s
- metallb - bare metal load balancer
- reloader - restart pods when Kubernetes
configmap
orsecret
changes - system-upgrade-controller - upgrade k3s
- traefik - ingress controller
For provisioning the following tools will be used:
- Ubuntu - this is a pretty universal operating system that supports running all kinds of home related workloads in Kubernetes
- Ansible - this will be used to provision the Ubuntu operating system to be ready for Kubernetes and also to install k3s
- Terraform - in order to help with the DNS settings this will be used to provision an already existing Cloudflare domain and DNS settings
- One or more nodes with a fresh install of Ubuntu Server 20.04. These nodes can be bare metal or VMs.
- A Cloudflare account with a domain, this will be managed by Terraform.
- Some experience in debugging problems and a positive attitude ;)
📍 You should install the below CLI tools on your workstation. Make sure you pull in the latest versions.
Tool | Purpose |
---|---|
ansible | Preparing Ubuntu for Kubernetes and installing k3s |
direnv | Exports env vars based on present working directory |
flux | Operator that manages your k8s cluster based on your Git repository |
age | A simple, modern and secure encryption tool (and Go library) with small explicit keys, no config options, and UNIX-style composability. |
go-task | A task runner / simpler Make alternative written in Go |
ipcalc | Used to verify settings in the configure script |
jq | Used to verify settings in the configure script |
kubectl | Allows you to run commands against Kubernetes clusters |
sops | Encrypts k8s secrets with Age |
terraform | Prepare a Cloudflare domain to be used with the cluster |
Tool | Purpose |
---|---|
helm | Manage Kubernetes applications |
kustomize | Template-free way to customize application configuration |
pre-commit | Runs checks pre git commit |
gitleaks | Scan git repos (or files) for secrets |
prettier | Prettier is an opinionated code formatter. |
It is advisable to install pre-commit and the pre-commit hooks that come with this repository. sops-pre-commit and gitleaks will check to make sure you are not by accident committing your secrets un-encrypted.
After pre-commit is installed on your machine run:
pre-commit install-hooks
The Git repository contains the following directories under cluster
and are ordered below by how Flux will apply them.
- base directory is the entrypoint to Flux
- crds directory contains custom resource definitions (CRDs) that need to exist globally in your cluster before anything else exists
- core directory (depends on crds) are important infrastructure applications (grouped by namespace) that should never be pruned by Flux
- apps directory (depends on core) is where your common applications (grouped by namespace) could be placed, Flux will prune resources here if they are not tracked by Git anymore
cluster
├── apps
│ ├── default
│ ├── networking
│ └── system-upgrade
├── base
│ └── flux-system
├── core
│ ├── cert-manager
│ ├── metallb-system
│ ├── namespaces
│ └── system-upgrade
└── crds
└── cert-manager
Very first step will be to create a new repository by clicking the Use this template button on this page.
Clone the repo to you local workstation and cd
into it.
📍 All of the below commands are run on your local workstation, not on any of your cluster nodes.
📍 Here we will create a Age Private and Public key. Using SOPS with Age allows us to encrypt and decrypt secrets.
- 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
...
- Verify the nodes are online, after deploying the cluster with Sidero.
kubectl get 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
-
Bootstrap flux but don't deploy the full flux yet
-
Disable the apps as needed for your cluster
-
Install full flux and wait for everything to deploy
flux get kustomization -A
and after a bitflux get helmrelease -A
-
Wait for rook to figure its life out for a bit. Usually within about 10 minutes it'll get everything needed setup, and the WebUI will come up.
watch kubectl -n rook-ceph get pods
If desired, login to the web UI with the password from:
kubectl -n rook-ceph get secret rook-ceph-dashboard-password -o jsonpath="{['data']['password']}" | base64 --decode && echo
-
Node feature discovery will automatically label my nodes with the correct USB devices.
-
Restore from backup with method of choice