├── .gitignore ├── terraform ├── provider.tf ├── variables.tf ├── loadbalancing.tf ├── iam.tf ├── securitygroups.tf └── autoscaling.tf ├── packer ├── docker.conf ├── docker.json └── init.py └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | *tfstate* 2 | .env 3 | node_modules 4 | package.json 5 | -------------------------------------------------------------------------------- /terraform/provider.tf: -------------------------------------------------------------------------------- 1 | provider "aws" { 2 | region = "${var.aws_region}" 3 | } 4 | -------------------------------------------------------------------------------- /packer/docker.conf: -------------------------------------------------------------------------------- 1 | [Service] 2 | ExecStart= 3 | ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:2376 4 | -------------------------------------------------------------------------------- /terraform/variables.tf: -------------------------------------------------------------------------------- 1 | variable "aws_region" { 2 | type = "string" 3 | default = "us-east-1" 4 | } 5 | 6 | variable "aws_az" { 7 | type = "list" 8 | default = ["us-east-1a","us-east-1b","us-east-1c"] 9 | } 10 | 11 | variable "ec2_ami" {} 12 | 13 | variable "manager_count" {} 14 | 15 | variable "worker_count" {} 16 | 17 | variable "manager_size" { 18 | default = "t2.micro" 19 | } 20 | 21 | variable "worker_size" { 22 | default = "t2.micro" 23 | } 24 | 25 | variable "key" {} 26 | -------------------------------------------------------------------------------- /terraform/loadbalancing.tf: -------------------------------------------------------------------------------- 1 | resource "aws_elb" "swarm_lb" { 2 | name = "swarm-lb" 3 | availability_zones = "${var.aws_az}" 4 | security_groups = ["${aws_security_group.swarm_lb_sg.id}"] 5 | 6 | listener { 7 | instance_port = 80 8 | instance_protocol = "http" 9 | lb_port = 80 10 | lb_protocol = "http" 11 | } 12 | 13 | health_check { 14 | healthy_threshold = 2 15 | unhealthy_threshold = 2 16 | timeout = 3 17 | target = "TCP:2376" 18 | interval = 30 19 | } 20 | 21 | cross_zone_load_balancing = true 22 | idle_timeout = 400 23 | connection_draining = true 24 | connection_draining_timeout = 400 25 | 26 | tags { 27 | Name = "swarm-elb" 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /terraform/iam.tf: -------------------------------------------------------------------------------- 1 | resource "aws_iam_instance_profile" "swarm_cluster_profile" { 2 | name = "swarm-cluster-profile" 3 | roles = ["${aws_iam_role.swarm_cluster_role.name}"] 4 | } 5 | 6 | resource "aws_iam_role_policy" "swarm_cluster_policy" { 7 | name = "swarm-cluster-policy" 8 | role = "${aws_iam_role.swarm_cluster_role.id}" 9 | 10 | policy = < 0 or role == 'worker': 43 | #### JOIN CLUSTER 44 | manager = instances[0]['PrivateIpAddress'] # any running manager 45 | call(["echo", "'Here I join to the cluster {}'".format(manager)]) # a friendly message for debugging 46 | token = run(["docker", "-H {}:2376".format(manager), "swarm", "join-token", "-q", role], stdout=PIPE) # get the token 47 | call(['docker', 'swarm', 'join', '{}:2377'.format(manager), '--token', token.stdout.decode('utf-8').replace('\n', '')]) # join to cluster 48 | 49 | #### ADD TAGS 50 | hostname = run(['hostname'], stdout=PIPE) 51 | if role == 'manager': # add init flag and hostname 52 | add_tag([{'Key': 'Init', 'Value': 'true'}, {'Key': 'Hostname', 'Value': hostname.stdout.decode('utf-8').replace('\n', '')}]) 53 | else: # adds hostname only 54 | add_tag([{'Key': 'Hostname', 'Value': hostname.stdout.decode('utf-8').replace('\n', '')}]) 55 | 56 | #### CLEAN NODES 57 | if replaced_instances_count > 0 and role == 'manager': 58 | ## docker demote node && docker rm node 59 | for instance in replaced_instances: 60 | for tag in instance['Tags']: 61 | if tag['Key'] == 'Hostname': 62 | call(["docker", "node", "demote", tag['Value']]) 63 | call(["docker", "node", "rm", tag['Value']]) 64 | 65 | if replaced_workers_count > 0 and role == 'manager': 66 | ## docker rm node 67 | for instance in replaced_workers: 68 | for tag in instance['Tags']: 69 | if tag['Key'] == 'Hostname': 70 | call(["docker", "node", "rm", tag['Value']]) 71 | 72 | 73 | else: 74 | #### INIT CLUSTER 75 | call(["echo", "'Here I init the cluster'"]) 76 | call(["docker", "swarm", "init"]) 77 | 78 | #### ADD TAGS 79 | hostname = run(['hostname'], stdout=PIPE) 80 | add_tag([{'Key': 'Init', 'Value': 'true'}, {'Key': 'Hostname', 'Value': hostname.stdout.decode('utf-8').replace('\n', '')}]) 81 | --------------------------------------------------------------------------------