Automating K3s Node Provisioning with GitLab CI/CD
Welcome to another edition of Caffeinated Coder! Today we're going to be taking a quick look at my set up to maintain the state of my k3s cluster in my home lab. This guide unlocks the simplicity of setting up and scaling your K3s nodes in a home lab environment using GitLab CI/CD. Imagine a YAML-driven pipeline that seamlessly deploys and configures servers into your K3s cluster, all powered by the magic of K3sup. Let's dive into the steps to manage your Kubernetes infrastructure effortlessly, with just a few lines of code.
Prerequisites
Before starting, make sure you have:
- A GitLab account with a repository for your project.
- Access to servers intended to become K3s nodes.
- Basic knowledge of GitLab CI/CD and K3s.
The Pipeline
The .gitlab-ci.yml file defines the CI/CD pipeline and that's really the only component to this project. Here's the setup using GitLab's parallel matrix builds and K3sup:
1stages:
2 - deploy
3
4variables:
5 SERVERS:
6 - "192.168.0.1"
7 - "192.168.0.2"
8 - "192.168.0.3"
9 # Add more server IPs as needed
10
11.deploy_template: &deploy_definition
12 stage: deploy
13 image: alpine:latest
14 script:
15 - apk add --no-cache curl
16 - curl -sLS https://get.k3sup.dev | sh
17 - ./k3sup install --ip $SERVER_IP # Provision the server with K3s
18 - ./k3sup join --ip $SERVER_IP --user vagrant --skip-install # Generate kubeconfig for the server
19
20deploy_servers:
21 stage: deploy
22 parallel: matrix
23 matrix:
24 variables:
25 SERVER_IP: $SERVERS
26 <<: *deploy_definition
27
28deploy_artifact:
29 stage: deploy
30 dependencies:
31 - deploy_servers
32 artifacts:
33 paths:
34 - kubeconfig_$CI_JOB_NAME.yaml
35 script:
36 - kubectl config view --raw > kubeconfig_$CI_JOB_NAME.yaml
Summary
- At the very top of this pipeline, we're identifying all servers by their IP address in the local network
- Next we're defining a GitLab CI/CD template that actually does the heavy lifting of provisioning each node with k3sup
- Then we're calling the above template for each server IP in parallel to provision it with k3sup
- Lastly, we're creating a build artifact with the kube config generated from the above builds so that we can pull that from the CI/CD pipeline to wherever we need to talk to K3s
Do note, when using this pipeline, make sure you configure your servers with static IPs in your router
Conclusion
This setup simplifies provisioning K3s nodes in your home lab using GitLab CI/CD and K3sup, streamlining the management and scaling of your Kubernetes infrastructure. Embrace the power of automation to effortlessly orchestrate your Kubernetes environment.
Remember to adjust the configurations according to your environment and security needs, ensuring proper network setup and access controls.
Empower your Kubernetes journey by harnessing the capabilities of GitLab CI/CD for seamless infrastructure management!
This comprehensive guide showcases the seamless integration of GitLab CI/CD with K3sup for automating the provisioning and configuration of K3s nodes in your home lab environment.