Amazon EKS에서 ArgoCD 구성

22546 단어 eksargocdawskubernetes
Argo CD은 Kubernetes용 선언적 GitOps 지속적 전달 도구입니다. 이 블로그 게시물에서는 Amazon Elastic Container Service for Kubernetes(Amazon EKS)에서 ArgoCD를 설치, 구성 및 관리하는 방법을 살펴봅니다.

전제 조건



  • Installingconfiguring AWS CLI
  • eksctl
  • Kubectl
  • ArgoCD CLI
  • Route 53에서 퍼블릭 호스팅 영역을 생성합니다. See tutorial
  • AWS Certificate Manager로 공인 인증서를 요청합니다. See tutorial

  • EKS 구성



    EKS 클러스터를 생성하여 시작합니다.

    export AWS_PROFILE=<AWS_PROFILE>
    export AWS_REGION=eu-west-1
    export EKS_CLUSTER_NAME=devops
    export EKS_VERSION=1.19
    
    eksctl create cluster \
     --name $EKS_CLUSTER_NAME \
     --version $EKS_VERSION \
     --region $AWS_REGION \
     --managed
    


    ArgoCD 설치



    Kubernetes에 ArgoCD를 설치하기 전에 Amazon EKS에서 인증해야 합니다.

    aws eks --region $AWS_REGION update-kubeconfig --name $EKS_CLUSTER_NAME
    


    공식 yaml을 사용하여 ArgoCD 설치

    $ kubectl create namespace argocd
    $ kubectl config set-context --current --namespace=argocd 
    $ kubectl apply -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
    


    ArgoCD 구성



    이전에 생성한 공용 인증서를 ArgoCD 서버에 연결합니다.

    cat > argocd-server.patch.yaml << EOF
    apiVersion: v1
    kind: Service
    metadata:
      annotations:
        service.beta.kubernetes.io/aws-load-balancer-ssl-cert: "<ACM_ARGOCD_ARN>"
    spec:
      type: LoadBalancer
      loadBalancerSourceRanges:
      - "<LOCAL_IP_RANGES>"
    EOF
    
    ACM_ARGOCD_ARN=<ACM_ARGOCD_ARN>
    sed -i "s,<ACM_ARGOCD_ARN>,${ACM_ARGOCD_ARN},g; s/<LOCAL_IP_RANGES>/$(curl -s http://checkip.amazonaws.com/)\/32/g; " argocd-server.patch.yaml
    $ kubectl patch svc argocd-server -p "$(cat argocd-server.patch.yaml)"
    


    이전에 생성한 호스팅 영역에서 레코드 세트를 생성합니다. CNAME 레코드는 ArgoCD 서버의 수신 호스트 이름을 가리킵니다.

    PUBLIC_DNS_NAME=<PUBLIC_DNS_NAME>
    R53_HOSTED_ZONE_ID=<R53_HOSTED_ZONE_ID>
    cat > argocd-recordset.json << EOF
    {
                "Changes": [{
                "Action": "CREATE",
                            "ResourceRecordSet": {
                                        "Name": "argocd.${PUBLIC_DNS_NAME}.",
                                        "Type": "CNAME",
                                        "TTL": 300,
                                     "ResourceRecords": [{ "Value": "$(kubectl get services argocd-server --output jsonpath='{.status.loadBalancer.ingress[0].hostname}')"}]
    }}]
    }
    EOF
    
    aws route53 change-resource-record-sets --hosted-zone-id $R53_HOSTED_ZONE_ID --change-batch file://argocd-recordset.json
    


    AWS Elastic Load Balancer가 SSL을 수행하므로 TLS를 비활성화하여 HTTP(비보안) 모드에서 Argo CD를 시작해야 합니다. argocd-server 배포를 편집하여 --insecure 플래그를 argocd-server 명령에 추가합니다.

    cat > argocd-deployment-server.patch.yaml << EOF
    spec:
      template:
        spec:
          containers:
            - command:
              - argocd-server
              - --staticassets
              - /shared/app
              - --insecure
              name: argocd-server
    EOF
    
    $ kubectl patch deployment argocd-server -p "$(cat argocd-deployment-server.patch.yaml)" 
    


    Amazon EKS는 Classic Load Balancer를 생성합니다.

    An AWS Application Load Balancer can be used as Load Balancer for both UI and gRPC traffic. See ArgoCD Ingress Configuration.





    ArgoCD 웹 UI 구성



    초기 관리자 암호는 Argo CD API 서버의 포드 이름으로 자동 생성됩니다. 변경합시다.
    argocd-secret 비밀을 편집하고 새 bcrypt 해시로 admin.password 필드를 업데이트합니다. https://www.browserling.com/tools/bcrypt과 같은 사이트를 사용하여 새 해시를 생성할 수 있습니다.

    ARGOCD_ADDR="argocd.${PUBLIC_DNS_NAME}"
    
    $ kubectl patch secret argocd-secret \
      -p '{"stringData": {
        "admin.password": "<BCRYPT_HASH>",
        "admin.passwordMtime": "'$(date +%FT%T%Z)'"
      }}'
    


    사용자 이름 admin과 새 비밀번호를 사용하여 지금 로그인하십시오.

    argocd login $ARGOCD_ADDR
    


    관리 사용자는 superuser이며 시스템에 대한 무제한 액세스 권한이 있습니다. ArgoCD는 일상 업무에서 관리 사용자를 사용하지 않을 것을 권장합니다.

    두 명의 새 사용자democi를 추가해 보겠습니다.
  • 사용자demo는 웹 UI에 대한 읽기 전용 액세스 권한을 가집니다.
  • 사용자ci는 쓰기 권한을 가지며 CI/CD 파이프라인에서 argocd 명령을 실행하기 위한 액세스 토큰을 생성하는 데 사용됩니다.

  • Git 리포지토리가 있는 경우 repositories 속성에서 지정할 수 있습니다.

    cat > argocd-configmap.yaml << EOF
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: argocd-cm
      namespace: argocd
      labels:
        app.kubernetes.io/name: argocd-cm
        app.kubernetes.io/part-of: argocd
    data:
      repositories: |
        - url: <GIT_REPOSITORY_URL>
          passwordSecret:
            name: demo
            key: password
          usernameSecret:
            name: demo
            key: username
      admin.enabled: "true"
      accounts.demo.enabled: "true"
      accounts.demo: login
      accounts.ci.enabled: "true"
      accounts.ci: apiKey
    EOF
    
    GIT_USERNAME=<GIT_USERNAME>
    GIT_TOKEN=<GIT_TOKEN>
    
    $ kubectl create secret generic demo \
    --from-literal=username=$GIT_USERNAME \
    --from-literal=password=$GIT_TOKEN
    


    사용자를 생성해 보겠습니다.

    sed -i "s,<GIT_REPOSITORY_URL>,$GIT_REPOSITORY_URL,g" argocd-configmap.yaml
    
    $ kubectl apply -f argocd-configmap.yaml
    

    demo 사용자에게 암호를 추가합니다.

    argocd account update-password --account demo --current-password "${ADMIN_PASSWORD}" --new-password "<DEMO_PASSWORD>"
    


    이제 사용자에게 역할을 할당할 수 있습니다.
  • demo 사용자는 읽기 전용 액세스 권한을 가집니다.
  • ci 사용자는 프로젝트, 저장소, 클러스터 및 응용 프로그램을 관리합니다.

  • cat > argocd-rbac-configmap.yaml << EOF
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: argocd-rbac-cm
      namespace: argocd
      labels:
        app.kubernetes.io/name: argocd-rbac-cm
        app.kubernetes.io/part-of: argocd
    data:
      policy.default: role:readonly
      policy.csv: |
        p, role:ci, applications, sync, *, allow
        p, role:ci, applications, update, *, allow
        p, role:ci, applications, override, *, allow
        p, role:ci, applications, create, *, allow
        p, role:ci, applications, get, *, allow
        p, role:ci, applications, list, *, allow
        p, role:ci, clusters, create, *, allow
        p, role:ci, clusters, get, *, allow
        p, role:ci, clusters, list, *, allow
        p, role:ci, projects, create, *, allow
        p, role:ci, projects, get, *, allow
        p, role:ci, projects, list, *, allow
        p, role:ci, repositories, create, *, allow
        p, role:ci, repositories, get, *, allow
        p, role:ci, repositories, list, *, allow
    
        g, ci, role:ci
    EOF
    
    $ kubectl apply -f argocd-rbac-configmap.yaml
    




    CI/CD 통합


    ci 사용자를 사용하여 토큰을 생성하려면 다음 명령을 실행할 수 있습니다.

    AROGOCD_TOKEN=$(argocd account generate-token --account ci)
    


    토큰은 AWS Secret Manager에 저장하고 CI/CD 파이프라인에서 사용할 수 있습니다.

    aws secretsmanager create-secret --name argocd-token \
        --description "ArgoCD Token" \
        --secret-string "${AROGOCD_TOKEN}"
    


    다음 Gitlab 예제는 이 토큰을 사용하여 클러스터, 프로젝트를 생성하고 ArgoCD에서 애플리케이션을 동기화하는 방법을 보여줍니다.

    If you want to understand how an IAM role can be attached to a Gitlab runner, please refer to my previous post on



    stages:
      - init
      - deploy
    
    variables: 
       KUBECTL_VERSION: 1.20.5
       ARGOCD_VERSION: 1.7.4
       ARGOCD_ADDR: argocd.example.com
    
    # Get ArgoCD credentials from Secret Manager
    before_script:
        - export AROGOCD_TOKEN="$(aws secretsmanager get-secret-value --secret-id argocd-token --version-stage AWSCURRENT --query SecretString --output text)"
        # install kubectl
        - curl -L "https://storage.googleapis.com/kubernetes-release/release/v${KUBECTL_VERSION}/bin/linux/amd64/kubectl" -o /usr/bin/kubectl 
        # install argocd
        - curl -sSL -o /usr/local/bin/argocd "https://github.com/argoproj/argo-cd/releases/download/v${ARGOCD_VERSION}/argocd-linux-amd64"
    
    init demo project 🔬:
      stage: init
      when: manual
      image:
        name: amazon/aws-cli
      script:
        - argocd cluster add $BUSINESS_K8S_CONTEXT --name business-cluster-dev --kubeconfig $KUBE_CONFIG --auth-token=${AROGOCD_TOKEN} --server ${ARGOCD_ADDR} || echo 'cluster already added'
      tags:
        - k8s-dev-runner
      only:
        - master 
    
    deploy demo project 🚀:
      stage: init
      when: manual
      image:
        name: amazon/aws-cli
      script:
        - sed -i "s,<KUBERNETES_CLUSTER_URL>,$BUSINESS_K8S_CLUSTER_URL,g;s,<GIT_REPOSITORY_URL>,$CI_PROJECT_URL.git,g" application.yaml
        # Connect to aws eks devops cluster
        - aws eks update-kubeconfig --region $AWS_REGION --name $EKS_CLUSTER_NAME
        # Create ArgoCD project
        - argocd proj create demo-dev -d $BUSINESS_K8S_CLUSTER_URL,app-dev -s $CI_PROJECT_URL.git --auth-token=${AROGOCD_TOKEN} --server ${ARGOCD_ADDR} || echo 'project already created' 
        # Create ArgoCD application
        - kubectl apply -n argocd -f application.yaml
      tags:
        - k8s-dev-runner
      only:
        - master
    
    deploy demo app 🌐:
      stage: deploy
      image:
        name: amazon/aws-cli
      script:
        - cd envs/dev
        - argocd app sync demo-dev --auth-token=${AROGOCD_TOKEN} --server ${ARGOCD_ADDR}
      tags:
        - k8s-dev-runner
      only:
        - tags
    


    ArgoCD 응용 프로그램의 구성은 다음과 같습니다.

    apiVersion: argoproj.io/v1alpha1
    kind: Application
    metadata:
      name: demo-dev
      namespace: argocd
    spec:
      project: demo-dev
      source:
        repoURL: <GIT_REPOSITORY_URL>
        targetRevision: HEAD
        path: envs/dev
      destination:
        server: <KUBERNETES_CLUSTER_URL>
        namespace: app-dev
    


    이러한 파이프라인을 실행하기 전에 Gitlab 실행기는 argoproj.io API에 대한 액세스 권한이 있어야 합니다.

    RBAC를 만듭니다.

    cat - <<EOF | kubectl apply -f - --namespace "argocd"
    apiVersion: rbac.authorization.k8s.io/v1
    kind: Role
    metadata:
      name: $GITLAB_RUNNER_IAM_ROLE_NAME
      namespace: argocd
    rules:
      - apiGroups: ["argoproj.io"]
        resources: ["clusters", "projects", "applications", "repositories", "certificates", "accounts", "gpgkeys"]
        verbs: ["get", "create", "update", "delete", "sync", "override", "action"]
    
    EOF    
    
    cat - <<EOF | kubectl apply -f - --namespace "argocd"
    apiVersion: rbac.authorization.k8s.io/v1
    kind: RoleBinding
    metadata:
      name: $GITLAB_RUNNER_IAM_ROLE_NAME
      namespace: argocd
    subjects:
    - kind: User
      name: $GITLAB_RUNNER_IAM_ROLE_NAME
      apiGroup: rbac.authorization.k8s.io
    roleRef:
      kind: Role
      name: $GITLAB_RUNNER_IAM_ROLE_NAME
      apiGroup: rbac.authorization.k8s.io
    
    EOF
    


    GITLAB_RUNNER_IAM_ROLE_NAME is the name of the IAM role linked to the kubernetes service account attached to the runner.


    aws-auth configmap을 업데이트합니다.

    $ kubectl get configmap -n kube-system aws-auth -o yaml > aws-auth.yaml
    


    다음 역할로 구성 맵을 완료하십시오.

      mapRoles: | 
        - rolearn: $GITLAB_RUNNER_IAM_ROLE_ARN
          username: $GITLAB_RUNNER_IAM_ROLE_NAME
    


    변경 사항 적용

    $ kubectl apply -f aws-auth.yaml
    


    그게 다야!

    결론



    이 블로그 게시물에서 우리는 ArgoCD 서버, ArgoCD 웹 UI를 구성했고 결국 ArgoCD를 CI/CD 도구에 통합했습니다.

    이 블로그 게시물을 즐겁게 읽으셨기를 바랍니다.

    질문이나 의견이 있으시면 언제든지 의견을 남겨주세요.

    읽어 주셔서 감사합니다!

    좋은 웹페이지 즐겨찾기