Created
July 9, 2016 18:11
-
-
Save DazWilkin/291858898033a9988b9da0c05da6c497 to your computer and use it in GitHub Desktop.
Deploy Docker Engine in swarm mode to Google Cloud Platform
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# DazWilkin | |
# July 2016 | |
# | |
# Google Cloud Deployment Manager python script: | |
# Deploys Docker Engine in swarm mode to Google Cloud Platform | |
# | |
# 1. Create VM running Docker 1.12 | |
# 2. Derive a Master VM from it -- swarm init | |
# 3. Template the Worker VM | |
# 2. Create a MIG of Workers | |
# 3. Connect them | |
# Defaults | |
PROJECT = 'project' | |
ZONE = 'zone' | |
NAME = 'name' | |
GOOGLE_API_ROOT = 'https://www.googleapis.com/' | |
COMPUTE_URL = GOOGLE_API_ROOT + 'compute/v1/' | |
SCOPES_URL = GOOGLE_API_ROOT + 'auth/' | |
def GlobalComputeUrl(project, collection, name): | |
return ''.join([ | |
COMPUTE_URL, | |
'projects/', | |
project, | |
'/global/', | |
collection, | |
'/', | |
name | |
]) | |
def ZonalComputeUrl(project, zone, collection, name): | |
return ''.join([ | |
COMPUTE_URL, | |
'projects/', | |
project, | |
'/zones/', | |
zone, | |
'/', | |
collection, | |
'/', | |
name | |
]) | |
def Ref(name): | |
return ''.join([ | |
'$(ref.', | |
name, | |
'.selfLink)' | |
]) | |
def Scopes(scopes): | |
return [SCOPES_URL + scope for scope in scopes] | |
def GenerateConfig(context): | |
"""Generate configuration.""" | |
base_name = context.env[NAME] | |
# Disk(s) | |
disks = [ | |
{ | |
'deviceName': 'boot', | |
'type': 'PERSISTENT', | |
'boot': True, | |
'autoDelete': True, | |
'initializeParams': { | |
'sourceImage': GlobalComputeUrl( | |
context.properties['image-project'], | |
'images', context.properties['image-name'] | |
) | |
} | |
} | |
] | |
# Network(s) | |
network_interfaces = [ | |
{ | |
'network': GlobalComputeUrl( | |
context.env[PROJECT], | |
'networks', | |
'default' | |
), | |
'accessConfigs': [ | |
{ | |
'name': 'External NAT', | |
'type': 'ONE_TO_ONE_NAT' | |
} | |
] | |
} | |
] | |
# Service Account(s) | |
service_accounts = [ | |
{ | |
"email": "default", | |
"scopes": Scopes([ | |
'devstorage.read_only', | |
'logging.write', | |
'monitoring.write', | |
'servicecontrol', | |
'service.management' | |
]) | |
} | |
] | |
# Properties for the swarm-master VM instance. | |
swarm_master = { | |
'type': 'compute.v1.instance', | |
'name': context.properties['swarm-master'], | |
'properties': { | |
'zone': context.properties[ZONE], | |
'machineType': ZonalComputeUrl( | |
context.env[PROJECT], | |
context.properties[ZONE], | |
'machineTypes', | |
context.properties['machineType'] | |
), | |
'disks': disks, | |
'networkInterfaces': network_interfaces, | |
'metadata': { | |
'items': [{ | |
'key': 'startup-script', | |
'value': ''.join([ | |
context.properties['startup-script-base'], | |
context.properties['startup-script-master'] | |
]), | |
}] | |
}, | |
}, | |
} | |
# Properties for the swarm-worker Template | |
# NB machineType | |
swarm_worker_template = { | |
'type': 'compute.v1.instanceTemplate', | |
'name': context.properties['swarm-worker-template'], | |
'properties': { | |
'properties': { | |
'machineType': context.properties['machineType'], | |
'disks': disks, | |
'networkInterfaces': network_interfaces, | |
'metadata': { | |
'items': [ | |
{ | |
'key': 'startup-script', | |
'value': ''.join([ | |
context.properties['startup-script-base'], | |
context.properties['startup-script-worker'] | |
]) | |
} | |
] | |
}, | |
'serviceAccounts': service_accounts | |
} | |
} | |
} | |
# Properties for the Managed Instance Group of swarm-worker(s) | |
# Based on the swarm-worker Template | |
swarm_worker_mig = { | |
'type': 'compute.v1.instanceGroupManagers', | |
'name': context.properties['swarm-worker-mig'], | |
'properties': { | |
'baseInstanceName': context.properties['swarm-worker'], | |
'instanceTemplate': Ref('swarm-worker-template'), | |
'targetSize': context.properties['number-swarm-workers'], | |
ZONE: context.properties[ZONE] | |
} | |
} | |
return { | |
'resources': [ | |
swarm_master, | |
swarm_worker_template, | |
swarm_worker_mig | |
], | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# DazWilkin | |
# July 2016 | |
# | |
# Google Cloud Deployment Manager template: | |
# Deploys Docker Engine in swarm mode to Google Cloud Platform | |
# | |
imports: | |
- path: dockerswarm.py | |
resources: | |
- name: myswarm | |
type: dockerswarm.py | |
properties: | |
zone: us-east1-d | |
machineType: custom-1-2048 | |
image-project: ubuntu-os-cloud | |
image-family: ubuntu-1604-lts | |
image-name: ubuntu-1604-xenial-v20160610 | |
swarm-master: swarm-master | |
swarm-worker: swarm-worker | |
swarm-worker-template: swarm-worker-template | |
swarm-worker-mig: swarm-worker-mig | |
number-swarm-workers: 3 | |
startup-script-base: | | |
#! /bin/bash | |
sudo apt-key adv \ | |
--keyserver hkp://p80.pool.sks-keyservers.net:80 \ | |
--recv-keys 58118E89F3A912897C070ADBF76221572C52609D | |
echo "deb [arch=amd64] https://apt.dockerproject.org/repo ubuntu-xenial testing" | sudo tee /etc/apt/sources.list.d/docker.list | |
sudo apt-get update && \ | |
sudo apt-get -y install linux-image-extra-4.4.0-28-generic && \ | |
sudo apt-get -y install docker-engine && \ | |
sudo apt-get -y upgrade && \ | |
sudo apt-get clean | |
sudo systemctl start docker | |
startup-script-master: | | |
sudo docker swarm init | |
startup-script-worker: | | |
sudo docker swarm join swarm-master:2377 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment