Skip to content

Commit 47766be

Browse files
committed
☄️ Add Terraform support to deploy Kapsule clusters
1 parent fb8efbe commit 47766be

12 files changed

Lines changed: 300 additions & 0 deletions

File tree

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
*.pyc
22
*.swp
33
*~
4+
45
prepare-vms/tags
56
prepare-vms/infra
67
prepare-vms/www
8+
9+
prepare-scw/.terraform*
10+
prepare-scw/terraform.*
11+
prepare-scw/stage2/*.tf
12+
prepare-scw/stage2/kubeconfig.*
13+
prepare-scw/stage2/wildcard_dns.*
14+
715
slides/*.yml.html
816
slides/autopilot/state.yaml
917
slides/index.html

prepare-scw/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
This directory contains a Terraform configuration to deploy
2+
a bunch of Kubernetes clusters on Scaleway, using their managed
3+
Kubernetes (Kapsule).
4+
5+
To use it:
6+
7+
```bash
8+
scw init
9+
terraform init
10+
terraform apply
11+
cd stage2
12+
terraform apply
13+
```
14+
15+
Edit `variables.tf` to change the number of clusters.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
resource "scaleway_k8s_cluster" "my_cluster" {
2+
name = var.cluster_name
3+
tags = var.common_tags
4+
version = var.k8s_version
5+
cni = var.cni
6+
}
7+
8+
resource "scaleway_k8s_pool" "my_pool" {
9+
cluster_id = scaleway_k8s_cluster.my_cluster.id
10+
name = "pool-0"
11+
tags = var.common_tags
12+
node_type = var.node_type
13+
size = var.pool_size
14+
min_size = var.pool_min_size
15+
max_size = var.pool_max_size
16+
autoscaling = true
17+
autohealing = true
18+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
output "kubeconfig" {
2+
value = scaleway_k8s_cluster.my_cluster.kubeconfig
3+
}
4+
5+
output "cluster_id" {
6+
value = scaleway_k8s_cluster.my_cluster.id
7+
}
8+
9+
output "wildcard_dns" {
10+
value = scaleway_k8s_cluster.my_cluster.wildcard_dns
11+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
terraform {
2+
required_providers {
3+
scaleway = {
4+
source = "scaleway/scaleway"
5+
version = "2.0.0"
6+
}
7+
kubernetes = {
8+
source = "hashicorp/kubernetes"
9+
version = "2.0.3"
10+
}
11+
local = {
12+
source = "hashicorp/local"
13+
version = "2.1.0"
14+
}
15+
}
16+
required_version = ">= 0.14"
17+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
variable "cluster_name" {
2+
type = string
3+
default = "deployed-with-terraform"
4+
}
5+
6+
variable "cni" {
7+
type = string
8+
default = "cilium"
9+
}
10+
11+
variable "common_tags" {
12+
type = list(string)
13+
default = []
14+
}
15+
16+
variable "k8s_version" {
17+
type = string
18+
default = "1.22.2"
19+
}
20+
21+
variable "node_type" {
22+
type = string
23+
default = "DEV1-M"
24+
}
25+
26+
variable "pool_size" {
27+
type = number
28+
default = 2
29+
}
30+
31+
variable "pool_min_size" {
32+
type = number
33+
default = 1
34+
}
35+
36+
variable "pool_max_size" {
37+
type = number
38+
default = 5
39+
}

prepare-scw/locals.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
locals {
2+
# Common tags to be assigned to all resources
3+
common_tags = [
4+
"created-by=terraform"
5+
]
6+
}

prepare-scw/main.tf

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module "kapsule_cluster" {
2+
count = var.how_many_clusters
3+
source = "./kapsule_cluster"
4+
cluster_name = format("tf-%03d", count.index + 101)
5+
}
6+
7+
output "kubectl_config" {
8+
value = format("scw k8s kubeconfig install %s", split("/", module.kapsule_cluster.0.cluster_id)[1])
9+
}
10+
11+
resource "local_file" "stage2" {
12+
filename = "${path.module}/stage2/main.tf"
13+
content = templatefile(
14+
"${path.module}/stage2.tmpl",
15+
{ count = var.how_many_clusters }
16+
)
17+
}
18+
19+
resource "local_file" "kubeconfig" {
20+
count = var.how_many_clusters
21+
filename = format("%s/stage2/kubeconfig.%03d", path.module, count.index + 101)
22+
content = module.kapsule_cluster[count.index].kubeconfig.0.config_file
23+
}
24+
25+
resource "local_file" "wildcard_dns" {
26+
count = var.how_many_clusters
27+
filename = format("%s/stage2/wildcard_dns.%03d", path.module, count.index + 101)
28+
content = trimprefix(module.kapsule_cluster[count.index].wildcard_dns, "*.")
29+
}

prepare-scw/providers.tf

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
terraform {
2+
required_providers {
3+
scaleway = {
4+
source = "scaleway/scaleway"
5+
version = "2.0.0"
6+
}
7+
}
8+
required_version = ">= 0.14"
9+
}
10+
11+
provider "scaleway" {
12+
#zone = "nl-ams-1"
13+
#region = "nl-ams"
14+
#project_id = "7ee16446-7711-4171-a7c2-4bb6f0d4c4c8"
15+
}
16+

prepare-scw/stage2.tmpl

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
terraform {
2+
required_providers {
3+
kubernetes = {
4+
source = "hashicorp/kubernetes"
5+
version = "2.0.3"
6+
}
7+
}
8+
required_version = ">= 0.14"
9+
}
10+
11+
12+
%{ for index in range(101, 101+count) ~}
13+
14+
provider "kubernetes" {
15+
alias = "kapsule_cluster_${index}"
16+
config_path = "$${path.module}/kubeconfig.${index}"
17+
}
18+
19+
resource "kubernetes_namespace" "sshpod_${index}" {
20+
provider = kubernetes.kapsule_cluster_${index}
21+
metadata {
22+
name = "sshpod"
23+
}
24+
}
25+
26+
resource "kubernetes_deployment" "sshpod_${index}" {
27+
provider = kubernetes.kapsule_cluster_${index}
28+
metadata {
29+
name = "sshpod"
30+
namespace = kubernetes_namespace.sshpod_${index}.metadata.0.name
31+
}
32+
spec {
33+
selector {
34+
match_labels = {
35+
app = "sshpod"
36+
}
37+
}
38+
template {
39+
metadata {
40+
labels = {
41+
app = "sshpod"
42+
}
43+
}
44+
spec {
45+
service_account_name = "sshpod"
46+
container {
47+
image = "jpetazzo/sshpod"
48+
name = "sshpod"
49+
env {
50+
name = "PASSWORD"
51+
value = random_string.sshpod_${index}.result
52+
}
53+
lifecycle {
54+
post_start {
55+
exec {
56+
command = [ "sh", "-c", "curl http://myip.enix.org/REMOTE_ADDR > /etc/HOSTIP || true" ]
57+
}
58+
}
59+
}
60+
resources {
61+
limits = {
62+
cpu = "2"
63+
memory = "100M"
64+
}
65+
requests = {
66+
cpu = "100m"
67+
memory = "100M"
68+
}
69+
}
70+
}
71+
}
72+
}
73+
}
74+
}
75+
76+
resource "kubernetes_service" "sshpod_${index}" {
77+
provider = kubernetes.kapsule_cluster_${index}
78+
metadata {
79+
name = "sshpod"
80+
namespace = kubernetes_namespace.sshpod_${index}.metadata.0.name
81+
}
82+
spec {
83+
selector = {
84+
app = "sshpod"
85+
}
86+
port {
87+
port = 22
88+
target_port = 22
89+
node_port = 32222
90+
}
91+
type = "NodePort"
92+
}
93+
}
94+
95+
resource "kubernetes_service_account" "sshpod_${index}" {
96+
provider = kubernetes.kapsule_cluster_${index}
97+
metadata {
98+
name = "sshpod"
99+
namespace = kubernetes_namespace.sshpod_${index}.metadata.0.name
100+
}
101+
}
102+
103+
resource "kubernetes_cluster_role_binding" "sshpod_${index}" {
104+
provider = kubernetes.kapsule_cluster_${index}
105+
metadata {
106+
name = "sshpod"
107+
}
108+
role_ref {
109+
api_group = "rbac.authorization.k8s.io"
110+
kind = "ClusterRole"
111+
name = "cluster-admin"
112+
}
113+
subject {
114+
kind = "ServiceAccount"
115+
name = "sshpod"
116+
namespace = "sshpod"
117+
}
118+
}
119+
120+
resource "random_string" "sshpod_${index}" {
121+
length = 6
122+
special = false
123+
upper = false
124+
}
125+
126+
output "ssh_${index}" {
127+
value = format(
128+
"ssh -l %s -p %s ssh.%s # password=%s",
129+
"k8s",
130+
"32222",
131+
file(format("%s/wildcard_dns.%03d", path.module, ${index})),
132+
random_string.sshpod_${index}.result
133+
)
134+
}
135+
136+
%{ endfor ~}

0 commit comments

Comments
 (0)