{"id":22508536,"url":"https://github.com/bianchidotdev/k3s-gitops-cluster","last_synced_at":"2025-08-03T13:30:52.194Z","repository":{"id":38195629,"uuid":"448383403","full_name":"bianchidotdev/k3s-gitops-cluster","owner":"bianchidotdev","description":null,"archived":false,"fork":false,"pushed_at":"2023-06-01T12:16:47.000Z","size":171,"stargazers_count":0,"open_issues_count":46,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2023-06-10T22:23:14.740Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bianchidotdev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null}},"created_at":"2022-01-15T20:26:58.000Z","updated_at":"2022-01-15T21:32:54.000Z","dependencies_parsed_at":"2023-02-09T03:01:07.746Z","dependency_job_id":null,"html_url":"https://github.com/bianchidotdev/k3s-gitops-cluster","commit_stats":null,"previous_names":["bianchidotdev/k3s-gitops-cluster"],"tags_count":null,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bianchidotdev%2Fk3s-gitops-cluster","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bianchidotdev%2Fk3s-gitops-cluster/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bianchidotdev%2Fk3s-gitops-cluster/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bianchidotdev%2Fk3s-gitops-cluster/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bianchidotdev","download_url":"https://codeload.github.com/bianchidotdev/k3s-gitops-cluster/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228545875,"owners_count":17934814,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-07T01:21:17.313Z","updated_at":"2024-12-07T01:21:18.150Z","avatar_url":"https://github.com/bianchidotdev.png","language":"Shell","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Template for deploying k3s backed by Flux\n\nHighly opinionated template for deploying a single [k3s](https://k3s.io) cluster with [Ansible](https://www.ansible.com) and [Terraform](https://www.terraform.io) backed by [Flux](https://toolkit.fluxcd.io/) and [SOPS](https://toolkit.fluxcd.io/guides/mozilla-sops/).\n\nThe purpose here is to showcase how you can deploy an entire Kubernetes cluster and show it off to the world using the [GitOps](https://www.weave.works/blog/what-is-gitops-really) tool [Flux](https://toolkit.fluxcd.io/). When completed, your Git repository will be driving the state of your Kubernetes cluster. In addition with the help of the [Ansible](https://github.com/ansible-collections/community.sops), [Terraform](https://github.com/carlpett/terraform-provider-sops) and [Flux](https://toolkit.fluxcd.io/guides/mozilla-sops/) SOPS integrations you'll be able to commit Age encrypted secrets to your public repo.\n\n## Overview\n\n- [Introduction](https://github.com/k8s-at-home/template-cluster-k3s#wave-introduction)\n- [Prerequisites](https://github.com/k8s-at-home/template-cluster-k3s#memo-prerequisites)\n- [Repository structure](https://github.com/k8s-at-home/template-cluster-k3s#open_file_folder-repository-structure)\n- [Lets go!](https://github.com/k8s-at-home/template-cluster-k3s#rocket-lets-go)\n- [Post installation](https://github.com/k8s-at-home/template-cluster-k3s#mega-post-installation)\n- [Thanks](https://github.com/k8s-at-home/template-cluster-k3s#handshake-thanks)\n\n## :wave:\u0026nbsp; Introduction\n\nThe following components will be installed in your [k3s](https://k3s.io/) 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.\n\nFeel free to read up on any of these technologies before you get started to be more familiar with them.\n\n- [cert-manager](https://cert-manager.io/) - SSL certificates - with Cloudflare DNS challenge\n- [calico](https://www.tigera.io/project-calico/) - CNI (container network interface)\n- [flux](https://toolkit.fluxcd.io/) - GitOps tool for deploying manifests from the `cluster` directory\n- [hajimari](https://github.com/toboshii/hajimari) - start page with ingress discovery\n- [kube-vip](https://kube-vip.io/) - layer 2 load balancer for the Kubernetes control plane\n- [local-path-provisioner](https://github.com/rancher/local-path-provisioner) - default storage class provided by k3s\n- [metallb](https://metallb.universe.tf/) - bare metal load balancer\n- [reloader](https://github.com/stakater/Reloader) - restart pods when Kubernetes `configmap` or `secret` changes\n- [system-upgrade-controller](https://github.com/rancher/system-upgrade-controller) - upgrade k3s\n- [traefik](https://traefik.io) - ingress controller\n\nFor provisioning the following tools will be used:\n\n- [Ubuntu](https://ubuntu.com/download/server) - this is a pretty universal operating system that supports running all kinds of home related workloads in Kubernetes\n- [Ansible](https://www.ansible.com) - this will be used to provision the Ubuntu operating system to be ready for Kubernetes and also to install k3s\n- [Terraform](https://www.terraform.io) - in order to help with the DNS settings this will be used to provision an already existing Cloudflare domain and DNS settings\n\n## :memo:\u0026nbsp; Prerequisites\n\n### :computer:\u0026nbsp; Systems\n\n- One or more nodes with a fresh install of [Ubuntu Server 20.04](https://ubuntu.com/download/server). These nodes can be bare metal or VMs.\n- A [Cloudflare](https://www.cloudflare.com/) account with a domain, this will be managed by Terraform.\n- Some experience in debugging problems and a positive attitude ;)\n\n### :wrench:\u0026nbsp; Tools\n\n:round_pushpin: You should install the below CLI tools on your workstation. Make sure you pull in the latest versions.\n\n#### Required\n\n| Tool                                               | Purpose                                                                                                                                 |\n|----------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------|\n| [ansible](https://www.ansible.com)                 | Preparing Ubuntu for Kubernetes and installing k3s                                                                                      |\n| [direnv](https://github.com/direnv/direnv)         | Exports env vars based on present working directory                                                                                     |\n| [flux](https://toolkit.fluxcd.io/)                 | Operator that manages your k8s cluster based on your Git repository                                                                     |\n| [age](https://github.com/FiloSottile/age)          | A simple, modern and secure encryption tool (and Go library) with small explicit keys, no config options, and UNIX-style composability. |\n| [go-task](https://github.com/go-task/task)         | A task runner / simpler Make alternative written in Go                                                                                  |\n| [ipcalc](http://jodies.de/ipcalc)                  | Used to verify settings in the configure script                                                                                         |\n| [jq](https://stedolan.github.io/jq/)               | Used to verify settings in the configure script                                                                                         |\n| [kubectl](https://kubernetes.io/docs/tasks/tools/) | Allows you to run commands against Kubernetes clusters                                                                                  |\n| [sops](https://github.com/mozilla/sops)            | Encrypts k8s secrets with Age                                                                                                           |\n| [terraform](https://www.terraform.io)              | Prepare a Cloudflare domain to be used with the cluster                                                                                 |\n\n#### Optional\n\n| Tool                                                   | Purpose                                                  |\n|--------------------------------------------------------|----------------------------------------------------------|\n| [helm](https://helm.sh/)                               | Manage Kubernetes applications                           |\n| [kustomize](https://kustomize.io/)                     | Template-free way to customize application configuration |\n| [pre-commit](https://github.com/pre-commit/pre-commit) | Runs checks pre `git commit`                             |\n| [prettier](https://github.com/prettier/prettier)       | Prettier is an opinionated code formatter.               |\n\n### :warning:\u0026nbsp; pre-commit\n\nIt is advisable to install [pre-commit](https://pre-commit.com/) and the pre-commit hooks that come with this repository.\n[sops-pre-commit](https://github.com/k8s-at-home/sops-pre-commit) will check to make sure you are not by accident committing your secrets un-encrypted.\n\nAfter pre-commit is installed on your machine run:\n\n```sh\ntask pre-commit:init\n```\n**Remember to run this on each new clone of the repository for it to have effect.**\n\nCommands are of interest, for learning purposes:\n\nThis command makes it so pre-commit runs on `git commit`, and also installs environments per the config file.\n```\npre-commit install --install-hooks\n```\nThis command checks for new versions of hooks, though it will occasionally make mistakes, so verify its results.\n```\npre-commit autoupdate\n```\n\n## :open_file_folder:\u0026nbsp; Repository structure\n\nThe Git repository contains the following directories under `cluster` and are ordered below by how Flux will apply them.\n\n- **base** directory is the entrypoint to Flux\n- **crds** directory contains custom resource definitions (CRDs) that need to exist globally in your cluster before anything else exists\n- **core** directory (depends on **crds**) are important infrastructure applications (grouped by namespace) that should never be pruned by Flux\n- **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\n\n```\ncluster\n├── apps\n│   ├── default\n│   ├── networking\n│   └── system-upgrade\n├── base\n│   └── flux-system\n├── core\n│   ├── cert-manager\n│   ├── metallb-system\n│   ├── namespaces\n│   └── system-upgrade\n└── crds\n    └── cert-manager\n```\n\n## :rocket:\u0026nbsp; Lets go!\n\nVery first step will be to create a new repository by clicking the **Use this template** button on this page.\n\nClone the repo to you local workstation and `cd` into it.\n\n:round_pushpin: **All of the below commands** are run on your **local** workstation, **not** on any of your cluster nodes.\n\n### :closed_lock_with_key:\u0026nbsp; Setting up Age\n\n:round_pushpin: Here we will create a Age Private and Public key. Using SOPS with Age allows us to encrypt and decrypt secrets.\n\n1. Create a Age Private / Public Key\n\n```sh\nage-keygen -o age.agekey\n```\n\n2. Set up the directory for the Age key and move the Age file to it\n\n```sh\nmkdir -p ~/.config/sops/age\nmv age.agekey ~/.config/sops/age/keys.txt\n```\n\n3. Export the `SOPS_AGE_KEY_FILE` variable in your `bashrc`, `zshrc` or `config.fish` and source it, e.g.\n\n```sh\nexport SOPS_AGE_KEY_FILE=~/.config/sops/age/keys.txt\nsource ~/.bashrc\n```\n\n4. Fill out the Age public key in the `.config.env` under `BOOTSTRAP_AGE_PUBLIC_KEY`, **note** the public key should start with `age`...\n\n### :cloud:\u0026nbsp; Global Cloudflare API Key\n\nIn order to use Terraform and `cert-manager` with the Cloudflare DNS challenge you will need to create a API key.\n\n1. Head over to Cloudflare and create a API key by going [here](https://dash.cloudflare.com/profile/api-tokens).\n\n2. Under the `API Keys` section, create a global API Key.\n\n3. Use the API Key in the configuration section below.\n\n### :page_facing_up:\u0026nbsp; Configuration\n\n:round_pushpin: The `.config.env` file contains necessary configuration that is needed by Ansible, Terraform and Flux.\n\n1. Copy the `.config.sample.env` to `.config.env` and start filling out all the environment variables. **All are required** and read the comments they will explain further what is required.\n\n2. Once that is done, verify the configuration is correct by running `./configure.sh --verify`\n\n3. If you do not encounter any errors run `./configure.sh` to start having the script wire up the templated files and place them where they need to be.\n\n### :zap:\u0026nbsp; Preparing Ubuntu with Ansible\n\n:round_pushpin: Here we will be running a Ansible Playbook to prepare Ubuntu for running a Kubernetes cluster.\n\n:round_pushpin: Nodes are not security hardened by default, you can do this with [dev-sec/ansible-collection-hardening](https://github.com/dev-sec/ansible-collection-hardening) or something similar.\n\n1. Ensure you are able to SSH into you nodes from your workstation with using your private ssh key. This is how Ansible is able to connect to your remote nodes.\n\n2. Install the deps by running `task ansible:deps`\n\n3. Verify Ansible can view your config by running `task ansible:list`\n\n4. Verify Ansible can ping your nodes by running `task ansible:adhoc:ping`\n\n5. Finally, run the Ubuntu Prepare playbook by running `task ansible:playbook:ubuntu-prepare`\n\n6. If everything goes as planned you should see Ansible running the Ubuntu Prepare Playbook against your nodes.\n\n### :sailboat:\u0026nbsp; Installing k3s with Ansible\n\n:round_pushpin: Here we will be running a Ansible Playbook to install [k3s](https://k3s.io/) with [this](https://galaxy.ansible.com/xanmanning/k3s) wonderful k3s Ansible galaxy role. After completion, Ansible will drop a `kubeconfig` in `./provision/kubeconfig` for use with interacting with your cluster with `kubectl`.\n\n1. Verify Ansible can view your config by running `task ansible:list`\n\n2. Verify Ansible can ping your nodes by running `task ansible:adhoc:ping`\n\n3. Run the k3s install playbook by running `task ansible:playbook:k3s-install`\n\n4. If everything goes as planned you should see Ansible running the k3s install Playbook against your nodes.\n\n5. Verify the nodes are online\n\n```sh\nkubectl --kubeconfig=./provision/kubeconfig get nodes\n# NAME           STATUS   ROLES                       AGE     VERSION\n# k8s-0          Ready    control-plane,master      4d20h   v1.21.5+k3s1\n# k8s-1          Ready    worker                    4d20h   v1.21.5+k3s1\n```\n\n### :small_blue_diamond:\u0026nbsp; GitOps with Flux\n\n:round_pushpin: Here we will be installing [flux](https://toolkit.fluxcd.io/) after some quick bootstrap steps.\n\n1. Verify Flux can be installed\n\n```sh\nflux --kubeconfig=./provision/kubeconfig check --pre\n# ► checking prerequisites\n# ✔ kubectl 1.21.5 \u003e=1.18.0-0\n# ✔ Kubernetes 1.21.5+k3s1 \u003e=1.16.0-0\n# ✔ prerequisites checks passed\n```\n\n2. Pre-create the `flux-system` namespace\n\n```sh\nkubectl --kubeconfig=./provision/kubeconfig create namespace flux-system --dry-run=client -o yaml | kubectl --kubeconfig=./provision/kubeconfig apply -f -\n```\n\n3. Add the Age key in-order for Flux to decrypt SOPS secrets\n\n```sh\ncat ~/.config/sops/age/keys.txt |\n    kubectl --kubeconfig=./provision/kubeconfig \\\n    -n flux-system create secret generic sops-age \\\n    --from-file=age.agekey=/dev/stdin\n```\n\n:round_pushpin: Variables defined in `./cluster/base/cluster-secrets.sops.yaml` and `./cluster/base/cluster-settings.yaml` will be usable anywhere in your YAML manifests under `./cluster`\n\n4. **Verify** the `./cluster/base/cluster-secrets.sops.yaml` and `./cluster/core/cert-manager/secret.sops.yaml` files are **encrypted** with SOPS\n\n5. If you verified all the secrets are encrypted, you can delete the `tmpl` directory now\n\n6.  Push you changes to git\n\n```sh\ngit add -A\ngit commit -m \"initial commit\"\ngit push\n```\n\n7. Install Flux\n\n:round_pushpin: Due to race conditions with the Flux CRDs you will have to run the below command twice. There should be no errors on this second run.\n\n```sh\nkubectl --kubeconfig=./provision/kubeconfig apply --kustomize=./cluster/base/flux-system\n# namespace/flux-system configured\n# customresourcedefinition.apiextensions.k8s.io/alerts.notification.toolkit.fluxcd.io created\n# ...\n# unable to recognize \"./cluster/base/flux-system\": no matches for kind \"Kustomization\" in version \"kustomize.toolkit.fluxcd.io/v1beta1\"\n# unable to recognize \"./cluster/base/flux-system\": no matches for kind \"GitRepository\" in version \"source.toolkit.fluxcd.io/v1beta1\"\n# unable to recognize \"./cluster/base/flux-system\": no matches for kind \"HelmRepository\" in version \"source.toolkit.fluxcd.io/v1beta1\"\n# unable to recognize \"./cluster/base/flux-system\": no matches for kind \"HelmRepository\" in version \"source.toolkit.fluxcd.io/v1beta1\"\n# unable to recognize \"./cluster/base/flux-system\": no matches for kind \"HelmRepository\" in version \"source.toolkit.fluxcd.io/v1beta1\"\n# unable to recognize \"./cluster/base/flux-system\": no matches for kind \"HelmRepository\" in version \"source.toolkit.fluxcd.io/v1beta1\"\n```\n\n8. Verify Flux components are running in the cluster\n\n```sh\nkubectl --kubeconfig=./provision/kubeconfig get pods -n flux-system\n# NAME                                       READY   STATUS    RESTARTS   AGE\n# helm-controller-5bbd94c75-89sb4            1/1     Running   0          1h\n# kustomize-controller-7b67b6b77d-nqc67      1/1     Running   0          1h\n# notification-controller-7c46575844-k4bvr   1/1     Running   0          1h\n# source-controller-7d6875bcb4-zqw9f         1/1     Running   0          1h\n```\n\n:tada: **Congratulations** you have a Kubernetes cluster managed by Flux, your Git repository is driving the state of your cluster.\n\n### :cloud:\u0026nbsp; Configure Cloudflare DNS with Terraform\n\n:round_pushpin: Review the Terraform scripts under `./terraform/cloudflare/` and make sure you understand what it's doing (no really review it). If your domain already has existing DNS records be sure to export those DNS settings before you continue. Ideally you can update the terraform script to manage DNS for all records if you so choose to.\n\n1. Pull in the Terraform deps by running `task terraform:init:cloudflare`\n\n2. Review the changes Terraform will make to your Cloudflare domain by running `task terraform:plan:cloudflare`\n\n3. Finally have Terraform execute the task by running `task terraform:apply:cloudflare`\n\nIf Terraform was ran successfully head over to your browser and you _should_ be able to access `https://hajimari.${BOOTSTRAP_CLOUDFLARE_DOMAIN}`\n\n## :mega:\u0026nbsp; Post installation\n\n### :point_right:\u0026nbsp; Troubleshooting\n\nOur [wiki](https://github.com/k8s-at-home/template-cluster-k3s/wiki) is a good place to start troubleshooting issues. If that doesn't cover your issue, start a new thread in the #support channel on our [Discord](https://discord.gg/k8s-at-home).\n\n### :robot:\u0026nbsp; Integrations\n\nOur Check out our [wiki](https://github.com/k8s-at-home/template-cluster-k3s/wiki) for more integrations!\n\n## :grey_question:\u0026nbsp; What's next\n\nThe world is your cluster, try installing another application or if you have a NAS and want storage back by that check out the helm charts for [democratic-csi](https://github.com/democratic-csi/democratic-csi), [csi-driver-nfs](https://github.com/kubernetes-csi/csi-driver-nfs) or [nfs-subdir-external-provisioner](https://github.com/kubernetes-sigs/nfs-subdir-external-provisioner).\n\nIf you plan on exposing your ingress to the world from your home. Checkout [our rough guide](https://docs.k8s-at-home.com/guides/dyndns/) to run a k8s `CronJob` to update DDNS.\n\n## :handshake:\u0026nbsp; Thanks\n\nBig shout out to all the authors and contributors to the projects that we are using in this repository.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbianchidotdev%2Fk3s-gitops-cluster","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbianchidotdev%2Fk3s-gitops-cluster","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbianchidotdev%2Fk3s-gitops-cluster/lists"}