Skip to content

Flux v2 with AWS Managed Services

Interactive Diagram

새 탭 ↗

Flux v2 was first released in July 2020, and there are currently not very many references to consult. The official documentation describes the difference between v1 and v2 briefly as follows.

Flux v1 is a monolithic do-it-all operator; Flux v2 separates the functionalities into specialized controllers, collectively called the GitOps Toolkit.

Due to the manifest declaration format, the change of the default namespace, the GOTK components that are deployed separately, and so on, working by referring to v1 references is very difficult. It is better to build everything from scratch.

Using the CI Pipeline provided as an AWS Managed Service to build the Docker image, I worked on a CD configuration that automatically deploys to an Amazon EKS cluster using FluxCD.

  • Architecture

  • A pre-configured Amazon EKS cluster
  • A pre-configured ECR
  • A pre-configured AWS Code Pipeline (CodeCommit + CodeBuild)
  • Flux CLI installed
    • After installing flux, check whether the cluster, kubectl, etc. versions are satisfied
Terminal window
$ flux --version
flux version 0.14.2
$ flux check --pre
► checking prerequisites
✔ kubectl 1.21.1 >=1.18.0-0
✔ Kubernetes 1.19.6-eks-49a6c0 >=1.16.0-0
✔ prerequisites checks passed

Run Flux CLI bootstrap to deploy the flux v2 controllers to the flux-system namespace.

There are mostly many guides for bootstrapping using GitHub, a public repository, but to test deployment through a generic git repository, I created a repository in AWS CodeCommit. When running bootstrap, it pushes the flux v2 manifests to that repository via ssh or https and runs reconciliation.

Terminal window
flux bootstrap git \
--url=<git repository url> \
--username=<username> \
--password=<password> \
--token-auth=true \
--path=clusters/my-cluster

The installation order is roughly as follows.

  1. Generate the flux manifest and push it to the git repository
  2. Run reconcile to deploy the flux gotk components to the cluster
  3. Create the GitRepository source and the authentication secret, etc.
  4. Create the Kustomize reconciliation
  5. After pushing to the git repository, run reconcile to create the custom resources
  • Generated git repository manifests : After installation, you can confirm that the following manifests were pushed to the git repository.
  • Directory./clusters/
    • Directoryeks path=clusters/eks
      • Directoryflux-system namespace dir generated by bootstrap
        • gotk-components.yaml gotk component manifest
        • gotk-sync.yaml GitRepository, Kustomization manifest
        • kustomization.yaml kustomization to run the above yaml via kustomize (kustomize API)
  • Generated gotk components : It creates exactly what is defined in the gotk-components.yaml generated above.
Terminal window
$ kubectl get po -n flux-system
NAME READY STATUS RESTARTS AGE
helm-controller-5fc96c6bcf-drrfq 1/1 Running 0 8d
image-automation-controller-bb685fd95-f5wsp 1/1 Running 0 19d
image-reflector-controller-58c57b8f7d-fmnj2 1/1 Running 0 8d
kustomize-controller-7844d96565-5m2x9 1/1 Running 0 19d
notification-controller-7c9d5cdc4c-gkwkc 1/1 Running 0 19d
source-controller-655c987dff-pz9df 1/1 Running 0 8d

Among the manifests generated as a result of bootstrap, gotk-sync.yaml defines the GitRepository and the Kustomization.
It creates a secret from the connection information entered as parameters during installation and uses it to access the git repository.

  • gotk-sync.yaml
gotk-sync.yaml
---
apiVersion: source.toolkit.fluxcd.io/v1beta1
kind: GitRepository
metadata:
name: flux-system
namespace: flux-system
spec:
interval: 1m0s
ref:
branch: eks
secretRef:
name: flux-system
url: <git repository url>
---
apiVersion: kustomize.toolkit.fluxcd.io/v1beta1
kind: Kustomization
metadata:
name: flux-system
namespace: flux-system
spec:
interval: 3m0s
path: ./clusters/eks
prune: true
sourceRef:
kind: GitRepository
name: flux-system
validation: client
  • Generated flux source and reconciliation : This is what is defined in gotk-sync.yaml, and flux resources can also be queried through the flux CLI. They are commonly created with the name flux-system.
Terminal window
$ flux get source all
NAME READY MESSAGE REVISION SUSPENDED
gitrepository/flux-system True Fetched revision: eks/9e7e6e799fd7450c20fe6b99e606d4d2d725efb5 eks/9e7e6e799fd7450c20fe6b99e606d4d2d725efb5 False
$ flux get kustomizations
NAME READY MESSAGE REVISION SUSPENDED
flux-system True Applied revision: eks/9e7e6e799fd7450c20fe6b99e606d4d2d725efb5 eks/9e7e6e799fd7450c20fe6b99e606d4d2d725efb5 False

Under the directory specified as path, I defined K8s object manifests by type and pushed them to the git repository.
Everything was deployed successfully.

  • Directoryclusters
    • Directoryeks
      • Directoryflux-system
        • gotk-components.yaml
        • gotk-sync.yaml
        • kustomization.yaml
      • Directoryshop create a directory and the related yaml
        • ecr-job.yml
        • shop-configmap.yml app. config
        • shop-deployment.yml app. deployment
        • shop-ingress.yml app. ingress
        • shop-policy.yml
        • shop-registry.yml
        • shop-service.yml app. service
        • update-policy.yml

As a notable point, only the YAMLs defined with the K8s API have apply / patch / delete executed by Kustomization reconciliation. For example, among the files above, update-policy.yml is an ImageUpdateAutomation manifest of the fluxcd API; if you modify and push this file, reconciliation does not occur automatically. That is, even if you manage YAML files that affect FluxCD’s own configuration in the git repository, after pushing the source code you must run the reconcile command via the flux CLI for the deployment to actually run.

As shown below, manual application is possible by running the command to reconcile the fluxcd kustomization based on the git repository source.

Terminal window
$ flux reconcile kustomization flux-system --with-source
► annotating GitRepository flux-system in flux-system namespace
✔ GitRepository annotated
◎ waiting for GitRepository reconciliation
✔ GitRepository reconciliation completed
✔ fetched revision eks/9e7e6e799fd7450c20fe6b99e606d4d2d725efb5
► annotating Kustomization flux-system in flux-system namespace
✔ Kustomization annotated
◎ waiting for Kustomization reconciliation
✔ Kustomization reconciliation completed
✔ applied revision eks/9e7e6e799fd7450c20fe6b99e606d4d2d725efb5

In the currently configured environment, a separate git repository that manages the K8s manifests is created, and FluxCD detects changes in the repository and modifies the K8s objects.
This is the configuration and test details where, using AWS Managed Services, the source code is built and the docker image is built / pushed, and then the deployment image tag in the git repository is automatically modified so that FluxCD’s reconciliation can run through it.

  • CI/CD workflow
    • DEV: Modify local code and commit and push to the git repository.
    • CI: maven source code build, docker image build.
    • CI: timestamp-based image tagging and push.
    • CD: Through Flux’s image scanning, query the latest image metadata according to the image management policy. (Image reflector controller)
    • CD: Modify the image tag of the deployment manifest in the git repository to the latest. (Image automation controller)
    • CD: Deploy to the K8s cluster (Flux Git to cluster reconciliation)

See the FluxCD Guide https://fluxcd.io/docs/guides/image-update/

For this work, image-related gotk API resources are created and managed in the cluster.


| To use Amazon ECR, a process such as configuring IAM Role for Service Account and creating a cronjob to refresh the ECR credentials was needed, omitted here.


The application deployed above consists of 7 containers, so a total of 7 Repositories were created in ECR, and 7 deployments based on those images are in a created state.

Terminal window
$ kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
account 1/1 1 1 36d
apigateway 0/0 0 0 36d
bff 1/1 1 1 36d
cart 1/1 1 1 36d
order 1/1 1 1 36d
payment 1/1 1 1 36d
product 1/1 1 1 36d
  • ImageRepository manifest example : The ImageRepository is created normally only when all of these ImageRepositories are created per repository and the credentials, etc. are set correctly so that the Image reflector controller can scan the images.
apiVersion: image.toolkit.fluxcd.io/v1alpha1
kind: ImageRepository
metadata:
name: account
namespace: flux-system
spec:
secretRef:
name: ecr-credentials # ECR credential refreshed via cronjob
image: <image repository url>
interval: 1m0s
  • Generated ImageRepository resource : It can be queried with the flux CLI or kubectl, and it scans all tags created per repository according to the configured interval. Scanning can be managed by suspending it per repository.
