Shoulder.dev Logo Shoulder.dev

Image Automation with FluxCD

Scenario: A developer wants to automate image deployment using FluxCD. In this example, we will use FluxCD’s image automation controllers to manage container images.

First, let’s understand the FluxCD project and its structure. FluxCD is an open-source GitOps tool for Kubernetes that can automate deployments and manage infrastructure declarations. The project consists of various components, including the FluxCD controller, image automation controllers, and various utilities.

Codebase Files and Directories:

  • action/: Contains scripts for automating various tasks, such as creating and updating resources.
  • cmd/: Contains the FluxCD command-line interface (CLI) and its subcommands.
  • docs/: Contains documentation for FluxCD.
  • manifests/: Contains Kubernetes manifests for various components.

To automate image deployment using FluxCD, we will use the following subcommands:

  1. flux create image: This command creates or updates resources dealing with image automation.
  2. flux create image update: This command generates an ImageUpdateAutomation resource, which specifies an automated update to images mentioned in YAMLs in a git repository.

Let’s walk through an example of how to use these commands to automate image deployment.

Prerequisites:

  • A Kubernetes cluster
  • FluxCD installed and configured
  • Access to a Git repository containing Kubernetes manifests

Steps:

Step 1: Create a Git repository Create a new Git repository containing Kubernetes manifests for the desired images and their corresponding ImageUpdateAutomation resources. For example, create a file named image-update.yaml in the Git repository with the following content:

apiVersion: image.toolkit.fluxcd.io/v1alpha2
kind: ImageUpdateAutomation
metadata:
name: my-image
spec:
sourceRef:
kind: GitRepository
name: my-repo
branch: main
image:
repository: my-image-repo
tag: v1.0.0
automation:
schedule:
interval: 1h

Replace my-repo with the name of your Git repository and my-image-repo with the name of the container image repository.

Step 2: Create the ImageUpdateAutomation resource Use the flux create image update command to create the ImageUpdateAutomation resource:

$ flux create image update my-image --git-repo-ref=my-repo --git-repo-path="./path/to/git/repo" --author-name=flux

Replace my-repo with the URL of your Git repository and ./path/to/git/repo with the local path to the Git repository.

Step 3: Verify the ImageUpdateAutomation resource Use the flux get image update command to verify that the ImageUpdateAutomation resource has been created:

$ flux get image update my-image

Step 4: Wait for the image update The ImageUpdateAutomation controller will automatically detect when a new image is available and create a Git commit to apply the update to the cluster. The update will be applied in the standard GitOps way to the cluster.

Tests:

To verify that the image update has been applied, you can use various Kubernetes commands, such as kubectl get pods or kubectl describe pod <pod-name>.

Additionally, you can use the flux export image update command to export the ImageUpdateAutomation resource and verify its contents.

For more information on FluxCD and image automation, please refer to the following resources:

By following these steps, you have successfully automated image deployment using FluxCD.