Skip to content

Instantly share code, notes, and snippets.

Created August 1, 2017 04:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rbigeard/5dff0c1dcdeee2fde388861016836042 to your computer and use it in GitHub Desktop.
Save rbigeard/5dff0c1dcdeee2fde388861016836042 to your computer and use it in GitHub Desktop.
# Docker swarm + nginx + load balancer simple automation example
# Romain Bigeard 2017
# v 1.1
param (
[string]$omsWorkspaceKey = "",
[string]$omsWorkspaceId = ""
# manager is first machine in array
#$machines = @('manager','worker1','worker2')
$machines = @('manager','worker1')
# TODO add parameter array or config file
# getting current Azure subscription id
$subid = $(az account show --query "id")
# create VMs
Write-Host "Creating VMs" -BackgroundColor "Green" -ForegroundColor "Black"
For ($i=0; $i -lt $machines.length; $i++) {
docker-machine create -d azure --azure-subscription-id $subid --azure-ssh-user romain --azure-location "West US 2" `
--azure-open-port 80 $machines[$i]
# Getting external IP of Manager
$ipmanager = $(docker-machine ip manager)
# create swarm
Write-Host "Creating swarm" -BackgroundColor "Green" -ForegroundColor "Black"
docker-machine ssh manager "sudo docker swarm init --advertise-addr $ipmanager"
Write-Host "Opening port 2377" -BackgroundColor "Green" -ForegroundColor "Black"
#open port 2377
az network nsg rule create `
--resource-group docker-machine `
--nsg-name manager-firewall `
--name allow-swarm `
--description "Allow access to port 2377" `
--access Allow `
--protocol Tcp `
--direction Inbound `
--priority 102 `
--source-address-prefix "*" `
--source-port-range "*" `
--destination-address-prefix "*" `
--destination-port-range "2377"
$worker_token = $(docker-machine ssh manager "sudo docker swarm join-token worker -q")
Write-Host "Joining Workers to Swarm" -BackgroundColor "Green" -ForegroundColor "Black"
# joining workers to swarm
For ($i=1; $i -lt $machines.length; $i++) {
echo $machines[$i] joining
$ipworker1 = $(docker-machine ip $machines[$i])
docker-machine ssh $machines[$i] "sudo docker swarm join --token $worker_token --advertise-addr $ipworker1 $ipmanager"
# Setting docker environment
& "C:\ProgramData\chocolatey\lib\docker-machine\bin\docker-machine.exe" env manager | Invoke-Expression
Write-Host "Displaying Swarm State" -BackgroundColor "Green" -ForegroundColor "Black"
# display swarm state
docker-machine ssh manager sudo docker node ls
Write-Host "Deploying nginx" -BackgroundColor "Green" -ForegroundColor "Black"
# deploy nginx on workers
docker service create -p 80:80 --constraint=node.role==worker --name webserver nginx
Write-Host "Creating Public IP Address" -BackgroundColor "Green" -ForegroundColor "Black"
# create public IP address
#echo creating public IP address
az network public-ip create --name nginxtestauto -g docker-machine -l westus2 --allocation-method static
Write-Host "Creating Load Balancer" -BackgroundColor "Green" -ForegroundColor "Black"
# create loadbalancer
#echo creating load balancer
az network lb frontend-ip create --lb-name nginxlb --public-ip-address nginxtestauto -g docker-machine -n nginxfront
Start-Sleep -s 10
Write-Host "Creating Load Balancer" -BackgroundColor "Green" -ForegroundColor "Black"
az network lb create -l westus2 -g docker-machine -g docker-machine --frontend-ip-name nginxfront --public-ip-address nginxtestauto -n nginxlb
Start-Sleep -s 10
Write-Host "Creating Load Balancing Rule" -BackgroundColor "Green" -ForegroundColor "Black"
az network lb rule create --resource-group docker-machine --lb-name nginxlb --name lbrule1 --protocol tcp --frontend-port 80 --backend-port 80 --frontend-ip-name nginxfront --backend-pool-name nginxlbbepool
# Pausing for 10 sec
Start-Sleep -s 10
Write-Host "Attaching worker NICs to the backend pool" -BackgroundColor "Green" -ForegroundColor "Black"
For ($i=1; $i -lt $machines.length; $i++) {
az network nic update -g docker-machine --name "$($machines[1])-nic" --add ipConfigurations[name="ip"].loadBalancerBackendAddressPools id="/subscriptions/$subid/resourceGroups/docker-machine/providers/Microsoft.Network/loadBalancers/nginxlb/backendAddressPools/nginxlbbepool"
Write-Host "Deploying OMS Agent on VMs" -BackgroundColor "Green" -ForegroundColor "Black"
# Deploying OMS on VMs
if ($omsWorkespaceKey) {
For ($i=0; $i -lt $machines.length; $i++) {
Write-Host "Deploying OMS on $machines[$i]" -BackgroundColor "Green" -ForegroundColor "Black"
az vm extension set --resource-group docker-machine --vm-name $machines[$i] --name OmsAgentForLinux `
--publisher Microsoft.EnterpriseCloud.Monitoring `
--version 1.3.127-7 --protected-settings '{\"workspaceKey\": \"$omsWorkspaceKey\"}' `
--settings '{\"workspaceId\": \"$omsWorkspaceId\"}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment