Kubernetes

K8S - ArgoCD, Argo Rollout을 활용한 애플리케이션 배포

shj4895 2024. 2. 20. 13:50

AKS 환경에서 ArgoCD, Argo Rollout 을 활용하여 애플리케이션을 선언적으로 배포하는 방법에 대해서 알아보자

 

ArgoCD의 설치 방법으로는 공식 사이트에서 매니페스트를 다운받아 kubectl apply -f 하거나 kustomize로 설치하는 방식이 있고, Helm Chart를 사용하여 설치하는 방식이 있다. 이 글에서는 Kustomize로 설치하였다.

 

ArgoCD 설치

kustomization.yaml과 install.yaml만으로도 설치가 가능하긴 하지만, GitOps를 위해 별도의 kustomization.yaml을 생성하고 argocd kustomization.yaml, install.yaml 파일이 있는 레포지토리를 바라보게 설정하였음.

 

gitops용 kustomization.yaml

resources:
  - https://gitlab.com/<repo 이름>/management-repository//argo-cd?ref=main

 

 

argocd 공식 홈페이지의  intall.yaml을 다운 받고 kustomization.yaml을 생성하여 설치하는 과정

$ curl -O https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml

$ cat <<EOF > kustomization.yaml
> resources:
> - install.yaml
> EOF

$ kubectl create namespace argocd 

#gitops용 kustomization.yaml이 있는 곳으로 이동하여 설치
$ kubectl apply -k .

#초기 비밀번호 확인
$ kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath=``"{.data.password}" | base64 -d; echo

 

ArgoCD Web UI 접속

Port Forwading 방식과 ArgoCD 설치 시 기본적으로 생성되는 SVC의 타입을 LoadBalancer로 변경하여 퍼블릭 아이피로 접속하는 방법이 있다. ID는 admin, PW는 초기 비밀번호를 확인하여 입력하면 됨.

#localhost:8080으로 접속 가능
$kubectl port-forward service/argocd-server -n argocd 8080:443 

#이미 생성된 SVC의 타입을 로드밸런서로 변경, 로컬 환경이 윈도우인 경우에만 역슬래시 필요 리눅스는 '{"spec": {"type": "LoadBalancer"}}'
$kubectl patch svc argocd-server -n argocd -p "{\"spec\": {\"type\": \"LoadBalancer\"}}"

 

 

Gitlab – ArgoCD 간 연동(Github도 비슷)

gitlab 레포지토리를 연동하는 방법에는 https, ssh 등의 방법이 있지만 이 글에서는 ssh 통신으로 연동하는 방법을 기술함. 따라서 토큰을 생성하는 것이 아닌 ssh key를 생성하였음.

 

$ ssh-keygen -f test

 

위의 명령어로 퍼블릭/프라이빗 키가 생성되었고 이것을 각각 gitlab, argocd에 등록해야됨

 

gitlab에 퍼블릭 키 등록 과정

- gitlab repository 접속 -> Settings -> Repository -> Deploy keys에서 Enabled deploy keys -> add new key 클릭 후 위에서 생성한 퍼블릭 키 값 등록

 

ArgoCD에 프라이빗 키 등록 과정

- ArgoCD Web UI 접속 -> Setting -> Connect repo 클릭하여 gitlab url, 프라이빗 키 값 등록 후 연결

 

애플리케이션 배포 테스트

Service Repository에는 deploy, svc yaml이 있음. application.yaml을 같은 레포지토리에 둬도 되긴 하지만 GitOps를 위해 별도의 GitOps 레포지토리를 만들어서 관리했음.

 

application.yaml을 생성하여 argoCD에 배포할 수 있도록 명세

apiVersion: argoproj.io/v1alpha1
kind: Application #argoCD에서 배포에 사용하는 커스텀 리소스(CR)
metadata:
  name: guestbook
  namespace: argocd
spec:
  destination: #배포할 쿠버네티스 정보
    namespace: guestbook 
    server: https://kubernetes.default.svc
  project: default #argocd에서 별도의 프로젝트를 생성하여 애플리케이션들을 관리하는 것이 아니라면 default
  source: #git repo나 helm chart 정보
    path: guestbook # repoURL 기준으로 배포할 애플리케이션의 매니페스트가 있는 디렉토리
    repoURL: git@gitlab.com:<repo 주소>/service-repository.git #배포할 서비스의 매니페스트가 있는 레포지토리 주소
    targetRevision: HEAD
  syncPolicy:
    automated:
      selfHeal: true
    syncOptions:
    - CreateNamespace=true

 

Argo Rollout을 활용한 여러가지 배포 방식(Canary, Blue/Green)

ArgoCD만으로는 여러 배포 방식을 사용할 수 없으므로 Argo Rollout을 설치해야됨.

 

Argo Rollout 설치

# argo-rollouts의 namespace 생성
$ kubectl create namespace argo-rollouts

# manifest 파일 다운로드
$ curl -O https://github.com/argoproj/argo-rollouts/releases/latest/download/install.yaml

#그 이후의 과정은 ArgoCD 설치 과정과 동일

 

 

애플리케이션이 있는 레포지토리에 rollout.yaml 생성(canary 배포와, blue/green 배포 중 선택)

#canary 배포
apiVersion: argoproj.io/v1alpha1
kind: Rollout
metadata:
  name: bubble-pool
spec:
  replicas: 3
  strategy:
    canary:
      steps:
      - setWeight: 33
      - pause: {duration: 10}
      - setWeight: 66
      - pause: {duration: 10}
      - setWeight: 99
  revisionHistoryLimit: 2
  selector:
    matchLabels:
      app: bubble-pool
  template:
    metadata:
      labels:
        app: bubble-pool
    spec:
      containers:
      - name: bubble-pool
        image: argoproj/rollouts-demo:green
        ports:
        - name: http
          containerPort: 8080
          protocol: TCP
        resources:
          requests:
            memory: 32Mi
            cpu: 5m

 

#blue/green 배포
apiVersion: argoproj.io/v1alpha1
kind: Rollout
metadata:
  name: bubble-pool
spec:
  replicas: 3
  strategy:
    blueGreen: 
      #activeService는 이전의 배포된 Blue 서비스
      activeService: bubble-pool-blue
      
      #previewService는 새롭게 배포될 Green 서비스
      previewService: bubble-pool-green
      
      #autoPromotioEnabled 옵션은 Blue/Green 배포를 자동으로 진행할 것인지 여부. false 옵션을 사용해 수동으로 지정
      autoPromotionEnabled: true
  revisionHistoryLimit: 2
  selector:
    matchLabels:
      app: bubble-pool
  template:
    metadata:
      labels:
        app: bubble-pool
    spec:
      containers:
      - name: bubble-pool
        image: argoproj/rollouts-demo:green
        ports:
        - name: http
          containerPort: 8080
          protocol: TCP
        resources:
          requests:
            memory: 32Mi
            cpu: 5m

 

gitops 레포지토리에서 kubectl apply -f application.yaml로 파드 재배포