Skip to content

Commit 8e40c31

Browse files
committed
feat(infra): add Ansible automation for cluster deployment
Comprehensive Ansible automation for SwarmCracker cluster setup, management, and teardown. ## Structure infrastructure/ansible/ ├── site.yml # Main playbook (managers + workers) ├── setup-manager.yml # Manager-only setup ├── setup-worker.yml # Worker-only setup with join token ├── teardown.yml # Cluster cleanup ├── test-cluster.yml # Deploy test services ├── ansible.cfg # Ansible configuration ├── inventory/ # Production & staging inventories ├── group_vars/ # Global variables └── roles/ ├── common/ # Prerequisites (packages, kernel, firewall) ├── manager/ # Manager setup (download, config, service) └── worker/ # Worker setup (join cluster, config, service) ## Features ### Common Role - Install system dependencies (curl, wget, iptables, bridge-utils, etc.) - Load kernel modules (KVM, VXLAN, bridge) - Enable IP forwarding and bridge netfilter - Configure firewall rules (UFW/firewalld) - Setup NTP synchronization - Create required directories ### Manager Role - Download and install SwarmCracker binaries - Download Firecracker kernel - Generate manager configuration - Create systemd service - Initialize SwarmKit manager - Generate and display join tokens ### Worker Role - Download and install SwarmCracker binaries - Generate worker configuration - Create systemd service - Join SwarmKit cluster - Verify connection to manager ## Usage # Deploy complete cluster ansible-playbook -i inventory/production site.yml # Deploy manager only ansible-playbook -i inventory/production setup-manager.yml # Deploy workers (auto-fetches join token) ansible-playbook -i inventory/production setup-worker.yml # Deploy with custom manager ansible-playbook -i inventory/production setup-worker.yml --extra-vars "manager_host=192.168.1.10" # Test cluster ansible-playbook -i inventory/production test-cluster.yml # Teardown cluster ansible-playbook -i inventory/production teardown.yml --extra-vars "confirm_teardown=true" ## Configuration All variables in group_vars/all.yml: - swarmcracker_version: v0.2.0 - swarmcracker_arch: amd64/arm64 - Network settings (bridge, subnet, VXLAN) - Resource reservations - Cleanup policies ## Requirements - Ansible 2.9+ - SSH access to target hosts - Target: Ubuntu 20.04+/Debian 11+/CentOS 8+ - KVM support on all nodes ## Security - Systemd service hardening (NoNewPrivileges, ProtectSystem, etc.) - Firewall rules configured automatically - Resource limits (NOFILE, NPROC) - Secure directory permissions 24 files added, ~2,000+ lines of automation
1 parent 6e7d57a commit 8e40c31

25 files changed

Lines changed: 1281 additions & 0 deletions

File tree

infrastructure/ansible/.gitignore

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Ansible artifacts
2+
*.retry
3+
*.pyc
4+
__pycache__/
5+
6+
# Temporary files
7+
tmp/
8+
temp/
9+
10+
# Logs
11+
*.log
12+
13+
# Encrypted files (keep .enc files, ignore decrypted)
14+
!*.enc
15+
*.dec
16+
17+
# Vault password
18+
.vault_password
19+
vault_password
20+
vault_pass.txt
21+
22+
# SSH keys
23+
*.pem
24+
*.key
25+
!*.pub
26+
27+
# Inventory backups
28+
*.bak
29+
*.backup
30+
31+
# Facter output
32+
facter.txt
33+
34+
# Local overrides (keep example files)
35+
group_vars/local.yml
36+
host_vars/*.local.yml
37+
!group_vars/local.yml.example
38+
!host_vars/*.example.yml

infrastructure/ansible/README.md

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
# SwarmCracker Cluster Automation
2+
3+
Ansible playbooks and roles for automated SwarmCracker cluster deployment.
4+
5+
## Directory Structure
6+
7+
```
8+
infrastructure/ansible/
9+
├── ansible.cfg # Ansible configuration
10+
├── inventory/ # Cluster inventory
11+
│ ├── production/ # Production cluster
12+
│ └── staging/ # Staging cluster
13+
├── group_vars/ # Group variables
14+
│ ├── all.yml # Variables for all hosts
15+
│ ├── managers.yml # Manager-specific variables
16+
│ └── workers.yml # Worker-specific variables
17+
├── host_vars/ # Host-specific variables
18+
├── roles/ # Ansible roles
19+
│ ├── common/ # Common setup (prerequisites)
20+
│ ├── manager/ # SwarmCracker manager setup
21+
│ └── worker/ # SwarmCracker worker setup
22+
├── site.yml # Main playbook
23+
├── setup-manager.yml # Manager-only playbook
24+
└── setup-worker.yml # Worker-only playbook
25+
```
26+
27+
## Quick Start
28+
29+
### 1. Configure Inventory
30+
31+
Edit `inventory/production/hosts`:
32+
33+
```ini
34+
[managers]
35+
manager1 ansible_host=192.168.1.10
36+
37+
[workers]
38+
worker1 ansible_host=192.168.1.11
39+
worker2 ansible_host=192.168.1.12
40+
41+
[swarmcracker:children]
42+
managers
43+
workers
44+
```
45+
46+
### 2. Configure Variables
47+
48+
Edit `group_vars/all.yml`:
49+
50+
```yaml
51+
swarmcracker_version: "v0.2.0"
52+
swarmcracker_arch: "amd64" # or "arm64"
53+
swarmcracker_download_url: "https://github.com/restuhaqza/SwarmCracker/releases/download/{{ swarmcracker_version }}"
54+
```
55+
56+
### 3. Deploy Cluster
57+
58+
```bash
59+
# Deploy entire cluster (managers + workers)
60+
ansible-playbook -i inventory/production site.yml
61+
62+
# Deploy managers only
63+
ansible-playbook -i inventory/production setup-manager.yml
64+
65+
# Deploy workers only
66+
ansible-playbook -i inventory/production setup-worker.yml --extra-vars "manager_host=192.168.1.10"
67+
68+
# Deploy with custom variables
69+
ansible-playbook -i inventory/production site.yml \
70+
--extra-vars "swarmcracker_version=v0.2.0"
71+
```
72+
73+
### 4. Verify Deployment
74+
75+
```bash
76+
# Check manager status
77+
ansible managers -i inventory/production -m command -a "swarmctl node ls"
78+
79+
# Check worker status
80+
ansible workers -i inventory/production -m command -a "systemctl status swarmd-firecracker"
81+
```
82+
83+
## Requirements
84+
85+
- Ansible 2.9+
86+
- Python 3.8+ on control node
87+
- SSH access to target hosts
88+
- Target hosts: Ubuntu 20.04+/Debian 11+/CentOS 8+
89+
90+
## Roles
91+
92+
### common
93+
- Install system dependencies (Go, Firecracker, etc.)
94+
- Configure kernel modules (KVM, VXLAN)
95+
- Setup network bridges
96+
- Configure firewall rules
97+
98+
### manager
99+
- Download and install SwarmCracker binaries
100+
- Initialize SwarmKit manager
101+
- Configure manager service
102+
- Generate worker join tokens
103+
104+
### worker
105+
- Download and install SwarmCracker binaries
106+
- Join SwarmKit cluster
107+
- Configure worker service
108+
- Setup VXLAN networking
109+
110+
## Variables
111+
112+
| Variable | Default | Description |
113+
|----------|---------|-------------|
114+
| `swarmcracker_version` | `v0.2.0` | SwarmCracker release version |
115+
| `swarmcracker_arch` | `amd64` | Architecture (amd64/arm64) |
116+
| `swarmcracker_install_dir` | `/usr/local/bin` | Binary installation directory |
117+
| `swarmcracker_state_dir` | `/var/lib/swarmcracker` | State directory |
118+
| `swarmcracker_kernel_path` | `/usr/share/firecracker/vmlinux` | Firecracker kernel path |
119+
| `swarmcracker_rootfs_dir` | `/var/lib/firecracker/rootfs` | Rootfs directory |
120+
| `swarmcracker_bridge_name` | `swarm-br0` | Network bridge name |
121+
| `swarmcracker_subnet` | `192.168.127.0/24` | VM subnet |
122+
| `swarmcracker_bridge_ip` | `192.168.127.1/24` | Bridge IP |
123+
| `swarmcracker_vxlan_enabled` | `true` | Enable VXLAN networking |
124+
| `swarmcracker_vxlan_vni` | `100` | VXLAN VNI |
125+
| `swarmcracker_vxlan_port` | `4789` | VXLAN UDP port |
126+
127+
## Examples
128+
129+
### Single Node (Development)
130+
131+
```ini
132+
[managers]
133+
localhost ansible_connection=local
134+
135+
[workers]
136+
137+
[swarmcracker:children]
138+
managers
139+
```
140+
141+
### Multi-Node Production
142+
143+
```ini
144+
[managers]
145+
manager1 ansible_host=192.168.1.10
146+
manager2 ansible_host=192.168.1.11
147+
manager3 ansible_host=192.168.1.12
148+
149+
[workers]
150+
worker1 ansible_host=192.168.1.20
151+
worker2 ansible_host=192.168.1.21
152+
worker3 ansible_host=192.168.1.22
153+
worker4 ansible_host=192.168.1.23
154+
worker5 ansible_host=192.168.1.24
155+
156+
[swarmcracker:children]
157+
managers
158+
workers
159+
```
160+
161+
### Mixed Architecture
162+
163+
```ini
164+
[managers]
165+
manager1 ansible_host=192.168.1.10 swarmcracker_arch=amd64
166+
167+
[workers]
168+
worker1 ansible_host=192.168.1.11 swarmcracker_arch=amd64
169+
worker2 ansible_host=192.168.1.12 swarmcracker_arch=arm64 # ARM server
170+
```
171+
172+
## Troubleshooting
173+
174+
### Check Ansible Connection
175+
176+
```bash
177+
ansible all -i inventory/production -m ping
178+
```
179+
180+
### Verbose Output
181+
182+
```bash
183+
ansible-playbook -i inventory/production site.yml -vvv
184+
```
185+
186+
### Dry Run
187+
188+
```bash
189+
ansible-playbook -i inventory/production site.yml --check --diff
190+
```
191+
192+
### Cleanup
193+
194+
```bash
195+
ansible-playbook -i inventory/production teardown.yml
196+
```
197+
198+
## See Also
199+
200+
- [SwarmCracker Installation Guide](../../docs/getting-started/installation-guide.md)
201+
- [SwarmCracker Documentation](../../docs/)
202+
- [Ansible Documentation](https://docs.ansible.com/)

infrastructure/ansible/ansible.cfg

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[defaults]
2+
inventory = ./inventory/production
3+
remote_user = ubuntu
4+
host_key_checking = False
5+
retry_files_enabled = False
6+
gathering = smart
7+
fact_caching = jsonfile
8+
fact_caching_connection = /tmp/ansible_facts
9+
fact_caching_timeout = 86400
10+
11+
# Output formatting
12+
stdout_callback = yaml
13+
callback_whitelist = profile_tasks, timer
14+
15+
# SSH settings
16+
ssh_args = -o ControlMaster=auto -o ControlPersist=60s
17+
pipelining = True
18+
19+
[privilege_escalation]
20+
become = True
21+
become_method = sudo
22+
become_user = root
23+
become_ask_pass = False
24+
25+
[inventory]
26+
enable_plugins = host_list, script, auto, yaml, ini
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
---
2+
# SwarmCracker Global Variables
3+
4+
# Version and download
5+
swarmcracker_version: "v0.2.0"
6+
swarmcracker_arch: "amd64" # amd64 or arm64
7+
swarmcracker_download_url: "https://github.com/restuhaqza/SwarmCracker/releases/download/{{ swarmcracker_version }}"
8+
swarmcracker_tarball: "swarmcracker-{{ swarmcracker_version }}-linux-{{ swarmcracker_arch }}.tar.gz"
9+
10+
# Installation paths
11+
swarmcracker_install_dir: "/usr/local/bin"
12+
swarmcracker_state_dir: "/var/lib/swarmcracker"
13+
swarmcracker_config_dir: "/etc/swarmcracker"
14+
15+
# Firecracker configuration
16+
swarmcracker_kernel_path: "/usr/share/firecracker/vmlinux"
17+
swarmcracker_kernel_url: "https://s3.amazonaws.com/spec.ccfc.min/img/quickstart_guide/{{ swarmcracker_arch }}/kernels/vmlinux.bin"
18+
swarmcracker_rootfs_dir: "/var/lib/firecracker/rootfs"
19+
swarmcracker_socket_dir: "/var/run/firecracker"
20+
21+
# Network configuration
22+
swarmcracker_bridge_name: "swarm-br0"
23+
swarmcracker_subnet: "192.168.127.0/24"
24+
swarmcracker_bridge_ip: "192.168.127.1/24"
25+
swarmcracker_ip_mode: "static"
26+
swarmcracker_nat_enabled: true
27+
28+
# VXLAN configuration
29+
swarmcracker_vxlan_enabled: true
30+
swarmcracker_vxlan_vni: 100
31+
swarmcracker_vxlan_port: 4789
32+
33+
# Service configuration
34+
swarmcracker_manager_port: "4242"
35+
swarmcracker_worker_api_port: "4243"
36+
swarmcracker_debug: false
37+
38+
# System requirements
39+
swarmcracker_required_packages:
40+
- curl
41+
- wget
42+
- iptables
43+
- bridge-utils
44+
- iproute2
45+
- dnsmasq
46+
- qemu-utils
47+
48+
# Go installation (for building SwarmKit tools)
49+
go_version: "1.24"
50+
go_install_dir: "/usr/local"
51+
52+
# Firecracker installation
53+
firecracker_version: "1.14.0"
54+
firecracker_download_url: "https://github.com/firecracker-microvm/firecracker/releases/download/{{ firecracker_version }}"
55+
firecracker_tarball: "firecracker-{{ firecracker_version }}-{{ swarmcracker_arch }}.tgz"
56+
57+
# Security settings
58+
swarmcracker_enable_seccomp: true
59+
swarmcracker_enable_selinux: false
60+
61+
# Logging
62+
swarmcracker_log_level: "info"
63+
swarmcracker_log_dir: "/var/log/swarmcracker"
64+
65+
# Timeouts
66+
swarmcracker_shutdown_timeout: "30s"
67+
swarmcracker_startup_timeout: "120s"
68+
69+
# Resource reservations
70+
swarmcracker_reserved_cpus: 1
71+
swarmcracker_reserved_memory_mb: 512
72+
73+
# Cleanup settings
74+
swarmcracker_max_image_age_days: 7
75+
swarmcracker_auto_cleanup: true
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# SwarmCracker Cluster Inventory - Production
2+
3+
[managers]
4+
manager1 ansible_host=192.168.1.10
5+
6+
[workers]
7+
worker1 ansible_host=192.168.1.11
8+
worker2 ansible_host=192.168.1.12
9+
10+
[swarmcracker:children]
11+
managers
12+
workers
13+
14+
[swarmcracker:vars]
15+
ansible_user=ubuntu
16+
ansible_become=yes
17+
ansible_become_method=sudo
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# SwarmCracker Cluster Inventory - Staging
2+
3+
[managers]
4+
staging-manager ansible_host=192.168.1.100
5+
6+
[workers]
7+
staging-worker1 ansible_host=192.168.1.101
8+
9+
[swarmcracker:children]
10+
managers
11+
workers
12+
13+
[swarmcracker:vars]
14+
ansible_user=ubuntu
15+
ansible_become=yes
16+
ansible_become_method=sudo

0 commit comments

Comments
 (0)