Create & import a cluster using CAPI providers

This guide goes over the process of creating and importing CAPI clusters with a selection of the officially certified providers.

Keep in mind that most Cluster API Providers are upstream projects maintained by the Kubernetes open-source community.

Prerequisites

  • Rancher Manager cluster with Rancher Turtles installed

  • Cluster API Providers: you can find a guide on how to install a provider using the CAPIProvider resource here

    • Infrastructure provider for AWS, this is an example of AWS provider installation, follow the provider documentation if some options need to be customized:

      ---
      apiVersion: v1
      kind: Namespace
      metadata:
        name: capa-system
      ---
      apiVersion: v1
      kind: Secret
      metadata:
        name: aws
        namespace: capa-system
      type: Opaque
      stringData:
        AWS_B64ENCODED_CREDENTIALS: xxx
      ---
      apiVersion: turtles-capi.cattle.io/v1alpha1
      kind: CAPIProvider
      metadata:
        name: aws
        namespace: capa-system
      spec:
        type: infrastructure
      yaml
    • If using RKE2 or Kubeadm, it’s required to have Bootstrap/Control Plane provider for RKE2(installed by default) or Bootstrap/Control Plane provider for Kubeadm, example of Kubeadm installation:

      ---
      apiVersion: v1
      kind: Namespace
      metadata:
        name: capi-kubeadm-bootstrap-system
      ---
      apiVersion: turtles-capi.cattle.io/v1alpha1
      kind: CAPIProvider
      metadata:
        name: kubeadm-bootstrap
        namespace: capi-kubeadm-bootstrap-system
      spec:
        name: kubeadm
        type: bootstrap
      ---
      apiVersion: v1
      kind: Namespace
      metadata:
        name: capi-kubeadm-control-plane-system
      ---
      apiVersion: turtles-capi.cattle.io/v1alpha1
      kind: CAPIProvider
      metadata:
        name: kubeadm-control-plane
        namespace: capi-kubeadm-control-plane-system
      spec:
        name: kubeadm
        type: controlPlane
      yaml

Create Your Cluster Definition

Before creating an AWS+RKE2 workload cluster, it is required to build an AMI for the RKE2 version that is going to be installed on the cluster. You can follow the steps in the RKE2 image-builder README to build the AMI.

We recommend you refer to the CAPRKE2 repository where you can find a samples folder with different CAPA+CAPRKE2 cluster configurations that can be used to provision downstream clusters.

We will use the internal one for this guide, however the same steps apply for external.

To generate the YAML for the cluster, do the following:

  1. Open a terminal and run the following:

    export CLUSTER_NAME=cluster1
    export NAMESPACE=capi-clusters
    export CONTROL_PLANE_MACHINE_COUNT=1
    export WORKER_MACHINE_COUNT=1
    export RKE2_VERSION=v1.31.4+rke2r1
    export AWS_NODE_MACHINE_TYPE=t3a.large
    export AWS_CONTROL_PLANE_MACHINE_TYPE=t3a.large
    export AWS_SSH_KEY_NAME="aws-ssh-key"
    export AWS_REGION="aws-region"
    export AWS_AMI_ID="ami-id"
    
    curl -s https://raw.githubusercontent.com/rancher/cluster-api-provider-rke2/refs/heads/main/examples/templates/aws/cluster-template.yaml | envsubst > cluster1.yaml
    bash
  2. View cluster1.yaml and examine the resulting YAML file. You can make any changes you want as well.

    The Cluster API quickstart guide contains more detail. Read the steps related to this section here.

  3. Create the cluster using kubectl

    kubectl create namespace ${NAMESPACE}
    kubectl create -f cluster1.yaml
    bash

After your cluster is provisioned, you can check functionality of the workload cluster using kubectl:

kubectl describe cluster cluster1
bash

Remember that clusters are namespaced resources. These examples provision clusters in the capi-clusters namespace, but you will need to provide yours if using a different one.

Mark Namespace or Cluster for Auto-Import

To automatically import a CAPI cluster into Rancher Manager, there are 2 options:

  1. Label a namespace so all clusters contained in it are imported.

  2. Label an individual cluster definition so that it’s imported.

Labeling a namespace:

kubectl label namespace capi-clusters cluster-api.cattle.io/rancher-auto-import=true
bash

Labeling an individual cluster definition:

kubectl label cluster.cluster.x-k8s.io -n default cluster1 cluster-api.cattle.io/rancher-auto-import=true
bash