Fleet Examples

repository·master·Indexed 41 days ago

https://github.com/rancher/fleet-examples

A collection of practical examples for GitOps-based application delivery using Fleet. Includes patterns for single-cluster and multi-cluster deployments using GitRepo resources, Helm, Kustomize, and raw manifests, as well as specific configurations for hardened clusters and Windows Helm deployments.

Tokens
6.3K
Snippets
21
Records
25
Agent score
50%

What's inside Fleet

  1. Overview of Fleet usage examples

    master

    This repository provides categorized examples of how to use Fleet for different deployment scenarios:

    Single Cluster Examples

    Deploy content to clusters with no per-cluster customizations. Useful for learning basic Git repo structuring.

    • manifests: Raw Kubernetes YAML.
    • helm: Using Helm charts.
    • helm-multi-chart: Deploying multiple charts from a single repo.
    • kustomize: Using Kustomize.
    • helm-kustomize: Using Kustomize to modify a third-party Helm chart.

    Multi-Cluster Examples

    Deploy a single Git repo to multiple clusters simultaneously, configuring the application differently for each target cluster.

    • manifests: Raw Kubernetes YAML with per-target customization.
    • helm: Helm with per-target customization.
    • helm-external: Using a Helm chart from a third-party source with per-target customization.
    • kustomize: Kustomize with per-target customization.
    • helm-kustomize: Using Kustomize to modify a third-party Helm chart for multi-cluster targets.
    • windows-helm: Using Helm for Windows cluster(s).
  2. Deploy the Multi-GitRepo frontend application

    master

    This example demonstrates how to deploy the Kubernetes sample guestbook frontend application using a Helm chart. The application is part of the larger multi-gitrepo example pattern.

    Deployment Details:

    • Application: Kubernetes sample guestbook frontend.
    • Packaging: Helm chart.
    • Target Namespace: fleet-helm-example.
    • Context: This is a single-cluster deployment using multiple Git repositories to manage different components of the stack.
  3. Update expected test files

    master

    If an example or the Fleet CLI version changes, the rendered output will differ from the baseline. To update the expected files:

    1. Run the test script (it will fail at the diff step but generate new output).
    2. Copy the fresh output from tests/output/ to tests/expected/.
    # Run once — will fail at the diff step but generate the new output
    tests/test.sh || true
    
    # Accept the new output as the expected baseline
    cp -r tests/output/* tests/expected/
  4. Run Fleet examples tests locally

    master

    The test suite renders every example using the Fleet CLI and compares the output against committed expected files in tests/expected/. It executes fleet apply, fleet target, and fleet deploy -d for each example.

    tests/test.sh
  5. Install the Fleet CLI

    master

    To run Fleet examples or tests locally, you must have the Fleet CLI installed and available in your PATH. You can download a release binary from the official Fleet releases page.

    Example installation for Linux (amd64):

    FLEET_VERSION=v0.14.3
    curl -sL "https://github.com/rancher/fleet/releases/download/${FLEET_VERSION}/fleet-linux-amd64" \
      -o ~/.local/bin/fleet && chmod +x ~/.local/bin/fleet
  6. Prepare downstream clusters for Windows Helm deployment

    master

    To deploy Windows-based Helm charts using Fleet, your downstream clusters (clusters.fleet.cattle.io) must be labeled with a windows.version key. The value for this label depends on your Windows node release type:

    • Windows Server SAC nodes: Use the SAC release number prepended with win (e.g., win2004 for 2004 SAC).
    • Windows Server LTSC nodes: Use the SAC release number that the LTSC is based on, prepended with win (e.g., win1809 for 2019 LTSC).

    This label allows Fleet to target specific clusters using clusterSelector in a GitRepo resource.

  7. Deploy applications with environment-specific customizations using Kustomize

    master

    You can use Fleet's GitRepo resource to deploy applications across multiple clusters while applying different configurations based on the target environment. By defining multiple targets within a single GitRepo object, you can use clusterSelector to map specific paths or configurations to clusters matching certain labels (e.g., env: dev, env: test, or env: prod).

    In this pattern, Kustomize is used to handle the environment-specific overrides, such as scaling deployments or changing service types, while Fleet manages the distribution to the correct clusters.

    kind: GitRepo
    apiVersion: fleet.cattle.io/v1alpha1
    metadata:
      name: kustomize
      namespace: fleet-default
    spec:
      repo: https://github.com/rancher/fleet-examples
      paths:
      - multi-cluster/kustomize
      targets:
      - name: dev
        clusterSelector:
          matchLabels:
            env: dev
    
      - name: test
        clusterSelector:
          matchLabels:
            env: test
    
      - name: prod
        clusterSelector:
          matchLabels:
            env: prod
  8. Deploy applications using Helm and Kustomize in a multi-cluster environment

    master

    This example demonstrates how to use Fleet to deploy a third-party Helm chart while applying environment-specific customizations via Kustomize.

    In this pattern, you define a GitRepo resource that points to a repository containing your Fleet manifests. Within that repository, you use Kustomize to override Helm chart values or configurations based on the target environment.

    This specific example deploys a guestbook application into the fleet-mc-helm-kustomize-example namespace with the following environment-specific logic:

    • Dev clusters: Deploys only the redis leader (omits followers).
    • Test clusters: Scales the front deployment to 3 replicas.
    • Prod clusters: Scales the front deployment to 3 replicas and sets the service type to LoadBalancer.
    kind: GitRepo
    apiVersion: fleet.cattle.io/v1alpha1
    metadata:
      name: helm-kustomize
      namespace: fleet-default
    spec:
      repo: https://github.com/rancher/fleet-examples
      paths:
      - multi-cluster/helm-kustomize
      targets:
      - name: dev
        clusterSelector:
          matchLabels:
            env: dev
    
      - name: test
        clusterSelector:
          matchLabels:
            env: test
    
      - name: prod
        clusterSelector:
          matchLabels:
            env: prod
  9. Deploy a multi-cluster Helm external application

    master

    This example demonstrates how to use a GitRepo resource to deploy a third-party Helm chart across multiple clusters with environment-specific customizations.

    In this pattern, a single GitRepo resource defines multiple targets. Each target uses a clusterSelector to identify which clusters should receive the deployment. This allows you to apply different configurations (via Helm values or other overrides) to different environments like dev, test, and prod based on cluster labels.

    Deployment Logic for this Example:

    • Dev clusters (labeled env: dev): Deploys only the redis leader (no followers).
    • Test clusters (labeled env: test): Scales the front deployment to 3 replicas.
    • Prod clusters (labeled env: prod): Scales the front deployment to 3 replicas and sets the service type to LoadBalancer.
    kind: GitRepo
    apiVersion: fleet.cattle.io/v1alpha1
    metadata:
      name: helm-external
      namespace: fleet-default
    spec:
      repo: https://github.com/rancher/fleet-examples
      paths:
      - multi-cluster/helm-external
      targets:
      - name: dev
        clusterSelector:
          matchLabels:
            env: dev
    
      - name: test
        clusterSelector:
          matchLabels:
            env: test
    
      - name: prod
        clusterSelector:
          matchLabels:
            env: prod
  10. Set up a local cluster for Fleet testing

    master

    Before running tests, you need to set up a local environment using k3d, kubectl, and the Fleet CRDs. Ensure the version of the CRDs matches your installed Fleet CLI version.

    Steps:

    1. Create a minimal k3d cluster.
    2. Merge the kubeconfig.
    3. Install Fleet CRDs via a downloaded tarball.
    4. Create necessary namespaces (fleet-local, fleet-default) and apply setup objects.
    FLEET_VERSION=v0.14.3
    
    # Create cluster
    k3d cluster create fleet-test --no-lb --wait --timeout 120s
    k3d kubeconfig merge fleet-test --kubeconfig-merge-default
    
    # Install Fleet CRDs (use the same version as your fleet CLI)
    curl -sL "https://github.com/rancher/fleet/releases/download/${FLEET_VERSION}/fleet-crd-${FLEET_VERSION#v}.tgz" \
      | tar -xz -O fleet-crd/templates/crds.yaml \
      | kubectl apply -f -
    
    # Create namespaces and cluster objects
    kubectl create namespace fleet-local
    kubectl create namespace fleet-default
    kubectl apply -f tests/setup-cluster.yaml
  11. Deploy a multi-cluster Helm application using GitRepo

    master

    You can use a GitRepo resource to deploy a Helm chart across multiple clusters with environment-specific customizations. By defining multiple targets within the spec.targets array, you can use clusterSelector to match specific clusters (e.g., based on labels like env: dev, env: test, or env: prod) and apply different configurations to each.

    kind: GitRepo
    apiVersion: fleet.cattle.io/v1alpha1
    metadata:
      name: helm
      namespace: fleet-default
    spec:
      repo: https://github.com/rancher/fleet-examples
      paths:
      - multi-cluster/helm
      targets:
      - name: dev
        clusterSelector:
          matchLabels:
            env: dev
    
      - name: test
        clusterSelector:
          matchLabels:
            env: test
    
      - name: prod
        clusterSelector:
          matchLabels:
            env: prod
  12. Deploy the Istio SUSE Application Collection using AppCo

    master

    To deploy the Istio SUSE Application Collection via the AppCo example, you must first prepare the authentication credentials in the target namespace.

    1. Create the Secret: In the istio-system namespace, create a secret named application-collection.
    2. Configure Credentials:
    3. Deploy via GitRepo: Apply a GitRepo resource pointing to the appco path in the rancher/fleet-examples repository to trigger the deployment.
    kind: GitRepo
    apiVersion: fleet.cattle.io/v1alpha1
    metadata:
      name: appcotest
      namespace: fleet-local
    spec:
      repo: https://github.com/rancher/fleet-examples
      branch: master
      paths:
      - appco