All Projects → netbears → traefik-cluster-ecs

netbears / traefik-cluster-ecs

Licence: Apache-2.0 license
This is a tutorial on how to deploy a Traefik Load Balancer in AWS using CloudFormation to load balancer development ECS tasks using hosts (FQDN).

Programming Languages

shell
77523 projects

Projects that are alternatives of or similar to traefik-cluster-ecs

AutoSpotting
Saves up to 90% of AWS EC2 costs by automating the use of spot instances on existing AutoScaling groups. Installs in minutes using CloudFormation or Terraform. Convenient to deploy at scale using StackSets. Uses tagging to avoid launch configuration changes. Automated spot termination handling. Reliable fallback to on-demand instances.
Stars: ✭ 2,058 (+3219.35%)
Mutual labels:  cloudformation, ec2
Aws Scalable Big Blue Button Example
Demonstration of how to deploy a scalable video conference solution based on Big Blue Button
Stars: ✭ 29 (-53.23%)
Mutual labels:  cloudformation, ec2
aws-cloudformation-templates
CloudFormation Templates
Stars: ✭ 15 (-75.81%)
Mutual labels:  cloudformation, ec2
quickstart-microsoft-sql
AWS Quick Start Team
Stars: ✭ 60 (-3.23%)
Mutual labels:  cloudformation, cluster
Autospotting
Saves up to 90% of AWS EC2 costs by automating the use of spot instances on existing AutoScaling groups. Installs in minutes using CloudFormation or Terraform. Convenient to deploy at scale using StackSets. Uses tagging to avoid launch configuration changes. Automated spot termination handling. Reliable fallback to on-demand instances.
Stars: ✭ 2,014 (+3148.39%)
Mutual labels:  cloudformation, ec2
go-localstack
Go Wrapper for using localstack
Stars: ✭ 56 (-9.68%)
Mutual labels:  cloudformation, ec2
terraform-aws-eks-workers
Terraform module to provision an AWS AutoScaling Group, IAM Role, and Security Group for EKS Workers
Stars: ✭ 82 (+32.26%)
Mutual labels:  ec2, cluster
Kubernetes Pfsense Controller
Integrate Kubernetes and pfSense
Stars: ✭ 100 (+61.29%)
Mutual labels:  cluster, ingress
Aws Sdk Perl
A community AWS SDK for Perl Programmers
Stars: ✭ 153 (+146.77%)
Mutual labels:  cloudformation, ec2
Awscloudformation Samples
Sample AWS CloudFormation templates
Stars: ✭ 153 (+146.77%)
Mutual labels:  cloudformation, ec2
Terraform Aws Alb
Terraform module to provision a standard ALB for HTTP/HTTP traffic
Stars: ✭ 53 (-14.52%)
Mutual labels:  ec2, ingress
Aws Toolkit Eclipse
AWS Toolkit for Eclipse – an open-source plugin for developing, deploying, and managing AWS applications.
Stars: ✭ 252 (+306.45%)
Mutual labels:  cloudformation, ec2
Elasticluster
Create clusters of VMs on the cloud and configure them with Ansible.
Stars: ✭ 298 (+380.65%)
Mutual labels:  ec2, cluster
autoscaling-ec2-gitlab-runners-fargate
Autoscaling EC2 GitLab Runners Spawned by Fargate
Stars: ✭ 20 (-67.74%)
Mutual labels:  cloudformation, ec2
laniakea
Laniakea is a utility for managing instances at various cloud providers and aids in setting up a fuzzing cluster.
Stars: ✭ 28 (-54.84%)
Mutual labels:  ec2, cluster
Udacity Data Engineering Projects
Few projects related to Data Engineering including Data Modeling, Infrastructure setup on cloud, Data Warehousing and Data Lake development.
Stars: ✭ 458 (+638.71%)
Mutual labels:  cloudformation, cluster
Azure-AKS-ApplicationGateway-WAF
No description or website provided.
Stars: ✭ 16 (-74.19%)
Mutual labels:  cluster, ingress
K8s Multicluster Ingress
kubemci: Command line tool to configure L7 load balancers using multiple kubernetes clusters
Stars: ✭ 345 (+456.45%)
Mutual labels:  cluster, ingress
Awesome Aws
A curated list of awesome Amazon Web Services (AWS) libraries, open source repos, guides, blogs, and other resources. Featuring the Fiery Meter of AWSome.
Stars: ✭ 9,895 (+15859.68%)
Mutual labels:  cloudformation, ec2
Cluster Lifecycle Manager
Cluster Lifecycle Manager (CLM) to provision and update multiple Kubernetes clusters
Stars: ✭ 200 (+222.58%)
Mutual labels:  cloudformation, cluster

Traefik Load Balancer for ECS services

This is a tutorial on how to deploy a Traefik Load Balancer in AWS to create hosts (FQDN) for development applications launched in ECS based on application name and tags.

The CloudFormation template and explanation is also posted on the NETBEARS company blog. You might want to check the website out for more tutorials like this.

What is Traefik?

Træfik (pronounced like traffic) is a modern HTTP reverse proxy and load balancer developed initially by ContainoUs, which helps you deploy microservices with ease. It supports several backends (Docker, Swarm mode, Kubernetes, Marathon, Consul, Etcd, Rancher, Amazon ECS, and a lot more) to manage its configuration automatically and dynamically.

What this means is that you don't need to rely on hard-to-manage config files nor on some orchestration mechanism for updating them (using Consul for instance), as this load balancer automatically reads and updates its configuration on its own based on the current status of the backend that you're using.

How you ask? Well, if you've ever used Prometheus and have played at least a bit with its own config file, you've noticed that it uses a scraping mechanism to test the targets at a specific interval, also known as a polling mechanism. That's what's behind Traefik's logic as well.