Terminal window
$ flux get images repository
NAME READY MESSAGE LAST SCAN SUSPENDED
account True successful scan, found 9 tags 2021-06-12T08:59:27+09:00 False
apigateway True successful scan, found 5 tags 2021-06-12T08:59:28+09:00 False
bff True successful scan, found 21 tags 2021-06-12T08:59:27+09:00 False
cart True successful scan, found 6 tags 2021-06-12T08:59:27+09:00 False
order True successful scan, found 7 tags 2021-06-12T08:59:27+09:00 False
payment True successful scan, found 6 tags 2021-06-12T08:59:27+09:00 False
product True successful scan, found 6 tags 2021-06-12T08:59:27+09:00 False

For Image update automation, create a policy to fetch the image tag to apply.
Various settings like the following are possible; below, I set an alphabetical policy as follows to fetch the latest version tag created based on timestamp.

  • Image Policy List
    • SemVer: supports the use of semantic version and operators. Constraints such as range, in can be set. (ex> range: ’>=1.0.0’, range: ‘1.9.x’)
    • Alphabetical: sorts tags by selecting alphabetical and ascending/descending order, and fetches the tag that appears last.
    • Numerical: if all numbers can be sorted numerically, sorts tags by selecting ascending/descending order and fetches the tag that appears last.

For Image Policy-related policies, see the official docs https://fluxcd.io/docs/components/image/imagepolicies/

  • ImagePolicy manifest example : I created a policy for each repository.
apiVersion: image.toolkit.fluxcd.io/v1alpha1
kind: ImagePolicy
metadata:
name: account
namespace: flux-system
spec:
imageRepositoryRef:
name: account
policy:
alphabetical: # apply a policy that sorts the latest images in ascending string order.
order: asc
  • Generated ImagePolicy resource : It can be queried with the flux CLI or kubectl. You can check the latest tag fetched by the Policy as the current Latest Image.
Terminal window
$ flux get images policy
NAME READY MESSAGE LATEST IMAGE
account True Latest image tag for '<ecr-domain>/shop/account' resolved to: 20210608052721 <ecr-domain>/shop/account:20210608052721
apigateway True Latest image tag for '<ecr-domain>/shop/apigateway' resolved to: 20210608010319 <ecr-domain>/shop/apigateway:20210608010319
bff True Latest image tag for '<ecr-domain>/shop/bff' resolved to: 20210608024231 <ecr-domain>/shop/bff:20210608024231
cart True Latest image tag for '<ecr-domain>/shop/cart' resolved to: 20210608054357 <ecr-domain>/shop/cart:20210608054357
order True Latest image tag for '<ecr-domain>/shop/order' resolved to: 20210608071228 <ecr-domain>/shop/order:20210608071228
payment True Latest image tag for '<ecr-domain>/shop/payment' resolved to: 20210608071556 <ecr-domain>/shop/payment:20210608071556
product True Latest image tag for '<ecr-domain>/shop/product' resolved to: 20210608071959 <ecr-domain>/shop/product:20210608071959

After image scanning and applying the latest tag policy, create the ImageUpdateAutomation resource.
It finds the manifest that manages the image tag in the flux-system K8s manifest git repository and updates the tag name to the latest tag.

  • ImageUpdateAutomation manifest example : You only need to create one ImageUpdateAutomation. The entire image update process can be suspended.
apiVersion: image.toolkit.fluxcd.io/v1alpha2
kind: ImageUpdateAutomation
metadata:
name: flux-system
namespace: flux-system
spec:
sourceRef:
kind: GitRepository
name: flux-system # GitRepository Resource for managing k8s manifests, created while installing flux system
interval: 1m
update:
strategy: Setters
path: ./clusters/eks
git:
checkout:
ref:
branch: eks # branch to checkout
commit:
author:
name: fluxbot
email: fluxbot@example.com
messageTemplate: |
An automated update from FluxBot
[ci skip]
push:
branch: eks
  • Generated ImageUpdateAutomation resource : You can query the GitRepository for the most recent commit message, etc.
Terminal window
$ flux get images update
NAME READY MESSAGE LAST RUN SUSPENDED
flux-system True no updates made; last commit 515a681 at 2021-06-08T07:23:03Z 2021-06-12T09:22:02+09:00 False

Finally, you need to add a marker so that flux can find and update the part that indicates the Image Update policy will be used. As in the example below, next to the image name,

# {"$imagepolicy": "flux-system:<policy-name>"}

you must add a marker like the above. It is not a comment.

apiVersion: apps/v1
kind: Deployment
metadata:
name: account
namespace: shop
spec:
replicas: 1
selector:
matchLabels:
app: account
template:
metadata:
labels:
app: account
spec:
containers:
- name: account
image: <ecr-domain>:20210608052721 # {"$imagepolicy": "flux-system:account"}
...

