LinuxKit as an ECS-instance with Terraform

How to build a system that can connect to the specified ECS-cluster and run test services and tasks on AWS.

LinuxKit as an ECS-instance with Terraform.

When Docker offered convenient way to build, ship, and run applications with their unique runtimes independently inside containers the next was to strip systems leaving the only possibility to run containers. LinuxKit is a tool designed to build such minimal Linux dystros. It is like Lego — every part of the system is a container, so you can change and customize everything as you wish using containers and base images you want. LinuxKit dystros are immutable and stateless, but you are able to attach and mount persistent storage. It also supports imposing list of hypervisors and cloud platforms and can be built on any system with docker from yaml file.

tl;dr

LinuxKit yaml for ECS instance:

After we’ve played enough with examples we decided to build our own system which is able to connect to the specified ECS-cluster and run our test services and tasks on AWS. The core of the ECS is the ecs-agent which registers/deregisters instances in cluster, manages resources on each instance, runs tasks and services, grants permissions and roles etc. Amazon builds it’s own ecs-optimized images but also it is possible to do this by yourself. There are several requirements to run agent properly:

  • mount /data volume and docker.sock to the agent’s container
  • set net.ipv4.conf.all.route_localnet=1 and execute iptables rules to use task role arns
  • also container should run in privileged mode with net: host

To execute sysctl commands on LinuxKit we should run sysctl container in rw mode and mount sysctl.conf with prefered content to the /etc/sysctl.d/:


where /etc/sysctl.d/01-ecs.conf:


To execute iptables rules we built and added custom image to the onboot part:


with Dockerfile:


and simple iptables.sh:


Then we configured docker to use /var/run/docker.sock and mounted /var/run to the docker and ecs-agent services:


To configure agent we use json config file which we get from the userdata during onboot by metadata task. This task gets all aws metadata and places it to/var/configso we just need to mount /var/config/userdata to /ecs.config.json inside agent container.

# cat /var/config/userdata
{
"Cluster": "linuxkit-ecs",
"TaskIAMRoleEnabled": true,
"TaskIAMRoleEnabledForNetworkHost": true
}

You can see the whole example here.

I used this post as a base for terraform code which creates s3 bucket and grants policy for AMI import, to build and import image just run:


Terraform will create all necessary resources, build and upload LinuxKit image.

Custom Software and Mobile App Development.
DevOps as a Service.

DaaS (DevOps as a Service), wut?!

DaaS (DevOps as a Service), wut?!

DaaS (DevOps as a Service), wut?!

Everyone was surprised at how quickly and dynamically products could be developed and updated at the beginning of agile development. Despite the fact...

Apr 22, 2019
MadOps: DevOps at Mad Devs.

DevOps at Mad Devs: a list of skills & practices we...

DevOps at Mad Devs: a list of skills...

DevOps at Mad Devs: a list of skills & practices we use to train our DevOps internally

Our team started to apply DevOps practices long before we registered Mad Devs as a company. Specifically, I ran into this approach more than 10 years...

Sep 22, 2020
How to Implement Infrastructure as Code with Hetzner.

Terraform + Hetzner

Terraform + Hetzner

Terraform + Hetzner

Let’s talk about “Infrastructure as code”. It has already become a standard. It will be used by default in a few years in the deploying...

May 08, 2019