I’m in a bit of a bind here. I have containers running in my ECS cluster to host a SaaS solution with an ALB sitting in front of it. Problem is the containers keep failing the health checks and I’m at my wits end. The path is set to / over HTTPS on the traffic port. Need I do anything else for the health check to work? The traffic is being routes to the target on 8443 and I’m certain that my security groups allow ports as needed, any help would be appreciated.
Edit: So I figured out the issue, the ALB was doing its job correctly and I had the security groups configured correctly, the problem was the success code wasn’t returning 200 but 401(?). I’ve changed the success code from 200 to 401 and its picking up my containers as healthy.
By default if you set the health check to “HTTPS” it’ll check 443. If you need 8443, I recommend a TCP health check on 8443 (not HTTPS).
Next, let’s say the LB is listening on 443 and the instance is listening on 8443, your listener will have to reference 443->8443. That means the LB security group will have to be open to 443 to your traffic source, whether that be a whitelist oF IPs or 0.0.0.0/0 for the world, and your ec2 sec group (ecs boxes) should be open on 8443 only to the LB sec group as a source.
Check these and let me know if I can help further.
EDIT and finally, I’d recommend a /health endpoint that runs a script that checks all the things, cache, dB, all that, and returns http/200 if all’s well as part of your app. This would give the alb a proper picture of what is and isn’t working.
First, an ALB does not support TCP health checks. ALBs operate on OSI Layer 7 and therefore only sees HTTP level traffic. For TCP checks you need to use an ELB (now called Classic Load Balancer) or an NLB. If OP just wants this thing to work, I would actually recommend switching to an ELB because it’s easier to get up and running, however they will need to recreate the entire service.
Second, when using an ALB with ECS, you typically use dynamic port mapping, not fixed port assignments. It’s unclear which one OP is trying to set up.
Lastly, having a separate /health endpoint is a great idea, but I strongly disagree that health checks should check anything downstream. It’s a very common mistake and I’ve seen it lead to serious read-world downtime on multiple occasions. Seriously, don’t do it! For instance, let’s say your Redis cache dies. Shouldn’t be a problem, just fall back to the database right? Unfortunately, your ALB now thinks the application is dead and so it starts terminating containers. Or perhaps the network connection to one of your database servers or another backend service is kinda slow for a few minutes. You application could just retry and hit a different server, or return some 500 errors for a while, but nope your health check is failing so all your containers are now killed. By tightly coupling these pieces together, a single intermittent failure in some minor backend service can suddenly burst into a cascading failure that propagates up the entire chain and brings down your entire site. Even worse, all that stopping and starting may actually cause new problems, and it’s going to make it really hard to find out the actual root cause because everything is in a reboot loop. This goes against every good design principle of distributed systems engineering. You want fault-tolerance and isolation, ideally each piece of the system should degrade gracefully in the face of failure. Cache is down? Site might be slow but it should still work. Database fucked up and went into read-only mode? Ok so we can’t do updates but at least the users can browse what’s already there. Image service down? Site will be missing images until it’s fixed, but customers should still be able to login and cancel their subscription. There’s plenty of great information out there on this topic, but here’s a decent blog post to get started.
tl;dr: Your health check should only fail when the service itself is in an unrecoverable state (ie it failed to initialize), or some problem with the host is detected (ie out of disk space).
When using an ALB with ECS, you typically use Dynamic Port Mapping, not fixed port mapping. OP, can you clarify if you are using fixed or dynamic port mapping? To set up dynamic port routing, you set a fixed outward-facing port on the ALB, and then the scheduler gets an unused port on each host when container is placed and it notifies the ALB. Full walkthrough here or here
Check the logs. Go to the ALB and target group and see what the error says. Double check their config and ports. Go to ECS, find the service, dig into the container configuration. What does it say in the networkBindings section? Get container logs if possible (forward to AWS, ELK, whatever). The health check might be failing because your container failed to start, for any number of reasons.
Start the container using docker on your own local machine. Does it start successfully? Can you do an HTTP GET to the health check route (ie using curl)? On what ports? Do not proceed until you can actually run the thing locally and hit the health check and get a 200 back, otherwise debugging will be a nightmare.
SSH to the container host where its running and either disable health checks or run the container manually via Docker. Run ‘docker ps’, and check that a) it’s running and b) see what ports are shown. Curl the health check route from the host machine. Check the docker and ecs agent logs here.
What port(s) are exposed/published on your docker container? See this and this.
What port(s) is your webserver listening on?
If using a fixed port, is that port being used by another container on that host? You can’t have two services bind to the same port on the same host.
Does the security group attached to the ALB allow traffic to the health check route, port range, and subnet? (ephemeral port ranges are 49153–65535 and 32768–61000 if using dynamic mapping)
ALB and ECS cluster in same VPC / Subnet?
Does the security group on the container hosts allow traffic from the ALB, port range, subnet?
What networking mode are you using in the task definition?
If you are using dynamic mapping, follow the steps I linked above very closely. It is not a simple process, and make sure you have setup your ALB, Target Group(s), Task Definition, etc correctly.
Reach out to AWS support for help. You do have a support contract, right? Developer support is like $30/month, but if you’re running production workload you really should be on Business or Enterprise. I don’t work for Amazon, I’m not a shill, I’m just a guy on the internet telling you that when shit goes wrong or you can’t figure something out, it’s worthwhile to have someone you can call that will actually pick up the phone because you paid them money and signed a contract.
If you have set up bridge networking on ECS, then the container itself is listening on a random ephemeral port range. You need to also enable these ports in the security groups coming from your ALB.
I don’t know if this is your problem but my recent frustration with alb failing health checks was a bad cert on the instance so the alb would not do a tls handshake. It took me a long time to figure it out.
My case was a tomcat that worked with a classic elb but not an alb, the jks file the tomcat was using was bad.
Nah, but I spent a lot of time setting up ECS and debugging similar issues a while back. Was kinda bored yesterday and spent a bunch of time answering AWS threads. I’ve actually complained to AWS before about how difficult it is to debug failing health checks. I’m a big fan of ECS in general but I wish they would improve visibility into some of the operations for when things go wrong.