Deploying Your First Application with Flux CD

Flux CD is a powerful tool that enables continuous delivery in a Kubernetes environment. With automated GitOps practices, it simplifies the deployment process and enhances software delivery quality. If you've set up Flux CD, you're now ready to deploy your first application. In this article, we will walk you through a simple tutorial that demonstrates deploying a sample application using Flux CD, showcasing its core functionalities.

Prerequisites

Before we dive into the deployment process, ensure you have the following prerequisites:

  • A Kubernetes cluster up and running (you can use Minikube, GKE, EKS, or any other managed Kubernetes service).
  • Flux CD installed and configured in your Kubernetes cluster.
  • Git repository set up to hold your deployment configurations (we recommend using GitHub or GitLab).
  • kubectl command-line tool installed and configured to interact with your Kubernetes cluster.
  • Basic knowledge of how Kubernetes manifests work.

Step 1: Prepare Your Git Repository

First things first, let’s prepare the Git repository that will hold your application's configuration. If you haven’t created it yet, do so as follows, or use an existing one:

  1. Create a new Git repository.
  2. In this repository, you’ll store your Kubernetes manifests. For this example, let’s create a directory structure like this:
my-app/
├── kustomization.yaml
├── deployment.yaml
└── service.yaml

deployment.yaml

Here’s a simple example of a deployment.yaml file to deploy an Nginx application.

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

service.yaml

Next, define a service.yaml to expose your Nginx application.

apiVersion: v1
kind: Service
metadata:
  name: my-nginx
spec:
  type: LoadBalancer
  ports:
    - port: 80
      targetPort: 80
  selector:
    app: my-nginx

kustomization.yaml

Finally, create the kustomization.yaml file that will help Flux identify your resources.

resources:
  - deployment.yaml
  - service.yaml

Once you have these files set up, commit and push them to your Git repository:

git add .
git commit -m "Add Nginx deployment and service"
git push origin main

Step 2: Configure Flux to Monitor the Repository

Next, we’ll need to configure Flux to monitor your Git repository and apply changes automatically. Use the following commands for setting up the Flux Git repository source:

Create a GitRepository Resource

Create a YAML file named gitrepository.yaml in your local directory:

apiVersion: source.toolkit.fluxcd.io/v1beta1
kind: GitRepository
metadata:
  name: my-app
  namespace: flux-system
spec:
  interval: 1m
  url: git@github.com:<your-github-username>/my-app.git
  secretRef:
    name: flux-system

Remember to replace <your-github-username> with your actual GitHub username and adjust the parameters according to your setup.

Apply the GitRepository Resource

Now apply the gitrepository.yaml to your cluster:

kubectl apply -f gitrepository.yaml

Step 3: Define a Kustomization Resource

Next, we will create a kustomization.yaml in the same way to tell Flux how to manage the resources defined in the Git repository. Create a file named kustomization.yaml:

apiVersion: kustomize.toolkit.fluxcd.io/v1beta1
kind: Kustomization
metadata:
  name: my-app
  namespace: flux-system
spec:
  interval: 1m
  path: "./my-app"
  prune: true
  sourceRef:
    kind: GitRepository
    name: my-app
    namespace: flux-system

Apply this kustomization.yaml to your cluster:

kubectl apply -f kustomization.yaml

Step 4: Verify the Deployment

After setting things up, Flux will start synchronizing your application. You can check the status of your resources by running:

kubectl get all -n flux-system

You should see Flux managing your Deployment and Service. To check the status of your Kustomization, you can run:

kubectl get kustomizations -n flux-system

Step 5: Access Your Application

Once everything is deployed and running, you can access your application. If you’ve set the service type to LoadBalancer and your Kubernetes provider supports it, you can find the external IP address of your Nginx service with:

kubectl get svc my-nginx

Navigate to the provided external IP in your web browser, and you should see the Nginx welcome page. If your service type is ClusterIP, you could also use port-forwarding:

kubectl port-forward svc/my-nginx 8080:80

Then access your application at http://localhost:8080.

Conclusion

Congratulations! You have successfully deployed your first application using Flux CD in a Kubernetes environment. This simple example demonstrated the core functionalities of Flux CD, including monitoring a Git repository, automatic synchronization, and basic resource management.

As you continue your journey with Flux CD and Kubernetes, consider exploring advanced features, such as automated rollback, integration with CI/CD pipelines, and managing more complex applications. Happy deploying!