Deploy fleet packages

This page explains how to use Config Sync fleet packages to deploy Kubernetes resources across clusters that are registered to a fleet. After you create and deploy a fleet package, when you add a new cluster to your fleet, the fleet package automatically deploys the Kubernetes configuration files in the Git repository to the new cluster.

A FleetPackage is a declarative API to deploy Kubernetes raw manifests to a fleet of clusters. Any Kubernetes resources that you want to deploy with a fleet package must be already-hydrated (WET).

Before you begin

  1. Create, or make sure you have access to, a Git repository with the Kubernetes resources that you want to deploy across a fleet.

  2. Install and initialize the Google Cloud CLI, which provides the gcloud, and nomos commands. If you use Cloud Shell, the Google Cloud CLI is pre-installed. If you previously installed the Google Cloud CLI, get the latest version by running gcloud components update.

  3. Enable the Google Kubernetes Engine (GKE) Enterprise edition API, the ConfigManagement API, and the ConfigDelivery API:

    gcloud services enable anthos.googleapis.com anthosconfigmanagement.googleapis.com configdelivery.googleapis.com
    
  4. Set a default location:

    gcloud config set config_delivery/location us-central1
    
  5. Set a default project:

    gcloud config set project PROJECT_ID
    

    Replace PROJECT_ID with the project ID of the fleet host project.

  6. Ensure that your clusters are registered to a fleet.

  7. Use Cloud Build repositories to create a connection to a supported provider like GitHub or GitLab. When you use a fleet package, you need to set up Cloud Build only once per repository that you want to sync.

Review cluster requirements

Before you install Config Sync on your cluster, review cluster configuration recommendations and requirements.

Prepare your environment

To prepare your environment for Config Sync fleet packages, complete the following steps:

  1. Grant the required IAM roles to the user registering the cluster.

  2. Ensure that your GKE clusters are enrolled in the Rapid channel so that your clusters are on a version of Config Sync that supports fleet packages.

Install Config Sync

You can install Config Sync with either the Google Cloud console or Google Cloud CLI.

Console

To install Config Sync, all clusters must be registered to a fleet. When you install Config Sync in the Google Cloud console, selecting individual clusters automatically registers those clusters to your fleet.

  1. In the Google Cloud console, go to the Config page under the Features section.

    Go to Config

  2. Click Install Config Sync.

  3. Select Auto-upgrades (Preview).

  4. Under Installation options, select Install Config Sync on entire fleet (recommended).

  5. Click Install Config Sync. In the Settings tab, after a few minutes, you should see Enabled in the Status column for the clusters in your fleet.

gcloud

  1. Enable the ConfigManagement fleet feature:

    gcloud beta container fleet config-management enable
    
  2. To enable Config Sync with auto-upgrades, create a file named apply-spec.yaml with the following content:

    applySpecVersion: 1
    spec:
      upgrades: auto
      configSync:
        enabled: true
    
  3. Apply the apply-spec.yaml file:

    gcloud beta container fleet config-management apply \
        --membership=MEMBERSHIP_NAME \
        --config=apply-spec.yaml
    

    Replace MEMBERSHIP_NAME with the fleet membership name that you chose when you registered your cluster. To find the membership name, run the gcloud container fleet memberships list command.

Create a service account for Cloud Build

Fleet packages use Cloud Build to fetch the Kubernetes resources from your Git repository and deploys to your clusters. Cloud Build requires a service account that has the permissions to run this job. To create the service account and grant the required permissions, complete the following steps:

  1. Create the service account:

    gcloud iam service-accounts create "SERVICE_ACCOUNT_NAME"
    

    Replace SERVICE_ACCOUNT_NAME with a name for the service account. The name must be an alphanumeric ID between 6 and 30 characters, for example my-service-account. After you create a service account, you cannot change its name.

  2. Add an IAM policy binding for the Resource Bundle Publisher role:

    gcloud projects add-iam-policy-binding PROJECT_ID \
       --member="serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com" \
       --role='roles/configdelivery.resourceBundlePublisher'
    

    If prompted, select None as the condition for the policy.

  3. Add an IAM policy binding for the Logs Writer role:

    gcloud projects add-iam-policy-binding PROJECT_ID \
       --member="serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com" \
       --role='roles/logging.logWriter'
    

    If prompted, select None as the condition for the policy.

