Skip to content

Commit

Permalink
k8s: create cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
furiousme committed Oct 20, 2024
1 parent b8c95c0 commit 9d50d28
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 2 deletions.
5 changes: 3 additions & 2 deletions bastion.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
resource "aws_instance" "terraform_course_bastion" {
ami = "ami-06b21ccaeff8cd686"
ami = "ami-0ddc798b3f1a5117e"
instance_type = "t2.micro"
subnet_id = aws_subnet.terraform_course_public_subnet_1.id
key_name = var.ec2_key_name
Expand All @@ -13,4 +13,5 @@ resource "aws_instance" "terraform_course_bastion" {
output "terraform_course_bastion_ip_addr" {
value = aws_instance.terraform_course_bastion.public_ip
sensitive = true
}
}

41 changes: 41 additions & 0 deletions cluster.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
resource "aws_instance" "terraform_course_k8s_server" {
ami = "ami-0ddc798b3f1a5117e"
instance_type = "t2.micro"
subnet_id = aws_subnet.terraform_course_private_subnet_1.id
key_name = var.ec2_key_name
security_groups = [aws_security_group.terraform_course_k8s_sg.id]

user_data = templatefile("k8s_server.sh", {})

tags = {
Name = "terraform_course_k8s_server"
}
}

resource "aws_instance" "terraform_course_k8s_agent" {
ami = "ami-0ddc798b3f1a5117e"
instance_type = "t2.micro"
subnet_id = aws_subnet.terraform_course_private_subnet_2.id
key_name = var.ec2_key_name
security_groups = [aws_security_group.terraform_course_k8s_sg.id]
depends_on = [aws_instance.terraform_course_k8s_server]

user_data = templatefile("k8s_agent.sh", {
token = var.k8s_token,
server_private_ip = aws_instance.terraform_course_k8s_server.private_ip
})

tags = {
Name = "terraform_course_k8s_agent"
}
}

output "terraform_course_k8s_server_ip_addr" {
value = aws_instance.terraform_course_k8s_server.private_ip
sensitive = true
}

output "terraform_course_k8s_agent_ip_addr" {
value = aws_instance.terraform_course_k8s_agent.private_ip
sensitive = true
}
2 changes: 2 additions & 0 deletions k8s_agent.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
curl -sfL https://get.k3s.io | K3S_URL="https://${server_private_ip}:6443" K3S_TOKEN=${token} sh -s -
1 change: 1 addition & 0 deletions k8s_server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
curl -sfL https://get.k3s.io | sh -
20 changes: 20 additions & 0 deletions securitygroups.tf
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,23 @@ resource "aws_security_group" "terraform_course_bastion_sg" {
Name = "terraform_course_bastion_sg"
}
}

resource "aws_security_group" "terraform_course_k8s_sg" {
vpc_id = aws_vpc.terraform_course_main_vpc.id
ingress {
from_port = 6443
to_port = 6443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}

tags = {
Name = "terraform_course_k8s_sg"
}
}
5 changes: 5 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@ variable "ip_address" {
variable "ec2_key_name" {
type = string
sensitive = true
}

variable "k8s_token" {
type = string
sensitive = true
}

0 comments on commit 9d50d28

Please sign in to comment.