Skip to content

NimTechnology

Trình bày các công nghệ CLOUD một cách dễ hiểu.

  • Kubernetes & Container
    • Docker
    • Kubernetes
      • Ingress
    • Helm Chart
    • Isito-EnvoyFilter
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Vault
    • Longhorn – Storage
    • VictoriaMetrics
    • MetalLB
    • Kong Gateway
  • CI/CD
    • ArgoCD
    • ArgoWorkflows
    • Spinnaker
    • Jenkins
    • Harbor
    • TeamCity
    • Git
      • Bitbucket
  • Coding
    • Terraform
      • GCP – Google Cloud
      • AWS – Amazon Web Service
    • Golang
    • Laravel
    • Python
    • Jquery & JavaScript
    • Selenium
  • Log & Monitor
    • DataDog
    • Prometheus
    • Grafana
    • ELK
      • Kibana
      • Logstash
  • BareMetal
    • NextCloud
  • Toggle search form

[ArgoCD Image Updater] How does Argocd trigger images on Dockerhub and deploy workload on k8s automatically?

Posted on October 5, 2022December 5, 2022 By nim No Comments on [ArgoCD Image Updater] How does Argocd trigger images on Dockerhub and deploy workload on k8s automatically?

Contents

  • 1) Overview and Install ArgoCD Image Updater on K8s
  • 2) Adjusting the configuration of ArgoCD Image Updater
    • 2.1) Access ECR
      • 2.1.2) Using Access and Secret Key
  • 3) argocd-image-updater need to access github
    • 3.1) access git through Token
    • 3.2) access git through ssh-key
  • 4) Practicing argocd-image-updater in fact
    • 4.1) argocd-image-updater – kustomize – manifest(deployment, service,..)
    • 4.2) argocd-image-updater – kustomize – Helm chart
  • 5) Invalid Semantic Version

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.

Bạn sẽ chuẩn bị 1 file Application của 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:

[AWS] Login and get secret/token/credential of ECR

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:

OK giờ application đã lên
Chúng ta kiêm tra log của arogcd-image-updater

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

Như Anh tây mô ta thì arogcd-image-updater integrate với nhiều Docker Registry.


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

https://argocd-image-updater.readthedocs.io/en/stable/basics/update-methods/#specifying-git-credentials

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

Khi bạn mới tạo application thì deploy sẽ lấy image có trong deployment của git.

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

Nó đã nhận nhiện được image vfarcic/devops-toolkit và chuẩn bị update tag mới cho image.
argocd-image-updater sẽ commit lên repo github.
giờ workload đã được nâng revision lên 2 và image đã được cập nhật mơi

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

Bạn nhớ là repo phải có index.yaml nhé

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

Bạn cũng nên nhờ là each workload have a individual folder
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
Bạn để ý workload vẫn revision là 1

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.

Bạn thây là workload đã được cập nhật image mới.

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

StrategyDescription
semverUpdate to the tag with the highest allowed semantic version
latestUpdate to the tag with the most recent creation date
nameUpdate to the tag with the latest entry from an alphabetically sorted list
digestUpdate 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

ArgoCD

Post navigation

Previous Post: [Kafka] How many partitions are enough in a topic on Kafka?
Next Post: [Helm chart] Where will you save or push helm packages?

More Related Articles

[ArgoCD]Hướng dẫn cấu hình argocd deploy application trên k8s bằng helm ArgoCD
[ArgoCD] Sử dụng ArgoCD deploy lên k8s. ArgoCD
[Argocd] Configure Role and Grant permission for the users that sign in to Argocd ArgoCD
[Argocd/Vault] Integrate Vault inside Argocd by the plugin ArgoCD
[ArgoCD/KSOPS/AWS] Encrypt secrets before pushing them to GitHub. ArgoCD
[ArgoCD] Thiết kế App of Apps trong ArgoCD ArgoCD

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tham Gia Group DevOps nhé!
Để Nim có nhiều động lực ra nhiều bài viết.
Để nhận được những thông báo mới nhất.

Recent Posts

  • Experiences for IP Addresses Shortage on EKS Clusters March 29, 2023
  • [Talisman] Discover the sensitive information in your code. March 28, 2023
  • [Prometheus/Grafana] Install Prometheus and Grafana on ubuntu. March 27, 2023
  • [Kong Gateway] WebSocket connection failed March 26, 2023
  • [Nextcloud] Can’t download files to have a size bigger than 2Gi on NextCloud – RaspBerry March 24, 2023

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Categories

  • BareMetal
    • NextCloud
  • CI/CD
    • ArgoCD
    • ArgoWorkflows
    • Git
      • Bitbucket
    • Harbor
    • Jenkins
    • Spinnaker
    • TeamCity
  • Coding
    • Golang
    • Jquery & JavaScript
    • Laravel
    • Python
    • Selenium
    • Terraform
      • AWS – Amazon Web Service
      • GCP – Google Cloud
  • Kubernetes & Container
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Docker
    • Helm Chart
    • Isito-EnvoyFilter
    • Kong Gateway
    • Kubernetes
      • Ingress
    • Longhorn – Storage
    • MetalLB
    • Vault
    • VictoriaMetrics
  • Log & Monitor
    • DataDog
    • ELK
      • Kibana
      • Logstash
    • Grafana
    • Prometheus
  • Uncategorized
  • Admin

Copyright © 2023 NimTechnology.