手记

『中级篇』docker-swarm创建一个多节点集群(43)

docker Swarm是Docker官方提供的一款集群管理工具,其主要作用是把若干台Docker主机抽象为一个整体,并且通过一个入口统一管理这些Docker主机上的各种Docker资源。源码地址:https://github.com/limingios/docker中的No.5

创建3台虚拟机

还是通过vagrant + virtualbox的方式来创建

  • 源码文件夹

  • 查看创建虚拟机的源码
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.require_version ">= 1.6.0"

boxes = [
    {
        :name => "swarm-manager",
        :mem => "1024",
        :cpu => "1"
    },
    {
        :name => "swarm-worker1",
        :mem => "1024",
        :cpu => "1"
    },
    {
        :name => "swarm-worker2",
        :mem => "1024",
        :cpu => "1"
    }
]

Vagrant.configure(2) do |config|

  config.vm.box = "centos/7"

  boxes.each do |opts|
      config.vm.define opts[:name] do |config|
        config.vm.hostname = opts[:name]
        config.vm.provider "vmware_fusion" do |v|
          v.vmx["memsize"] = opts[:mem]
          v.vmx["numvcpus"] = opts[:cpu]
        end

        config.vm.provider "virtualbox" do |v|
          v.customize ["modifyvm", :id, "--memory", opts[:mem]]
          v.customize ["modifyvm", :id, "--cpus", opts[:cpu]]
        end

        config.vm.network :private_network, type: "dhcp"
      end
  end

  config.vm.synced_folder "./labs", "/home/vagrant/labs"
  config.vm.provision "shell", privileged: true, path: "./setup.sh"

end

用SecureCRT连接着3个虚拟机
  • 查看每个虚拟机的ip地址
    
    vagrant ssh swarm-manager
    #swarm-manager的ip是172.28.128.3
    ip a
![](https://upload-images.jianshu.io/upload_images/11223715-ad3619672ae9886b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

![](https://upload-images.jianshu.io/upload_images/11223715-e7e7b767549fdd32.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

``` bash
vagrant ssh swarm-worker1
#swarm-worker1的ip是172.28.128.4
ip a

vagrant ssh swarm-worker2
#swarm-worker2的ip是172.28.128.5
ip a

创建docker-swarm集群
  • swarm-manager 建立manager
    sudo docker swarm init --help
    sudo docker swarm init --advertise-addr=172.28.128.3

  • swarm-worker1 和 swam-worker2 连接manager
    sudo service docker restart
    docker swarm join --token SWMTKN-1-1vafvfucsur6iqd6ba5i2njty6djvzxnhwefsmar4sivcg8isi-dgm1aigtcu1rtotfc8y43j17m 172.28.128.3:2377

  • swarm-manager 查看连接状态
    docker node ls

PS:这样基本上通过docker-swarm完成集群管理。

0人推荐
随时随地看视频
慕课网APP