DevOps, Infra

[ArgoCD] App of Apps 패턴으로 서비스 배포하기

seulseul 2023. 11. 21. 01:28

개요

서비스는 GitOps Repo 와 ArgoCD 를 이용하여 자동 배포, Declarative Setup 를 달성했다.

그러나 ArgoCD 에 Application 을 생성할때는 UI  클릭을 통해 생성한다.

다른 클러스터로 생성돼있는 Application 을 모두 이전해야한다면 Web UI 클릭으로 실수 없이 가능할까?

Application 을 Web UI 로 수정하다보면 수정 기록은 어디서 확인 할 수 있으며, 해당 관리자가 변경될때는 history 관리는 어떻게 할것인가?

 

App of Apps 패턴은 ArgoCD 를 이용하여 여러 클러스터에 동일 어플리케이션을 배포할 수 있는 장점과 GitOps 의 장점을 100% 활용할 수 있다.

App of Apps 패턴GitOps로 Application 을 일괄적으로 관리하고 새로운  Application 이 추가되어도 구조가 바뀌지 않도록 하기 위한 패턴이다.

또한 App of Apps 패턴은 Application 을 선언적으로 관리할 수 있다.

 

Application

Application CR (Custom Resources) 는 Application 인스턴스를 나타내는  k8s API 오브젝트 이다.

 

apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: guestbook
  namespace: argocd
  finalizers:
  - resources-finalizer.argocd.argoproj.io
spec:
  project: default
  
  destination:
    namespace: argocd
    server: {{ .Values.spec.destination.server }}
  
  source:
	helm:
      valueFiles:
      - {{ .Values.valuesfile }} # App of Apps의 values에 환경별 values 파일을 참조하도록 선언
    path: guestbook
    repoURL: <https://github.com/argoproj/argocd-example-apps>
    targetRevision: HEAD
  
  syncPolicy:
    automated: 
      prune: true 
      selfHeal: true   
    syncOptions:
      - CreateNamespace=true

 

    • . metadata.finalizers
      • Application 리소스를 삭제하면 관련된 쿠버네티스 리소스(파드 등)까지 같이 삭제하는 옵션
    • . spec.destination.server
      •  ArgoCD가 애플리케이션을 배포하는 목적지를 지정
      • 원격 쿠버네티스 클러스터도 지정할 수 있고 ArgoCD가 설치된 쿠버네티스도 지정 가능
      • 주소를 https://kubernetes.default.svc 로 지정하면 ArgoCD가 설치된 클러스터에 배포한다.
    • . spec.source.helm
      • ArgoCD는 애플리케이션 설치 형태를 헬름, 매니페스트 등을 지원한다.
    • . spec.source.path, repoURL
      • 배포할 소스를 지정
      • 헬름 차트가 위치한 깃허브 레포지토리 URL과 해당 헬름 차트 경로를 지정
    • .spec.syncPolicy.automated.prune/selfHeal
      • ArgoCD에서 자동으로 애플리케이션을 배포할지 지정하는 옵션
      • 깃에 소스가 올라가면 자동으로 쿠버네티스에 헬름을 배포하고 싶으면 ‘automated’ 옵션을 지정
      • 실제 현업에서 Dev, Stage 환경에서는 automated 옵션을 사용하고 운영 환경에서는 수동으로 변화(‘Diff’) 내역을 확인하고 배포하기 위하여 automated 옵션을 사용하지 않는 경우가 많다.
      • prune은 깃에서 코드를 삭제하면 실제 리소스도 같이 삭제하는 옵션이며 selfHeal 옵션은 실패한 작업을 자동으로 복구한다.
    • . spec.syncOptions.CreateNamespace
      • 네임스페이스를 자동으로 생성하는 옵션

 

 

Application CRD도 같은 쿠버네티스 리소스이므로 k apply 명령어로 리소스를 생성할 수 있습니다.

 

Argocd Application 이라는 별도의 쿠버네티스 객체가 생성되었습니다.

 

 

 

ArgoCD Application Option

1. Cascading deletion

참고

https://kubernetes.io/docs/concepts/architecture/garbage-collection/#foreground-deletion

 

Garbage Collection

Garbage collection is a collective term for the various mechanisms Kubernetes uses to clean up cluster resources. This allows the clean up of resources like the following: Terminated pods Completed Jobs Objects without owner references Unused containers an

kubernetes.io

상위 Application  이 삭제될 때 하위 Application  과 해당 리소스가 모두 삭제되도록 하려면 Application 에 적절한 finalizer (종료자)  를 추가해야한다.

metadata:
  finalizers:
    # The default behaviour is foreground cascading deletion
    - resources-finalizer.argocd.argoproj.io
    # Alternatively, you can use background cascading deletion
    # - resources-finalizer.argocd.argoproj.io/background

finalizer 로 응용 프로그램을 삭제할 때 ArgoCD application-controller 는 Application Resource 의 계단식 삭제를 수행한다.

 

finalizers:

  • - resources-finalizer.argocd.argoproj.io 는 ArgoCD Application  의 종료(Finalization) 과정에서 수행되어야 하는 리소스 정리 작업을 정의한다.
  • Kubernetes에서는 리소스를 삭제할 때 해당 리소스와 관련된 종속 리소스들이 제거되는 과정이 있는데, 이때 종속 리소스들의 삭제 작업이 완료된 후에 리소스의 종료 작업이 진행되어야 한다. 이를 위해 finalizer를 사용한다.
  • 즉, resources-finalizer.argocd.argoproj.io 는 ArgoCD Application 의 삭제와 관련된 종료 작업을 수행하기 위해 필요 한 finalizer를 나타낸다.


 

App Deletion - Argo CD - Declarative GitOps CD for Kubernetes

App Deletion Apps can be deleted with or without a cascade option. A cascade delete, deletes both the app and its resources, rather than only the app. Deletion Using argocd To perform a non-cascade delete: argocd app delete APPNAME --cascade=false To perfo

argo-cd.readthedocs.io

 

 

2. Sync Phases and Waves

참고 : https://argo-cd.readthedocs.io/en/stable/user-guide/sync-waves/

https://redhat-scholars.github.io/argocd-tutorial/argocd-tutorial/04-syncwaves-hooks.html

 

 

metadata:
  annotations:
    argocd.argoproj.io/sync-wave: "5"

각 Application 마다 argocd.argoproj.io/sync-wave 라는 annotation 설정을 통해 설치 순서를 제어할 수 있는 기능으로, 기본값은 0으로 설정되어 있으나, 이 값을 조절하여 원하는대로 설치 순서를 조절할 수 있게 해준다.

 

Argo CD가 동기화를 시작하면 다음 우선순위에 따라 리소스를 정렬한다.

It then determines the number of the next wave to apply.
This is the first number where any resource is out-of-sync or unhealthy.
It applies resources in that wave.
It repeats this process until all phases and waves are in-sync and healthy.
Because an application can have resources that are unhealthy in the first wave, it may be that the app can never get to healthy.
Note that there's currently a delay between each sync wave in order give other controllers a chance to react to the spec change that we just applied.
This also prevent Argo CD from assessing resource health too quickly (against the stale object), causing hooks to fire prematurely.
The current delay between each sync wave is 2 seconds and can be configured via environment variable ARGOCD_SYNC_WAVE_DELAY.

그런 다음 적용할 다음 웨이브의 수를 결정합니다. 

이는 리소스가 동기화되지 않거나 비정상인 첫 번째 숫자입니다.

해당 웨이브에 리소스를 적용합니다.

모든 위상과 웨이브가 동기화되고 정상 상태가 될 때까지 이 프로세스를 반복합니다.