swarm集羣搭建
一鍵搭建請參考:https://github.com/shenshouer/swarm-cluster
環境準備
- coreos alpha 928.0.0
- vagrant
- virtualbox
配置
synced_folders.yaml:
- name: default
source: .
destination: /vagrant
nfs: true
mount_options: 'nolock,vers=3,udp,noatime'
disabled: false
Vagrantfile:
require 'fileutils'
require 'yaml'
# Size of the cluster created by Vagrant
num_instances=4
# Read YAML file with mountpoint details
MOUNT_POINTS = YAML::load_file('synced_folders.yaml')
module OS
def OS.windows?
(/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil
end
def OS.mac?
(/darwin/ =~ RUBY_PLATFORM) != nil
end
def OS.unix?
!OS.windows?
end
def OS.linux?
OS.unix? and not OS.mac?
end
end
# Change basename of the VM
instance_name_prefix="swarm"
Vagrant.configure("2") do |config|
# always use Vagrants insecure key
config.ssh.insert_key = false
config.vm.box = "coreos-alpha"
config.vm.provider :virtualbox do |v|
# On VirtualBox, we don't have guest additions or a functional vboxsf
# in CoreOS, so tell Vagrant that so it can be smarter.
v.check_guest_additions = false
v.memory = 2048
v.cpus = 2
v.functional_vboxsf = false
end
# Set up each box
(1..num_instances).each do |i|
vm_name = "%s-%02d" % [instance_name_prefix, i]
config.vm.define vm_name do |host|
host.vm.hostname = vm_name
host.vm.synced_folder ".", "/vagrant", disabled: true
begin
MOUNT_POINTS.each do |mount|
mount_options = ""
disabled = false
nfs = true
if mount['mount_options']
mount_options = mount['mount_options']
end
if mount['disabled']
disabled = mount['disabled']
end
if mount['nfs']
nfs = mount['nfs']
end
if File.exist?(File.expand_path("#{mount['source']}"))
if mount['destination']
host.vm.synced_folder "#{mount['source']}", "#{mount['destination']}",
id: "#{mount['name']}",
disabled: disabled,
mount_options: ["#{mount_options}"],
nfs: nfs
end
end
end
rescue
end
ip = "172.18.19.#{i+100}"
host.vm.network :private_network, ip: ip
host.vm.provision :shell, :inline => "/usr/bin/timedatectl set-timezone Asia/Shanghai ", :privileged => true
host.vm.provision :docker, images: ["swarm:latest"]
sedInplaceArg = OS.mac? ? " ''" : ""
end
end
end
創建swarm集羣
- swarm-01 172.18.19.101 as swarm master && consul
- swarm-02 172.18.19.102 as swarm master
- swarm-03 172.18.19.103 swarm node
- swarm-04 172.18.19.104 swarm node
設置服務發現後臺
docker run -d -p 8500:8500 --name=consul progrium/consul -server -bootstrap
創建swarm高可用集羣
# 在swarm-01節點
# docker run -d -p 4000:4000 swarm manage -H :4000 --replication --advertise <manager0_ip>:4000 consul://<consul_ip> 如:
docker run -d -p 4000:4000 swarm manage -H :4000 --replication --advertise 172.18.19.101:4000 consul://172.18.19.101:8500
# 在swarm-02節點
docker run -d -p 4000:4000 swarm manage -H :4000 --replication --advertise 172.18.19.102:4000 consul://172.18.19.101:8500
# 在swarm-03節點
docker run -d swarm join --advertise=172.18.19.103:2375 consul://172.18.19.101:8500
# 在swarm-04節點
docker run -d swarm join --advertise=172.18.19.104:2375 consul://172.18.19.101:8500