Comparison vs NGiNX

"Another load balancer and reverse proxy mechanism? But we have nginx and haproxy for that?!" - you might ask.

Well, first of all, NGiNX is NOT a load balancer :) It's just a reverse proxy as a web server that can also do that. Second of all, anyone that has actually used NGiNX in production to manage more than just 2 microservices, will agree that creating a workaround with Consul that tests the configuration, reloads it gracefully and makes sure that it can respond to requests even when an upstream is a FQDN and not an IP (which is the case for load balancers in AWS), is really a pain in the a$$.

That's actually the beauty that Traefik brings into play. Besides the fact that it is proven to be 85% faster than NGiNX, it never changes its PID (making it extremely suitable to be ran in a container) and handles extremely gracefully hot reloading when it is required.

More than that, it also handles extremely gracefully cases in which microservices are published as different variants of the same URL (http://example.com/ms1 and http://example.com/ms2), and it's also suitable for cases in which the microservices are published under different host variants (http://ms1-example.com and http://ms2-example.com), which is actually the topic of this tutorial!

And if all of this didn't yet convince you to take Traefik into consideration when building the infrastructure for your microservices network, then keep in mind that out-of-the-box support is offered using Let's Encrypt for SSL termination, meaning that not only that the initial config for it is low-to-none, but there's also no need for maintenance in changing/refreshing your certificates.

For a quick demo on how powerful Traefik actually is and how simple it is to set up, check out Emile Vauge (Founder and CEO of containo.us - creator of traefik.io) giving a talk at GopherCon 2017 on the basis of Traefik in less than 10 minutes.

GopherCon 2017

Building the Traefik container

I don't know about you guys, but I'm a big fan of the alpine containers and using default and tagged vendor pre-vetted images, so my Dockerfile pretty much looks like this:

FROM      traefik:v1.4.1-alpine

LABEL     maintainer="NETBEARS <[email protected]>"

COPY      traefik_ecs.toml /etc/traefik/traefik.toml

COPY      docker-entrypoint.sh /docker-entrypoint.sh

ENTRYPOINT ["/docker-entrypoint.sh"]

The traefik_ecs.toml file contains the standard ECS backend definition:

[web]
address = ":8080"
readOnly = true
[web.metrics.prometheus]
buckets=[0.1,0.3,1.2,5.0]


[ecs]
clusters = ["CLUSTER_HOST"]
watch = true
domain = "ENVIRONMENT.DOMAIN"
autoDiscoverClusters = false
refreshSeconds = 15
exposedByDefault = true
region = "AWS_REGION"

And the docker-entrypoint.sh is just a shell script to replace the variables in the TOML file using environment variables:

#!/bin/sh
set -e

# replace cluster definition
sed -i "s/CLUSTER_HOST/$CLUSTER_HOST/;s/ENVIRONMENT/$ENVIRONMENT/;s/AWS_REGION/$AWS_REGION/;s/DOMAIN/$DOMAIN/" /etc/traefik/traefik.toml

# first arg is `-f` or `--some-option`
if [ "${1#-}" != "$1" ]; then
    set -- traefik "$@"
fi

# if our command is a valid Traefik subcommand, let's invoke it through Traefik instead
# (this allows for "docker run traefik version", etc)
if traefik "$1" --help 2>&1 >/dev/null | grep "help requested" /dev/null 2>&1; then
    set -- traefik "$@"
fi

exec "$@"

Using these files, all one has to do is just build the container and send it to ECR:

export AWS_REGION='your_aws_region'

export AWS_ACCOUNT_ID='your_account_id'         # https://goo.gl/9Y4ana

export ECR_REPO_NAME='traefik'

(aws ecr create-repository --repository-name $ECR_REPO_NAME) || true

eval $(aws ecr get-login --no-include-email --region $AWS_REGION)

docker build -t $ECR_REPO_NAME:latest .

docker tag $ECR_REPO_NAME:latest $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$ECR_REPO_NAME:latest

docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$ECR_REPO_NAME:latest

Launching the Traefik container

  • Login to the AWS console and browse to the CloudFormation section
  • Select the cloudformation-template-traefik.yaml file
  • Before clicking "Create", make sure that you scroll down and tick the “I acknowledge that AWS CloudFormation might create IAM resources” checkbox
  • ...drink coffee...
  • Go to the URL in the output section for the environment that you want to access

Launching a test application in a container

For the purpose of simplicity, we're going to launch 2 versions of the same container with different tags so that we demonstrate the fact that 2 URLs will be created using the format:

http://${application}-${build_tag}.${environment}.${domain}
  • Login to the AWS console and browse to the CloudFormation section
  • Select the cloudformation-template-service.yaml file
  • Before clicking "Create", make sure that you scroll down and tick the “I acknowledge that AWS CloudFormation might create IAM resources” checkbox
  • ...drink coffee...
  • Go to the URL in the output section for the environment that you want to access

Checking status in Traefik

After we've done all this, it's time to see if the services that we launched in the cluster to be monitored have been loaded in Traefik.

To do that, we just browse to the Traefik ECS service, click on any of the running tasks and open the link which exposes the port 8080. If everything went well, then something like this should appear:

Traefik Dashboard

As you can see, we currently have the same service, exposes twice, with 2 different URLs, based on the tag that was supplied:

http://whoami-v1.dev.aws.netbears.com/

http://whoami-v2.dev.aws.netbears.com/

Accessing your containers

Well, I'm assuming you've already guesses that this is the easiest part :-).

All you have to do is browse to that URL. If everything went fine, then you'll see your application.

For instance, for this specific tutorial, I've purposely included a custom script that displays all cookies and headers of your request when accessing it via Traefik:

Traefik Containers

Final notes

Need help implementing this?

Feel free to contact us using this form.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].