|
| 1 | +# eks-gitops-argocd |
| 2 | + |
| 3 | +This repository walks through implementing GitOps with GitHub Actions and ArgoCD to deploy applications via helm to EKS |
| 4 | + |
| 5 | +## High Level Architecture |
| 6 | + |
| 7 | + |
| 8 | +## Build EKS Cluster |
| 9 | +For this demo, we will create the EKS cluster using EKSCTL. Before that, some housekeeping |
| 10 | +``` |
| 11 | +export ACCOUNT_ID=$(aws sts get-caller-identity --output text --query Account) |
| 12 | +export AWS_REGION=$(aws ec2 describe-availability-zones --query 'AvailabilityZones[0].[RegionName]' --output text) |
| 13 | +export AZ1=$(aws ec2 describe-availability-zones --query 'AvailabilityZones[0].ZoneName' --region $AWS_REGION --output text) |
| 14 | +export AZ2=$(aws ec2 describe-availability-zones --query 'AvailabilityZones[1].ZoneName' --region $AWS_REGION --output text) |
| 15 | +export AZ3=$(aws ec2 describe-availability-zones --query 'AvailabilityZones[2].ZoneName' --region $AWS_REGION --output text) |
| 16 | +export K8S_VERSION=1.25 |
| 17 | +``` |
| 18 | + |
| 19 | +If you do not have eksctl installed, install it. |
| 20 | +``` |
| 21 | +curl --silent --location "https://github.com/weaveworks/eksctl/releases/latest/download/eksctl_$(uname -s)_amd64.tar.gz" | tar xz -C /tmp |
| 22 | +
|
| 23 | +sudo mv -v /tmp/eksctl /usr/local/bin |
| 24 | +eksctl completion bash >> ~/.bash_completion |
| 25 | +. /etc/profile.d/bash_completion.sh |
| 26 | +. ~/.bash_completion |
| 27 | +``` |
| 28 | + |
| 29 | +Create a CMK for the EKS cluster to use when encrypting your Kubernetes secrets: |
| 30 | +``` |
| 31 | +aws kms create-alias --alias-name alias/eksworkshop --target-key-id $(aws kms create-key --query KeyMetadata.Arn --output text) |
| 32 | +``` |
| 33 | + |
| 34 | +Let’s retrieve the ARN of the CMK to input into the create cluster command. |
| 35 | +``` |
| 36 | +export KEY_ARN=$(aws kms describe-key --key-id alias/eksworkshop --query KeyMetadata.Arn --output text) |
| 37 | +``` |
| 38 | + |
| 39 | +We set the KEY_ARN environment variable to make it easier to refer to the KMS key later. |
| 40 | + |
| 41 | +Now, let’s save the KEY_ARN environment variable into the bash_profile |
| 42 | +``` |
| 43 | +echo "export KEY_ARN=${KEY_ARN}" | tee -a ~/.bash_profile |
| 44 | +``` |
| 45 | + |
| 46 | + |
| 47 | +Now let's create the cluster |
| 48 | +``` |
| 49 | +eksctl create cluster -f config/cluster.yaml |
| 50 | +``` |
| 51 | + |
| 52 | +Test the cluster |
| 53 | +``` |
| 54 | +kubectl get nodes |
| 55 | +# if we see our 3 nodes, we know we have authenticated correctly |
| 56 | +``` |
| 57 | + |
| 58 | +Update kubeconfig file to interact with the EKS cluster |
| 59 | +``` |
| 60 | +aws eks update-kubeconfig --name eks-gitops --region ${AWS_REGION} |
| 61 | +``` |
| 62 | + |
| 63 | +## Create an ECR Repository |
| 64 | +... In the same region, and call it `eks-gitops-argocd`. This is because the files/ configurations in this demo use that name. |
| 65 | + |
| 66 | +## Setup GitHub Actions |
| 67 | + |
| 68 | +Use IAM roles for GitHub Actions to connect to Amazon ECR. Follow [this blog](https://aws.amazon.com/blogs/security/use-iam-roles-to-connect-github-actions-to-actions-in-aws/) to set it up. |
| 69 | + |
| 70 | +Use the contents below for `.github/main.yml` |
| 71 | +``` |
| 72 | +# This is a basic workflow to help you get started with Actions |
| 73 | +
|
| 74 | +name: CI |
| 75 | +
|
| 76 | +on: |
| 77 | + push: |
| 78 | + branches: [ main ] |
| 79 | + pull_request: |
| 80 | + branches: [ main ] |
| 81 | +env: |
| 82 | + AWS_REGION : "<AWS_REGION>" |
| 83 | +# Permission can be added at job level or workflow level |
| 84 | +permissions: |
| 85 | + id-token: write # This is required for requesting the JWT |
| 86 | + contents: write # This is required for actions/checkout |
| 87 | +jobs: |
| 88 | + build: |
| 89 | + name: Building and Pushing the Image |
| 90 | + runs-on: ubuntu-latest |
| 91 | +
|
| 92 | + steps: |
| 93 | + - name: Checkout |
| 94 | + uses: actions/checkout@v2 |
| 95 | +
|
| 96 | + - name: configure aws credentials |
| 97 | + uses: aws-actions/configure-aws-credentials@v1.7.0 |
| 98 | + with: |
| 99 | + role-to-assume: arn:aws:iam::<AWS_ACCOUNT_ID>:role/<IAM_ROLE_NAME> |
| 100 | + role-session-name: GitHub_to_AWS_via_FederatedOIDC |
| 101 | + aws-region: ${{ env.AWS_REGION }} |
| 102 | + # Hello from AWS: WhoAmI |
| 103 | + - name: Sts GetCallerIdentity |
| 104 | + run: | |
| 105 | + aws sts get-caller-identity |
| 106 | +
|
| 107 | + - name: Login to Amazon ECR |
| 108 | + id: login-ecr |
| 109 | + uses: aws-actions/amazon-ecr-login@v1 |
| 110 | +
|
| 111 | + - name: Build, tag, and push image to Amazon ECR |
| 112 | + id: build-image |
| 113 | + env: |
| 114 | + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} |
| 115 | + ECR_REPOSITORY: eks-gitops-argocd |
| 116 | +
|
| 117 | + run: | |
| 118 | + # Build a docker container and push it to ECR |
| 119 | + git_hash=$(git rev-parse --short "$GITHUB_SHA") |
| 120 | + docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:${GITHUB_REF##*/}-$git_hash docker/. |
| 121 | + echo "Pushing image to ECR..." |
| 122 | + docker push $ECR_REGISTRY/$ECR_REPOSITORY:${GITHUB_REF##*/}-$git_hash |
| 123 | + echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:${GITHUB_REF##*/}-$git_hash" |
| 124 | + |
| 125 | + - name: Update Version |
| 126 | + run: | |
| 127 | + git_hash=$(git rev-parse --short "$GITHUB_SHA") |
| 128 | + version=$(cat ./charts/helm-example/values.yaml | grep version: | awk '{print $2}') |
| 129 | + sed -i "s/$version/${GITHUB_REF##*/}-$git_hash/" ./charts/helm-example/values.yaml |
| 130 | + |
| 131 | + - name: Commit and push changes |
| 132 | + uses: devops-infra/action-commit-push@v0.3 |
| 133 | + with: |
| 134 | + github_token: ${{ secrets.GITHUB_TOKEN }} |
| 135 | + commit_message: Version updated |
| 136 | +
|
| 137 | +``` |
| 138 | + |
| 139 | +## Install ArgoCD on EKS |
| 140 | + |
| 141 | +Install helm |
| 142 | +``` |
| 143 | +curl -sSL https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash |
| 144 | +helm version --short |
| 145 | +
|
| 146 | +``` |
| 147 | + |
| 148 | +Install ArgoCD |
| 149 | +``` |
| 150 | +helm repo add argo https://argoproj.github.io/argo-helm |
| 151 | +helm install argocd argo/argo-cd --set server.service.type=LoadBalancer |
| 152 | +``` |
| 153 | + |
| 154 | +Access the UI which is sitting behing an ELB. To get the password: |
| 155 | +``` |
| 156 | +kubectl get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d && echo |
| 157 | +``` |
| 158 | + |
| 159 | +Connect to the Repo -> Add/Create an application |
| 160 | +``` |
| 161 | +sudo curl --silent --location -o /usr/local/bin/argocd https://github.com/argoproj/argo-cd/releases/download/v2.4.7/argocd-linux-amd64 |
| 162 | +
|
| 163 | +sudo chmod +x /usr/local/bin/argocd |
| 164 | +
|
| 165 | +export ARGOCD_SERVER=`kubectl get svc argocd-server -o json | jq --raw-output '.status.loadBalancer.ingress[0].hostname'` |
| 166 | +
|
| 167 | +export ARGO_PWD=`kubectl get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d` |
| 168 | +
|
| 169 | +argocd login $ARGOCD_SERVER --username admin --password $ARGO_PWD --insecure |
| 170 | +``` |
| 171 | + |
| 172 | +Add ECR helm repo |
| 173 | +``` |
| 174 | +argocd repo add XXXXXXXXXX.dkr.ecr.us-west-2.amazonaws.com --type helm --name eks-gitops-argocd --enable-oci --username AWS --password $(aws ecr get-login-password --region us-west-2) |
| 175 | +``` |
0 commit comments