ArgoCD를 배워보자 - 2일차 - ArgoCD 설치, Enterprise github repository 설정, Application 배포

0

ArgoCD

목록 보기
2/2

ArgoCD 설치

이제 본격적으로 ArgoCD를 설치해보도록 하자.

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

몇 분정도 기다리면 다음과 같이 설치된다.

argocd        argocd-application-controller-0                     1/1     Running             0              2m24s
argocd        argocd-applicationset-controller-5c787df94f-zbsms   1/1     Running             0              2m24s
argocd        argocd-dex-server-6bb9b5fc75-fqf2l                  1/1     Running             0              2m24s
argocd        argocd-notifications-controller-7ccbd7fb6-vlt9l     1/1     Running             0              2m24s
argocd        argocd-redis-c5c567495-kr728                        1/1     Running             0              2m24s
argocd        argocd-repo-server-799b498d8b-w658d                 1/1     Running             0              2m24s
argocd        argocd-server-f6d4d8775-vpw52                       1/1     Running             0              2m24s

만약 argocd의 core부분만 설치하고 싶다면, 다음과 같이 가능하다. https://argo-cd.readthedocs.io/en/latest/operator-manual/core/#installing

export ARGOCD_VERSION=<desired argo cd release version (e.g. v2.7.0)>
kubectl create namespace argocd
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/$ARGOCD_VERSION/manifests/core-install.yaml

그런데, 굳이 ArgoCD를 core만 설치할 필요는 없어 보인다.

  • linux argocli 설치
curl -sSL -o argocd-linux-amd64 https://github.com/argoproj/argo-cd/releases/latest/download/argocd-linux-amd64
sudo install -m 555 argocd-linux-amd64 /usr/local/bin/argocd
rm argocd-linux-amd64
  • argocli 설치 확인
argocd version
argocd: v2.13.2+dc43124
  BuildDate: 2024-12-11T19:01:33Z
  GitCommit: dc43124058130db9a747d141d86d7c2f4aac7bf9
  GitTreeState: clean
  GoVersion: go1.22.9
  Compiler: gc
  Platform: linux/amd64
FATA[0000] Argo CD server address unspecified

ArgoCD API server는 기본적으로 external하게 외부에 오픈되어 있지 않다. 따라서, LoadBalancer type service를 사용하거나, port-forward 등을 사용하여 외부로 노출시키면 된다.

  • argocd-server service type을 LoadBalancer로 교체
kubectl patch svc argocd-server -n argocd -p '{"spec": {"type": "LoadBalancer"}}'
  • port-forwarding
kubectl port-forward svc/argocd-server -n argocd 8080:443

맨날 헷갈리는데, 8080:443이면 443이 service의 port이고 8080이 localhost의 port이다. 따라서, https://localhost:8080로 접근하면 된다.

  • argocd-nodeport.yaml
apiVersion: v1
kind: Service
metadata:
  name: argocd-server-nodeport
  namespace: argocd
spec:
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: 8080
    nodePort: 30007
  - name: https
    port: 443
    protocol: TCP
    targetPort: 8080
    nodePort: 30008
  selector:
    app.kubernetes.io/name: argocd-server
  type: NodePort

NodePort를 만들어서 접근해도 된다.

kubectl patch svc argocd-server -n argocd -p '{"spec": {"type": "NodePort"}}'

이것도 가능한데, 원본 svc 건드리기 싫어서 새로 만들었다.

argocd-server에 접속하면 login UI가 나오는데, id는 admin이고 password가 필요하다.

  • argocd password
argocd admin initial-password -n argocd
A2Pe22-Wc-9MAse3

 This password must be only used for first time login. We strongly recommend you update the password using `argocd account update-password`.

A2Pe22-Wc-9MAse3 이런 값이 나오는데, 이게 password이다. 사실 argoCD가 처음 설치될 때 Secretargocd-initial-admin-secretpassword 값이 자동으로 설정된다. 이 값을 base64 디코딩하면 argocd cli를 통해 나오는 값이다.

argocd-initial-admin-secret은 말 그대로 초기화로 맨 처음에만 쓰이고, 그 이후에는 안써도 된다. 오히려 삭제하는 것을 추천하고 개인의 password들을 사용하는 것을 추천한다.

argocd core와 argocd cli를 연동하고 싶다면 login을 해주어야 한다.

argocd login <argocd-server-url>

Username: admin
Password:
'admin:login' logged in successfully

다음으로 argocd-cli를 통해서 password를 바꿔보도록 하자.

argocd account update-password

*** Enter password of currently logged in user (admin):
*** Enter new password for user admin:
*** Confirm new password for user admin:
Password updated

이제 수정된 password를 통해서 login 할 수 있다.

만약, ArgoCD를 통해서 배포할 application이 현재 argocd가 설치된 kubernetes cluster가 아닌 경우, 다음의 명령어로 추가할 수 있다.

  • 현재 cluster context 이름 가져오기
kubectl config get-contexts -o name

argocd를 통해서 배포할 cluster context를 설정하도록 하자.

argocd cluster add <context-name>