Source commit history modified by flux-bot

Section titled “Source commit history modified by flux-bot”

It finds and changes only the image tag, and flux’s Kustomization runs reconcile to deploy the deployment with the changed spec to the K8s cluster.

diff --git a/clusters/eks/shop/shop-deployment.yml b/clusters/eks/shop/shop-deployment.yml
index 5d947df..74f8196 100644
--- a/clusters/eks/shop/shop-deployment.yml
+++ b/clusters/eks/shop/shop-deployment.yml
@@ -248,7 +248,7 @@ spec:
spec:
containers:
- name: product
- image: 170247361816.dkr.ecr.ap-northeast-2.amazonaws.com/shop/product:20210608071959 # {"$imagepolicy": "flux-system:product"}
+ image: 170247361816.dkr.ecr.ap-northeast-2.amazonaws.com/shop/product:20210608041922 # {"$imagepolicy": "flux-system:product"}
ports:
- containerPort: 8184
resources:

Flux uses kube-prometheus-stack and provides the following basic monitoring manifests through its GitHub main repository.

  • Default monitoring manifests provided

    • Prometheus Operator - manages Prometheus clusters on Kubernetes
    • Prometheus - collects metrics from the Flux controllers and the Kubernetes API
    • Grafana Dashboards - display Flux control plane resource usage and reconciliation statistics.
    • kube-state-metrics - generates metrics about the state of Kubernetes objects.
  • See the monitoring manifest of flux v2 github : I deployed prometheus / grafana with the defined kustomize, and default dashboards are included. https://github.com/fluxcd/flux2/tree/main/manifests/monitoring

Terminal window
> cd ./flux2/tree/main/manifests/monitoring
> tree grafana prometheus kustomization.yaml
grafana
├── dashboards
│ ├── cluster.json
│ └── control-plane.json
├── datasources.yaml
├── deployment.yaml
├── kustomization.yaml
├── providers.yaml
└── service.yaml
prometheus
├── account.yaml
├── deployment.yaml
├── kustomization.yaml
├── prometheus.yml
├── rbac.yaml
└── service.yaml
kustomization.yaml

FluxCD guides you to install the monitoring stack by creating flux resources. Create the fluxv2 GitHub main repository as a GitRepository resource, and create a Kustomization resource to deploy monitoring.


| I installed only prometheus and grafana separately as needed, not the entire monitoring stack.


  • Create the flux source and reconciliation : Create the GitHub main repository as the source, and create the reconciliation based on the kustomization.yaml in the source’s monitoring directory.
Terminal window
> flux create source git monitoring \
--interval=30m \
--url=https://github.com/fluxcd/flux2 \
--branch=main
> flux create kustomization monitoring-stack \
--interval=1h \
--prune=true \
--source=monitoring \
--path="./manifests/monitoring" # kustomize manifest path for deploying prometheus / grafana.
  • Generated flux resources : In addition to the source / reconciliation related to the application manifest, monitoring-related resources were created.
Terminal window
> flux get source git
NAME READY MESSAGE REVISION SUSPENDED
flux-system True Fetched revision: eks/08231b0c6993577d71af7f0ac069381684d43697 eks/08231b0c6993577d71af7f0ac069381684d43697 False
monitoring True Fetched revision: main/fd364828a120421b9a2ca169c4284d78eeb38d16 main/fd364828a120421b9a2ca169c4284d78eeb38d16 False
> flux get kustomization
NAME READY MESSAGE REVISION SUSPENDED
flux-system True Applied revision: eks/08231b0c6993577d71af7f0ac069381684d43697 eks/08231b0c6993577d71af7f0ac069381684d43697 False
monitoring-stack True Applied revision: main/fd364828a120421b9a2ca169c4284d78eeb38d16 main/fd364828a120421b9a2ca169c4284d78eeb38d16 False

The monitoring dashboards provided by FluxCD.


| The default dashboards do not include monitoring related to ImageRepository and ImageUpdateAutomation. | Also, in conjunction with monitoring, integration with FluxCD’s Alert and Notification features appears possible.


  • Flux Cluster Status : Monitoring of custom resources created by FluxCD
    • List-up and status of reconciliations / sources
    • Execution time of reconciliation and source acquisition

  • Flux Control Plane : Monitoring of K8s objects created by the FluxCD control plane
    • Query the Flux Controller status and resource usage
    • Check the status of the Controller’s Kubernetes API requests
    • Check the status of reconciliation requests (Kustomization, HelmRelease)