How to Use Vultr Cluster API to Manage Kubernetes Clusters

Updated on November 14, 2024
How to Use Vultr Cluster API to Manage Kubernetes Clusters header image

Introduction

The Vultr Cluster API is a set of RESTful endpoints designed to manage and automate the deployment, scaling, and lifecycle of clusters, particularly Kubernetes clusters, on Vultr's cloud platform. Through the Cluster API, developers can programmatically create new clusters, configure node pools, scale resources, and delete clusters. The API’s compatibility makes deploying on Vultr Kubernetes Engine (VKE) almost identical to deploying on other major cloud platforms, supporting seamless hybrid and multi-cloud setups for versatile cloud strategies. This API is crucial for DevOps and cloud-native workflows, helping to streamline infrastructure provisioning, enhance workload scalability, optimize resource utilization, and integrate efficiently across diverse cloud environments.

In this article, you will learn how to use the Cluster API to create and manage a Kubernetes cluster.

Prerequisites

Before you begin:

In the server:

Cluster API Core Components

  • Management Cluster: A central cluster that runs the Cluster API controllers and manages the lifecycle of other clusters, known as workload clusters.

  • Workload Cluster: A cluster dedicated to running user applications and workloads, managed by the management cluster.

  • Control Plane: The set of components responsible for maintaining the cluster’s desired state, including the API server, etcd database, and controllers.

  • Infrastructure Provider: Provides the resources and integration for the underlying cloud or infrastructure provider (e.g. Vultr) in Cluster API.

  • Cluster API Provider: The main interface in the Cluster API, responsible for managing clusters, machines, and related components across infrastructure providers.

Build a Vultr snapshot

In this section, you will create a snapshot of the Ubuntu 22.04 operating system using the image builder repository, and initialize the management cluster.

  1. Export your Vultr API key as an environment variable.

    console
    $ export VULTR_API_KEY=yourapikey
    
  2. Create an SSH key.

    console
    $ vultr-cli ssh create --name="cluster-api-key" --key="ssh-rsa AAAAB3NzaC1yc...."
    
  3. Clone the image builder repository.

    console
    $ git clone https://github.com/kubernetes-sigs/image-builder.git
    
  4. Navigate to directories in the repository.

    console
    $ cd image-builder/images/capi
    
  5. Create a Vultr snapshot.

    console
    $ make build-vultr-ubuntu-2204
    

    The snapshot creation will take approximately 30 minutes to complete. Once created, you will be able to view the snapshot in your Vultr Customer Portal like below.

    snapshot in vultr customer portal

  6. Verify the snapshot presence and note the snapshot ID for later use.

    console
    $ vultr-cli snapshot list
    
  7. Initialize the management cluster.

    console
    $ clusterctl init
    

    Exit the directory for the steps in the next section.

Create a Workload Cluster

In this section, you will create a workload cluster, deploy a CNI, and deploy Vultr Cloud Controller Manager to get all the cluster nodes ready and running for operations.

  1. Clone the cluster api repository.

    console
    $ git clone https://github.com/vultr/cluster-api-provider-vultr.git
    
  2. Navigate to config/default/manager_image_patch.yaml file in the cloned repository.

    console
    $ cd config/default/manager_image_patch.yaml
    
  3. Update value of the image field below to your controller image URL in the Vultr provider. Save and exit the file.

  4. Navigate to config/default/credentials.yaml file in the cloned repository.

    console
    $ cd config/default/credentials.yaml
    
  5. Add your Vultr API key. Save and exit the file.

  6. Create environment variables.

    console
    $ export CLUSTER_NAME=<clustername>
    $ export KUBERNETES_VERSION=v1.29.7
    $ export CONTROL_PLANE_MACHINE_COUNT=1
    $ export CONTROL_PLANE_PLANID=<plan_id>
    $ export WORKER_MACHINE_COUNT=1
    $ export WORKER_PLANID=<plan_id>
    $ export MACHINE_IMAGE=<snapshot_id>
    $ export REGION=<region>
    $ export PLANID=<plan_id>
    $ export VPCID=<vpc_id>
    $ export SSHKEY_ID=<sshKey_id>
    

Example values can be found in scripts/capvultr-config-example.sh file in the repository.

  1. Make the bash script executable.

    console
    $ chmod +x scripts/capvultr-config-example.sh &&source scripts/capvultr-config-example.sh
    
  2. Generate the cluster definition.

    console
    $ clusterctl generate cluster capvultr-quickstart --from templates/cluster-template.yaml > cluster.yaml
    
  3. Apply the template.

    console
    $ kubectl apply -f cluster.yaml
    
  4. View the workload cluster resources.

    console
    $ kubectl get cluster-api
    

    The control planes won't be ready until you install the CNI and Vultr Cloud Controller Manager.

  5. Verify that the first control plane is up.

    console
    $ kubectl get kubeadmcontrolplane
    
  6. Once the control plane node has initialized, retrieve the workload cluster's Kubeconfig.

    console
    $ clusterctl get kubeconfig capvultr-quickstart > capvultr-quickstart.kubeconfig
    
  7. Verify the presence of Kubernetes nodes in the workload cluster.

    console
    $ KUBECONFIG=capvultr-quickstart.kubeconfig kubectl get node
    
  8. Deploy Cilium CNI, you can also bring your own CNI.

  9. Create a Vultr secret.

    console
    $ KUBECONFIG=capvultr-quickstart.kubeconfig kubectl create secret generic vultr-ccm --namespace kube-system --from-literal api-key=$VULTR_API_KEY
    
  10. Deploy Vultr Cloud Controller Manager.

    console
    $ KUBECONFIG=capvultr-quickstart.kubeconfig kubectl apply -f https://raw.githubusercontent.com/vultr/vultr-cloud-controller-manager/master/docs/releases/latest.yml
    
  11. Verify the status of the status of workload cluster nodes.

    console
    $ KUBECONFIG=capvultr-quickstart.kubeconfig kubectl get node
    

Conclusion

In conclusion, the Vultr Cluster API enables efficient deployment and management of Kubernetes clusters on Vultr. By completing these steps, you’ve set up scalable clusters with automated networking and management, optimizing your cloud infrastructure for streamlined, cloud-native application deployment.