0

I'm new to AWS. So I have an ECS cluster with 2 instances. Each instance runs 1 task via a service. Both instances are in the same target group which has port 80 open. Each tasks runs nginx(port 80 connected to host port 80), react and express container. I also have a rds instance.

Now, I'm trying to load balance to the nginx container. When I try to assign a load balancer to this target group, it doesn't load any web page, just shows blank. But when I have only 1 instance running then it works fine, the web pages loads.

I tried to check and reconfigure all my security groups to see if they are causing the issue. But haven't worked.

Here's my task definition

{
"family": "task-td",
"containerDefinitions": [
    {
        "name": "task-client",
        "image": "public.ecr.aws/react-container",
        "cpu": 0,
        "portMappings": [
            {
                "name": "task-client-5173-tcp",
                "containerPort": 5173,
                "hostPort": 5173,
                "protocol": "tcp",
                "appProtocol": "http"
            }
        ],
        "essential": true,
        "environment": [],
        "mountPoints": [],
        "volumesFrom": []
    },
    {
        "name": "task-server",
        "image": "public.ecr.aws/express-api-container",
        "cpu": 0,
        "portMappings": [
            {
                "name": "task-server-3000-tcp",
                "containerPort": 3000,
                "hostPort": 3000,
                "protocol": "tcp",
                "appProtocol": "http"
            }
        ],
        "essential": true,
        "environment": [
            {
                "name": "PGHOST",
                "value": "rds.amazonaws.com"
            },
            {
                "name": "PGPORT",
                "value": "5432"
            },
            {
                "name": "PGUSER",
                "value": "postgres"
            },
            {
                "name": "PGDATABASE",
                "value": "task"
            },
            {
                "name": "PGPASSWORD",
                "value": "postgres123"
            }
        ],
        "mountPoints": [],
        "volumesFrom": []
    },
    {
        "name": "nginx",
        "image": "public.ecr.aws/nginx-container",
        "cpu": 0,
        "links": [
            "task-client",
            "task-server"
        ],
        "portMappings": [
            {
                "name": "nginx-80-tcp",
                "containerPort": 80,
                "hostPort": 80,
                "protocol": "tcp",
                "appProtocol": "http"
            }
        ],
        "essential": true,
        "environment": [],
        "mountPoints": [],
        "volumesFrom": []
    }
],
"taskRoleArn": "/ecsTaskExecutionRole",
"executionRoleArn": "/ecsTaskExecutionRole",
"networkMode": "bridge",
"requiresCompatibilities": [
    "EC2"
],
"cpu": "256",
"memory": "256",
"runtimePlatform": {
    "cpuArchitecture": "X86_64",
    "operatingSystemFamily": "LINUX"
}

}

RBT
  • 24,161
  • 21
  • 159
  • 240
Jon
  • 1
  • 2
  • So you have two tasks running an nginx container, and an express container? Both tasks are running both containers? It would be real helpful to see your task definition to understand how you have things setup. I guess my main question is, when you have two targets configured for the load balancer, are both of those targets identical? – Mark B Apr 19 '23 at 19:41
  • Edited my task defination. Yes both targets are identical. – Jon Apr 19 '23 at 20:07
  • You've hidden what your docker images are for some reason, so it's hard to tell what the different containers are. If you have a service with one task running, and point your load balancer to it, and it works. But then you increase the task count to two, and things stop working, then you need to look at your container logs to see what the exact error message is. My guess is you are doing something stateful that can't be load balanced, so you may need to enable sticky sessions. – Mark B Apr 19 '23 at 20:12
  • I edited it again to reflect my container names. what does stateful mean in this context? – Jon Apr 19 '23 at 20:25
  • i have 2 ec2 instances(ran using asg) which belong to one target group. each ec2 runs a task. so there are 2 tasks run using a service. each task has nginx,react and express api containers. express container accesses rds instance. the service is deployed without any issue. i tried to access each task public ip and both tasks work fine. but it doesnt work with a load balancer. i tried to select a load balancer and point it to the nginx container during the service configuration. hope this clears any confusion – Jon Apr 19 '23 at 20:39
  • Stateful would mean anything stored in an HTTP session, on the server, such as the logged in user info. – Mark B Apr 19 '23 at 20:45
  • Are you saying you can't get it to work with a load balancer, even if there is just 1 task? What type of errors do you see in your browser's developer console? Also check for errors in the Network tab. – Mark B Apr 19 '23 at 20:47
  • Also does the Load Balancer's target group show that the targets are healthy? – Mark B Apr 19 '23 at 20:48
  • yes cant make it work with a lb. with one tasks it works fine, but when i try to run 2 or more it fails as in it doesnt show anything when i access the elb link. yes both targets are shown as healthy. – Jon Apr 19 '23 at 20:59
  • You're really not being clear. When you say "with one tasks it works fine" is that using the load balancer, or using the public IP of the task? – Mark B Apr 19 '23 at 21:09
  • with one task it works fine with a load balancer and task public ip. if i start more than one task everything works with perfectly with the public ips of each task. but when i try to use load balancer with more than 1 task , using the dns link it doesnt work, as in my react frontend is not loaded. – Jon Apr 20 '23 at 10:01
  • Did you try enabling sticky sessions on the load balancer? Did you look at the container logs of the two tasks for any errors when it is not working? Did you check the browser's developer console for any errors when it is not working? – Mark B Apr 20 '23 at 12:00

0 Answers0