Skip to content

Commit 294e7cd

Browse files
authored
chore: k3s tf module & spectrum module cleanup && cilium l2 support (#19)
1 parent e721dcc commit 294e7cd

File tree

16 files changed

+177
-51
lines changed

16 files changed

+177
-51
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
.direnv
66
kubeconfig
77
talosconfig
8+
provider_project/*

ephemeral/main.tf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ module "spectrum" {
3030
local_sensitive_file.kubeconfig,
3131
]
3232
source = "../terraform-modules/spectrum"
33-
components = ["kubevirt"]
3433
network = var.github_branch
3534
cluster = "ephemeral"
3635

examples/k3s/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Kubernetes cluster based on k3s
2+
3+
This example deploys *k3s based* Kubernetes cluster on a specific host.
4+
5+
### Requirements
6+
- installed [**Terraform**](https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli) on your laptop
7+
- installed [**autok3s**](https://github.com/cnrancher/autok3s?tab=readme-ov-file#quick-start-tldr) on your laptop
8+
- target server accessible via `ssh`
9+
10+
### Instruction
11+
- Copy files in this directory to your Fluence related *provider* directory
12+
- Update values with your own in `config.tf` file
13+
```
14+
locals {
15+
server_name = "example"
16+
server_ip_address = "1.1.1.1.1"
17+
ssh_key = "~/.ssh/key"
18+
ssh_user = "root"
19+
ssh_port = "22"
20+
}
21+
```
22+
- deploy using `terraform`
23+
```
24+
terraform init
25+
terraform apply
26+
```
27+
- you can check your freshly installed cluster in [**autok3s UI**](https://github.com/cnrancher/autok3s?tab=readme-ov-file#quick-start-tldr)

examples/k3s/config.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
locals {
2+
server_name = "example"
3+
server_ip_address = "1.1.1.1.1"
4+
ssh_key = "~/.ssh/key"
5+
ssh_user = "root"
6+
ssh_port = "22"
7+
}

examples/k3s/main.tf

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module "k3s" {
2+
source = "github.com/fluencelabs/spectrum//terraform-modules/k3s"
3+
kubeconfigs_location = "${path.root}/secrets"
4+
server_name = local.server_name
5+
server_ip_address = local.server_ip_address
6+
ssh_key = local.ssh_key
7+
ssh_user = local.ssh_user
8+
ssh_port = local.ssh_port
9+
}
10+
11+
provider "helm" {
12+
kubernetes {
13+
config_path = module.k3s.kubeconfig_file
14+
}
15+
}
16+
17+
module "spectrum" {
18+
depends_on = [module.k3s]
19+
source = "github.com/fluencelabs/spectrum//terraform-modules/spectrum"
20+
cluster_flavour = "k3s"
21+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
apiVersion: kustomize.config.k8s.io/v1beta1
2+
kind: Kustomization
3+
resources:
4+
- ./manifests.yaml
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
apiVersion: "cilium.io/v2alpha1"
3+
kind: CiliumL2AnnouncementPolicy
4+
metadata:
5+
name: fluence-l2
6+
namespace: kube-system
7+
spec:
8+
serviceSelector:
9+
matchLabels:
10+
fluence: cloudless.dev
11+
externalIPs: true
12+
loadBalancerIPs: true
13+
---
14+
apiVersion: "cilium.io/v2alpha1"
15+
kind: CiliumLoadBalancerIPPool
16+
metadata:
17+
name: fluence-l2
18+
namespace: kube-system
19+
spec:
20+
serviceSelector:
21+
matchLabels:
22+
fluence: cloudless.dev
23+

terraform-modules/k3s/autok3s.tf

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
resource "terraform_data" "k3s-init" {
2+
3+
input = var.server_name
4+
provisioner "local-exec" {
5+
command = <<EOT
6+
autok3s create --provider native --docker-script https://get.docker.com --k3s-channel stable --k3s-install-script https://get.k3s.io \
7+
--master-extra-args '--disable servicelb,traefik --flannel-backend none --disable-kube-proxy --disable-network-policy' \
8+
--name ${var.server_name} --rollback --ssh-key-path ${var.ssh_key} --ssh-port ${var.ssh_port} --ssh-user ${var.ssh_user} --master-ips ${var.server_ip_address} \
9+
--enable explorer
10+
EOT
11+
12+
}
13+
14+
provisioner "local-exec" {
15+
when = destroy
16+
command = <<EOT
17+
autok3s delete -p native --name ${self.input} -f
18+
EOT
19+
20+
}
21+
}
22+
23+
resource "terraform_data" "k3s-gen-kubeconfig" {
24+
depends_on = [
25+
terraform_data.k3s-init
26+
]
27+
input = "${var.kubeconfigs_location}/kubeconfig.yaml"
28+
provisioner "local-exec" {
29+
command = <<EOT
30+
mkdir -p ${var.kubeconfigs_location} && \
31+
autok3s kubectl config use-context ${var.server_name} && \
32+
autok3s kubectl config view --minify=true --raw > ${var.kubeconfigs_location}/kubeconfig.yaml
33+
EOT
34+
35+
}
36+
37+
provisioner "local-exec" {
38+
when = destroy
39+
command = <<EOT
40+
rm -rf ${self.input}.yaml
41+
EOT
42+
}
43+
}
44+
45+
resource "terraform_data" "os-init" {
46+
47+
connection {
48+
type = "ssh"
49+
user = var.ssh_user
50+
port = var.ssh_port
51+
private_key = file(var.ssh_key)
52+
host = var.server_ip_address
53+
}
54+
55+
provisioner "remote-exec" {
56+
inline = [
57+
"curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash"
58+
]
59+
}
60+
}

terraform-modules/k3s/output.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
output "kubeconfig_file" {
2+
description = "kubeconfig file location"
3+
value = "${terraform_data.k3s-gen-kubeconfig.input}"
4+
}

terraform-modules/k3s/variables.tf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
variable "kubeconfigs_location" {
2+
default = "./secrets"
3+
}
4+
5+
variable "server_name" {
6+
}
7+
8+
variable "server_ip_address" {
9+
}
10+
11+
variable "ssh_key" {
12+
}
13+
14+
variable "ssh_port" {
15+
default = "22"
16+
}
17+
18+
variable "ssh_user" {
19+
default = "root"
20+
}

0 commit comments

Comments
 (0)