본문 바로가기
kubernetes

argocd 배포 환경 구성

by misankim 2023. 3. 9.
argocd 배포 환경 구성



argocd 는 Kubernetes 용 선언적 GitOps 지속적 배포 도구
-> git 등의 레포를 자동/수동으로 sync 하여 kubernetes 리소스 배포



공식 사이트

 

공식 가이드

 

helm chart github



# 배포

 

kubectl create namespace argocd

kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml

kubectl get all -n argocd



# argocd-server 외부에서 접속 가능하도록 설정

 

## ingress 사용

 

ingress-nginx 사용 기준

 

 

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: argocd-server-ingress
  namespace: argocd
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
    nginx.ingress.kubernetes.io/ssl-passthrough: "true"
    nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"
spec:
  rules:
  - host: argocd.example.com
    http:
      paths:
      - backend:
          serviceName: argocd-server
          servicePort: https

 

## 포트 포워딩

 

kubectl port-forward svc/argocd-server -n argocd 8080:443
-> 이후 putty 등을 이용하여 ssh 터널링을 통해 localhost:8080 접속

 

## 로드밸런서 서비스 타입 사용

 

kubectl patch svc argocd-server -n argocd -p '{"spec": {"type": "LoadBalancer"}}'
-> 클라우드 환경인 경우

 

## terminate https 구성인 경우
-> 앞단 lb 혹은 istio ingress gateway 파드에서 tls 를 종료시키는 경우
-> 백엔드인 argocd-server deploy 에 "--insecure" 플래그 추가

 

 

kubectl edit deployment.apps/argocd-server -n argocd

 

spec:
  template:
    spec:
      containers:
      - name: argocd-server
        command:
        - argocd-server
        - --insecure

 

❯ k exec -it pod/argocd-server-5bbd4cfc66-sb5fc -n argocd -- ps -ef
UID          PID    PPID  C STIME TTY          TIME CMD
argocd         1       0  1 02:07 ?        00:00:00 argocd-server --insecure
argocd        16       0  0 02:08 pts/0    00:00:00 ps -ef



# argocd cli 다운로드

 

최신 릴리즈 확인

 

curl -sSL -o /usr/local/bin/argocd https://github.com/argoproj/argo-cd/releases/latest/download/argocd-linux-amd64

 

mac os 환경인 경우
sudo curl -sSL -o /usr/local/bin/argocd https://github.com/argoproj/argo-cd/releases/download/argocd-darwin-arm64


chmod +x /usr/local/bin/argocd



# 비밀번호 확인 및 초기 비밀번호 변경

 

아이디 - admin

 

## 비밀번호 확인

 

kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d; echo;

 

## 로그인

 

argocd login argocd.example.com
-> 아이디와 비밀번호 입력

 

## 비밀번호 변경

 

argocd account update-password
-> 기존 비밀번호와 새 비밀번호 입력



# bootcamp 앱 생성

 

## git 레포 생성

 

github 에서 새 프로젝트(argotest) 생성해줌

 

git clone https://github.com/my-github-id/argotest.git

 

## 소스 작성

 

ingress-nginx를 사용하여 외부에서 접속하는 것을 가정하여 작성되어 있음

 

[root@centos7 argotest]# cat bootcamp-ns.yaml 
apiVersion: v1
kind: Namespace
metadata:
  name: bootcamp



[root@centos7 argotest]# cat bootcamp-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: bootcamp
  name: deployment-bootcamp
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: bootcamp
  replicas: 1
  template:
    metadata:
      labels:
        app.kubernetes.io/name: bootcamp
    spec:
      containers:
      - image: gcr.io/google-samples/kubernetes-bootcamp:v1
        imagePullPolicy: Always
        name: bootcamp
        ports:
        - containerPort: 8080



[root@centos7 argotest]# cat bootcamp-svc.yaml
apiVersion: v1
kind: Service
metadata:
  namespace: bootcamp
  name: service-bootcamp
spec:
  ports:
    - port: 80
      targetPort: 8080
      protocol: TCP
  type: NodePort
  selector:
    app.kubernetes.io/name: bootcamp



[root@centos7 argotest]# cat bootcamp-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: bootcamp-ingress
  namespace: bootcamp
  annotations:
    kubernetes.io/ingress.class: "nginx"
    ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: 
    http:
      paths:
        - path: /
          pathType: ImplementationSpecific
          backend:
            service:
              name:  service-bootcamp
              port:
                number: 80



## 소스 푸쉬

 

git add .
git commit -m 'init'
git push -u origin master

 

소스 업로드 확인

 

## argocd 웹에서 git repo 접속 정보 설정

 

프라이빗 git 레포의 경우 먼저 argocd 웹 -> Settings -> Repositories -> Connect repo using https 클릭하여 git 접속 정보 설정 해줌

 

## 웹 UI 를 통해 앱 생성

 

메인 -> new app

 

Repo URL - https://github.com/my-github-id/argotest.git
Revision - master
Path - .


Destination
Cluster - https://kubernetes.default.svc
Namespace - bootcamp

 

Create 버튼 클릭하여 마무리

 

아직 sync 되지 않았기 때문에 수동으로 sync 명령어 실행하거나, 웹 UI 에서 Sync 버튼 클릭 -> Syncronize

 

## argocd cli 사용법

 

앱 생성
argocd app create guestbook --repo https://github.com/my-github-id/argotest.git --path . --dest-server https://kubernetes.default.svc --dest-namespace bootcamp

 

앱 리스트 확인
argocd app list

 

특정 프로젝트의 앱 리스트 확인
argocd app list -p 프로젝트명

 

