实例处于不健康状态 - ALB
我正在尝试使用Terraform创建ALB,但我的实例处于不健康状态。我的应用程序是在3000端口上收听的,因此我试图在3000端口上达到EC2实例(以私有子网为单位)。如果我安装Apache并创建index.html并通过ALB健康检查中的路径会变得健康。我想在端口3000
resource "aws_lb" "alb" {
name = "APP-LB"
internal = false
load_balancer_type = "application"
security_groups = [aws_security_group.alb_sg.id]
subnets = aws_subnet.public.*.id
}
resource "aws_lb_target_group" "alb_tg" {
name = "ALB-TG"
port = 3000
protocol = "HTTP"
target_type = "instance"
vpc_id = aws_vpc.main.id
health_check {
healthy_threshold = 2
unhealthy_threshold = 3
timeout = 5
interval = 20
protocol = "HTTP"
port = 3000
path = "/"
matcher = "200"
}
}
resource "aws_lb_listener" "listener" {
load_balancer_arn = aws_lb.alb.arn
port = 80
protocol = "HTTP"
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.alb_tg.arn
}
}
resource "aws_lb_listener" "listener2" {
load_balancer_arn = aws_lb.alb.arn
port = 3000
protocol = "HTTP"
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.alb_tg.arn
}
}
resource "aws_lb_target_group_attachment" "ec2_instance" {
target_group_arn = aws_lb_target_group.alb_tg.arn
target_id = aws_instance.web.id
port = 3000
}
resource "aws_security_group" "alb_sg" {
name = "alb_sg"
description = "For internet traffic"
vpc_id = aws_vpc.main.id
ingress {
description = "incoming for ec2-instance"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "incoming for ec2-instance"
from_port = 3000
to_port = 3000
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 = "alb_sg"
}
}
ec2.tf
resource "aws_security_group" "web_sg" {
name = "web_sg"
description = "Allow TLS inbound traffic"
vpc_id = aws_vpc.main.id
ingress {
description = "MongoDB Traffic"
from_port = 27017
to_port = 27017
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "For application deployed on ec2"
from_port = 3000
to_port = 3000
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "For traffic from appliaction load balancer"
from_port = 80
to_port = 80
protocol = "tcp"
# security_groups = [aws_security_group.alb_sg.id]
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "To ssh into the instancess"
from_port = 22
to_port = 22
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 = "web_sg"
}
}
用户data
#!/bin/bash
curl -sL https://rpm.nodesource.com/setup_17.x | sudo bash -
yum -y install nodejs
sudo rm -rf /etc/yum.repos.d/mongod*
sudo yum clean all
echo '============================================'
echo "======> Installing Mongodb 4.0 ..."
#yum clean all
cat > /etc/yum.repos.d/mongodb-org-4.2.repo << EOF
[mongodb-org-4.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/amazon/2/mongodb-org/4.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc
EOF
sudo yum install -y mongodb-org-4.2.18 mongodb-org-server-4.2.18 mongodb-org-shell-4.2.18 mongodb-org-mongos-4.2.18 mongodb-org-tools-4.2.18
vpc.tf
resource "aws_vpc" "main" {
cidr_block = var.cidr_vpc_block
tags = {
Name = "RMIT Assignment 2"
}
}
data "aws_availability_zones" "azs" {}
resource "aws_subnet" "public" {
vpc_id = aws_vpc.main.id
count = length(var.public)
cidr_block = element(var.public, count.index)
availability_zone = element(var.availability_zone, count.index)
map_public_ip_on_launch = true
tags = {
Name = "public_az${count.index + 1}"
}
}
resource "aws_subnet" "private" {
vpc_id = aws_vpc.main.id
count = length(var.private)
cidr_block = element(var.private, count.index)
availability_zone = element(var.availability_zone, count.index)
tags = {
Name = "private_az${count.index + 1}"
}
}
resource "aws_subnet" "data" {
vpc_id = aws_vpc.main.id
count = length(var.data)
cidr_block = element(var.data, count.index)
availability_zone = element(var.availability_zone, count.index)
tags = {
Name = "data_az${count.index + 1}"
}
}
resource "aws_internet_gateway" "IGW" {
vpc_id = aws_vpc.main.id
tags = {
Name = "IGW"
}
}
resource "aws_default_route_table" "default" {
default_route_table_id = aws_vpc.main.default_route_table_id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.IGW.id
}
tags = {
Name = "default_route_table"
}
}
resource "aws_route" "simulation_default_route" {
route_table_id = aws_vpc.main.default_route_table_id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.IGW.id
}
resource "aws_route_table_association" "association" {
count = length(var.public)
subnet_id = element(aws_subnet.public.*.id, count.index)
route_table_id = aws_default_route_table.default.id
}
resource "aws_eip" "nat_gw" {
vpc = true
}
resource "aws_nat_gateway" "priv_ngw" {
allocation_id = aws_eip.nat_gw.id
subnet_id = aws_subnet.public[0].id
depends_on = [aws_eip.nat_gw]
tags = {
Name = "private_ngw"
}
}
resource "aws_route_table" "private_route" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.priv_ngw.id
}
tags = {
Name = "priv_route"
}
}
resource "aws_route_table_association" "private_association" {
count = length(var.private)
subnet_id = element(aws_subnet.private.*.id, count.index)
route_table_id = aws_route_table.private_route.id
}
resource "aws_route_table_association" "db_private_association" {
count = length(var.data)
subnet_id = element(aws_subnet.data.*.id, count.index)
route_table_id = aws_route_table.private_route.id
}
varible.tf上工作
variable "cidr_vpc_block" {
type = string
default = "10.0.0.0/16"
}
variable "public" {
type = list(string)
default = ["10.0.0.0/22", "10.0.4.0/22", "10.0.8.0/22"]
}
variable "private" {
type = list(string)
default = ["10.0.12.0/22", "10.0.16.0/22", "10.0.20.0/22"]
}
variable "availability_zone" {
type = list(string)
default = ["us-east-1a", "us-east-1b", "us-east-1c"]
}
variable "data" {
type = list(string)
default = ["10.0.24.0/22", "10.0.28.0/22", "10.0.32.0/22"]
}
variable "ami" {
type = string
default = "ami-0ed9277fb7eb570c9"
}
I am trying to create alb using terraform but my instance are in unhealthy state. My application is listening on 3000 port so I am trying to reach the ec2 instance (in private subnet) on port 3000. If I install apache and create index.html and pass the path in alb health checks it become healthy. I want to make it work on port 3000
resource "aws_lb" "alb" {
name = "APP-LB"
internal = false
load_balancer_type = "application"
security_groups = [aws_security_group.alb_sg.id]
subnets = aws_subnet.public.*.id
}
resource "aws_lb_target_group" "alb_tg" {
name = "ALB-TG"
port = 3000
protocol = "HTTP"
target_type = "instance"
vpc_id = aws_vpc.main.id
health_check {
healthy_threshold = 2
unhealthy_threshold = 3
timeout = 5
interval = 20
protocol = "HTTP"
port = 3000
path = "/"
matcher = "200"
}
}
resource "aws_lb_listener" "listener" {
load_balancer_arn = aws_lb.alb.arn
port = 80
protocol = "HTTP"
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.alb_tg.arn
}
}
resource "aws_lb_listener" "listener2" {
load_balancer_arn = aws_lb.alb.arn
port = 3000
protocol = "HTTP"
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.alb_tg.arn
}
}
resource "aws_lb_target_group_attachment" "ec2_instance" {
target_group_arn = aws_lb_target_group.alb_tg.arn
target_id = aws_instance.web.id
port = 3000
}
resource "aws_security_group" "alb_sg" {
name = "alb_sg"
description = "For internet traffic"
vpc_id = aws_vpc.main.id
ingress {
description = "incoming for ec2-instance"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "incoming for ec2-instance"
from_port = 3000
to_port = 3000
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 = "alb_sg"
}
}
ec2.tf
resource "aws_security_group" "web_sg" {
name = "web_sg"
description = "Allow TLS inbound traffic"
vpc_id = aws_vpc.main.id
ingress {
description = "MongoDB Traffic"
from_port = 27017
to_port = 27017
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "For application deployed on ec2"
from_port = 3000
to_port = 3000
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "For traffic from appliaction load balancer"
from_port = 80
to_port = 80
protocol = "tcp"
# security_groups = [aws_security_group.alb_sg.id]
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "To ssh into the instancess"
from_port = 22
to_port = 22
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 = "web_sg"
}
}
user-data
#!/bin/bash
curl -sL https://rpm.nodesource.com/setup_17.x | sudo bash -
yum -y install nodejs
sudo rm -rf /etc/yum.repos.d/mongod*
sudo yum clean all
echo '============================================'
echo "======> Installing Mongodb 4.0 ..."
#yum clean all
cat > /etc/yum.repos.d/mongodb-org-4.2.repo << EOF
[mongodb-org-4.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/amazon/2/mongodb-org/4.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc
EOF
sudo yum install -y mongodb-org-4.2.18 mongodb-org-server-4.2.18 mongodb-org-shell-4.2.18 mongodb-org-mongos-4.2.18 mongodb-org-tools-4.2.18
vpc.tf
resource "aws_vpc" "main" {
cidr_block = var.cidr_vpc_block
tags = {
Name = "RMIT Assignment 2"
}
}
data "aws_availability_zones" "azs" {}
resource "aws_subnet" "public" {
vpc_id = aws_vpc.main.id
count = length(var.public)
cidr_block = element(var.public, count.index)
availability_zone = element(var.availability_zone, count.index)
map_public_ip_on_launch = true
tags = {
Name = "public_az${count.index + 1}"
}
}
resource "aws_subnet" "private" {
vpc_id = aws_vpc.main.id
count = length(var.private)
cidr_block = element(var.private, count.index)
availability_zone = element(var.availability_zone, count.index)
tags = {
Name = "private_az${count.index + 1}"
}
}
resource "aws_subnet" "data" {
vpc_id = aws_vpc.main.id
count = length(var.data)
cidr_block = element(var.data, count.index)
availability_zone = element(var.availability_zone, count.index)
tags = {
Name = "data_az${count.index + 1}"
}
}
resource "aws_internet_gateway" "IGW" {
vpc_id = aws_vpc.main.id
tags = {
Name = "IGW"
}
}
resource "aws_default_route_table" "default" {
default_route_table_id = aws_vpc.main.default_route_table_id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.IGW.id
}
tags = {
Name = "default_route_table"
}
}
resource "aws_route" "simulation_default_route" {
route_table_id = aws_vpc.main.default_route_table_id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.IGW.id
}
resource "aws_route_table_association" "association" {
count = length(var.public)
subnet_id = element(aws_subnet.public.*.id, count.index)
route_table_id = aws_default_route_table.default.id
}
resource "aws_eip" "nat_gw" {
vpc = true
}
resource "aws_nat_gateway" "priv_ngw" {
allocation_id = aws_eip.nat_gw.id
subnet_id = aws_subnet.public[0].id
depends_on = [aws_eip.nat_gw]
tags = {
Name = "private_ngw"
}
}
resource "aws_route_table" "private_route" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.priv_ngw.id
}
tags = {
Name = "priv_route"
}
}
resource "aws_route_table_association" "private_association" {
count = length(var.private)
subnet_id = element(aws_subnet.private.*.id, count.index)
route_table_id = aws_route_table.private_route.id
}
resource "aws_route_table_association" "db_private_association" {
count = length(var.data)
subnet_id = element(aws_subnet.data.*.id, count.index)
route_table_id = aws_route_table.private_route.id
}
variable.tf
variable "cidr_vpc_block" {
type = string
default = "10.0.0.0/16"
}
variable "public" {
type = list(string)
default = ["10.0.0.0/22", "10.0.4.0/22", "10.0.8.0/22"]
}
variable "private" {
type = list(string)
default = ["10.0.12.0/22", "10.0.16.0/22", "10.0.20.0/22"]
}
variable "availability_zone" {
type = list(string)
default = ["us-east-1a", "us-east-1b", "us-east-1c"]
}
variable "data" {
type = list(string)
default = ["10.0.24.0/22", "10.0.28.0/22", "10.0.32.0/22"]
}
variable "ami" {
type = string
default = "ami-0ed9277fb7eb570c9"
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论