1) Overview and Install ArgoCD Image Updater on K8s
Để hoàn thành flow Gitops chúng ta cần là ghi CI đã build image mới và push image lên Docker Hub thì Argocd sẽ tự động cập nhật image mới này.
và chúng ta có 1 project là ArgoCD Image Updater
Đây là link có manifest của ArgoCD Image Updater
https://raw.githubusercontent.com/argoproj-labs/argocd-image-updater/stable/manifests/install.yaml
Bạn nên cài nó chung với namespace: argocd.

2) Adjusting the configuration of ArgoCD Image Updater
Đầu tiền thì ArgoCD Image Updater cần access được vào Docker Hub để monitor việc thay đổi image hay có image mới được push lên.
2.1) Access ECR
Để tương tác và lấy tocken của ECR thì bạn đọc bài này:
2.1.2) Using Access and Secret Key
Mình nghĩ đây là 1 cách dễ
bạn sẽ đưa cho ArgoCD Image Updater access key và secret key, Tiếp đến là nó sẽ gen ra token và tự access vào ECR.
Đâu tiên là file secret sẽ được mount thành environment của file.
secret-aws-credential.yaml >>>>>>>>>> >>>>>>>>>>>> apiVersion: v1 data: AWS_ACCESS_KEY_ID: XXXXXXXXXXXXXXX= AWS_DEFAULT_REGION: dXMtZWFzdC0x AWS_SECRET_ACCESS_KEY: Y3RSNEpTUDFyUVI3SnVUXXXXXXXXXXXXXXXX== kind: Secret metadata: name: aws-credential namespace: argocd type: Opaque
Tiếp đến là mình merge “environment from secret” vào trong deployment của helm chart. mà không cần sửa helm chart.
nếu bạn chưa biết kĩ thuật này thì tham khảo bài sau:
https://nimtechnology.com/2022/05/22/kustomize-2/
env-from-secret.yaml >>>>>>>>>> >>>>>> apiVersion: apps/v1 kind: Deployment metadata: name: argocd-image-updater spec: template: spec: containers: - name: argocd-image-updater envFrom: - secretRef: name: aws-credential
Tiếp đến là file Kustomization
kustomization.yaml >>>>>>> >>>>>>>>>>>> apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization resources: - secret-aws-credential.yaml patchesStrategicMerge: - env-from-secret.yaml helmGlobals: chartHome: argocd-image-updater_0.8.0/ helmCharts: - name: argocd-image-updater includeCRDs: false releaseName: argocd-image-updater version: 0.8.0 #look at repo: https://argoproj.github.io/argo-helm valuesFile: values_helm.yaml namespace: argocd
và có file values của helm chart.
values_helm.yaml >>>>> >>>>>>>>> config: registries: - name: ECR api_url: https://250887682577.dkr.ecr.us-east-1.amazonaws.com prefix: 250887682577.dkr.ecr.us-east-1.amazonaws.com ping: yes insecure: no credentials: ext:/scripts/ecr-login.sh credsexpire: 6h authScripts: enabled: true scripts: ecr-login.sh: | #!/bin/sh aws ecr --region us-east-1 get-authorization-token --output text --query 'authorizationData[].authorizationToken' | base64 -d ###https://github.com/argoproj-labs/argocd-image-updater/issues/422
và mình đã tham khảo ý kiến ở đây:
https://github.com/argoproj-labs/argocd-image-updater/issues/422
Repo links:
mà mình thực hiện deploy bằng argocd:


Bạn sẽ thấy là arogcd-image-updater run script OK và check được image latest trên ECR.

this configuration “argocd-image-updater.argoproj.io/image-list:” –> You can specify one or more image(s) for each application that should be considered for updates
Bạn có thể tham khảo các link bên dưới:
– https://argocd-image-updater.readthedocs.io/en/stable/configuration/applications/
– https://argocd-image-updater.readthedocs.io/en/stable/configuration/images/
3) argocd-image-updater need to access github
Gitops thì tất các yaml liên quan đến application sẽ được để trên git (VCS)
chúng ta sẽ sử dụng link như sau:
argocd-image-updater.argoproj.io/write-back-method: git:secret:argocd/git-creds
3.1) access git through Token
Giờ bạn tạo 1 secret trên k8s
trên là username dưới là token của github
kubectl --namespace argocd \ create secret generic git-creds \ --from-literal=username=$GITHUB_USER \ --from-literal=password=$GITHUB_TOKEN
3.2) access git through ssh-key
Tạo 1 secret và dụng key sau sshPrivateKey
kubectl -n argocd create secret generic git-creds \ --from-file=sshPrivateKey=~/.ssh/id_rsa
4) Practicing argocd-image-updater in fact
4.1) argocd-image-updater – kustomize – manifest(deployment, service,..)
Bạn tạo 1 file devops-toolkit.yaml
apiVersion: argoproj.io/v1alpha1 kind: Application metadata: name: devops-toolkit namespace: argocd finalizers: - resources-finalizer.argocd.argoproj.io annotations: argocd-image-updater.argoproj.io/image-list: vfarcic/devops-toolkit argocd-image-updater.argoproj.io/write-back-method: git:secret:argocd/git-creds argocd-image-updater.argoproj.io/git-branch: main spec: project: default source: repoURL: https://github.com/mrnim94/argo-cd-image-updater targetRevision: HEAD path: kustomize/overlays/production destination: server: https://kubernetes.default.svc namespace: production syncPolicy: automated: selfHeal: true prune: true allowEmpty: true
Để kubectl apply -f devops-toolkit.yaml thì cluster k8s đã cài trước argocd


