Advanced End-to-End DevOps Project: Implementing GitOps on AWS
GitOps is revolutionizing the way we manage and deploy applications in cloud environments. This modern approach combines the power of Git as a
source of truth with automated deployments, making infrastructure and application management more reliable and efficient
GitOps is a practice that uses Git repositories as the single source of truth for declarative infrastructure and application configuration. It
leverages continuous deployment automation to ensure that the live state of your system mirrors the state defined in Git. GitOps brings the best practices of software development to infrastructure management, providing a robust, automated, and reliable framework for continuous deployment. Argo CD, with its powerful features and seamless integration with Git repositories, is an excellent tool for implementing GitOps. By leveraging pull requests, organizations can streamline changes, ensure quality, and maintain tight control over infrastructure modifications. Embrace GitOps today to transform your DevOps practices and achieve continuous deployment with confidence.
GitOps automates repetitive tasks, allowing developers to focus on writing code. By using Git as the single source of truth, developers can manage both application and infrastructure changes through pull requests.
With GitOps, every change is version-controlled and auditable. Rollbacks are as simple as reverting a commit, and the state of the system can be easily replicated across different environments.
GitOps ensures that the same process is followed for every change, promoting consistency. It standardizes how infrastructure and applications are managed, reducing the likelihood of errors.
By using Git as the source of truth, access control is simplified. Only those with permission to commit to the repository can make changes, and every change is logged.
1. Declarative Descriptions: The desired state of the system is described declaratively, using files stored in Git repositories.
2. Version Control: Git serves as the version control system, providing a single source of truth for both infrastructure and application configuration.
3. Automated Deployment: Changes to the Git repository automatically trigger deployment pipelines that reconcile the live system state with the desired state described in Git.
4. Observability and Auditability: Every change to the system is tracked, logged, and auditable through Git commit history, enhancing observability and compliance.
GitOps is ideal for managing multiple environments (development, staging, production). The desired state for each environment is stored in Git, ensuring consistency and reliability.
In case of a disaster, GitOps allows for quick recovery by redeploying the last known good state from Git. This reduces downtime and speeds up recovery processes.
GitOps facilitates compliance by providing a clear, auditable trail of changes. It helps organizations meet regulatory requirements by maintaining a detailed history of infrastructure and application changes.
I have written previously about implementing GitOps using Jenkins, SonarQube, Helm, ArgoCD etc. You can find that here. Today we want to explore how to implement GitOps using AWS suite of DevOps tools.
Developers push code changes to an AWS CodeCommit repository. CodeCommit is a secure, scalable, managed source control service that hosts priv
ate Git repositories.
The commit triggers AWS CodePipeline, which automates the CI/CD process. CodePipeline orchestrates the build, test, and deployment phases, ens
uring quick and reliable application updates.
AWS CodeBuild compiles the source code, runs tests, and produces software packages. It builds a Docker image from the application code and pus
hes it to Amazon ECR (Elastic Container Registry), a fully managed Docker container registry.
Another CodeBuild project tags the Docker image for versioning. This tag is committed to a Helm repository in CodeCommit. Helm is a package ma
nager for Kubernetes, aiding in the definition, installation, and upgrade of complex applications.
Argo CD, a GitOps continuous delivery tool for Kubernetes, synchronizes with the Helm repository. It monitors the repository for changes and u
pdates the Kubernetes cluster to match the desired state defined in Git.
Argo CD deploys the application to Amazon EKS (Elastic Kubernetes Service), a managed Kubernetes service. EKS runs Kubernetes without needing to install and operate your own control plane or nodes.
During deployment, Kubernetes pulls the Docker image from Amazon ECR to run the application containers.
Argo CD is a declarative, GitOps continuous delivery tool for Kubernetes. It continuously monitors Git repositories for changes and synchronizes the desired state defined in those repositories with the live state of Kubernetes clusters.
The diagram above illustrates a CI/CD (Continuous Integration/Continuous Deployment) pipeline for deploying applications to Amazon EKS (Elastic Kubernetes Service) using AWS services and Argo for GitOps. Let's break down the steps in detail:
1. Developer Commit:
- A developer commits code changes to an AWS CodeCommit repository. AWS CodeCommit is a source control service that hosts secure Git repositories.
2. AWS CodePipeline:
- The commit triggers AWS CodePipeline, which is a continuous integration and continuous delivery service for fast and reliable application and infrastructure updates. AWS CodePipeline automates the build, test, and deploy phases of your release process every time there is a code change.
3. AWS CodeBuild (Build & Push Image):
- AWS CodePipeline initiates a build process using AWS CodeBuild. AWS CodeBuild is a fully managed continuous integration service that compiles source code, runs tests, and produces software packages that are ready to deploy.
- The build process includes building a Docker image from the application code and then pushing this image to Amazon ECR (Elastic Container Registry), which is a fully managed Docker container registry that makes it easy for developers to store, manage, and deploy Docker container images.
4. AWS CodeBuild (Tag Info):
- After the Docker image is built and pushed to Amazon ECR, another AWS CodeBuild project is triggered to handle tagging. This project creates a tag for the Docker image, ensuring it is versioned properly and can be uniquely identified.
5. Commit Image Tag:
- The tag information is committed back to a Helm repository in AWS CodeCommit. Helm is a package manager for Kubernetes that helps you define, install, and upgrade even the most complex Kubernetes applications.
6. Sync Helm with Argo:
- The Helm repository changes trigger Argo CD to synchronize. Argo CD is a declarative, GitOps continuous delivery tool for Kubernetes. It continuously monitors the Git repository (in this case, the Helm repo) for changes and automatically updates the Kubernetes cluster based on the desired state defined in the Git repository.
7. Argo CD Deployment:
- Argo CD deploys the application to the Amazon EKS cluster. Amazon EKS is a managed Kubernetes service that makes it easy to run Kubernetes on AWS without needing to install and operate your own Kubernetes control plane or nodes.
8. Image Pull:
- During the deployment process, Kubernetes pulls the Docker image from Amazon ECR to run the application containers.
- Developer: Initiates the process by committing code changes.
- AWS CodeCommit: Hosts the source code and Helm repository.
- AWS CodePipeline: Orchestrates the CI/CD workflow.
- AWS CodeBuild: Builds and tags the Docker images.
- Amazon ECR: Stores Docker images.
- Helm: Manages Kubernetes application deployment configurations.
- Argo CD: Implements GitOps to ensure the Kubernetes cluster state matches the desired state defined in the Git repository.
- Amazon EKS: Runs the Kubernetes clusters where the application is deployed.
1. Commit Code: Developer pushes code to CodeCommit.
2. Build & Push: CodePipeline triggers CodeBuild to build a Docker image and push it to ECR.
3. Tag & Commit: Another CodeBuild project tags the Docker image and commits this tag to a Helm repository.
4. Sync & Deploy: Argo CD detects changes in the Helm repository and deploys the updated application to the EKS cluster.
This setup ensures a fully automated, continuous delivery pipeline that leverages GitOps principles to maintain consistency between the code repository and the deployed application state.
Here's a detailed guide on how to implement the project:
1. AWS Account: Ensure you have an AWS account with the necessary permissions.
2. AWS CLI: Install and configure the AWS CLI.
3. kubectl: Install kubectl to interact with your Kubernetes cluster.
4. Helm: Install Helm for managing Kubernetes applications.
5. Argo CD: Install Argo CD in your Kubernetes cluster.
6. Docker: Install Docker for building container images.
- Create a CodeCommit repository to store your application code.
aws codecommit create-repository --repository-name my-app-repo --repository-description "My application repository"
- Create a CodePipeline to automate the build and deployment process.
- Define the source stage to pull code from CodeCommit.
- Define the build stage to build the Docker image using CodeBuild.
- Define the deploy stage to push the Docker image to Amazon ECR.
- Create a CodeBuild project to build your Docker image.
- Define the buildspec.yml file in your repository with the following content:
version: 0.2
phases:
install:
runtime-versions:
docker: 18
pre_build:
commands:
- echo Logging in to Amazon ECR...
- aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com
build:
commands:
- echo Build started on `date`
- echo Building the Docker image...
- docker build -t my-app .
- docker tag my-app:latest $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/my-app:latest
post_build:
commands:
- echo Build completed on `date`
- echo Pushing the Docker image...
- docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/my-app:latest
- Create an ECR repository to store your Docker images.
aws ecr create-repository --repository-name my-app
- Create a Helm chart for your application and store it in a CodeCommit repository.
- Install Argo CD in your Kubernetes cluster.
kubectl create namespace argocd
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
- Access the Argo CD UI and log in.
kubectl port-forward svc/argocd-server -n argocd 8080:443
Open https://localhost:8080
in your browser and log in using the admin password.
- Create an Argo CD application that syncs with the Helm repository.
argocd app create my-app \
--repo https://git-codecommit.<region>.amazonaws.com/v1/repos/helm-repo \
--path my-app-chart \
--dest-server https://kubernetes.default.svc \
--dest-namespace default
- Sync the application to deploy it to the Kubernetes cluster.
argocd app sync my-app
- Set up an additional CodeBuild project to update the Helm values with the new image tag and push the changes to the Helm repository.
- Define a buildspec.yml file for this project:
version: 0.2
phases:
pre_build:
commands:
- echo Logging in to Amazon ECR...
- aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com
build:
commands:
- echo Build started on `date`
- export IMAGE_TAG=$(date +%Y%m%d%H%M%S)
- sed -i 's|<image-tag>|'$IMAGE_TAG'|g' helm/values.yaml
post_build:
commands:
- git config --global user.name "codebuild"
- git config --global user.email "codebuild@example.com"
- git add helm/values.yaml
- git commit -m "Update image tag to $IMAGE_TAG"
- git push
By following these steps, you will have set up an automated GitOps workflow that integrates AWS services with Helm and Argo CD. This workflow ensures that your application is automatically built, tested, and deployed whenever changes are made to the codebase. The use of Argo CD provides continuous delivery and synchronization of the desired state defined in your Git repositories with your Kubernetes cluster, offering a robust, automated solution for managing and deploying applications.
Happy Clouding!!!
Did you like this post?
If you did, please buy me coffee 😊
No comments yet.