Automate your Deployments on Kubernetes using FluxCD

Automate your Deployments on Kubernetes using FluxCD

Publish Date: May 8
0 0

In the ever-evolving world of Kubernetes, automation and consistency are key. Enter FluxCD, a powerful GitOps tool designed to simplify and supercharge your deployment pipelines. When paired with custom Kubernetes controllers, Flux can orchestrate even the most complex deployment flows. Let’s dive into how you can use FluxCD alongside Kubernetes controllers to build a robust, automated, and Git-driven infrastructure

What Is FluxCD?

FluxCD is a CNCF-graduated open-source GitOps tool that automatically synchronizes the desired state of your Kubernetes cluster with configuration stored in Git repositories. It continuously monitors the Git repo and reconciles the live state of Kubernetes resources with what’s defined in Git.

Why Combine FluxCD with Kubernetes Controllers?

Kubernetes controllers are control loops that watch the state of your cluster and attempt to move the current state toward the desired state.

Key Components of FluxCD

  • Source Controller: Monitors Git repositories or Helm repositories.
  • Kustomize Controller: Applies manifests using Kustomize.
  • Helm Controller: Handles Helm releases.
  • Image Automation Controller: Watches image registries and updates manifests.
  • Notification Controller: Sends alerts or takes action based on events.

Prerequisite

  • Kubernetes Cluster Up and Running
  • Gitlab Repository
  • PAT Token
  • Deploy Token

Install the Flux CLI

curl -s https://fluxcd.io/install.sh | sudo bash
. <(flux completion bash)
Enter fullscreen mode Exit fullscreen mode

Image description

Flux bootstrap for GitLab

The flux bootstrap gitlab command deploys the Flux controllers on a Kubernetes cluster and configures the controllers to sync the cluster state from a Gitlab repository. Besides installing the controllers, the bootstrap command pushes the Flux manifests to the Gitlab repository and configures Flux to update itself from Git.

After running the bootstrap command, any operation on the cluster (including Flux upgrades) can be done via Git push, without the need to connect to the Kubernetes cluster.

Create Deploy Token

Group Name -> Setting -> Repository -> Default Token -> Add Token

Image description

Create flux-system namespace

Image description

Bootstrap Command to Create flux-cd controller

flux bootstrap gitlab --token-auth --owner=<group-name> --repository=flux-systemtest --branch=main --path=clusters/dev
Enter fullscreen mode Exit fullscreen mode

Image description

Verify the component in flux-system namespace
Image description

Verify the repository flux-systemtest must be created

Image description

Note

  • gotk-components.yaml file is a pre-generated manifest used by FluxCD to install its core components into your Kubernetes cluster.
  • gotk-sync.yaml is another key Flux file — it's the manifest that tells Flux where and how to sync your cluster state from Git.
  • kustomization.yaml is used to declare how to apply a specific folder of Kubernetes manifests from your Git repository. This file is read by the Flux kustomize-controller, and it tells Flux:
    • Which directory in your repo to apply
    • How often to check for changes
    • Whether to prune deleted resources
    • Whether to validate the manifests

Lets Deploy Application in Kubernetes using Flux-cd

Create a folder under flux-systemtest/clusters/dev/demo-1

Image description

Create Kubernetes related file under demo-1 dir and commit the code

  • deployment.yaml
  • service.yaml
  • namespace.yaml
  • dockerced.yaml

Image description

Note : As soon we commit the code it should create the resources

Image description

Image description

Access from the browser
Image description

Lets update the image version 2 in deployment file and see the new deployment and access from the browser

Image description

New Code Deployed once commit the code

Image description

Congratulations: Using FluxCD we have successfully automated the Kubernetes deployments.

Note: Next we will see how to automate infrastructure deployment using terraform and flux cd

Comments 0 total

    Add comment