Scenario: A developer wants to automate the deployment of Helm charts using FluxCD. In this example, we will use FluxCD’s Helm controller to manage Helm chart deployments.
First, let’s understand the project structure of FluxCD. FluxCD is an open-source GitOps tool that enables declarative updates for Kubernetes. The project consists of various components, including the Helm controller.
Codebase Files and Directories:
- manifests/bases/helm-controller/: Contains the Helm controller configuration.
- manifests/crds/: Contains the Custom Resource Definitions (CRDs) for FluxCD.
- manifests/install/: Contains the installation manifests for FluxCD.
- manifests/policies/: Contains the policy manifests for FluxCD.
To get started, we need to install FluxCD in our Kubernetes cluster. Assuming you have already installed FluxCD, let’s create a Helm release using FluxCD.
- Create a new directory for your Helm release:
mkdir my-helm-release
cd my-helm-release
- Create a
values.yaml
file to define the values for your Helm chart:
image:
repository: my-chart-repository
tag: v1.0.0
pullPolicy: IfNotPresent
Replace my-chart-repository
with the repository URL of your Helm chart.
- Create a
Chart.yaml
file to define the metadata for your Helm chart:
apiVersion: v2
name: my-helm-chart
version: 1.0.0
- Create a
kustomization.yaml
file to define the FluxCD configuration for your Helm release:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- charts/
- my-helm-chart/
configMapGenerator:
- name: values
literals:
appVersion: "1.0.0"
Create a new directory named charts
inside your my-helm-release
directory and copy your Helm chart into it.
- Create a
flux.yaml
file to define the FluxCD configuration for your Helm release:
apiVersion: flux.tools/v1beta2
kind: HelmRelease
metadata:
name: my-helm-release
namespace: my-namespace
spec:
chart:
gitRepository:
url: https://github.com/my-user/my-helm-chart.git
branch: main
directory: charts/my-helm-chart
values:
values.yaml
sync:
interval: 30s
Replace my-namespace
with the namespace where you want to deploy your Helm chart.
- Apply the FluxCD configuration using
kubectl
:
kubectl apply -f flux.yaml
FluxCD will now automatically deploy and manage your Helm chart based on the configuration in the flux.yaml
file.
Tests to verify the answer:
- Verify that your Helm chart is deployed by checking the status of the corresponding Kubernetes deployment.
- Make changes to the
values.yaml
file and verify that FluxCD updates the Helm chart deployment accordingly. - Verify that the Helm chart is synced with the desired interval by checking the FluxCD logs.