Did you know that you can navigate the posts by swiping left and right?

Ansible + Machine = AWS Swarmkit clusters

Swarmkit was presented some weeks ago, but no native support to create Swarmkit clusters has come yet, AFAIK. For an overview of Swarmkit, see Chanwit’s blog post.

I played a little bit with Ansible to automate a workflow to provision Swarmkit clusters hosts of any size by using Machine.

Two playbooks are published here:

  • aws_provision_master.yml
  • aws_provision_slave.yml

Here the steps to create a Swarmkit cluster on AWS EC2:

  1. Source your AWS credentials, for example by using awscli. Then, open port 4242 for in and outbound traffic on EC2 console in the region/s where you'll create the cluster since Swarmkit will use this port.

  2. Create the Swarmkit master:

    ansible-playbook aws_provision_master.yml
    The playbook includes some commands to provision the master host, called statically swarmkit-master. After provisioning the host, a container from an image containing the Swarmkit binaries is started:

    - name: Run the Swarmkit Master
        name: swarmkit-master
        image: "fsoppelsa/swarmkit"
        command: swarmd --listen-remote-api
          - "4242"
          - ""
          - "/var/run/docker.sock:/var/run/docker.sock"
    The swarmd binary starts accepting connections on port 4242/tcp, mapping it from the host to the container (so that it will be remotely reachable by the cluster nodes solely by referring to the master IP). swarmd uses the machine Docker Engine itself, whose UNIX socket is mounted inside the container.

  3. Now script and create as many Swarmkit slaves you want with the second playbook, for example:

    for i in `seq 0 5`; do ansible-playbook aws_provision_slave.yml; done
    The playbook runs the same image, but this time swarmd joins to the Swarmkit master:

    - name: Join the slave to the Swarmkit cluster
        name: "\{\{machine_uuid}}"
        image: "fsoppelsa/swarmkit"
        command: swarmd --join-addr "\{\{ masterip }}":4242
    This command will take some time to complete, of course.

  4. When the play is over, we are ready to check the status of cluster. We can do that by invoking swarmctl, included in container running on the master host, making it read from the UNIX socket opened (inside container) at /swarmkitstate/swarmd.sock. Take note of container's ID, source the Machine variables of swarmkit-master and:

    docker exec -ti c0946f034c1e swarmctl -s /swarmkitstate/swarmd.sock node ls

    Cool, no?

  5. Remember finally to remove all the created machines:

    for i in `docker-machine ls | grep ^swarmkit | awk '{print $1;}'`; do
        docker-machine rm -f $i