Create a fleet package

To create a fleet package, you define a FleetPackage spec that points to the repository with your Kubernetes resources that you connected to Cloud Build. Then you apply the FleetPackage which fetches the resources from Git and deploys them across the fleet.

  1. Create a file named fleetpackage-spec.yaml with the following content:

    resourceBundleSelector:
      cloudBuildRepository:
        name: projects/PROJECT_ID/locations/us-central1/connections/CONNECTION_NAME/repositories/REPOSITORY_NAME
        tag: TAG
        serviceAccount: projects/PROJECT_ID/serviceAccounts/SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com
        path: CONFIG_FILE_PATH
    target:
      fleet:
        project: projects/PROJECT_ID
    rolloutStrategy:
      rolling:
        maxConcurrent: MAX_CLUSTERS
    

    Replace the following:

    • CONNECTION_NAME: the name that you chose when you connected your Git host to Cloud Build. You can view all Cloud Build connections in your project by running gcloud builds connections list or by opening the Repositories page in the Google Cloud console:

      Open the Repositories page

    • REPOSITORY_NAME: the name of your repository. This must be identical to the value entered when you set up your Cloud Build connection.

    • TAG: the Git tag of your repository. The format must be the semantic version, for example v1.0.0.

    • CONFIG_FILE_PATH: the path to your Kubernetes resources in the repository. If your files are in the root of the repository, you can omit this field.

    • MAX_CLUSTERS: the maximum number of clusters to deploy Kubernetes resources at one time. For example, if you set this to 1, resource bundles deploy to one cluster at a time.

      For a complete list of all fields you can configure, see the FleetPackage reference documentation.

  2. Create the fleet package:

    gcloud alpha container fleet packages create FLEET_PACKAGE_NAME \
        --source=fleetpackage-spec.yaml
    

    Replace FLEET_PACKAGE_NAME with a name for your fleet package rollout.

  3. Verify that the fleet package was created:

    gcloud alpha container fleet packages list
    

    The output lists the status of the build trigger. After a few seconds, the MESSAGE field updates with output similar to the following:

    MESSAGES: Build status: WORKING. The release is still being built; see the build status on the following page:
    

    You can click the link provided to view the streaming logs for the Cloud Build job. It can take a few minutes for Cloud Build to process the build trigger.

    If the build trigger is successful, the fleet package starts rolling out the Kubernetes resources across your fleet.

  4. When the build trigger successfully completes, the output of gcloud alpha container fleet packages list is similar to the following:

    NAME: my-fleet-package
    STATE: ACTIVE
    CREATE_TIME: 2024-07-09T15:15:56
    ACTIVE_ROLLOUT: rollout-20240709-153621
    LAST_COMPLETED_ROLLOUT:
    MESSAGES:
    

    The fleet package starts rolling out the Kubernetes resources across your fleet.

Now that you've deployed a fleet package, when you add a new cluster to your fleet, the Kubernetes resources defined in the fleet package automatically deploy to the new cluster.

Update a fleet package

You can update a fleet package to change settings such as the rollout strategy. To update the Kubernetes resources that the fleet package deploys, update the tag field to pull from a different Git tag.

To update a fleet package, complete the following steps:

  1. Update your FleetPackage spec.

  2. Update the fleet package to start a new rollout:

    gcloud alpha container fleet packages update FLEET_PACKAGE_NAME \
        --source=fleetpackage-spec.yaml
    

    It can take a few minutes before the change is picked up and starts rolling out to your clusters.

Use labels to deploy to different clusters

Labels are key-value pairs that you attach to objects. Fleet packages supports only fleet membership labels. GKE cluster labels are not supported.

You can use labels to deploy a fleet package to a subset of clusters in your fleet.

Add membership labels

To add a membership label, complete the following steps:

  1. Get a list of memberships in the fleet:

    gcloud container fleet memberships list
    
  2. Add a label to the membership:

    gcloud container fleet memberships update MEMBERSHIP_NAME \
        --update-labels=KEY=VALUE
    

    Replace the following:

    • MEMBERSHIP_NAME: the name of the cluster registered to the fleet.
    • KEY and VALUE: the label to add to the membership. If a label exists, its value is modified. Otherwise, a new label is created. Keys must start with a lowercase character and contain only hyphens (-), underscores (_), lowercase characters, and numbers. Values must contain only hyphens (-), underscores (_), lowercase characters, and numbers.

    Repeat this command for each membership to which you want to add a label.

Deploy to a subset of clusters

You can deploy to a subset of clusters by specifying the target.fleet.selector.matchLabels field with your key-value pair. For example, if you set matchLabels as country: "us", the fleet package service deploys your resources only to clusters with the label country that matches "us".

To deploy a fleet package to a subset of clusters, complete the following steps:

  1. Create or update your FleetPackage spec with the label selector:

    resourceBundleSelector:
      cloudBuildRepository:
        name: projects/PROJECT_ID/locations/us-central1/connections/CONNECTION_NAME/repositories/REPOSITORY_NAME
        tag: TAG
        serviceAccount: projects/PROJECT_ID/serviceAccounts/SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com
        path: CONFIG_FILE_PATH
    target:
      fleet:
        project: projects/PROJECT_ID
        selector:
          matchLabels:
            KEY: "VALUE"
    rolloutStrategy:
      rolling:
        maxConcurrent: MAX_CLUSTERS
    
  2. Create or update the fleet package:

    Create a fleet package

    gcloud alpha container fleet packages create FLEET_PACKAGE_NAME \
        --source=fleetpackage-spec.yaml
    

    Update a fleet package

    gcloud alpha container fleet packages update FLEET_PACKAGE_NAME \
        --source=fleetpackage-spec.yaml
    

Deploy variant resources to clusters

Variants are different versions of a resource. These resources might have different values depending on the location, project, or name of the cluster. You can deploy variant resources to different clusters by specifying the variantsPattern and variantNameTemplate fields.

You can use membership labels or other membership metadata like location, project, or name to match variants.

To deploy a fleet package with variants, complete the following steps:

  1. Create or update your FleetPackage spec with the variant details:

    resourceBundleSelector:
      cloudBuildRepository:
        name: projects/PROJECT_ID/locations/us-central1/connections/CONNECTION_NAME/repositories/REPOSITORY_NAME
        tag: TAG
        serviceAccount: projects/PROJECT_ID/serviceAccounts/SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com
        path: CONFIG_FILE_PATH
        variantsPattern: VARIANT_PATTERN
    target:
      fleet:
        project: projects/PROJECT_ID
    rolloutStrategy:
      rolling:
        maxConcurrent: MAX_CLUSTERS
    target:
      fleet:
        project: projects/PROJECT_ID
     variantSelector:
      variantNameTemplate: VARIANT_NAME_TEMPLATE
    

    Replace the following:

    • VARIANT_PATTERN: the pattern for the variant, for example "\*.yaml" or "us-*".
    • VARIANT_NAME_TEMPLATE : A template string that refers to variables containing cluster membership metadata such as location, project, name, or label to determine the name of the variant for a target cluster. For more examples, see the FleetPackage reference documentation.
  2. Create or update the fleet package:

    Create a fleet package

    gcloud alpha container fleet packages create FLEET_PACKAGE_NAME \
        --source=fleetpackage-spec.yaml
    

    Update a fleet package

    gcloud alpha container fleet packages update FLEET_PACKAGE_NAME \
        --source=fleetpackage-spec.yaml
    

Delete a fleet package

Deleting a fleet package also deletes the following resources:

  • The Kubernetes resources deployed on your clusters
  • The fleet package rollout history

To delete a fleet package, run the following command:

gcloud alpha container fleet packages delete FLEET_PACKAGE_NAME --force

Troubleshoot

To find methods for diagnosing and resolving errors related to Cloud Build, see Troubleshooting build errors.

What's next