Swarmchestrate cluster builder
Project description
Swarmchestrate - Cluster Builder
This repository contains the codebase for [cluster-builder], which builds K3s clusters for Swarmchestrate using OpenTofu.
Key features:
- Create: Provisions infrastructure using OpenTofu and installs K3s.
- Add: Add worker or HA nodes to existing clusters.
- Remove: Selectively remove nodes from existing clusters.
- Delete: Destroys the provisioned infrastructure when no longer required.
Prerequisites
Before proceeding, ensure the following prerequisites are installed:
- Git: For cloning the repository.
- Python: Version 3.9 or higher.
- pip: Python package manager.
- OpenTofu: Version 1.6 or higher for infrastructure provisioning.
- Make: To run the provided
Makefile. - PostgreSQL: For storing OpenTofu state.
- (Optional) Docker: To create a dev Postgres
Getting Started
1. Clone the Repository
To get started, clone this repository:
git clone https://github.com/Swarmchestrate/cluster-builder.git
2. Navigate to the Project Directory
cd cluster-builder
3. Install Dependencies and Tools
Run the Makefile to install all necessary dependencies, including OpenTofu:
make install
This command will:
- Install Python dependencies listed in requirements.txt.
- Download and configure OpenTofu for infrastructure management.
Optional
make db
This command will:
- Spin up an empty dev Postgres DB (in Docker) for storing state
4. Populate .env file with access config
First, rename or copy the example file to .env
cp .env_example .env
Then populate postgres connection details and needed cloud credential data.
## PG Configuration
POSTGRES_USER=postgres
POSTGRES_PASSWORD=secret
POSTGRES_HOST=db.example.com
POSTGRES_DATABASE=terraform_state
POSTGRES_SSLMODE=prefer
## AWS Auth
AWS_REGION=us-west-2
AWS_ACCESS_KEY=AKIAXXXXXXXXXXXXXXXX
AWS_SECRET_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Basic Usage
Initialisation
from cluster_builder import Swarmchestrate
# Initialise the orchestrator
orchestrator = Swarmchestrate(
template_dir="/path/to/templates",
output_dir="/path/to/output"
)
Creating a New Cluster
To create a new k3s cluster, use the add_node method with the master role:
# Configuration for a new cluster
config = {
"cloud": "aws",
"k3s_role": "master",
"ami": "ami-0123456789abcdef",
"instance_type": "t3.medium",
"ssh_key_name": "your-ssh-key",
"k3s_token": "your-k3s-token"
}
# Create the cluster (returns the cluster name)
cluster_name = orchestrator.add_node(config)
print(f"Created cluster: {cluster_name}")
Adding Nodes to an Existing Cluster
To add worker or high-availability nodes to an existing cluster:
# Configuration for adding a worker node
worker_config = {
"cloud": "aws",
"k3s_role": "worker", # can be "worker" or "ha"
"master_ip": "1.2.3.4", # IP of the master node
"cluster_name": "existing-cluster-name", # specify an existing cluster
"ami": "ami-0123456789abcdef",
"instance_type": "t2.medium",
"ssh_key_name": "your-ssh-key",
"k3s_token": "k3s-cluster-token" # Token of existing cluster
}
# Add the worker node
cluster_name = orchestrator.add_node(worker_config)
print(f"Added worker node to cluster: {cluster_name}")
Important requirements:
- For
k3s_role="worker"ork3s_role="ha", you must specify amaster_ip - For
k3s_role="master", you must not specify amaster_ip
Removing a Specific Node
To remove a specific node from a cluster:
# Remove a node by its resource name
orchestrator.remove_node(
cluster_name="your-cluster-name",
resource_name="aws_eloquent_feynman" # The resource identifier of the node
)
The remove_node method:
- Destroys the node's infrastructure resources
- Removes the node's configuration from the cluster
Destroying an Entire Cluster
To completely destroy a cluster and all its nodes:
# Destroy the entire cluster
orchestrator.destroy(
cluster_name="your-cluster-name"
)
The destroy method:
- Destroys all infrastructure resources associated with the cluster
- Removes the cluster directory and configuration files
Advanced Usage
Dry Run Mode
All operations support a dryrun parameter, which validates the configuration
without making changes. A node created with dryrun should be removed with dryrun.
# Validate configuration without deploying
orchestrator.add_node(config, dryrun=True)
# Validate removal without destroying
orchestrator.remove_node(cluster_name, resource_name, dryrun=True)
# Validate destruction without destroying
orchestrator.destroy(cluster_name, dryrun=True)
Custom Cluster Names
By default, cluster names are generated automatically. To specify a custom name:
config = {
"cloud": "aws",
"k3s_role": "master",
"cluster_name": "production-cluster",
# ... other configuration ...
}
orchestrator.add_node(config)
Template Structure
Templates should be organised as follows:
templates/- Base directory for templatestemplates/{cloud}/- Terraform modules for each cloud providertemplates/{role}_user_data.sh.tpl- Node initialisation scriptstemplates/{cloud}_provider.tf.j2- Provider configuration templates
Edge Device Requirements
To connect edge devices as part of your K3s cluster, ensure that the following ports are open on each edge device to enable communication within nodes:
Inbound Rules:
| Port Range | Protocol | Purpose |
|---|---|---|
| 2379-2380 | TCP | Internal servers communication for embedded etcd |
| 6443 | TCP | K3s API server communication |
| 8472 | UDP | Flannel VXLAN (network overlay) |
| 10250 | TCP | Kubelet metrics and communication |
| 51820 | UDP | WireGuard IPv4 (for encrypted networking) |
| 51821 | UDP | WireGuard IPv6 (for encrypted networking) |
| 5001 | TCP | Embedded registry (Spegel) |
| 22 | TCP | SSH access for provisioning and management |
| 80 | TCP | HTTP communication for web access |
| 443 | TCP | HTTPS communication for secure access |
| 53 | UDP | DNS (CoreDNS) for internal service discovery |
| 5432 | TCP | PostgreSQL database access |
Outbound Rule:
| Port Range | Protocol | Purpose |
|---|---|---|
| all | all | Allow all outbound traffic for the system's operations |
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cluster_builder-0.2.1.tar.gz.
File metadata
- Download URL: cluster_builder-0.2.1.tar.gz
- Upload date:
- Size: 20.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a02983cf8b35950f25c0c537497184ec3b77c8d25b3239df24821b1192984a90
|
|
| MD5 |
f07fefeb39e24e257643c2b473d03aeb
|
|
| BLAKE2b-256 |
dc57e4ca4091f154b26b42264c6c9d5cf9eb4fd243a6cf2aab0e539b5a3a9b78
|
Provenance
The following attestation bundles were made for cluster_builder-0.2.1.tar.gz:
Publisher:
release.yml on Swarmchestrate/cluster-builder
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cluster_builder-0.2.1.tar.gz -
Subject digest:
a02983cf8b35950f25c0c537497184ec3b77c8d25b3239df24821b1192984a90 - Sigstore transparency entry: 189519250
- Sigstore integration time:
-
Permalink:
Swarmchestrate/cluster-builder@f4e5eff430b1c8c77837f36c69c830b5775d3b5d -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/Swarmchestrate
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@f4e5eff430b1c8c77837f36c69c830b5775d3b5d -
Trigger Event:
push
-
Statement type:
File details
Details for the file cluster_builder-0.2.1-py3-none-any.whl.
File metadata
- Download URL: cluster_builder-0.2.1-py3-none-any.whl
- Upload date:
- Size: 20.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6750dc73970ade14610208a4255e121d2f0cf1783d72991f8b9a01ad496b9d85
|
|
| MD5 |
db0b0bc5c873e89cc0a7f4a6d25298a7
|
|
| BLAKE2b-256 |
32702b5657dd3a73806bb9fcf39bf2ea50ae1e3e8ada98a6e0a2974203d90f66
|
Provenance
The following attestation bundles were made for cluster_builder-0.2.1-py3-none-any.whl:
Publisher:
release.yml on Swarmchestrate/cluster-builder
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cluster_builder-0.2.1-py3-none-any.whl -
Subject digest:
6750dc73970ade14610208a4255e121d2f0cf1783d72991f8b9a01ad496b9d85 - Sigstore transparency entry: 189519251
- Sigstore integration time:
-
Permalink:
Swarmchestrate/cluster-builder@f4e5eff430b1c8c77837f36c69c830b5775d3b5d -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/Swarmchestrate
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@f4e5eff430b1c8c77837f36c69c830b5775d3b5d -
Trigger Event:
push
-
Statement type: