This post has been republished via RSS; it originally appeared at: Azure Global articles.
In this article you will learn how to create a fully automated GitOps workflow where your apps will be automatically deployed when you update their definitions in a Git repository. We will create a cluster in Azure Kubernetes Service (AKS) and configure Flux CD, including secret management with Mozilla SOPS and Azure Key Vault. We will push our app manifests and encrypted secrets to the repo and Flux will decrypt them using a cryptographic key in Key Vault and apply our changes. The figure below depicts the overall continuous deployment (CD) workflow.
Create a Kubernetes cluster
First, let's export a few environment variables that will be used throughout the article to create and reference Azure resources. Replace the values with your own names and preferences.
Create a resource group to contain all the resource that we will create as part of this guide.
Create an AKS cluster. The command below will create a cluster with the default configuration options, i.e., one node pool with three nodes, a system-assigned identity, kubenet network plugin, no network policy, etc. Feel free to customize the cluster to your needs.
Once created, obtain the credentials to access the cluster.
And check that you are able to interact with it.
The output should look something like this.
Set up Flux CD
Install Flux CD locally.
Validate Flux pre-requisites.
The output should be something like this.
We are going to connect Flux CD to a GitHub repository, however, you can configure Flux with any Git repository. Check the bootstrap section of the Flux CD installation guide to learn how to configure Flux with other Git services like Azure Repos.
Create a GitHub repository and clone it locally.
Create a GitHub Personal Access Token with full access to the repo scope, and export the token, your GitHub username, and the AKS cluster name.
Export the token, your GitHub username, and the AKS cluster name.
Bootstrap the Flux system components.
You should see how Flux starts to install and sync its components and eventually get a confirmation that all components are healthy.
Additionally, you can run the following command to validate the Flux installation.
Output.
Pull the latest updates published by Flux from the repository.
Set up the application
Now that the Flux system is up and running, let's configure the GitOps for our application called "demoapp".
Since we are using the same repository for both the Flux system and our app, we will create a Kustomization referencing the same "flux-system" source, however, you could use different repositories for Flux and your app. Check the Flux CD getting started guide for more information about creating additional repository sources.
We are telling Flux that our app manifests are defined in `./manifests`, and Flux is going to keep that directory in sync with the cluster as soon as we make any changes to the app manifests.
Let's create some manifests to give Flux something to apply.
Create the "manifests" directory.
Create the namespace definition.
The deployment.
And the service.
After a few moments you should see how your Kustomization has been applied the latest commit.
And our app gets automatically deployed.
Secret management
At this point Flux will apply any manifest we commit to the repo under the "manifests" folder. That will be fine for every Kubernetes resource except for Secrets, which we do not want to disclose and source control their plain values.
A solution for this would be using the Azure Key Vault Provider for Secrets Store CSI Driver, which allows us to define our secrets in Key Vault and automatically make them available as Kubernetes secrets. However, this approach breaks our the GitOps workflow where the Git repository is the single source of truth for our application desired state.
A popular GitOps approach for secret management is using Bitnami's Sealed Secrets. Sealed Secrets require an additional controller and a new "SealedSecret" CRD that is safe to store in a Git repository. After Flux applies the "SealedSecret" object, their controller decrypts the sealed secret and applies the plain secrets.
Another popular approach for managing secrets in Flux is using Mozilla's SOPS. Unlike Sealed Secrets, SOPS does not require us to deploy any additional controller because Flux's kustomize-controller can perform the decryption of the secrets. Moreover, SOPS has integration with Azure Key Vault to store the cryptographic used to encrypt and decrypt secrets. Therefore, making it an ideal option for managing secrets in Azure.
To configure secret management with Mozilla SOPS and Azure Key Vault we have to create a few resources first.
Install AAD Pod Identity
AAD Pod Identity enables Kubernetes applications to access Azure resources securely with Azure Active Directory. It will allow us to bind a Managed Identity to Flux's kustomize-controller.
Before installing AAD Pod Identity, we need to give the AKS Kubelet identity permissions to attach identities to the AKS nodes in the AKS-managed resource group. Let's obtain the relevant IDs.
And create the role assignment granting "Virtual Machine Contributor" permissions.
We are going to install AAD Pod Identity in a GitOps way, because Flux is also capable of managing Helm charts with the helm-controller, which is installed by default. Therefore, instead of installing the Helm chart directly from our computer as the AAD Pod Identity documentation indicates, we will create a "HelmRepository" and a "HelmRelease" resource that Flux will apply and keep in sync for us. This will allow us to manage and upgrade AAD Pod Identity from the Git repository.
Commit and push the changes to the repo and AAD Pod Identity will be deployed in a few seconds.
Output.
And let's check the pods.
Output
If your Helm chart is not applied, check the status and logs of the kustomize-controller and helm-controller.
Create a Managed Identity
The Managed Identity will be used by the Flux kustomize-controller to obtain the cryptographic key from Key Vault and decrypt secrets.
Obtain the client Id, object Id, and the resource Id of the identity.
Create a Key Vault
Now it's time to create a Key Vault instance, the cryptographic key and give permissions to our identity.
Create an environment variable with the desired name for your Key Vault resource.
Create a Key Vault instance.
Create the cryptographic key.
Add an access policy for the identity.
Obtain the key ID and save it for later.
The key ID will have a the following form.
Configure in-cluster secrets decryption
Now let's create the Azure identity and binding to attach the Managed Identity we created previously to the kustomize-controller.
The identity will be bound to the pods that have the "sops-akv-decryptor" label, therefore, we need to patch the kustomize-controller to set such label and allow AAD Pod Identity to bind the identity.
Patch the kustomize-controller Pod template so that the label matches the `AzureIdentity` name. Additionally, the SOPS specific environment variable "AZURE_AUTH_METHOD=msi" to activate the proper auth method within kustomize-controller.
Create a kustomization.
And a file to patch the Flux system kustomize controller deployment.
We also have to tell the kustomize-controller that our app Kustomization needs to use SOPS as the decryption provider and therefore be able to decrypt certain fields of our manifests.
Update the kustomization YAML file in "clusters/$CLUSTER_NAME/demoapp-kustomization.yaml" and add the `spec.decryption` block as shown below.
Apply the changes and check that the identity binding and patching have been applied successfully.
At this point, the kustomize-controller should be able to decrypt files encrypted with SOPS via our Key Vault key.
Encrypt secrets
Install SOPS locally following the instructions from their repository. Then create a ".sops.yaml" file to contain the SOPS configuration. We will tell SOPS to encrypt only the "data" and "stringData" blocks of our YAML files. Therefore, only the values of our Kubernetes secrets will be encrypted. If that rule is omitted, SOPS will encrypt all keys in our YAML files, which is not necessary.
Also, we need to tell SOPS to use our Azure Key Vault key to encrypt the files. Before running SOPS make sure you are logged in with a user that has encrypt access to the Key Vault key being used, otherwise the encryption will fail.
Create a temporary secret file which we won't commit to the repository.
SOPS will use the logged in user in AZ CLI, therefore, make sure the logged in user has "encrypt" and "decrypt" Key Permissions as shown below.
You can also add those permissions with the following commands.
And encrypt the secret.
The encrypted secret at "./manifests/secret.enc.yaml" will look something like this.
We can now delete the plain secret and push the encrypted secret to the repo.
Flux will read the encrypted secret, decrypt it using the identity and the key, and apply it.
Output.
From this point on, Flux will keep our app up to date with the latest Kubernetes definitions in your repository, including secrets. You can find the code generated by this guide in the github.com/adrianmo/aks-flux GitHub repository.
Next steps
Automatic image updates
Flux has a couple of optional components called Image Automation Controllers, which can monitor a container registry and detect when new image tags are uploaded. Then, they can automatically update your deployments to roll out an update to the new container image. Check the official documentation to know more.
Keep Flux up to date
Flux's system components are defined in the "./clusters/${CLUSTER_NAME}/flux-system/gotk-components.yaml" manifest file, therefore, if we regenerate that file with a newer version of Flux (i.e. running "flux install --export ./clusters/${CLUSTER_NAME}/flux-system/gotk-components.yaml"), the manifests will be updated with the new container images and configurations and the Flux system running in the cluster will apply those changes and update itself.
The above procedure can be turned into a scheduled CI workflow and create a Pull Request when changes are made to the manifests, giving us the possibility to review and approve the changes before they are applied. An example implementation of this workflow can be found here.
Notifications and monitoring
Since there are many operations that happen automatically, it can be become quite challenging to understand what is going on in our cluster. Therefore, it is crucial to know the state of our system at any time, but especially when things go south.
With Flux we can configure Notifications to forward events to collaboration and messaging apps like Microsoft Teams or Slack, and also to Git repositories in the form of commit statuses.
Flux also comes with a Monitoring stack composed of Prometheus, for metric collection, and Grafana dashboards, for displaying the control plane resource usage and reconciliation stats.