How do I: Setup GitOps with Rancher’s Fleet

Part 1: Introduction to GitOps with Rancher Fleet – The Foundation of Automated Infrastructure

What is GitOps?

GitOps is a modern approach to managing and deploying infrastructure and applications in a declarative manner using Git as the single source of truth. In a GitOps workflow, all changes to infrastructure are made via Git pull requests, which are automatically applied to the infrastructure by continuous deployment (CD) agents.

Rancher Fleet, a GitOps engine developed by SUSE, is specifically designed to manage Kubernetes clusters at scale. Fleet’s architecture allows you to manage thousands of Kubernetes clusters, enabling consistent deployments across environments.

Benefits of GitOps with Rancher Fleet

  • Scalability: Rancher Fleet is designed to scale, making it ideal for large deployments across multiple clusters.
  • Consistency: By using Git as the source of truth, every deployment is consistent and repeatable.
  • Automation: Rancher Fleet automates the entire deployment process, reducing human error and speeding up the deployment cycle.
  • Security: All changes are tracked in Git, providing a clear audit trail and enabling easy rollback in case of issues.

Use Cases

  • Multi-Cluster Management: Deploying and managing applications across multiple Kubernetes clusters from a single Git repository.
  • Disaster Recovery: Ensuring that your infrastructure can be restored to a known good state using Git history.
  • Compliance: Maintaining compliance with organizational policies by ensuring that all changes are tracked and auditable.

Rancher Fleet Architecture Overview

Core Components

  • Git Repository: The central repository where all the desired state configurations are stored.
  • Rancher Fleet Manager: The control plane component responsible for managing clusters and applying the desired state from the Git repository.
  • Fleet Agents: Deployed on each managed cluster, these agents apply the desired state and report back to the Fleet Manager.
  • CI/CD Integration: While Rancher Fleet focuses on CD, it integrates seamlessly with CI tools like Jenkins, GitLab CI, and others to automate testing and code validation.
Visual representation showing the Git repository, Fleet Manager, Fleet Agents, and their interaction courtesy of https://fleet.rancher.io

Setting Up a GitOps Environment with Rancher Fleet

Tools and Technologies

  • Rancher Fleet: Install Fleet as part of a Rancher installation or directly into your Kubernetes cluster.
  • Git: Set up a Git repository to store your Kubernetes manifests.
  • Kubernetes Cluster: Ensure you have a working Kubernetes cluster to deploy Fleet and manage workloads.

Keep in mind Rancher Fleet is installed by default with Rancher Manger installations. The Basic Setup below is for non-Rancher managed clusters.

Basic Setup without Rancher

helm repo add rancher-fleet https://rancher.github.io/fleet-helm 
helm install rancher-fleet rancher-fleet/fleet --namespace fleet-system --create-namespace

Install Rancher Fleet:

  • Ensure Rancher is installed on your Kubernetes cluster.
    • Use this Quickstart to get k3s and Rancher installed

Initialize a GitOps Project:

  • Create a new Git repository for your Kubernetes manifests.
  • Organize your repository into directories for different environments (e.g., dev, staging, production).

Connect Fleet to Your Git Repository:

Create a GitRepo Custom Resource (CR) in Fleet to link the Git repository:

apiVersion: fleet.cattle.io/v1alpha1 
kind: GitRepo 
metadata: 
  name: my-git-repo
  namespace: fleet-default
spec:
  repo: "https://github.com/myorg/my-gitops-repo.git"
  branch: "main"
  paths:
  - "clusters/dev"

Apply the Configuration:

Apply the GitRepo resource to your cluster:

kubectl apply -f my-gitrepo.yaml

Diagram: Basic GitOps Setup with Rancher Fleet

Illustration showing Git repo, Fleet Manager, and Fleet Agents interacting.

4. Common Challenges and Best Practices

Potential Pitfalls

  • Misconfigurations: Ensure all Kubernetes manifests are valid before committing to Git to avoid deployment failures.
  • Repository Sprawl: As your infrastructure grows, so will your repository. Organize it well to avoid complexity.

Best Practices

  • Repository Structure: Use a hierarchical structure for environments and applications.
  • Security: Implement Git commit signing and use Fleet’s RBAC features to control access to deployments.

5. Wrapping it up!

By the end of this post, you should have a solid understanding of GitOps principles and the architecture of Rancher Fleet. In the next part, we’ll dive into building and managing a GitOps pipeline using Fleet.