위 명령어가 실행되면 해당 kubectl context의 kube-system namespace로 ServiceAccount를 설치한다. 또한, 해당 service account를 admin 수준의 ClusterRole에 바인딩해준다. ArgoCD는 이 service account token을 사용하여 argoCD의 기능인 배포, 모니터링 기능을 외부 cluster에 수행하는 것이다.

이제 간단하게 Application을 배포해보도록 하자.

  • 먼저 argocd cli를 사용하기 위해서 kubernetes cluster의 namespace context를 argocd로 설정해주도록 하자.
kubectl config get-contexts
CURRENT   NAME                          CLUSTER      AUTHINFO           NAMESPACE
*         kubernetes-admin@kubernetes   kubernetes   kubernetes-admin

namespace가 비어있는 것을 볼 수 있다. 다음의 명령어로 argocd namespace를 넣어주도록 하자.

kubectl config set-context --current --namespace=argocd

kubectl config get-contexts
CURRENT   NAME                          CLUSTER      AUTHINFO           NAMESPACE
*         kubernetes-admin@kubernetes   kubernetes   kubernetes-admin   argocd

argocd가 설정된 것을 볼 수 있다.

다음의 argocd cli 명령어로 application을 설치할 수 있다.

argocd app create guestbook --repo https://github.com/argoproj/argocd-example-apps.git --path guestbook --dest-server https://kubernetes.default.svc --dest-namespace default

설치가 된 다음, sync를 해주어 cluster에 최신 버전이 반영되도록 한다.

argocd app sync guestbook

Enterprise github에서 ArgoCD 설정

enterprise github가 아니라도, 아래의 과정의 1. Known_hosts 설정 부분만 스킵하고 나머지는 일반적인 github 설정과 동일하다.

내가 google에 다니는데, google에서는 public github 대신 enterprise github을 사용한다고 하자. 따라서, url도 github가 아니라 github.google인 경우, 어떻게 ArgoCD에 설정할 수 있을까?

각 회사마다 사용하고 있는 enterprise github에서 ArgoCD repo를 설정하는 방법은 다음과 같다.

1. Known_hosts 설정

각 회사에서 사용하고 있는 tool이 enterprise github처럼 public과는 다른 URL(ex, github.google)을 가졌다면, 일반적인 ssh 설정으로는 repo 설정에 실패하게 된다.

kubectl logs -n argocd argocd-server-f6d4d8775-vpw52  -f
...
time="2024-12-30T08:10:44Z" level=error msg="finished unary call with code Unknown" error="error testing repository connectivity: ssh: handshake failed: knownhosts: key is unknown" grpc.code=Unknown grpc.method=TestRepository grpc.service=repository.RepoServerService grpc.start_time="2024-12-30T08:10:44Z" grpc.time_ms=40.262 span.kind=server system=grpc

뒤에 후술하는 repo 설정에서, 이런 에러가 발생한다.

ArgoCD는 ConigMap을 통해서 ssh의 known_hosts를 관리하므로, ssh 설정을 하기 전에 ArgoCD의 configmap에 ssh known_host key값들을 넣어주면 된다.

ssh-keyscan <enterprise_github_url> | argocd cert add-ssh --batch

private url이 github.google.fake라면 위와 같이 입력하면 된다.

다음의 명령어로 enterprise github가 ArgoCD known_hosts에 설정되었는 지 확인할 수 있다.

argocd cert list
HOSTNAME                 TYPE  SUBTYPE              INFO
github.google.fake       ssh   ecdsa-sha2-nistp256  SHA256:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
github.google.fake       ssh   ssh-ed25519          SHA256:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
github.google.fake       ssh   ssh-rsa              SHA256:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

2. SSH key 생성

ssh key를 생성해보도록 하자.

ssh-keygen -t ed25519 -C "argocd test" -f sample

엔터 연타를 하도록 하자.

samplesample.pub 파일이 생겼을 것이다.

ls -al
-rw-------  1 root      root       399 Dec 30 17:00 sample
-rw-r--r--  1 root      root        94 Dec 30 17:00 sample.pub

sample.pub은 private github에 추가하면되고, sample은 argocd에서 repo를 생성할 때 사용하면 된다.

3. Repo 설정

먼저 위에서 만든 ssh public key를 배포하려는 git repository에 등록해주도록 하자.

가령 내가 배포하려는 repository가 다음이라고 하자.

https://github.google.fake/john/argocd-example-app

해당 repo에 접속하고 다음의 순서로 입력하면 된다.
1. 화면 중상단에 있는 repo Settings 접속
2. Security -> Deploy keys
3. Add deploy key 클릭
4. Title 입력, keysample.pub에 있는 public key값 복사-입력
5. Allow write access 체크
6. Add key 입력

다음은 ArgoCD를 통해서 repository를 등록할 차례이다. repository를 등록하는 방법은 3가지 정도 있다.

  1. ArgoCD CLI 사용
  2. ArgoCD UI 사용
  3. Secret 사용 (ArgoCD annotation 추가)

우리는 Secret을 사용해서 등록해보도록 하자, 다음의 annotation을 secret에 추가해주어야 한다.

argocd.argoproj.io/secret-type: repository

아래의 secret을 해포해보도록 하자.

  • argocd-example-app-repo-secret.yaml
apiVersion: v1
kind: Secret
metadata:
  name: argocd-example-app-repo-secret
  namespace: argocd
  labels:
    argocd.argoproj.io/secret-type: repository
stringData:
  type: git
  url: git@github.google.fake:john/argocd-example-app.git
  sshPrivateKey: |
    -----BEGIN OPENSSH PRIVATE KEY-----
    ...
    -----END OPENSSH PRIVATE KEY----

sshPrivateKey에 아까 만들었던 private key인 sample을 넣어주면 된다.

UI에 접속해서 Settings -> Repositories를 클릭하면 Secret에 등록한 url repo가 입력된 것을 볼 수 있다. CONNECTION STATUSSuccessful이면 정상적으로 연결이 된 것이고, failed면 실패한 것으로 앞 부분에 뭔가 잘못 설정한 것이다.

4. Application 생성

이제 다음으로 Application을 생성해보도록 하자.

UI로도 쉽게 생성이 가능한데, yaml을 통해서 해보도록 하자.

  • guest-application.yaml
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: guestbook
  namespace: argocd
spec:
  project: default
  source:
    repoURL: git@github.google.fake:john/argocd-example-app.git
    targetRevision: HEAD
    path: guestbook
  destination:
    server: https://kubernetes.default.svc
    namespace: argocd
  syncPolicy:
    automated:
      prune: true
      allowEmpty: true

syncPolicyautomated를 설정해야 kubectl로 설치 시에 자동으로 github에 있는 code를 바탕으로 application을 배포한다.

kubectl을 통해서 배포되었는 지 확인해보도록 하자.

kubectl get po -A
NAMESPACE     NAME                                                READY   STATUS              RESTARTS       AGE
argocd        guestbook-ui-56c646849b-d6p66                       1/1     Running             0              9m45s
...

잘 배포된 것을 볼 수 있다.

ArgoCD UI로 가서 왼쪽의 sidebar의 Applications를 보면 guestbook이라는 application이 만들어졌을 것이다.

cli를 통해서도 확인이 가능하다. argocd app get <appliation-name>으로 검색하면 된다.

argocd app get guestbook

Name:               argocd/guestbook
Project:            default
Server:             https://kubernetes.default.svc
Namespace:          argocd
URL:                https://10.251.72.203:30007/applications/guestbook
Source:
- Repo:             git@github.google.fake:john/argocd-example-app.git
  Target:           HEAD
  Path:             guestbook
SyncWindow:         Sync Allowed
Sync Policy:        Automated (Prune)
Sync Status:        Synced to HEAD (b3de595)
Health Status:      Healthy

GROUP  KIND        NAMESPACE  NAME          STATUS  HEALTH   HOOK  MESSAGE
       Service     argocd     guestbook-ui  Synced  Healthy
apps   Deployment  argocd     guestbook-ui  Synced  Healthy

만약 autosync를 안했거나, 바로 sync를 cli로 하고 싶다면 다음의 명령어를 사용하면 된다.

argocd app sync guestbook

5. HTTP로 Repository 생성하기

http로도 가능하다. 단, bot과 같이 자동화된 방식으로 ArgoCD를 사용할 때는 ssh가 http방식보다 더 좋다.

http의 경우 token을 발급받고, 해당 계정에 대한 권한을 통해 각 repository들에 접근하는 방식이다. 반면에 ssh는 repository마다의 ssh public, private key를 통해 권한이 관리되기 때문에 더욱 세밀하고, 정교한 관리가 가능하다. 따라서, 보안과 관리 측면에서 ssh방식이 더 좋다. 또한, 어차피 http 방식을 사용하려면 ssh 설정을 처음에 해주어야 하기 때문에 별로 좋은 사용 방법은 아니다.

http 방식은 사용하기 매우 간단한데, github에 들어가서 가장 오른쪽 상단에 있는 자신의 계정 아이콘을 누르도록 하자.

  1. 가장 오른쪽 상단의 본인 계정 아이콘 클릭
  2. Settings 클릭
  3. 좌측 하단의 Developer settings 클릭
  4. 좌측의 Personal access tokens 클릭
  5. Tokens (classic) 클릭
  6. Generate new token(classic) 클릭
  7. 권한에 대한 check (test를 위해 모두 check 하도록 하자)
  8. Generate token 클릭

가운데 화면에 아주 작게 private token key값이 나온다. 해당 값을 저장하도록 하자.

xxx_XXXXXXXXXXXXXXXXXXXXXXXXXXX

저장 후에 ArgoCD에 github repository를 등록하는 secret을 하나 만들도록 하자.

apiVersion: v1
kind: Secret
metadata:
  name: argocd-example-private-repo
  namespace: argocd
  labels:
    argocd.argoproj.io/secret-type: repository
stringData:
  type: git
  url: https://github.google.fake/john/argocd-example-app
  username: testbot
  password: xxx_XXXXXXXXXXXXXXXXXXXXXXXXXXX

argocd UI로 확인하면 repositories가 설정되어 있을 것이다. 이제 해당 repository를 통해서 application 배포가 가능하다.

0개의 댓글