k3os master ok

This commit is contained in:
Karim Naufal
2021-11-30 23:09:34 +01:00
parent f308220bfe
commit 61f8093951
9 changed files with 114 additions and 234 deletions

View File

@@ -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 -<<EOF\n${data.template_file.ccm.rendered}\nEOF",
"kubectl -n kube-system create secret generic hcloud-csi --from-literal=token=${var.hcloud_token}",
"kubectl apply -f https://raw.githubusercontent.com/hetznercloud/csi-driver/master/deploy/kubernetes/hcloud-csi.yml",
"kubectl apply -f https://raw.githubusercontent.com/rancher/system-upgrade-controller/master/manifests/system-upgrade-controller.yaml",
"sleep 33",
"kubectl apply -f -<<EOF\n${data.template_file.plans.rendered}\nEOF",
"kubectl apply -f -<<EOF\n${data.template_file.kured.rendered}\nEOF",
"apt install -y grub-efi grub-pc-bin mtools xorriso",
"latest=$(curl -s https://api.github.com/repos/rancher/k3os/releases | jq '.[0].tag_name')",
"curl -Lo ./install.sh https://raw.githubusercontent.com/rancher/k3os/$(echo $latest | xargs)/install.sh",
"chmod +x ./install.sh",
"./install.sh --config /tmp/config.yaml /dev/sda https://github.com/rancher/k3os/releases/download/$(echo $latest | xargs)/k3os-amd64.iso",
"shutdown -r now"
]
connection {
@@ -49,15 +43,19 @@ resource "hcloud_server" "first_control_plane" {
}
provisioner "local-exec" {
command = "scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ${var.private_key} root@${self.ipv4_address}:/etc/rancher/k3s/k3s.yaml ${path.module}/kubeconfig.yaml"
command = <<-EOT
ping ${self.ipv4_address} | grep --line-buffered "bytes from" | head -1 && sleep 60 && scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ${var.private_key} rancher@${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
EOT
}
provisioner "local-exec" {
command = "sed -i -e 's/127.0.0.1/${self.ipv4_address}/g' ${path.module}/kubeconfig.yaml"
}
provisioner "local-exec" {
command = "helm repo add cilium https://helm.cilium.io/ --kubeconfig ${path.module}/kubeconfig.yaml; helm repo update --kubeconfig ${path.module}; helm install --values=manifests/helm/cilium/values.yaml cilium cilium/cilium -n kube-system --kubeconfig ${path.module}/kubeconfig.yaml"
command = <<-EOT
kubectl -n kube-system create secret generic hcloud --from-literal=token=${random_password.k3s_token.result} --from-literal=network=${hcloud_network.k3s.name} --kubeconfig ${path.module}/kubeconfig.yaml
kubectl apply -f ${path.module}/manifests/hcloud-ccm-net.yaml --kubeconfig ${path.module}/kubeconfig.yaml
kubectl -n kube-system create secret generic hcloud-csi --from-literal=token=${random_password.k3s_token.result} --kubeconfig ${path.module}/kubeconfig.yaml
kubectl apply -f https://raw.githubusercontent.com/hetznercloud/csi-driver/master/deploy/kubernetes/hcloud-csi.yml --kubeconfig ${path.module}/kubeconfig.yaml
EOT
}
network {