Giờ sem log của arocd-image-updater.




Ngoài chúng ta có nhiều cách filter image cho đúng.
https://github.com/Masterminds/semver
Multi-arch currently only is supported for update strategies which fetch image meta-data: latest
and digest
. Multi-arch will be ignored for the update strategies that do not fetch meta-data, semver
and name
.
https://www.padok.fr/en/blog/argocd-image-updater
Anh tây cũng có dặn là:
– repo code of the developer and repo DevOps (integrate with argocd-image-updater) don’t stay on same repository.
– ====> Nó sẽ tạo ra loop

4.2) argocd-image-updater – kustomize – Helm chart
Hiện tại nếu anh đang deploy các workload của developer thông qua helm chart mà team devops tự custom thì bạn có thể thao khảo cách bên dưới.
Đầu tiên bạn có public helm repo và bạn có thể tham khảo repo này của Nim
https://github.com/mrnim94/kubernetes-manifest

Tiếp theo bạn cần chuận bị 1 repo chứa kutomization của các workload.
https://github.com/mrnim94/meta-workloads/tree/begin

2 workload không thể chung 1 folder vì sẽ có nhiều tình huống sảy ra.
>>>>>>>>>>> kustomization.yaml >>>>>>>>>>>>>>>>>>>>> apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization helmCharts: - name: nimtechnology includeCRDs: false releaseName: workload1 version: 0.1.0 #look at repo: https://raw.githubusercontent.com/mrnim94/kubernetes-manifest/master/ # valuesFile: values_helm.yaml namespace: default
Bạn tạo file dưới và kubectl apply -f production-3.yaml
apiVersion: argoproj.io/v1alpha1 kind: Application metadata: name: workload1 namespace: argocd finalizers: - resources-finalizer.argocd.argoproj.io annotations: argocd-image-updater.argoproj.io/image-list: nginx argocd-image-updater.argoproj.io/write-back-method: git:secret:argocd/git-creds argocd-image-updater.argoproj.io/git-branch: master argocd-image-updater.argoproj.io/write-back-target: kustomization spec: project: default source: repoURL: https://github.com/mrnim94/meta-workloads targetRevision: HEAD path: project1/env1/workload1 plugin: name: kustomize-build-with-helm destination: server: https://kubernetes.default.svc namespace: production syncPolicy: automated: selfHeal: true prune: false allowEmpty: true

Mình thực hiện quan sat log của “argocd-image-updater”

Tiếp đến là bạn sẽ thấy “argocd-image-updater” sẽ commit lên branch master
và thêm thông số image.


5) Invalid Semantic Version
Argocd Image Updater mặc định nó sẽ đọc tag theo chuẩn “SemVer”
https://github.com/semver/semver
Nếu image của bạn không theo sematic version thì sẽ gặp lỗi sau:
time=”2022-10-25T02:43:01Z” level=debug msg=”could not parse input tag staging-ccd8f0b5-linux as semver: Invalid Semantic Version”
Tag của mình đắng rất là ngao ngơ. và mình nghĩ là nhiều bạn devops cũng đang đánh như ví dụ này:
313363500340.dkr.ecr.us-west-2.amazonaws.com/nim_engines_apiv4_result_producer:staging-ccd8f0b5-linux
Vậy bạn cần điều chỉnh anotation của Kind Application như sau:
annotations: .... argocd-image-updater.argoproj.io/image-list: >- myrepo=313363500340.dkr.ecr.us-west-2.amazonaws.com/nim_engines_apiv4_result_producer argocd-image-updater.argoproj.io/myrepo.update-strategy: latest ......
Đầu tiên là bạn đánh alias
https://argocd-image-updater.readthedocs.io/en/stable/configuration/images/#annotation-format
[<alias_name>=]<image_path>[:<version_constraint>] >>>> >>>>>> myrepo=313363500340.dkr.ecr.us-west-2.amazonaws.com/nim_engines_apiv4_result_producer
Bạn configure strategy:
https://argocd-image-updater.readthedocs.io/en/stable/configuration/images/#update-strategies
Strategy | Description |
---|---|
semver | Update to the tag with the highest allowed semantic version |
latest | Update to the tag with the most recent creation date |
name | Update to the tag with the latest entry from an alphabetically sorted list |
digest | Update to the most recent pushed version of a mutable tag |
Mình sẽ cung cấp thêm cho bạn các declarations để các bạn tham khảo thêm
annotations: argocd-image-updater.argoproj.io/git-branch: master argocd-image-updater.argoproj.io/image-list: >- myrepo=313363500340.dkr.ecr.us-west-2.amazonaws.com/nim_engines_apiv4_result_producer argocd-image-updater.argoproj.io/myrepo.allow-tags: regexp:^staging-.*-linux$ argocd-image-updater.argoproj.io/myrepo.force-update: 'true' argocd-image-updater.argoproj.io/myrepo.helm.image-name: image.repository argocd-image-updater.argoproj.io/myrepo.helm.image-tag: image.tag argocd-image-updater.argoproj.io/myrepo.update-strategy: latest argocd-image-updater.argoproj.io/write-back-method: git:secret:argocd/git-creds argocd-image-updater.argoproj.io/write-back-target: kustomization
Reference Links:.
https://github.com/argoproj-labs/argocd-image-updater/issues/218