앱 정보 확인
argocd app get bootcamp

 

앱 싱크
argocd app sync bootcamp



# 자동 sync 활성화

 

해당 앱 클릭 -> app details -> sync policy -> enable auto sync 클릭

 

웹훅을 따로 등록하지 않아도 3분마다 git 레포를 폴링하여 자동으로 sync



# git 웹훅 구성

 

 

웹훅을 구성하지 않아도 3분마다 폴링하지만 빠른 싱크를 위해 웹훅 설정
웹훅을 사용하려면 github 서버에서 argocd 도메인으로 퍼블릭 액세스가 가능해야함

 

해당 git 레포 -> Settings -> Webhooks -> Add webhook

 

Payload URL - https://argocd.example.com/api/webhook
Content type - application/json
Secret - (비밀번호 설정은 선택) 임의의 비밀번호(다음 단계에서 argocd에 등록할 예정) 입력(my-password)
SSL verification - 현재 argocd 사이트의 인증서가 도메인과 일치하지 않는 경우 필요에 따라 Disable 선택

 

## git 웹훅 구성(argocd 웹훅 비밀번호 설정)

 

git 웹훅 구성 시 secret 설정한 경우 argocd 에서도 동일한 비밀번호를 생성해줘야함
인증되지 않은 웹훅을 막기 위함

 

kubectl edit secret argocd-secret -n argocd

 

가장 마지막 라인 밑에 아래 내용 추가

 

stringData:
    webhook.github.secret: my-password



(참고) 웹 UI에서 Unable to load data: grpc: the client connection is closing 에러 발생 시

 

argocd-dex-server 포드 삭제 후 웹 UI 새로 고침

 

kubectl get all -n argocd
kubectl delete pod -n argocd argocd-dex-server-76ff776f97-rlncw

 

(참고) 외부 k8s 클러스터를 argocd 의 clusters 항목에 등록

 

클러스터 이름 확인
kubectl config get-contexts -o name

 

클러스터 등록
argocd cluster add 클러스터_이름



(참고) 원격 클러스터를 제어하기 위한 argocd 서비스 어카운트 관련 리소스
-> argocd cluster add 명령어를 수행하면 원격 클러스터에 아래 리소스가 생성됨

 

❯ cat sa.yaml
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: argocd-manager
  namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: argocd-manager-role
rules:
  - apiGroups:
      - '*'
    resources:
      - '*'
    verbs:
      - '*'
  - nonResourceURLs:
      - '*'
    verbs:
      - '*'
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: argocd-manager-role-binding
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: argocd-manager-role
subjects:
  - kind: ServiceAccount
    name: argocd-manager
    namespace: kube-system



# argocd 사용자 생성
-> 생성할 사용자명이 test 인 경우

 

 

kubectl edit cm argocd-cm -n argocd

data:
  accounts.test: login



유저 목록 조회
argocd account list

 

유저 상세 정보 확인
argocd account get --account test

 

argocd account update-password \
  --account test \
  --current-password 관리자_계정_패스워드 \
  --new-password test_유저의_새_패스워드

argocd account update-password \
  --account test \
  --current-password password \
  --new-password test123



# argocd 사용자 권한 부여
-> test 사용자에게 default 프로젝트의 모든 애플리케이션에 대한 모든 작업을 허용
-> repositories, get 권한의 경우 애플리케이션 rollback 을 위해 필요

 

 

kubectl edit cm argocd-rbac-cm -n argocd

 

data:
  policy.default: ""
  policy.csv: |
    p, test, applications, *, default/*, allow
    p, test, repositories, get, *, allow

 

혹은 롤을 생성하여 권한을 부여하고 사용자를 롤에 연결

 

data:
  policy.default: ""
  policy.csv: |
    p, role:test-role, applications, *, default/*, allow
    p, role:test-role, repositories, get, *, allow
    g, test, role:test-role



# oidc 인증을 사용하는 경우 argocd cli 로그인

 

argocd login argocd.example.com --sso
-> 웹 페이지 열리면서 로그인 진행



# argocd cli 로 애플리케이션의 github 소스 레포 변경

 

앱 리스트 확인
argocd app list

 

특정 레포를 사용하는 앱 리스트 확인
argocd app list -r git@github.com:my-github-id/my-flask-app-deploy.git

 

앱 세부정보 확인
argocd app get my-flask-app -o json | jq -r .spec.source.repoURL

 

앱 소스 레포 url 수정
argocd app patch my-flask-app --patch='[{"op": "replace", "path": "/spec/source/repoURL", "value": "git@github.com:my-github-id/my-flask-app-deploy.git"}]' --type json



# 소스 레포 일괄 수정 스크립트

 

#!/bin/sh
before_repo_url="git@github.com:my-github-id/my-flask-app-deploy.git"
after_repo_url="git@github.com:my-github-id/my-flask-app-deploy-v2.git"
app_list=`argocd app list -r $before_repo_url --grpc-web | grep -v SYNCPOLICY | awk '{ print $1 }'`

echo $app_list

for app in $app_list
do
  argocd app patch $app --patch="[{\"op\": \"replace\", \"path\": \"/spec/source/repoURL\", \"value\": \"${after_repo_url}\"}]" --type json --grpc-web
done

 

'kubernetes' 카테고리의 다른 글

secret 으로 값 전달  (0) 2023.03.09
configmap 으로 값 전달  (0) 2023.03.09
istio envoy access log  (0) 2023.03.08
istio 배포  (0) 2023.03.08
argo-rollouts  (2) 2023.03.07