initial k3s on MicroOS on Hetzner ok

This commit is contained in:
Karim Naufal
2022-02-06 08:40:51 +01:00
parent 4eaebce270
commit 7532e7a4d5
14 changed files with 286 additions and 265 deletions

View File

@@ -14,23 +14,6 @@ resource "hcloud_server" "first_control_plane" {
"engine" = "k3s"
}
provisioner "file" {
content = templatefile("${path.module}/templates/master.tpl", {
name = self.name
ssh_public_key = local.ssh_public_key
k3s_token = random_password.k3s_token.result
master_ip = local.first_control_plane_network_ip
})
destination = "/tmp/config.yaml"
connection {
user = "root"
private_key = local.ssh_private_key
agent_identity = local.ssh_identity
host = self.ipv4_address
}
}
provisioner "file" {
content = templatefile("${path.module}/templates/config.ign.tpl", {
name = self.name
@@ -46,9 +29,9 @@ resource "hcloud_server" "first_control_plane" {
}
}
# Install k3os
# Install MicroOS
provisioner "remote-exec" {
inline = local.microOS_install_commands
inline = local.MicroOS_install_commands
connection {
user = "root"
@@ -57,18 +40,61 @@ resource "hcloud_server" "first_control_plane" {
host = self.ipv4_address
}
}
/*
# Wait for MicroOS to be ready and fetch kubeconfig.yaml
# Wait for MicroOS to reboot and be ready
provisioner "local-exec" {
command = "sleep 60 && ping ${self.ipv4_address} | grep --line-buffered 'bytes from' | head -1 && sleep 30"
}
# Generating k3s master config file
provisioner "file" {
content = templatefile("${path.module}/templates/master_config.yaml.tpl", {
node_ip = local.first_control_plane_network_ip
token = random_password.k3s_token.result
node_name = self.name
})
destination = "/etc/rancher/k3s/config.yaml"
connection {
user = "root"
private_key = local.ssh_private_key
agent_identity = local.ssh_identity
host = self.ipv4_address
}
}
# Run the first control plane
provisioner "remote-exec" {
inline = [
"set -ex",
# first we disable automatic reboot (after transactional updates), and configure the reboot method as kured
"rebootmgrctl set-strategy off && echo 'REBOOT_METHOD=kured' > /etc/transactional-update.conf",
# then we initiate the cluster
"systemctl --now enable k3s-server",
]
connection {
user = "root"
private_key = local.ssh_private_key
agent_identity = local.ssh_identity
host = self.ipv4_address
}
}
# Get the Kubeconfig, and wait for the node to be available
provisioner "local-exec" {
command = <<-EOT
sleep 60 && ping ${self.ipv4_address} | grep --line-buffered "bytes from" | head -1 && sleep 100 && scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ${local.ssh_identity_file} rancher@${self.ipv4_address}:/etc/rancher/k3s/k3s.yaml ${path.module}/kubeconfig.yaml
set -ex
sleep 30
scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ${local.ssh_identity_file} root@${self.ipv4_address}:/etc/rancher/k3s/k3s.yaml ${path.module}/kubeconfig.yaml
sed -i -e 's/127.0.0.1/${self.ipv4_address}/g' ${path.module}/kubeconfig.yaml
sleep 30
EOT
}
# Install Hetzner CCM and CSI
# Install the Hetzner CCM and CSI
provisioner "local-exec" {
command = <<-EOT
set -ex
kubectl -n kube-system create secret generic hcloud --from-literal=token=${var.hcloud_token} --from-literal=network=${hcloud_network.k3s.name} --kubeconfig ${path.module}/kubeconfig.yaml
kubectl apply -k ${dirname(local_file.hetzner_ccm_config.filename)} --kubeconfig ${path.module}/kubeconfig.yaml
kubectl -n kube-system create secret generic hcloud-csi --from-literal=token=${var.hcloud_token} --kubeconfig ${path.module}/kubeconfig.yaml
@@ -80,7 +106,7 @@ resource "hcloud_server" "first_control_plane" {
provisioner "local-exec" {
command = "kubectl apply -f ${local_file.traefik_config.filename} --kubeconfig ${path.module}/kubeconfig.yaml"
}
*/
network {
network_id = hcloud_network.k3s.id
ip = local.first_control_plane_network_ip