Application Deployment using Helm Charts and Flux
In this guide, we will demonstrate how to deploy an application using Helm charts and configure it for different environments using Flux. We will use the flux2-kustomize-helm-example project as an example.
Prerequisites
Before we begin, make sure you have the following tools installed:
Design Philosophy
The project follows the GitOps philosophy, which means that all the application configuration and deployment artifacts are stored in a Git repository, and changes to the configuration are made by modifying the Git repository. Flux monitors the Git repository and automatically applies the changes to the cluster.
Programming Languages
The project uses the following programming languages:
- Helm charts (written in YAML)
- Kustomize (written in YAML)
Key Technologies and Dependencies
The project uses the following technologies and dependencies:
- Flux: a GitOps tool for Kubernetes
- Helm: a package manager for Kubernetes
- Kustomize: a tool for customizing Kubernetes objects
- GitHub: a Git repository hosting service
- Cert-Manager: a tool for managing TLS certificates
- Ingress-NGINX: a Kubernetes Ingress controller
- Kubernetes: a container orchestration platform
- HelmRepository: a Helm repository for hosting Helm charts
- HelmRelease: a Helm release for deploying Helm charts
- Kustomize API: an API for customizing Kubernetes objects
Deploying the Application
To deploy the application, follow these steps:
- Install Flux in the
flux-system
namespace:
$ flux install --components=source-controller,kustomize-controller,helm-controller,notification-controller
- Configure the deploy key:
$ flux create secret git deploy-key --url=https://github.com/fluxcd/flux2-kustomize-helm-example --key=<path-to-deploy-key>
- Create a
Kustomization
object to deploy the application:
apiVersion: kustomize.toolkit.fluxcd.io/v1
kind: Kustomization
metadata:
name: webapp
namespace: apps
spec:
interval: 60m0s
wait: true
timeout: 3m0s
retryInterval: 2m0s
prune: true
force: false
targetNamespace: apps
sourceRef:
kind: GitRepository
name: webapp
namespace: apps
path: ./deploy/production
- Create a
HelmRelease
object to deploy the application:
apiVersion: helm.toolkit.fluxcd.io/v2beta1
kind: HelmRelease
metadata:
name: webapp
namespace: apps
spec:
interval: 60m0s
chart:
spec:
chart: webapp
version: <chart-version>
sourceRef:
kind: GitRepository
name: webapp
namespace: apps
targetNamespace: apps
- Create a
GitRepository
object to reference the Git repository:
apiVersion: source.toolkit.fluxcd.io/v1beta2
kind: GitRepository
metadata:
name: webapp
namespace: apps
spec:
url: https://github.com/fluxcd/flux2-kustomize-helm-example
ref:
branch: main
- Apply the configuration to the cluster:
$ kubectl apply -f <path-to-configuration>
Configuring the Application for Different Environments
To configure the application for different environments, we can use Kustomize overlays. An overlay is a directory that contains Kustomize patches that modify the base configuration.
For example, to create an overlay for the production
environment, we can create a directory called overlays/production
and add the following Kustomize patch:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
metadata:
name: webapp
namespace: apps
spec:
interval: 60m0s
wait: true
timeout: 3m0s
retryInterval: 2m0s
prune: true
force: false
targetNamespace: apps
patchesStrategicMerge:
- patch.yaml
The patch.yaml
file contains the Kustomize patches that modify the base configuration. For example, to set the replicaCount
to 2
and change the service
type to NodePort
, we can add the following patch:
replicaCount: 2
service:
type: NodePort
To enable the overlay, we need to add a Kustomization
object to the flux-system
namespace:
apiVersion: kustomize.toolkit.fluxcd.io/v1beta1
kind: Kustomization
metadata:
name: core-webapp
namespace: flux-system
spec:
interval: 5m
sourceRef:
kind: GitRepository
name: webapp
namespace: apps
path: ./overlays/production
Possible Options
The following options are possible when deploying an application using Helm charts and Flux:
- Using Helm repositories to host Helm charts
- Using Helm values to customize Helm charts
- Using Kustomize overlays to customize Kubernetes objects
- Using Kustomize patches to modify Kubernetes objects
- Using GitOps to automate the deployment of applications
- Using Flux to monitor Git repositories and apply changes to the cluster
Examples
The following examples demonstrate how to use the different options:
- Using Helm repositories:
apiVersion: helm.toolkit.fluxcd.io/v2beta1
kind: HelmRelease
metadata:
name: webapp
namespace: apps
spec:
interval: 60m0s
chart:
spec:
chart: webapp
version: <chart-version>
sourceRef:
kind: HelmRepository
name: webapp
namespace: apps
targetNamespace: apps
- Using Helm values:
apiVersion: helm.toolkit.fluxcd.io/v2beta1
kind: HelmRelease
metadata:
name: webapp
namespace: apps
spec:
interval: 60m0s
chart:
spec:
chart: webapp
version: <chart-version>
sourceRef:
kind: GitRepository
name: webapp
namespace: apps
values:
- replicaCount: 2
- service:
type: NodePort
targetNamespace: apps
- Using Kustomize overlays:
apiVersion: kustomize.toolkit.fluxcd.io/v1beta1
kind: Kustomization
metadata:
name: webapp
namespace: apps
spec:
interval: 60m0s
wait: true
timeout: 3m0s
retryInterval: 2m0s
prune: true
force: false
targetNamespace: apps
sourceRef:
kind: GitRepository
name: webapp
namespace: apps
path: ./deploy/production
- Using Kustomize patches:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
metadata:
name: webapp
namespace: apps
spec:
interval: 60m0s
wait: true
timeout: 3m0s
retryInterval: 2m0s
prune: true
force: false
targetNamespace: apps