Introduction#
GitHub this note how to getting started with a CI/CD pipeline for EKS using Flux.
- Setup Flux on EKS
- Monitor ECR image
- Flask polly app
Install Flux#
Let install flux
curl -s https://fluxcd.io/install.sh | sudo bash
Check the environment
flux check --pre
Setup GitHub connection with Flux
export GITHUB_TOKEN=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export GITHUB_USER=entest-hai
Boostrap Flux into the EKS cluster
flux bootstrap github \--components-extra=image-reflector-controller,image-automation-controller \--owner=$GITHUB_USER \--repository=eks-flux-demo \--branch=main \--path=clusters/EksClusterLevel1 \--read-write-key \--personal
Should change the repository name
flux-image-updates => eks-flux-demo
Add an yaml file such as an flask-app.yaml, please specify namespace
apiVersion: apps/v1kind: Deploymentmetadata:name: flask-app-deploymentnamespace: defaultspec:replicas: 2selector:matchLabels:app: flask-apptemplate:metadata:labels:app: flask-appspec:containers:- image: 392194582387.dkr.ecr.ap-southeast-1.amazonaws.com/flask-app:latestname: flask-appports:- containerPort: 8080resources:limits:cpu: 50mrequests:cpu: 50m
Then wait a minute or run
flux reconcile kustomization flux-system --with-source
Check the update by flux
watch flux get kustomizations
Scan Image#
Basically, flux will scan ECR image for tags and update the flask-app.yaml with the new tags. Then flux will deploy the updated flask-app.yaml
- Register ecr image
- Create ecr credentials
- Create image update policy
Check the image tag which is using now by the flask-app service
kubectl get deployment/flask-app-deployment -oyaml | grep 'image:'
First, we need to create ImageRepository to tell Flux which container registry to scan for
flux create image repository flask-app \--image=$ACCOUNT_ID.dkr.ecr.ap-southeast-1.amazonaws.com/flask-app \--interval=1m \--export > ./clusters/EksClusterLevel1/flask-app-registry.yaml
and the generated yaml
---apiVersion: image.toolkit.fluxcd.io/v1beta2kind: ImageRepositorymetadata:name: flask-appnamespace: flux-systemspec:image: $ACCOUNT_ID.dkr.ecr.ap-southeast-1.amazonaws.com/flask-appinterval: 1m0s
Second, we need grant permissiosn so Flux can scan ecr images by updaing the flux-system/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1kind: Kustomizationresources:- gotk-components.yaml- gotk-sync.yamlpatches:- patch: |-- op: addpath: /spec/template/spec/containers/0/args/-value: --aws-autologin-for-ecrtarget:version: v1group: appskind: Deploymentname: image-reflector-controllernamespace: flux-system
Third, create an ImagePolicy to tell Flux which semver range to use when filtering tags
flux create image policy flask-app \--image-ref=flask-app \--select-semver=5.0.x \--export > ./clusters/EksClusterLevel1/flask-app-policy.yaml
and the generated yaml
apiVersion: image.toolkit.fluxcd.io/v1beta2kind: ImagePolicymetadata:name: flask-appnamespace: flux-systemspec:imageRepositoryRef:name: flask-apppolicy:semver:range: 5.0.x
Finally, we need to create ImageUpdateAutomation
flux create image update flux-system \--interval=30m \--git-repo-ref=flux-system \--git-repo-path="./clusters/EksClusterLevel1" \--checkout-branch=main \--push-branch=main \--author-name=fluxcdbot \--author-email=fluxcdbot@users.noreply.github.com \--commit-template="{{range .Updated.Images}}{{println .}}{{end}}" \--export > ./clusters/EksClusterLevel1/flux-system-automation.yaml
and the generated yaml
apiVersion: image.toolkit.fluxcd.io/v1beta1kind: ImageUpdateAutomationmetadata:name: flux-systemnamespace: flux-systemspec:git:checkout:ref:branch: maincommit:author:email: hai@entest.ioname: fluxcdbotmessageTemplate: "{{range .Updated.Images}}{{println .}}{{end}}"push:branch: maininterval: 1m0ssourceRef:kind: GitRepositoryname: flux-systemupdate:path: ./clusters/EksClusterLevel1strategy: Setters
Troubleshooting#
Check image tag of a deployment
kubectl get deployment/flask-app-deployment -oyaml | grep 'image:'
Get image repository
flux get image repository flask-app
Or describe
kubectl -n flux-system describe imagerepositories podinfo
Get all image of a namespace
flux get images all --all-namespaces