From 4da32935696b9add8e6f250314902b73ebe5b718 Mon Sep 17 00:00:00 2001 From: kjuulh Date: Sat, 19 Feb 2022 21:29:38 +0100 Subject: [PATCH] Add nodes --- .../ansible/roles/k3s/node/tasks/main.yml | 15 +++++++ .../roles/k3s/node/templates/k3s.service.j2 | 24 ++++++++++ infrastructure/create-resources/hcloud.tf | 45 +++++++++++++++++-- .../create-resources/templates/hosts.tpl | 5 ++- 4 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 infrastructure/create-resources/ansible/roles/k3s/node/tasks/main.yml create mode 100644 infrastructure/create-resources/ansible/roles/k3s/node/templates/k3s.service.j2 diff --git a/infrastructure/create-resources/ansible/roles/k3s/node/tasks/main.yml b/infrastructure/create-resources/ansible/roles/k3s/node/tasks/main.yml new file mode 100644 index 0000000..64050cf --- /dev/null +++ b/infrastructure/create-resources/ansible/roles/k3s/node/tasks/main.yml @@ -0,0 +1,15 @@ +--- +- name: Copy K3s service file + template: + src: "k3s.service.j2" + dest: "{{ systemd_dir }}/k3s-node.service" + owner: root + group: root + mode: 0755 + +- name: Enable and check K3s service + systemd: + name: k3s-node + daemon_reload: yes + state: restarted + enabled: yes diff --git a/infrastructure/create-resources/ansible/roles/k3s/node/templates/k3s.service.j2 b/infrastructure/create-resources/ansible/roles/k3s/node/templates/k3s.service.j2 new file mode 100644 index 0000000..4c6f3f8 --- /dev/null +++ b/infrastructure/create-resources/ansible/roles/k3s/node/templates/k3s.service.j2 @@ -0,0 +1,24 @@ +[Unit] +Description=Lightweight Kubernetes +Documentation=https://k3s.io +After=network-online.target + +[Service] +Type=notify +ExecStartPre=-/sbin/modprobe br_netfilter +ExecStartPre=-/sbin/modprobe overlay +ExecStart=/usr/local/bin/k3s agent --server https://{{ master_ip }}:6443 --token {{ hostvars[groups['serverctl_master_hosts'][0]]['token'] }} {{ extra_agent_args | default("") }} +KillMode=process +Delegate=yes +# Having non-zero Limit*s causes performance problems due to accounting overhead +# in the kernel. We recommend using cgroups to do container-local accounting. +LimitNOFILE=1048576 +LimitNPROC=infinity +LimitCORE=infinity +TasksMax=infinity +TimeoutStartSec=0 +Restart=always +RestartSec=5s + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/infrastructure/create-resources/hcloud.tf b/infrastructure/create-resources/hcloud.tf index 7332faf..3dc947d 100644 --- a/infrastructure/create-resources/hcloud.tf +++ b/infrastructure/create-resources/hcloud.tf @@ -1,11 +1,17 @@ +variable "serverctl_master_count" { + default = 2 +} + +variable "serverctl_node_count" { + default = 2 +} + + resource "hcloud_placement_group" "serverctl_master" { name = "serverctl_master_group" type = "spread" } -variable "serverctl_master_count" { - default = 1 -} resource "hcloud_server" "serverctl_master" { count = var.serverctl_master_count @@ -34,10 +40,43 @@ resource "hcloud_server" "serverctl_master" { } } +resource "hcloud_placement_group" "serverctl_node" { + name = "serverctl_node_group" + type = "spread" +} + +resource "hcloud_server" "serverctl_node" { + count = var.serverctl_node_count + name = "serverctl-node-${count.index}" + image = "debian-11" + server_type = "cx11" + ssh_keys = [ + var.hcloud_serverctl_ssh_key_id + ] + placement_group_id = hcloud_placement_group.serverctl_node.id + + + lifecycle { + create_before_destroy = true + } + + provisioner "remote-exec" { + inline = ["sudo apt update", "sudo apt install python3 -y", "echo Done!"] + + connection { + host = self.ipv4_address + type = "ssh" + user = "root" + private_key = file(var.pvt_key) + } + } +} + resource "local_file" "hosts_cfg" { content = templatefile("${path.module}/templates/hosts.tpl", { serverctl_masters = hcloud_server.serverctl_master.*.ipv4_address + serverctl_nodes = hcloud_server.serverctl_node.*.ipv4_address } ) filename = "ansible/inventory/hosts.cfg" diff --git a/infrastructure/create-resources/templates/hosts.tpl b/infrastructure/create-resources/templates/hosts.tpl index df1da0a..3d857e1 100644 --- a/infrastructure/create-resources/templates/hosts.tpl +++ b/infrastructure/create-resources/templates/hosts.tpl @@ -4,7 +4,10 @@ ${ip} %{ endfor ~} [serverctl_node_hosts] +%{ for ip in serverctl_nodes ~} +${ip} +%{ endfor ~} [serverctl_cluster:children] serverctl_master_hosts -serverctl_node_hosts \ No newline at end of file +serverctl_node_hosts