Skip to content

Instantly share code, notes, and snippets.

@machv
Last active April 25, 2019 07:53
Show Gist options
  • Save machv/8e619675c07cf54ed35d70e089acb467 to your computer and use it in GitHub Desktop.
Save machv/8e619675c07cf54ed35d70e089acb467 to your computer and use it in GitHub Desktop.
Initialize Docker Swarm cluster in Windows
$hosts = 1..2 | foreach { ("swarm{0:00}.{1}" -f $_, $env:USERDNSDOMAIN).ToLower() }
# Install Docker Engine to all nodes
Invoke-Command -ComputerName $hosts -ScriptBlock {
Install-PackageProvider -name NuGet -MinimumVersion 2.8.5.201 -Force
Install-Module -Name DockerMsftProvider -Repository PSGallery -Force
Install-Package -Name docker -ProviderName DockerMsftProvider -Force
}
# Restart is needed
Restart-Computer -ComputerName $hosts -Protocol WSMan -Wait
# Convert IP leases for Docker hosts to static reservations
$scope = Get-DhcpServerv4Scope
Get-DhcpServerv4Lease -ScopeId $scope.ScopeId | where { $_.Hostname.ToLower() -in $hosts } | Add-DhcpServerv4Reservation
# Configure master
$masterIp = Invoke-Command -ComputerName $hosts[0] -ScriptBlock {
New-NetFirewallRule -DisplayName "Allow dockerd process" -Program "C:\Program files\Docker\dockerd.exe" -Profile Any -Action Allow -Enabled True -Direction Inbound
$ip = Get-NetIPAddress -AddressFamily IPv4 -Type Unicast -PrefixOrigin Dhcp | Select-Object -Last 1
docker swarm init --listen-addr $ip.IPAddress --advertise-addr $ip.IPAddress
$ip
}
$joinToken = Invoke-Command -ComputerName $hosts[0] -ScriptBlock {
docker swarm join-token worker --quiet
}
Invoke-Command -ComputerName ($hosts | Select -Skip 1) -ScriptBlock {
$ip = $Using:masterIp
docker swarm join --token "$Using:joinToken" $ip.IPAddress
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment