Bootstrap cluster with Flux: the GitOps way
Title: Bootstrap cluster with Flux: the GitOps way
Author: Mitch Murphy
Date: 2023-12-27
- Bootstrap cluster with Flux: the GitOps way
- Introduction
- Prerequisites
- Install Flux
- Components
- Conclusion
Introduction
What is GitOps? GitOps is a way to do Continuous Delivery, it works by using Git as a single source of truth for declarative infrastructure and applications. With GitOps, we can make changes to our infrastructure and applications by making pull requests against our Git repository. These changes are then automatically applied to our cluster by a GitOps operator. This allows us to manage our infrastructure and applications in a declarative way, using the same Git workflow that we use for application code.
Flux CD is an open-source continuous delivery (CD) tool designed for Kubernetes applications. It automates the deployment and lifecycle management of applications running on Kubernetes clusters. Flux CD focuses on ensuring that the desired state of your Kubernetes resources matches the actual state, thus enabling GitOps practices.
Key features and concepts of Flux CD include:
- GitOps: Flux CD follows the GitOps operational model, where the desired state of your Kubernetes cluster is declared in a Git repository. Changes to the cluster are made by updating the Git repository, and Flux CD automatically applies those changes to the cluster, ensuring that the actual state converges with the declared state.
- Automation: Flux CD automates the deployment of applications by synchronizing the Kubernetes manifests stored in a Git repository with the resources running in the cluster. This allows for a declarative approach to managing infrastructure and applications.
- Continuous Delivery: Flux CD is commonly used for implementing continuous delivery pipelines on Kubernetes. When changes are pushed to the Git repository, Flux CD automatically detects them and triggers the deployment process.
- Helm Support: Flux CD has built-in support for Helm charts, a package manager for Kubernetes applications. It can automatically sync and apply changes to Helm releases based on the charts stored in the Git repository.
- Multi-Environment Support: Flux CD supports multi-environment setups, allowing you to manage and deploy applications across different clusters or namespaces.
- Notifications: Flux CD provides notifications and alerts to keep users informed about the state of deployments and any issues that may arise.
Flux CD is often used in conjunction with other tools and practices that adhere to the GitOps methodology, such as version control systems (like Git), CI/CD pipelines, and Kubernetes operators.
Prerequisites
Before you begin this guide, you’ll need the following:
- A Kubernetes cluster running RKE2
- Flux CLI
- Flux installed on your cluster
- A GitHub repository to store your cluster configuration
Install Flux
We will be using Flux CD to bootstrap our cluster. If you haven’t already, you will need to install Flux CD on your cluster. You can find a guide on how to do this here.
Bootstrap cluster with Flux
Once you have Flux CD installed, you are ready to boostrap your cluster. We will be using the Flux CLI to bootstrap our cluster. The Flux CLI is a command-line tool that allows you to interact with Flux CD. It can be used to create and manage Flux CD resources, such as Git repositories, Helm releases, and Kubernetes resources. We will be following this guide.
flux bootstrap github \
--owner=mkm29 \
--repository=kubula \
—-path=clusters/mgmt \
--token-auth \
--personal \
--branch=main
The bootstrap github command creates the GitHub repository if it doesn’t exists and commits the Flux manifests to the specified branch. Then it configures the target cluster to synchronize with that repository.
Components
Flux CD consists of several components that work together to provide the desired functionality. These components include:
- Flux: The core component of Flux CD, Flux is a Kubernetes operator that runs on the cluster and manages the deployment of applications. It monitors the Git repository for changes and automatically applies them to the cluster.
- Helm Operator: The Helm Operator is a Kubernetes operator that manages Helm releases. It can automatically sync and apply changes to Helm releases based on the charts stored in the Git repository.
- Memcached: Memcached is a distributed memory object caching system that is used by Flux CD to store and retrieve data.
- Notification Controller: The Notification Controller is a Kubernetes controller that sends notifications and alerts to users about the state of deployments and any issues that may arise.
- Image Automation Controller: The Image Automation Controller is a Kubernetes controller that automates the deployment of container images. It can automatically pull new images from a registry and update the corresponding Kubernetes resources.
- Kustomize Controller: The Kustomize Controller is a Kubernetes controller that manages Kustomize resources. It can automatically sync and apply changes to Kustomize resources based on the manifests stored in the Git repository.
- Source Controller: The Source Controller is a Kubernetes controller that manages Git repositories. It can automatically sync and apply changes to Git repositories based on the manifests stored in the Git repository.
We are going to use Flux CD to bootstrap a minimal set of Kubernetes components, including a Prometheus monitoring stack, persistent storage via Longhorn and Argo CD. The structure can be found here
Conclusion
In this guide, we have learned how to bootstrap a Kubernetes cluster with Flux CD using GitOps. We have also learned about the key features and concepts of Flux CD, as well as its components. We have also learned how to use the Flux CLI to bootstrap our cluster. Finally, we have learned how to use Flux CD to bootstrap a minimal set of Kubernetes components, including a Prometheus monitoring stack, persistent storage via Longhorn and Argo CD.