1. The Advantages of Multi-Cluster Tekton Builds
Thanks to Kubernetes, Tekton already has good elasticity and can support large-scale builds. At the same time, developing Tasks mainly uses Yaml and Shell, which widens the range of scenarios Tekton can adapt to.

The figure above is a sketch of Tekton across multiple clusters. Why does Tekton need multi-cluster pipeline execution?
- Kubernetes clusters that can change at any time. A single Kubernetes cluster cannot satisfy operations requirements, since the cluster cannot be changed at will. With multiple clusters, some clusters can be taken offline for maintenance.
- Larger-scale builds. CI consumes a great deal of CPU, memory, and IO resources, and can easily overwhelm a node or even a cluster. Multiple clusters effectively share the load pressure and improve availability.
- Business isolation. Different businesses have different requirements for code security level, build speed, and build environment. Multiple clusters can provide isolated environments and customized pipeline services.
2. Kubernetes Cluster Federation
Kubernetes Cluster Federation is abbreviated KubeFed. The biggest change in KubeFed v2 compared to v1 is that the API Server was removed, and the extension of Federated Resources is done through the CRD mechanism. The KubeFed Controller manages these CRDs and implements functions such as synchronizing Resources and orchestrating across clusters, achieving modularity and customization. Below is the community architecture diagram:

KubeFed configures two types of information:
- Type configuration, which declares the API types KubeFed handles
- Cluster configuration, which declares which clusters KubeFed manages
Type configuration has three basic concepts:
- Templates, which define the template description of a resource in a cluster
- Placement, which defines which clusters a resource needs to be distributed to
- Overrides, which define the field contents in a cluster that need to override the Templates
In addition, more advanced features can be achieved through Status, Policy, and Scheduling:
- Status collects the status of distributed resources in each cluster
- Policy provides policy control over which clusters a resource may be assigned to
- Scheduling allows resources to migrate replicas across clusters
Besides this, KubeFed also provides MultiClusterDNS, which can be used for service discovery between multiple clusters.
3. Federating Kubernetes Clusters
3.1 Preparing Clusters and Configuring Contexts
Here two clusters are deployed: dev1 serves as the primary cluster, used as the control plane for Tekton and not running pipeline tasks; dev2 serves as the secondary cluster, used to execute Tekton pipeline tasks.
- Prepare two clusters
Primary cluster dev1
| |
| |
Secondary cluster dev2
| |
- Configure the Contexts of all clusters on the primary cluster (the cluster Apiserver entry points are required to be on one network and directly connectable), in order to add the secondary cluster
Here the name in contexts cannot contain special characters such as @, otherwise an error is reported when joining. This is because the name is used to create a Secret, and it must conform to Kubernetes naming conventions.
Place the kubeconfig of the primary cluster dev1 at ~/.kube/config-1, and modify the name and other information, in the following format:
| |
Place the kubeconfig of the secondary cluster dev2 at ~/.kube/config-2, and modify the name and other information, in the following format:
| |
- Merge the kubeconfigs
| |
- View the added cluster Contexts
| |
- Switch to the primary cluster dev1
| |
3.2 Installing KubeFed on the Primary Cluster
- Install KubeFed using Helm
| |
- View the workloads
| |
3.3 Installing kubefedctl on the Primary Cluster
Run the commands:
| |
3.4 Adding Clusters
Run the commands on the primary cluster to add both dev1 and dev2 to the primary cluster dev1.
| |
| |
View the cluster list:
| |
3.5 Testing Whether the Clusters Are Federated Successfully
- View the resources that are already federated
After installing KubeFed, many common resources are already federated, which can be seen in the CRDs:
| |
The resources for which federation is already enabled can also be seen in federatedtypeconfigs.
| |
- Create a federated Namespace
Namespace-level resources need to be placed under a federated Namespace, otherwise the Controller reports an error when distributing resources.
| |
- Create a federated Deployment on the primary cluster
A common Deployment looks like this:
| |
A federated Deployment looks like this.
| |
When writing a FederatedDeployment, three fields need attention
- overrides, the field attributes that need to be overridden per cluster. Here the replica count on dev1 is changed to 2, and the replica count on dev2 is changed to 3.
- placement, the list of clusters the resource needs to be placed in. Here it is placed in the two clusters dev1 and dev2.
- template, the template of the resource. Here it is the remainder of the Deployment after removing apiVersion and kind.
- Verify whether the resources are distributed successfully
On the dev1 cluster
| |
On the dev2 cluster
| |
4. Federating Tekton’s CRD Resources
4.1 Installing Tekton
Tekton needs to be installed on all clusters
| |
Since the Tekton community uses images from gcr.io, some host environments may not be able to pull them. I backed them up on Dockerhub, and the relevant yaml can be found here, https://github.com/shaowenchen/image-syncer/tree/main/tekton .
4.2 Federating Tekton’s CRDs
When installing KubeFed, common resources such as Deployment and Secret are federated by default, but user-defined CRDs need to be enabled manually.
Run the commands:
| |
Taking taskruns as an example, kubefedctl enable taskruns.tekton.dev automatically creates two resources:
- customresourcedefinition.apiextensions.k8s.io/federatedtaskruns.types.kubefed.io, the federated CRD resource federatedtaskruns
- federatedtypeconfig.core.kubefed.io/taskruns.tekton.dev, which creates a resource of type federatedtypeconfig named taskruns in the kube-federation-system namespace to enable resource distribution
4.3 Editing the Newly Created Federated CRD Resource to Add a Field
Skipping this step causes the content of the CR resources synchronized to the secondary cluster to be empty. This is because the CRD resource federated by kubefedctl enable lacks the template field.
Run the command:
| |
At the same level as overrides and placement, simply add the template content shown in the example below.
| |
If this is not clear enough, you can refer to https://github.com/shaowenchen/demo/tree/master/tekton-0.24.1-kubefed for modifications. If you are also using version 0.24.1, you can kubectl apply these CRD resources directly.
4.4 Testing Distribution of Tekton Objects Across Multiple Clusters
To avoid pasting a large amount of yaml here, the Task resource is created directly on the secondary cluster in advance, rather than using FederatedTask for distribution.
- Create a Task on the secondary cluster
| |
- Create a FederatedTaskRun resource on the primary cluster dev1 to distribute to the secondary cluster dev2
| |
- View the Tekton Taskrun task on the secondary cluster dev2
| |
5. Summary
This article mainly introduces and puts into practice the use of KubeFed to manage multiple clusters and federate Tekton CRD resources.
With Tekton across multiple clusters, using the primary cluster to manage resources and the secondary clusters to execute pipelines can effectively balance the load, increase the concurrent execution capacity of pipelines, and improve the maintainability of the CICD system.
The KubeFed here is mainly used to store and distribute Tekton object resources. If you build your own encoding, this can be done with data storage plus a loop controller, but using the KubeFed Controller achieves it quickly while avoiding many potential problems. KubeFed is very well suited to cross-cluster resource distribution.
