Implementing Auto Sync Policies in Kubernetes (with Examples)

Updated: February 1, 2024 By: Guest Contributor Post a comment

Understanding how to maintain consistency across your deployments is paramount in Kubernetes, and the auto-sync policy feature offers a powerful mechanism to achieve this. Auto-sync simplifies the process of ensuring your environment configurations align with your source manifests. This tutorial will guide you through the steps of setting up auto-sync policies for your Kubernetes configurations using popular tools and practices.

Introduction

One pivotal advantage of using Kubernetes (K8s) is its ability to manage clusters efficiently. In a typical workflow, any changes to application configurations are performed in the manifest files, which are then applied to the environment. However, managing this flow manually can be error-prone. This is where auto-sync comes in handy.

Auto-sync operates by automatically checking your configured Git repository and ensuring the cluster state matches the manifests found in the repository. If it discovers a discrepancy, it syncs the cluster state to match the source manifests, based on a defined policy. Tools like Argo CD and Flux CD are prominent in the Kubernetes community for facilitating GitOps and come equipped with features for implementing auto-sync policies.

Prerequisites

  • A running Kubernetes cluster
  • Access to the cluster control plane with kubectl
  • A Git repository with Kubernetes manifests
  • Basic knowledge of Kubernetes resource management
  • Familiarity with GitOps principles

Auto-Sync with Argo CD

Argo CD is a declarative GitOps continuous delivery tool for Kubernetes. To set up an auto-sync policy with Argo CD, we will follow these steps:

1. Install Argo CD

kubectl create namespace argocd
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml

2. Access Argo CD UI

You can access the UI via the CLI by forwarding a local port to one of the Argo CD server ports.

 kubectl port-forward svc/argocd-server -n argocd 8080:443 

Now, open a web browser and visit http://localhost:8080. The default username is ‘admin’ and the password can be retrieved with:

kubectl get pods -n argocd -l app.kubernetes.io/name=argocd-server -o name | cut -d'/' -f 2 

3. Configure a Project and Application

Create a project in Argo CD which groups your applications. Then, define an application that links your Git repository to the project.

4. Define an Auto-Sync Policy

In the UI, navigate to your application, select ‘App Details’, then choose the ‘Edit’ option. Here, you will find sync policy options including manual, automated, and prune.

{
  "project": "default",
  "source": {
    "repoURL": "http://your-git-repo-url.git",
    "path": "path/to/your/manifests",
    "targetRevision": "HEAD"
  },
  "destination": {
    "server": "https://kubernetes.default.svc",
    "namespace": "your-namespace"
  },
  "syncPolicy": {
    "automated": {
      "prune": true,
      "selfHeal": true
    }
  }
}

Set ‘prune’ to ‘true’ to have Argo CD delete resources not in Git, and ‘selfHeal’ to automatically correct drift. Save your changes and the auto-sync policy will be in place.

Auto-Sync with Flux CD

Like Argo CD, Flux CD assists in keeping your clusters in sync with source manifests in Git. Here’s how to implement auto-sync policies with Flux CD.

1. Install Flux

curl -s https://toolkit.fluxcd.io/install.sh | sudo bash
flux check --pre
flux bootstrap git --repository=your-repo --branch=main --path=path/to/your/manifests --personal

2. Define Sources and Policies

Define a GitRepository source and a Kustomization for your manifests in your Git repository.

apiVersion: source.toolkit.fluxcd.io/v1beta1
kind: GitRepository
metadata:
  name: my-repo
  namespace: flux-system
spec:
  interval: 1m
  url: 'http://your-git-repo-url.git'
  ref:
    branch: main

---
apiVersion: kustomize.toolkit.fluxcd.io/v1beta1
kind: Kustomization
metadata:
  name: my-apps
  namespace: flux-system
spec:
  interval: 5m
  path: './path/to/your/manifests'
  prune: true
  sourceRef:
    kind: GitRepository
    name: my-repo
  healthChecks:
    - kind: Deployment
      name: my-app
      namespace: your-namespace

With these resources, Flux continuously reconciles your cluster state with your Git repository every 5 minutes and applies ‘prune’ to remove stale resources.

Conclusion

Through auto-sync policies, Kubernetes enables you to ensure your deployments are consistent and that your clusters are always up to date with the desired state described in your version-controlled configuration manifests. Remember that automation comes with the need for careful monitoring and testing strategies to prevent unintentional changes from causing issues in production environments.

Evaluating tools like Argo CD and Flux CD against your requirements is essential, as each comes with its own set of features and nuances. Whichever tool you choose, auto-sync policies offer an additional layer of robustness to your GitOps strategies and help you manage your applications more effectively in a Kubernetes ecosystem.