moved k3s-selinux install to combustion

This commit is contained in:
Karim Naufal 2022-02-17 13:19:21 +01:00
parent f59225bf9c
commit 1f0a03b335
6 changed files with 38 additions and 47 deletions

View File

@ -31,6 +31,12 @@ resource "hcloud_server" "agents" {
destination = "/root/config.ign"
}
# Combustion script file to install k3s-selinux
provisioner "file" {
content = local.combustion_script
destination = "/root/script"
}
# Install MicroOS
provisioner "remote-exec" {
inline = local.microOS_install_commands
@ -69,20 +75,6 @@ resource "hcloud_server" "agents" {
inline = local.install_k3s_agent
}
# Issue a reboot command and wait for the node to reboot
provisioner "local-exec" {
command = "ssh ${local.ssh_args} root@${self.ipv4_address} '(sleep 2; reboot)&'; sleep 3"
}
provisioner "local-exec" {
command = <<-EOT
until ssh ${local.ssh_args} -o ConnectTimeout=2 root@${self.ipv4_address} true 2> /dev/null
do
echo "Waiting for MicroOS to reboot and become available..."
sleep 2
done
EOT
}
# Upon reboot verify that k3s agent starts correctly
provisioner "remote-exec" {
inline = [
@ -97,7 +89,6 @@ resource "hcloud_server" "agents" {
]
}
network {
network_id = hcloud_network.k3s.id
ip = cidrhost(hcloud_network_subnet.k3s.ip_range, 513 + count.index)

View File

@ -20,6 +20,7 @@ locals {
microOS_install_commands = [
"set -ex",
"apt-get update",
"apt-get install -y aria2",
"aria2c --follow-metalink=mem https://download.opensuse.org/tumbleweed/appliances/openSUSE-MicroOS.x86_64-kvm-and-xen.qcow2.meta4",
"qemu-img convert -p -f qcow2 -O host_device $(ls -a | grep -ie '^opensuse.*microos.*qcow2$') /dev/sda",
@ -32,9 +33,20 @@ locals {
"mount /dev/sda5 /mnt",
"mkdir /mnt/ignition",
"cp /root/config.ign /mnt/ignition/config.ign",
"mkdir /mnt/combustion",
"cp /root/script /mnt/combustion/script",
"umount /mnt"
]
combustion_script = <<EOF
#!/bin/bash
# combustion: network
rpm --import https://rpm.rancher.io/public.key
zypper refresh
zypper --gpg-auto-import-keys install -y https://rpm.rancher.io/k3s/stable/common/microos/noarch/k3s-selinux-0.4-1.sle.noarch.rpm
udevadm settle
EOF
common_commands_install_k3s = [
"set -ex",
# first we disable automatic reboot (after transactional updates), and configure the reboot method as kured
@ -45,7 +57,7 @@ locals {
"mv /tmp/config.yaml /etc/rancher/k3s/config.yaml"
]
install_k3s_server = concat(local.common_commands_install_k3s, ["curl -sfL https://get.k3s.io | INSTALL_K3S_SKIP_START=true INSTALL_K3S_EXEC=server sh -"])
install_k3s_server = concat(local.common_commands_install_k3s, ["curl -sfL https://get.k3s.io | INSTALL_K3S_SKIP_SELINUX_RPM=true INSTALL_K3S_SKIP_START=true INSTALL_K3S_EXEC=server sh -"])
install_k3s_agent = concat(local.common_commands_install_k3s, ["curl -sfL https://get.k3s.io | INSTALL_K3S_SKIP_START=true INSTALL_K3S_EXEC=agent sh -"])
install_k3s_agent = concat(local.common_commands_install_k3s, ["curl -sfL https://get.k3s.io | INSTALL_K3S_SKIP_SELINUX_RPM=true INSTALL_K3S_EXEC=agent sh -"])
}

View File

@ -153,8 +153,10 @@ resource "hcloud_placement_group" "k3s" {
}
}
/*
data "hcloud_load_balancer" "traefik" {
name = "traefik"
depends_on = [hcloud_server.agents[0]]
}
*/

View File

@ -29,6 +29,12 @@ resource "hcloud_server" "first_control_plane" {
destination = "/root/config.ign"
}
# Combustion script file to install k3s-selinux
provisioner "file" {
content = local.combustion_script
destination = "/root/script"
}
# Install MicroOS
provisioner "remote-exec" {
inline = local.microOS_install_commands
@ -66,30 +72,15 @@ resource "hcloud_server" "first_control_plane" {
destination = "/tmp/config.yaml"
}
# Install k3s server
provisioner "remote-exec" {
inline = local.install_k3s_server
}
# Issue a reboot command and wait for the node to reboot
provisioner "local-exec" {
command = "ssh ${local.ssh_args} root@${self.ipv4_address} '(sleep 2; reboot)&'; sleep 3"
}
provisioner "local-exec" {
command = <<-EOT
until ssh ${local.ssh_args} -o ConnectTimeout=2 root@${self.ipv4_address} true 2> /dev/null
do
echo "Waiting for MicroOS to reboot and become available..."
sleep 2
done
EOT
}
# Upon reboot verify that the k3s server is starts, and wait for k3s to be ready to receive commands
provisioner "remote-exec" {
inline = [
"systemctl start k3s",
# prepare the post_install directory
"mkdir -p /tmp/post_install",
# wait for k3s to become ready

View File

@ -8,10 +8,12 @@ output "agents_public_ip" {
description = "The public IP addresses of the agent server."
}
/*
output "load_balancer_public_ip" {
description = "The public IPv4 address of the Hetzner load balancer"
value = data.hcloud_load_balancer.traefik.ipv4
}
*/
output "kubeconfig_file" {
value = local.kubeconfig_external

View File

@ -30,6 +30,12 @@ resource "hcloud_server" "control_planes" {
destination = "/root/config.ign"
}
# Combustion script file to install k3s-selinux
provisioner "file" {
content = local.combustion_script
destination = "/root/script"
}
# Install MicroOS
provisioner "remote-exec" {
inline = local.microOS_install_commands
@ -74,23 +80,10 @@ resource "hcloud_server" "control_planes" {
inline = local.install_k3s_server
}
# Issue a reboot command and wait for the node to reboot
provisioner "local-exec" {
command = "ssh ${local.ssh_args} root@${self.ipv4_address} '(sleep 2; reboot)&'; sleep 3"
}
provisioner "local-exec" {
command = <<-EOT
until ssh ${local.ssh_args} -o ConnectTimeout=2 root@${self.ipv4_address} true 2> /dev/null
do
echo "Waiting for MicroOS to reboot and become available..."
sleep 2
done
EOT
}
# Upon reboot verify that the k3s server starts correctly
provisioner "remote-exec" {
inline = [
"systemctl start k3s",
<<-EOT
timeout 120 bash <<EOF
until systemctl status k3s > /dev/null; do