DEV Community

Cover image for GitOps and FluxCD: Continuous Deployment for Kubernetes on AWS
Sidra Saleem for SUDO Consultants

Posted on • Originally published at sudoconsultants.com

GitOps and FluxCD: Continuous Deployment for Kubernetes on AWS

Introduction

In today's fast-paced software development world, efficient deployment and management of applications are critical to business success. Kubernetes, the popular container orchestration tool, simplifies scaling, deployment, and management of containerized applications. However, managing Kubernetes deployments can become complex as the infrastructure grows. This is where GitOps comes into play. GitOps is a modern approach to continuous deployment that uses Git repositories as the source of truth for infrastructure and application deployment.

In this article, we will explore how FluxCD can be implemented on AWS to leverage GitOps for continuous deployment in Kubernetes environments. We will cover the steps required for implementation, best practices, troubleshooting, and more.

Background

GitOps is an operational model that treats Git repositories as the source of truth for both infrastructure and application deployments. It leverages tools like FluxCD, ArgoCD, and others to continuously monitor and synchronize the Kubernetes cluster with the desired state defined in the Git repository.

Key Concepts

  1. GitOps: The practice of using Git repositories to manage and deploy Kubernetes infrastructure and applications.
  2. FluxCD: A Kubernetes-native continuous delivery tool that automates the deployment of applications to Kubernetes clusters by syncing Kubernetes manifests from a Git repository.
  3. Kubernetes: A container orchestration platform that automates the deployment, scaling, and management of containerized applications.
  4. Continuous Deployment (CD): The practice of automatically deploying code changes to production as soon as they pass testing and validation.

Technical Implementation

High-Level Architecture

In a GitOps workflow with FluxCD on AWS, the architecture typically consists of:

  • AWS EKS Cluster: The Kubernetes cluster running on AWS managed by Amazon EKS.
  • Git Repository: A Git repository (e.g., GitHub, GitLab) containing Kubernetes manifests or Helm charts for application deployment.
  • FluxCD: The tool that watches the Git repository for changes and syncs those changes to the Kubernetes cluster.

The flow of deployment looks like this:

Image description

Prerequisites

Before starting the implementation, ensure the following prerequisites are met:

  1. AWS Account: Set up AWS credentials and permissions for managing EKS and other AWS resources.
  2. kubectl: Install kubectl CLI to interact with your Kubernetes cluster.
  3. AWS CLI: Install the AWS CLI for managing AWS resources.
  4. FluxCD: Install FluxCD CLI on your local machine.

Step-by-Step Implementation

1. Set Up an EKS Cluster on AWS

You can create an EKS cluster using AWS CLI or the AWS Management Console. Here’s how you can do it using AWS CLI:

aws eks create-cluster \
--name my-cluster \
--role-arn arn:aws:iam::<aws-account-id>:role/eks-cluster-role \
--resources-vpc-config subnetIds=<subnet-ids>,securityGroupIds=<security-group-ids>

Once the cluster is created, update your kubeconfig to interact with the EKS cluster:

aws eks update-kubeconfig --name my-cluster

2. Install FluxCD

FluxCD can be installed using the Flux CLI. To install Flux, run the following commands:

curl -s https://fluxcd.io/install.sh | sudo bash

Verify the installation by checking the Flux version:

flux --version

3. Connect FluxCD to Your Git Repository

Create a Git repository that holds your Kubernetes manifests or Helm charts. FluxCD will watch this repository for changes.

  • Generate a personal access token in GitHub (or your Git provider).
  • Create a secret for your Git credentials:
kubectl create secret generic flux-git-auth \
--from-literal=token=<your-token> \
--namespace=flux-system
  • Configure Flux to point to your repository:
flux create source git flux-repo \
--url=https://github.com/<your-org>/<repo> \
--branch=main \
--secret-ref=flux-git-auth \
--namespace=flux-system

4. Create and Apply FluxCD Configuration

Now that Flux is connected to your Git repository, configure Flux to deploy your Kubernetes manifests.

flux create kustomization my-app \
--source=GitRepository/flux-repo \
--path="./k8s" \
--prune=true \
--interval=10m \
--namespace=flux-system

5. Verify Deployment

FluxCD will automatically apply any changes from your Git repository to the Kubernetes cluster. To check the status of FluxCD:

flux get kustomizations

If your Kubernetes manifests are correct, Flux will sync the changes, and your applications will be deployed automatically.

Troubleshooting

  • Flux not syncing changes: Ensure the Flux system is running with kubectl get pods -n flux-system. If Flux isn’t syncing, check the Flux logs:
kubectl logs -n flux-system deployment/flux
  • Permission issues: Ensure that Flux has the right permissions to interact with your Kubernetes resources and your Git repository.

Best Practices

  1. Use Helm: FluxCD integrates well with Helm. It’s a good practice to use Helm charts for more complex applications, as they offer flexibility and reusability.
  2. Immutable Infrastructure: Define your desired infrastructure as code in Git. This ensures that the entire application lifecycle can be managed by GitOps principles.
  3. Security: Use encryption for Git secrets, and ensure proper access control to the Git repository.

Example 1: Deploying a Sample Web Application

Imagine you're deploying a simple web application on Kubernetes using FluxCD. Your Git repository has a deployment.yaml file for the app.

apiVersion: apps/v1
kind: Deployment
metadata:
name: my-web-app
spec:
replicas: 2
selector:
matchLabels:
app: my-web-app
template:
metadata:
labels:
app: my-web-app
spec:
containers:
- name: web-app
image: my-web-app:latest
ports:
- containerPort: 80

FluxCD will continuously monitor the repository for any changes. Once the deployment.yaml file is updated, it automatically deploys the updated application on EKS.

Future Outlook

The future of GitOps with FluxCD is promising, especially with the increasing use of Kubernetes in production environments. Here are a few trends to watch:

  • Automation and AI: AI-based tools for automated rollback and optimization of Kubernetes resources could become more mainstream.
  • Enhanced Observability: Integration of FluxCD with monitoring tools like Prometheus and Grafana to provide real-time visibility of GitOps workflows.

Conclusion

GitOps with FluxCD offers a powerful, efficient, and automated approach to continuous deployment in Kubernetes environments. It leverages Git as the source of truth, allowing developers to manage infrastructure and application deployment in a declarative and version-controlled way. AWS and FluxCD together create a robust platform for scaling Kubernetes applications, ensuring reliability and agility. By following best practices and avoiding common pitfalls, organizations can realize the full potential of GitOps.

Top comments (0)