chore/k8s-single-node-k3s via Terraform

Se connecte à la machine on-premise via SSH et installe k3s single-node puis instancie le module.

apply reste à faire une fois la machine prête.
This commit is contained in:
Dorian PESCE
2026-09-15 10:13:41 +02:00
parent 06a8ae42d2
commit a2727f9b5a
19 changed files with 204 additions and 1404 deletions
+11
View File
@@ -0,0 +1,11 @@
module "k3s" {
source = "../../modules/k3s"
ssh_host = var.ssh_host
ssh_port = var.ssh_port
ssh_user = var.ssh_user
ssh_private_key_path = var.ssh_private_key_path
k3s_version = var.k3s_version
k3s_disable_components = var.k3s_disable_components
kubeconfig_output_path = var.kubeconfig_output_path
}
@@ -0,0 +1,9 @@
output "kubeconfig_path" {
description = "Chemin local du kubeconfig recupere apres installation."
value = module.k3s.kubeconfig_path
}
output "node_host" {
description = "Adresse du serveur sur lequel k3s est installe."
value = module.k3s.node_host
}
@@ -0,0 +1,7 @@
ssh_host = "10.0.0.10"
ssh_port = 22
ssh_user = "root"
ssh_private_key_path = "~/.ssh/id_ed25519_enervision"
k3s_version = ""
k3s_disable_components = ["traefik"]
kubeconfig_output_path = "./kubeconfig"
@@ -0,0 +1,40 @@
variable "ssh_host" {
type = string
description = "Adresse IP ou nom d'hote du serveur on-premise de l'ecole."
}
variable "ssh_port" {
type = number
description = "Port SSH du serveur."
default = 22
}
variable "ssh_user" {
type = string
description = "Utilisateur SSH utilise pour l'installation."
default = "root"
}
variable "ssh_private_key_path" {
type = string
description = "Chemin local vers la cle privee SSH."
sensitive = true
}
variable "k3s_version" {
type = string
description = "Version k3s a installer. Chaine vide = derniere version stable."
default = ""
}
variable "k3s_disable_components" {
type = list(string)
description = "Composants embarques k3s a desactiver."
default = ["traefik"]
}
variable "kubeconfig_output_path" {
type = string
description = "Chemin local ou ecrire le kubeconfig recupere apres installation."
default = "./kubeconfig"
}
@@ -0,0 +1,14 @@
terraform {
required_version = ">= 1.7"
required_providers {
null = {
source = "hashicorp/null"
version = "~> 3.2"
}
}
backend "local" {
path = "terraform.tfstate"
}
}
View File
+45
View File
@@ -0,0 +1,45 @@
locals {
sudo_prefix = var.ssh_user == "root" ? "" : "sudo "
install_env = var.k3s_version != "" ? "INSTALL_K3S_VERSION=${var.k3s_version} " : ""
disable_flags = join(" ", [for c in var.k3s_disable_components : "--disable=${c}"])
kubeconfig_cmd = "${local.sudo_prefix}cat /etc/rancher/k3s/k3s.yaml"
}
resource "null_resource" "k3s_install" {
triggers = {
ssh_host = var.ssh_host
k3s_version = var.k3s_version
disable_components = join(",", var.k3s_disable_components)
}
connection {
type = "ssh"
host = var.ssh_host
port = var.ssh_port
user = var.ssh_user
private_key = file(var.ssh_private_key_path)
}
provisioner "remote-exec" {
inline = [
"${local.sudo_prefix}sh -c 'curl -sfL https://get.k3s.io | ${local.install_env}sh -s - server --write-kubeconfig-mode 644 ${local.disable_flags}'",
"until ${local.sudo_prefix}test -f /etc/rancher/k3s/k3s.yaml; do sleep 2; done",
]
}
}
resource "null_resource" "fetch_kubeconfig" {
depends_on = [null_resource.k3s_install]
triggers = {
install_id = null_resource.k3s_install.id
}
provisioner "local-exec" {
interpreter = ["bash", "-c"]
command = <<-EOT
ssh -i "${var.ssh_private_key_path}" -p ${var.ssh_port} -o StrictHostKeyChecking=accept-new ${var.ssh_user}@${var.ssh_host} '${local.kubeconfig_cmd}' \
| sed 's/127.0.0.1/${var.ssh_host}/' > "${var.kubeconfig_output_path}"
EOT
}
}
+9
View File
@@ -0,0 +1,9 @@
output "kubeconfig_path" {
description = "Chemin local du kubeconfig recupere apres installation."
value = var.kubeconfig_output_path
}
output "node_host" {
description = "Adresse de la machine sur laquelle k3s est installe."
value = var.ssh_host
}
+39
View File
@@ -0,0 +1,39 @@
variable "ssh_host" {
type = string
description = "Adresse IP ou nom d'hote de la machine on-premise cible."
}
variable "ssh_port" {
type = number
description = "Port SSH de la machine cible."
default = 22
}
variable "ssh_user" {
type = string
description = "Utilisateur SSH. Si different de root, les commandes d'installation sont prefixees par sudo."
default = "root"
}
variable "ssh_private_key_path" {
type = string
description = "Chemin local vers la cle privee SSH utilisee pour se connecter a la machine cible."
sensitive = true
}
variable "k3s_version" {
type = string
description = "Version k3s a installer (ex: v1.31.2+k3s1). Chaine vide = derniere version stable."
default = ""
}
variable "k3s_disable_components" {
type = list(string)
description = "Composants embarques a desactiver a l'installation (ex: traefik, servicelb)."
default = ["traefik"]
}
variable "kubeconfig_output_path" {
type = string
description = "Chemin local ou ecrire le kubeconfig recupere apres installation."
}
+10
View File
@@ -0,0 +1,10 @@
terraform {
required_version = ">= 1.7"
required_providers {
null = {
source = "hashicorp/null"
version = "~> 3.2"
}
}
}