Configuring RancherOS for use with AWS autoscaling + Swarm cluster
Story and disposition
In my new project I had to implement Docker Swarm cluster on AWS. I know about AWS ECS, but it didn’t support autoscaling based on system load at the moment, so it looked easier to use Swarm.
There’s a problem here. Swarm (at least, before docker 1.12) doesn’t support autoscaling and automatic rebalancing, so if you’ve started containers on 1 instance and than added 10 new instances, it won’t migrate the containers until the first instance would fail.
Another problem is autoscaling itself. Cloud is quite specific piece of infrastructure: you have to store “state” of your app separately and create a “stateless” config to launch nodes dynamically.
So, here’s the challenge. Let’s go :)
RancherOS Docker configuration
To configure Docker in RancherOS, you need to change rancher.docker.args
parameter in config and restart Docker. The problem is that you cannot correctly restart Docker automatically after it has started. It may have some containers that will be lost after restart.
Solution is to use system-docker to run a container with scripts that will reconfigure Docker before actual docker start. I created an image deadroot/rancheros-ec2-metadata for this. See this article and GitHub project.
AWS Autoscaling
To create a “stateless” autoscaling configuration, we need to:
- Create user-data that will correctly launch our instance with all configuration.
- Take configuration of the instance from AWS instance’s data where possible.
AWS tags
I’ve configured AWS autoscaling group the way it creates several tags:
docker.environment
- “environment”: staging, production, qa, etc.docker.role
- instance role. I use ‘frontend’, ‘db’, ‘queue’, ‘services’, ‘web’, etc.
Also there are some special tags. They are used to bypass config variables from AWS autoscaling group to Rancher’s environment:
example.db.host
- database host addressexample.queue.host
- queue host address
Here you may use any variables that should be configured manually. For example, you can bypass DNS name of your env, EFS filesystem name, etc. See User-data below.
User-data
RancherOS can take its config from EC2 instance’s user-data. Here’s my user-data sample that configures “generic” instance for Swarm cluster:
Here:
consul://consul.example.internal:8500
- discovery addressdocker.example.internal:5000
- Docker private registry address
What it does:
- Load AWS metadata and tags. Look at deadroot/rancheros-ec2-metadata for details. Here it:
- Imports all AWS metadata to environment;
- Adds all tags beginning with
docker.
to Docker host labels - Adds tag named
example.db.host
as environment variableDB_HOST
andexample.queue.host
asQUEUE_HOST
- Launches Swarm agent and joins to the Swarm cluster.
Conclusion
Here we configured a Swarm cluster “node”. You can create several autoscaling groups with different tags and use them to launch dirfferently-tagged instances. After this you can use Swarm filters to launch containers on specific instances.
blog comments powered by Disqus
Published
Category
dockerTags
2020
August
- August 11, 2020 » Identifying AWS EBS volumes on instance
June
- June 24, 2020 » AWS S3 Website with private-only access
2018
September
- September 4, 2018 » Multiple ways of PowerShell templating
2017
September
- September 17, 2017 » Link: RDP URI scheme
August
- August 15, 2017 » Link: SSL settings and checks
- August 15, 2017 » Link: Template files substituter for Docker
- August 8, 2017 » Link: Classes in PowerShell
April
- April 15, 2017 » Bash tricks you didn't usually use
2016
August
- August 30, 2016 » Configuring RancherOS for use with AWS autoscaling + Swarm cluster
April
- April 26, 2016 » Building JS assets with MSBuild
- April 25, 2016 » Applying Web.config transforms to all config files
2015
August
- August 29, 2015 » Check if identifier is declared in Bash
2014
April
- April 2, 2014 » PID file management in Bash
- April 2, 2014 » Logging routine for Bash
- April 2, 2014 » Lock file management in Bash