From 61f80939515dd0efe1c2595414a98c0399acae57 Mon Sep 17 00:00:00 2001 From: Karim Naufal Date: Tue, 30 Nov 2021 23:09:34 +0100 Subject: [PATCH] k3os master ok --- agents.tf | 51 --------------------------------------- init.cfg | 42 -------------------------------- main.tf | 57 +++++++++++++++++++------------------------- master.tf | 48 ++++++++++++++++++------------------- output.tf | 9 ------- servers.tf | 51 --------------------------------------- templates/agent.tpl | 29 ++++++++++++++++++++++ templates/master.tpl | 38 +++++++++++++++++++++++++++++ variables.tf | 23 ------------------ 9 files changed, 114 insertions(+), 234 deletions(-) delete mode 100644 agents.tf delete mode 100644 init.cfg delete mode 100644 output.tf delete mode 100644 servers.tf create mode 100644 templates/agent.tpl create mode 100644 templates/master.tpl diff --git a/agents.tf b/agents.tf deleted file mode 100644 index 62f50e5..0000000 --- a/agents.tf +++ /dev/null @@ -1,51 +0,0 @@ -resource "hcloud_server" "agents" { - count = var.agents_num - name = "k3s-agent-${count.index}" - - image = data.hcloud_image.linux.name - server_type = var.agent_server_type - location = var.location - ssh_keys = [hcloud_ssh_key.default.id] - firewall_ids = [hcloud_firewall.k3s.id] - - - labels = { - "provisioner" = "terraform", - "engine" = "k3s", - "k3s_upgrade" = "true" - } - - user_data = data.template_cloudinit_config.init_cfg.rendered - - provisioner "remote-exec" { - inline = var.initial_commands - - connection { - user = "root" - private_key = file(var.private_key) - host = self.ipv4_address - } - } - - provisioner "remote-exec" { - inline = [ - "curl -sfL https://get.k3s.io | K3S_TOKEN=${random_password.k3s_cluster_secret.result} sh -s - agent --server https://${local.first_control_plane_network_ip}:6443 --node-ip=${cidrhost(hcloud_network.k3s.ip_range, 2 + var.servers_num + count.index)} ${var.k3s_agent_flags}" - ] - - connection { - user = "root" - private_key = file(var.private_key) - host = self.ipv4_address - } - } - - network { - network_id = hcloud_network.k3s.id - ip = cidrhost(hcloud_network.k3s.ip_range, 2 + var.servers_num + count.index) - } - - depends_on = [ - hcloud_server.first_control_plane, - hcloud_network_subnet.k3s - ] -} diff --git a/init.cfg b/init.cfg deleted file mode 100644 index 7cd6f3e..0000000 --- a/init.cfg +++ /dev/null @@ -1,42 +0,0 @@ - #cloud-config - write_files: - - path: /etc/sysctl.d/k8s.conf - content: | - net.bridge.bridge-nf-call-ip6tables = 1 - net.bridge.bridge-nf-call-iptables = 1 - - path: /etc/sysctl.d/99-override_cilium_rp_filter.conf - content: | - net.ipv4.conf.lxc*.rp_filter = 0 - - path: /etc/fail2ban/jail.local - content: | - [sshd] - enabled = true - banaction = iptables-multiport - - path: /etc/yum.repos.d/rpm-rancher-io.repo - content: | - [rancher] - name=Rancher - baseurl=https://rpm.rancher.io/k3s/stable/common/centos/8/noarch - enabled=1 - gpgcheck=1 - gpgkey=https://rpm.rancher.io/public.key - - path: /etc/dnf/automatic.conf - content: | - [commands] - upgrade_type = default - random_sleep = 10000 - network_online_timeout = 60 - download_updates = yes - apply_updates = yes - [emitters] - emit_via = stdio - [base] - debuglevel = 1 - - runcmd: - - sed -i -e '/^PasswordAuthentication/s/^.*$/PasswordAuthentication no/' /etc/ssh/sshd_config - - sed -i -e '/^X11Forwarding/s/^.*$/X11Forwarding no/' /etc/ssh/sshd_config - - sed -i -e '/^#MaxAuthTries/s/^.*$/MaxAuthTries 2/' /etc/ssh/sshd_config - - sed -i -e '/^#AllowTcpForwarding/s/^.*$/AllowTcpForwarding no/' /etc/ssh/sshd_config - - sed -i -e '/^#AllowAgentForwarding/s/^.*$/AllowAgentForwarding no/' /etc/ssh/sshd_config - - sed -i -e '/^#AuthorizedKeysFile/s/^.*$/AuthorizedKeysFile .ssh\/authorized_keys/' /etc/ssh/sshd_config \ No newline at end of file diff --git a/main.tf b/main.tf index 69d36b3..722cd0c 100644 --- a/main.tf +++ b/main.tf @@ -1,11 +1,11 @@ -resource "random_password" "k3s_cluster_secret" { +resource "random_password" "k3s_token" { length = 48 special = false } resource "hcloud_ssh_key" "default" { name = "K3S terraform module - Provisioning SSH key" - public_key = file(var.public_key) + public_key = local.ssh_public_key } resource "hcloud_network" "k3s" { @@ -77,43 +77,34 @@ resource "hcloud_firewall" "k3s" { "0.0.0.0/0" ] } -} -data "hcloud_image" "linux" { - name = "fedora-34" -} - -data "template_file" "init_cfg" { - template = file("${path.module}/init.cfg") -} - -# Render a multi-part cloud-init config making use of the part -# above, and other source files -data "template_cloudinit_config" "init_cfg" { - gzip = true - base64_encode = true - - # Main cloud-config configuration file. - part { - filename = "init.cfg" - content_type = "text/cloud-config" - content = data.template_file.init_cfg.rendered + # Allow ping on ipv4 + rule { + direction = "in" + protocol = "icmp" + source_ips = [ + "0.0.0.0/0" + ] } } -data "template_file" "ccm" { - template = file("${path.module}/manifests/hcloud-ccm-net.yaml") +data "hcloud_image" "linux" { + name = "ubuntu-20.04" } -data "template_file" "plans" { - template = file("${path.module}/manifests/upgrade/plans.yaml") -} - -data "template_file" "kured" { - template = file("${path.module}/manifests/upgrade/kured.yaml") -} - - locals { first_control_plane_network_ip = cidrhost(hcloud_network.k3s.ip_range, 2) + name_master = "k3s-control-plane-0" + ssh_public_key = trimspace(file(var.public_key)) +} + +data "template_file" "master" { + template = file("${path.module}/templates/master.tpl") + + vars = { + name = local.name_master + ssh_public_key = local.ssh_public_key + k3s_token = random_password.k3s_token.result + ip = local.first_control_plane_network_ip + } } diff --git a/master.tf b/master.tf index e2377ee..d137431 100644 --- a/master.tf +++ b/master.tf @@ -1,23 +1,21 @@ resource "hcloud_server" "first_control_plane" { - name = "k3s-control-plane-0" + name = local.name_master image = data.hcloud_image.linux.name + rescue = "linux64" server_type = var.control_plane_server_type location = var.location ssh_keys = [hcloud_ssh_key.default.id] firewall_ids = [hcloud_firewall.k3s.id] - labels = { "provisioner" = "terraform", - "engine" = "k3s", - "k3s_upgrade" = "true" + "engine" = "k3s" } - user_data = data.template_cloudinit_config.init_cfg.rendered - - provisioner "remote-exec" { - inline = var.initial_commands + provisioner "file" { + content = data.template_file.master.rendered + destination = "/tmp/config.yaml" connection { user = "root" @@ -26,19 +24,15 @@ resource "hcloud_server" "first_control_plane" { } } + provisioner "remote-exec" { inline = [ - "curl -sfL https://get.k3s.io | K3S_TOKEN=${random_password.k3s_cluster_secret.result} sh -s - server --cluster-init --node-ip=${local.first_control_plane_network_ip} --advertise-address=${local.first_control_plane_network_ip} --tls-san=${local.first_control_plane_network_ip} ${var.k3s_server_flags}", - "until systemctl is-active --quiet k3s.service; do sleep 1; done", - "until kubectl get node ${self.name}; do sleep 1; done", - "kubectl -n kube-system create secret generic hcloud --from-literal=token=${var.hcloud_token} --from-literal=network=${hcloud_network.k3s.name}", - "kubectl apply -f -< /etc/network/interfaces +- rc-update del connman boot +- rc-update add networking boot +- rc-update add ntpd default +k3os: + k3s_args: + - server + - "--cluster-init" + - "--disable-cloud-controller" + - "--disable=traefik" + - "--disable=servicelb" + - "--disable=local-storage" + - "--flannel-iface=eth1" + - "--node-ip" + - "${ip}" + - "--advertise-address" + - "${ip}" + - "--tls-san" + - "${ip}" + - "--kubelet-arg" + - "cloud-provider=external" + token: ${k3s_token} + ntp_servers: + - 0.de.pool.ntp.org + - 1.de.pool.ntp.org + dns_nameservers: + - 8.8.8.8 + - 1.1.1.1 + - 2001:4860:4860::8888 + - 2606:4700:4700::1111 diff --git a/variables.tf b/variables.tf index b8736fa..c561b9e 100644 --- a/variables.tf +++ b/variables.tf @@ -43,26 +43,3 @@ variable "agent_server_type" { description = "Default agent server type" default = "cx21" } - -variable "k3s_server_flags" { - description = "Important flags to make our setup work" - default = "--disable-cloud-controller --disable-network-policy --disable=traefik --disable=servicelb --disable='local-storage' --kubelet-arg='cloud-provider=external' --flannel-backend=none" -} - -variable "k3s_agent_flags" { - description = "Important flags to make our setup work" - default = "--kubelet-arg='cloud-provider=external'" -} - -variable "initial_commands" { - description = "Initial commands to run on each machines." - default = [ - "dnf upgrade -y", - "dnf install -y container-selinux selinux-policy-base fail2ban k3s-selinux dnf-automatic jq dnf-utils", - "systemctl enable --now fail2ban", - "systemctl enable --now dnf-automatic.timer", - "systemctl disable firewalld", - "grubby --args='systemd.unified_cgroup_hierarchy=0' --update-kernel=ALL", - "sleep 11; shutdown -r +0" - ] -}