Deploying Azure AKS GitOps Flux extension with Terraform

In a previous blog post, I provided a comprehensive guide on deploying Azure AKS, Application Gateway, and GitOps extension with AKS. This involved an in-depth overview of flux/kustomization, among other components. In this post, I’ll specifically focus on deploying the Azure AKS GitOps extension and configuring flux using this extension.

The Terraform

Lets jump right in by going through the Terraform required

Flux AKS extension

To begin, we need an AKS extension as part of the GitOps/Flux installation. Below is the Terraform code snippet:

resource "azurerm_kubernetes_cluster_extension" "flux" {
  name           = "flux"
  cluster_id     = azurerm_kubernetes_cluster.k8s.id
  extension_type = "microsoft.flux"
}
  • cluster_id will be the Azure resource ID of your AKS cluster to where you want the extension installed onto
  • extension_type will be microsoft.flux as highlighted in the above Terraform snippet

Upon successful deployment, the extension will be visible in the Azure Portal.

Awesome, now we have the extension installed – time to add the flux configuration.

Flux AKS configuration

Now, let’s look at the Terraform configuration for the Flux AKS configuration (Quite a few arguments/blocks are available – do check the full list available here):

resource "azurerm_kubernetes_flux_configuration" "k8s_flux" {
  name       = "flux-system"
  cluster_id = azurerm_kubernetes_cluster.k8s.id
  namespace  = "flux-system"

  git_repository {
    url             = "https://github.com/thomast1906/azure-aks-flux2config-demo"
    reference_type  = "branch"
    reference_value = "main"
  }

  kustomizations {
    name                      = "kustomization-2"
    path                      = "./clusters/production/00"
    sync_interval_in_seconds  = 120
    retry_interval_in_seconds = 120

  }

  scope = "cluster"

  depends_on = [
    azurerm_kubernetes_cluster_extension.flux
  ]
}
  name       = "flux-system"
  cluster_id = azurerm_kubernetes_cluster.k8s.id
  namespace  = "flux-system"
  • name is the name for the flux configuration that will appear in the portal
  • cluster_id as mentioned above already
  • namespace is the namespace in which the configuration will be stored

Next the git_repository setup

  git_repository {
    url             = "https://github.com/thomast1906/azure-aks-flux2config-demo"
    reference_type  = "branch"
    reference_value = "main"
  }
  • url is the URL to where the flux config is stored
  • reference_type – Specify the source reference type for the GitRepository object
  • reference_value – in this case is the branch name to use

The kustomization is how flux will deploy your application(s)

  kustomizations {
    name                      = "kustomization-2"
    path                      = "./clusters/production/00"
    sync_interval_in_seconds  = 120
    retry_interval_in_seconds = 120

  }
  • name of the kustomization that will be referred to in the Azure Portal and AKS Cluster
  • path within the GitHub repository to where the kustomization is that will be deployed onto AKS cluster
  • sync_interval_in_seconds – The frequency at which the kustomization on the cluster should be re-reconciled.
  • retry_interval_in_seconds – The frequency at which to re-reconcile the kustomization on the cluster in case of reconciliation failure.

When deployed successfully, we can see in the Azure Portal

Finishing up

Deploying the Azure AKS GitOps extension and configuring flux using Terraform provides a streamlined and efficient way to manage Kubernetes applications. By highlighting key components such as the GitRepository setup, kustomizations, and synchronization intervals, users gain insights into the intricacies of the deployment process. The Azure Portal serves as a visual representation of the successfully deployed extension and configured flux settings, offering a user-friendly interface for monitoring and managing the GitOps workflow.

Leave a Reply