Implementa K3S-Cluster
This commit is contained in:
@@ -5,8 +5,20 @@ terraform {
|
||||
version = "5.17.0"
|
||||
configuration_aliases = [ aws.main ]
|
||||
}
|
||||
tls = {
|
||||
source = "hashicorp/tls"
|
||||
version = "3.1.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# TERRAFORM SSH KEYS
|
||||
|
||||
resource "tls_private_key" "terraform_ssh_key" {
|
||||
algorithm = "RSA"
|
||||
rsa_bits = 4096
|
||||
}
|
||||
|
||||
|
||||
# EC2 Instances
|
||||
@@ -29,25 +41,12 @@ locals {
|
||||
6443,
|
||||
10250
|
||||
]
|
||||
ports_out = [
|
||||
0,
|
||||
]
|
||||
}
|
||||
|
||||
resource "aws_security_group" "project_pool" {
|
||||
name = "${var.project_name}_pool_security_group"
|
||||
description = "Security group for project pool"
|
||||
|
||||
dynamic "egress" {
|
||||
for_each = toset(local.ports_out)
|
||||
content {
|
||||
from_port = egress.value
|
||||
to_port = egress.value
|
||||
protocol = "tcp"
|
||||
cidr_blocks = ["0.0.0.0/0"]
|
||||
}
|
||||
}
|
||||
|
||||
dynamic "ingress" {
|
||||
for_each = toset(local.ports_in)
|
||||
content {
|
||||
@@ -57,6 +56,15 @@ resource "aws_security_group" "project_pool" {
|
||||
cidr_blocks = ["0.0.0.0/0"]
|
||||
}
|
||||
}
|
||||
|
||||
egress {
|
||||
from_port = 0
|
||||
to_port = 0
|
||||
protocol = "-1"
|
||||
cidr_blocks = ["0.0.0.0/0"]
|
||||
ipv6_cidr_blocks = ["::/0"]
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
resource "aws_instance" "main" {
|
||||
@@ -68,8 +76,25 @@ resource "aws_instance" "main" {
|
||||
|
||||
user_data = templatefile("${path.module}/scripts/setup_server.sh", {
|
||||
extra_key = aws_key_pair.ssh_key_ci_cd.public_key
|
||||
terraform_key = tls_private_key.terraform_ssh_key.public_key_openssh
|
||||
})
|
||||
|
||||
provisioner "remote-exec" {
|
||||
connection {
|
||||
type = "ssh"
|
||||
user = "ubuntu"
|
||||
agent = false
|
||||
private_key = tls_private_key.terraform_ssh_key.private_key_pem
|
||||
host = self.public_ip
|
||||
}
|
||||
|
||||
inline = [
|
||||
"echo 'curl -sfL https://get.k3s.io | K3S_TOKEN=\"${var.k3s_token}\" sh -' >> /home/ubuntu/setup.sh",
|
||||
"chmod +x /home/ubuntu/setup.sh",
|
||||
"exec /home/ubuntu/setup.sh | tee logs.txt"
|
||||
]
|
||||
}
|
||||
|
||||
tags = {
|
||||
Name = "${var.project_name}-main"
|
||||
}
|
||||
@@ -85,8 +110,25 @@ resource "aws_instance" "worker" {
|
||||
|
||||
user_data = templatefile("${path.module}/scripts/setup_server.sh", {
|
||||
extra_key = aws_key_pair.ssh_key_ci_cd.public_key
|
||||
terraform_key = tls_private_key.terraform_ssh_key.public_key_openssh
|
||||
})
|
||||
|
||||
provisioner "remote-exec" {
|
||||
connection {
|
||||
type = "ssh"
|
||||
user = "ubuntu"
|
||||
agent = false
|
||||
private_key = tls_private_key.terraform_ssh_key.private_key_pem
|
||||
host = self.public_ip
|
||||
}
|
||||
|
||||
inline = [
|
||||
"echo 'curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC=\"agent\" K3S_TOKEN=\"${var.k3s_token}\" sh -s - --server ${var.project_domain}:6443' >> /home/ubuntu/setup.sh",
|
||||
"chmod +x /home/ubuntu/setup.sh",
|
||||
"exec /home/ubuntu/setup.sh | tee logs.txt"
|
||||
]
|
||||
}
|
||||
|
||||
tags = {
|
||||
Name = "${var.project_name}-worker-${count.index+1}"
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
#!/bin/bash
|
||||
|
||||
|
||||
echo -e "\n${extra_key}" >> /home/ubuntu/.ssh/authorized_keys;
|
||||
echo -e "${extra_key}" >> /home/ubuntu/.ssh/authorized_keys;
|
||||
echo -e "${terraform_key}" >> /home/ubuntu/.ssh/authorized_keys;
|
||||
|
||||
echo "export TERM='xterm-256color';" > /home/ubuntu/.profile;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user