khuedoan-homelab/infra/cluster.tf

145 lines
3.1 KiB
Terraform
Raw Normal View History

2021-02-13 21:02:32 +07:00
resource "lxd_profile" "kubenode" {
name = "kubenode"
2021-02-12 21:51:19 +07:00
config = {
2021-02-13 21:02:32 +07:00
"limits.cpu" = 2
2021-02-14 01:52:12 +07:00
"limits.memory.swap" = false
2021-02-14 12:54:11 +07:00
"user.access_interface" = "eth0"
2021-02-13 18:54:46 +07:00
"security.nesting" = true
2021-02-14 12:54:11 +07:00
"security.privileged" = true
"linux.kernel_modules" = "ip_tables,ip6_tables,nf_nat,overlay,br_netfilter"
2021-02-13 18:54:46 +07:00
"raw.lxc" = <<-EOT
2021-02-13 16:31:30 +07:00
lxc.apparmor.profile=unconfined
lxc.cap.drop=
lxc.cgroup.devices.allow=a
2021-02-14 01:52:12 +07:00
lxc.mount.auto=proc:rw sys:rw cgroup:rw
2021-02-13 16:31:30 +07:00
EOT
2021-02-13 18:54:46 +07:00
"user.user-data" = <<-EOT
2021-02-13 12:49:59 +07:00
#cloud-config
ssh_authorized_keys:
2021-02-14 12:54:11 +07:00
- ${file(var.ssh_public_key)}
2021-02-13 12:49:59 +07:00
disable_root: false
runcmd:
2021-02-13 22:41:31 +07:00
- curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
- add-apt-repository "deb [arch=$(dpkg --print-architecture)] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
- apt-get update -y
2021-02-14 01:52:12 +07:00
- apt-get install -y docker-ce docker-ce-cli containerd.io
2021-02-13 22:41:31 +07:00
- mkdir -p /etc/systemd/system/docker.service.d/
- printf "[Service]\nMountFlags=shared" > /etc/systemd/system/docker.service.d/mount_flags.conf
2021-02-14 01:52:12 +07:00
- mount --make-rshared /
2021-02-13 22:41:31 +07:00
- systemctl start docker
- systemctl enable docker
2021-02-13 12:49:59 +07:00
EOT
2021-02-12 21:51:19 +07:00
}
# echo "262144" > /sys/module/nf_conntrack/parameters/hashsize
device {
type = "disk"
name = "hashsize"
properties = {
source = "/sys/module/nf_conntrack/parameters/hashsize"
path = "/sys/module/nf_conntrack/parameters/hashsize"
}
}
2021-02-14 01:52:12 +07:00
device {
type = "unix-char"
name = "kmsg"
properties = {
source = "/dev/kmsg"
path = "/dev/kmsg"
}
}
2021-02-13 21:02:32 +07:00
device {
name = "eth0"
type = "nic"
properties = {
nictype = "macvlan"
parent = "eno1"
2021-02-13 21:02:32 +07:00
}
2021-02-12 21:51:19 +07:00
}
2021-02-13 21:02:32 +07:00
device {
type = "disk"
name = "root"
properties = {
pool = "default"
2021-02-13 21:02:32 +07:00
path = "/"
}
}
}
2021-02-14 12:54:11 +07:00
resource "lxd_container" "masters" {
count = 1
name = "master-${count.index}"
image = "ubuntu:20.04"
ephemeral = false
profiles = [lxd_profile.kubenode.name]
}
resource "lxd_container" "workers" {
2021-02-13 21:02:32 +07:00
count = 1
2021-02-14 12:54:11 +07:00
name = "worker-${count.index}"
2021-02-13 22:41:31 +07:00
image = "ubuntu:20.04"
2021-02-13 21:02:32 +07:00
ephemeral = false
profiles = [lxd_profile.kubenode.name]
2021-02-12 21:51:19 +07:00
}
2021-02-13 12:49:59 +07:00
2021-02-13 18:54:46 +07:00
resource "time_sleep" "wait_cloud_init" {
2021-02-14 12:54:11 +07:00
depends_on = [
lxd_container.masters,
lxd_container.workers
]
2021-02-13 12:49:59 +07:00
2021-02-13 22:41:31 +07:00
create_duration = "5m"
2021-02-13 18:54:46 +07:00
}
resource "rke_cluster" "cluster" {
dynamic "nodes" {
2021-02-14 12:54:11 +07:00
for_each = lxd_container.masters
2021-02-13 12:49:59 +07:00
2021-02-13 18:54:46 +07:00
content {
address = nodes.value.ip_address
user = "root"
role = [
"controlplane",
2021-02-14 12:54:11 +07:00
"etcd"
]
ssh_key = file(var.ssh_private_key)
}
}
dynamic "nodes" {
for_each = lxd_container.workers
content {
address = nodes.value.ip_address
user = "root"
role = [
2021-02-13 18:54:46 +07:00
"worker"
]
2021-02-14 12:54:11 +07:00
ssh_key = file(var.ssh_private_key)
2021-02-13 18:54:46 +07:00
}
}
ingress {
provider = "none"
}
2021-02-13 12:49:59 +07:00
2021-02-13 18:54:46 +07:00
ignore_docker_version = true
depends_on = [time_sleep.wait_cloud_init]
}
resource "local_file" "kube_config_yaml" {
filename = "${path.root}/kube_config.yaml"
content = rke_cluster.cluster.kube_config_yaml
}