Cluster API Provider Hetzner (CAPH)

repository·main·Indexed 22 days ago

https://github.com/syself/cluster-api-provider-hetzner

A Cluster API provider for the declarative management of self-managed Kubernetes clusters on Hetzner Cloud and Hetzner Bare Metal infrastructure. It enables automated deployment and lifecycle management, supporting both cloud and bare-metal instances. The documentation covers management cluster setup using clusterctl, workload cluster generation, HCloud secret configuration, and the installation of Cilium CNI and Cloud Controller Managers (CCM).

Tokens
43.5K
Snippets
84
Records
136
Agent score
78%

What's inside cluster-api-provider-hetzner

  1. How the default CSR controller works

    main

    To ensure secure operation, Kubernetes requires signing kubelet serving certificates. When the kubelet flag rotate-server-certificates: "true" is set (configured via initConfiguration/joinConfiguration.nodeRegistration.kubeletExtraArgs), the kubelet automatically submits a Certificate Signing Request (CSR) to the Kubernetes certificates API.

    CAPH includes a built-in CSR controller that automatically approves these requests. The controller validates the request by checking the DNS name and the IP address to ensure only the intended nodes receive signed certificates.

    Important Configuration Note: To avoid errors with the default controller, do not manually set the following kubelet flags:

    • tls-cert-file: "/var/lib/kubelet/pki/kubelet-client-current.pem"
    • tls-private-key-file: "/var/lib/kubelet/pki/kubelet-client-current.pem"
    # Do NOT set these flags if you want to use the default CSR controller
    tls-cert-file: "/var/lib/kubelet/pki/kubelet-client-current.pem"
    tls-private-key-file: "/var/lib/kubelet/pki/kubelet-client-current.pem"
  2. Manage the lifecycle of a HetznerBareMetalHost

    main

    The lifecycle of a host follows these stages:

    1. Creation: The host is available for consumption immediately after creation.
    2. Consumption: A HetznerBareMetalMachine selects the host, updating the host's status and triggering provisioning.
    3. Deprovisioning: When the consuming HetznerBareMetalMachine is deleted, the host deprovisions and returns to a neutral state.
    4. Deletion: A host can only be deleted when it is in a neutral state. To safely delete a host that is currently in use, you must first enable maintenanceMode to ensure no machines consume it.
  3. Understand HetznerCluster modes

    main

    The HetznerCluster object supports two primary deployment modes:

    1. HCloud Cluster: Uses Hetzner Cloud (HCloud) instances. It works with kubeadm and supports private networks.
    2. Bare Metal Cluster: Uses Hetzner dedicated servers for either control planes or workers.

    Important constraints for Bare Metal clusters:

    • Private networks are not supported for bare metal clusters.
    • Node images must support cloud-init for provisioning.
    • You can use either the Syself CCM or the upstream HCloud CCM. Ensure your ProviderID format matches the requirements for your chosen CCM.
  4. How HetznerBareMetalMachine and HetznerBareMetalHost interact

    main

    The HetznerBareMetalMachine lifecycle is managed by the HetznerBareMetalMachineController. Crucially, the controller does not create or delete physical Hetzner dedicated machines directly. Instead, it operates on an inventory of existing HetznerBareMetalHosts (which represent already existing bare metal servers).

    Provisioning Flow:

    1. A HetznerBareMetalMachine is created with specs defined in a HetznerBareMetalMachineTemplate.
    2. The machine uses selectors to pick an available HetznerBareMetalHost.
    3. If a host is selected and not yet consumed, the machine transfers provisioning information to that host.
    4. The host is provisioned according to the template specs. Once successful, the machine is marked as ready.

    Deprovisioning Flow:

    1. When a HetznerBareMetalMachine is deleted, it removes provisioning information from the associated host.
    2. The host triggers deprovisioning.
    3. Once deprovisioning completes, the controller removes the owner/consumer references and finalizers, allowing the machine object to be fully deleted.
  5. Run multiple Kubernetes clusters in a single Hetzner project

    main

    The cluster-api-provider-hetzner supports multi-tenancy, allowing you to run multiple Kubernetes clusters within the same Hetzner cloud project simultaneously.

    To ensure isolation and prevent resource conflicts, the controller uses labels on all provisioned Hetzner resources that correspond to the specific cluster name. This labeling mechanism allows the controller to manage and distinguish resources belonging to different clusters even when they reside in the same project.

  6. Understand the relationship between CAPH and CAPI objects

    main

    When working with the Cluster API Provider Hetzner (CAPH), it is important to distinguish between standard Cluster API (CAPI) objects and the provider-specific objects that implement them.

    • HetznerCluster is the provider-specific counterpart to the CAPI Cluster object.
    • CAPI's Machine object is implemented by either an HCloudMachine or a HetznerBareMetalMachine, depending on the infrastructure type.
  7. Configure HetznerBareMetalRemediationTemplate

    main

    The HetznerBareMetalRemediationTemplate allows you to define custom methods for how Machine Health Checks treat unhealthy HetznerBareMetalMachine objects. These templates are reconciled by the HetznerBareMetalRemediationController, which triggers the specified remediation on the relevant machine.

    To use this, you must define a strategy within the spec. Currently, only the Reboot strategy type is supported.

  8. Choose a Hetzner Bare Metal flavor for Cluster API

    main

    When deploying clusters using bare metal servers with CAPH, you can select from different pre-defined templates known as 'flavors'. These flavors determine whether control plane nodes run on bare metal or HCloud (virtualized) and how the cluster handles machine remediation (recovery from failure).

    Note: These flavors are intended for demonstration purposes and are not recommended for production environments.

    # Flavors are consumed using the clusterctl tool
    # Example usage pattern (conceptual):
    clusterctl generate cluster --flavor <flavor-name> ...
  9. High Availability components in CAPH

    main

    A standard CAPH installation managed via clusterctl involves four primary components. While the default installation is not highly available, these components can be scaled to improve reliability:

    1. Cluster API (CAPI): The core management layer.
    2. cluster-api-bootstrap-provider-kubeadm (CAPBK): Handles the bootstrapping of nodes.
    3. cluster-api-control-plane-kubeadm (KCP): Manages the control plane lifecycle.
    4. cluster-api-provider-hetzner (CAPH): The infrastructure provider for Hetzner.
  10. Understand the HetznerBareMetalHost object

    main

    The HetznerBareMetalHost object represents a one-to-one relationship with a Hetzner dedicated server. It is used to make bare metal servers available to clusters.

    Key characteristics:

    • Decoupled from Clusters: A host object does not belong to a specific HetznerCluster and can be used by multiple clusters. This allows you to track server availability independently.
    • Immutability: Host objects cannot be updated. If you need to change a property, you must delete the existing object and create a new one.
    • Provisioning Trigger: Provisioning is triggered when a HetznerBareMetalMachine selects the host and updates its status.
  11. Manage SSH keys for Hetzner Robot bare metal servers

    main

    Bare metal servers in Hetzner Robot require two SSH keys: one for the rescue system and one for the actual system. These keys are managed via Kubernetes Secrets containing both the public and private key data.

    Configuration Locations:

    • Rescue System Key: Defined cluster-wide in the HetznerCluster specification.
    • Actual System Key: Defined in the HetznerBareMetalMachineTemplate. There is no cluster-wide alternative for the actual system key.

    Important Lifecycle Notes:

    • You cannot change the reference to the secret in the spec, but you can update the data within the secret itself.
    • Provisioned Hosts: If a host is already provisioned, changing the secret data will trigger a warning event. Provisioned hosts cannot automatically update their SSH keys; you must delete and recreate the machine object to apply changes.
    • Provisioning Hosts: If a host is currently in the provisioning state, it may restart the provisioning process if the SSH key change is necessary, depending on whether the rescue or actual system key was modified.