Docker-Swarm集群
创建环境
1.使用Vagrant方式
Vagrantfile文件
Vagrant.require_version ">= 1.6.0" boxes = [ { :name => "swarm-manager", :eth1 => "192.168.88.100", :mem => "1024", :cpu => "1" }, { :name => "swarm-worker1", :eth1 => "192.168.88.101", :mem => "1024", :cpu => "1" }, { :name => "swarm-worker2", :eth1 => "192.168.88.102", :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
setup.sh文件
#/bin/sh # install some tools sudo yum install -y wget sudo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak sudo wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo sudo yum makecache sudo yum -y update sudo yum install -y git vim gcc glibc-static telnet bridge-utils net-tools # install docker sudo curl -fsSL get.docker.com -o get-docker.sh sudo sh get-docker.sh # start docker service sudo groupadd docker sudo usermod -aG docker vagrant sudo rm -rf get-docker.sh sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://grlbs31g.mirror.aliyuncs.com"] } EOF sudo systemctl daemon-reload sudo systemctl start docker sudo docker version sudo curl -L "https://github.com/docker/compose/releases/download/1.26.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose sudo chmod +x /usr/local/bin/docker-compose sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose sudo docker-compose --version sudo echo "Congratulations on your success!"
创建机器
# 先装插件 vagrant plugin install vagrant-vbguest # Vagrantfile文件所在目录执行 vagrant status vagrant up
参考:https://github.com/limingios/docker
2. docker-machine方式
安装Docker Toolbox
http://mirrors.aliyun.com/docker-toolbox/windows/docker-toolbox/
创建机器
docker-machine create swarm-manager docker-machine create swarm-worker1 docker-machine create swarm-worker2
搭建Swarm集群
# manager docker swarm init --advertise-addr 192.168.88.100 # worker1 & worker2 docker swarm join --token SWMTKN-1-1jtoogjh8go3dslv2zfbu7qqiz6xhs28qt3651nrdi206cwobp-e74f0yoqo6nwdxrdj3pfnkwkq 192.168.88.100:2377 # 如果要添加manager2,manager执行 docker swarm join-token manager # manager2 docker swarm join --token SWMTKN-1-1jtoogjh8go3dslv2zfbu7qqiz6xhs28qt3651nrdi206cwobp-3y38tktkfpq86lhjf2hmswb6f 192.168.88.100:2377 # 查看节点信息 docker node ls 节点支持升级为manager、降级为worker、退出集群 docker node --help docker swarm --help
集群中创建容器
docker service create --name demo busybox sh -c "while true;do sleep 3600;done" docker service ls # 查看容器在集群中哪个节点中创建 docker service ps demo # 横向扩展 docker service scale demo=5 docker service rm demo
集群中跨机器的容器间通信
# 创建overlay驱动类型的网络 docker network create -d overlay demo # 创建MySQL docker service create --name mysql --network demo --env MYSQL_ROOT_PASSWORD=root --env MYSQL_DATABASE=wordpress --mount type=volume,source=mysql-data,destination=/var/lib/mysql mysql:5.7 # 创建Wordpress docker service create --name wordpress --network demo -p 80:80 --env WORDPRESS_DB_PASSWORD=root WORDPRESS_DB_HOST=mysql wordpress # 浏览器访问 192.168.88.100 192.168.88.101 192.168.88.102 都可以访问通
Routing Mesh两种方式
Internal:容器间通过overlay网络通信(DNS域名解析+VIP虚拟IP+Iptables防火墙+LVS负载均衡)
# 查看DNS对应的VIP nslookup mysql # 查看VIP对象的容器真实IP nslookup tasks.mysql
Ingress:如果服务有绑定端口,则通过集群中各个节点IP:Port都可以访问到服务
# swarm-worker1中显示防火墙配置规则 sudo iptables -nL -t nat # 查看docker_gwbridge网桥对应的接口 sudo brctl show # 查看docker_gwbridge网桥对应的namespace网络命名空间 sudo docker network inspect docker_gwbridge # 列举namespace网络命名空间 sudo ls /var/run/docker/netns # 切换到ingress_sbox网络命名空间中 sudo nsenter --net=/var/run/docker/netns/ingress_sbox sudo iptables -nL -t mangle # 切换回swarm-worker1中 sudo yum install -y ipvsadm # 切换到ingress_sbox网络命名空间中 sudo nsenter --net=/var/run/docker/netns/ingress_sbox ipvsadm -l
Docker Stack集群环境方式一键部署多个容器
docker-compose.yml文件
version: '3' services: web: image: wordpress ports: - 8080:80 environment: WORDPRESS_DB_HOST: mysql WORDPRESS_DB_PASSWORD: root networks: - my-network depends_on: - mysql deploy: mode: replicated replicas: 3 restart_policy: condition: on-failure delay: 30s max_attempts: 3 update_config: parallelism: 1 delay: 10s mysql: image: mysql:5.7 environment: MYSQL_ROOT_PASSWORD: root MYSQL_DATABASE: wordpress volumes: - mysql-data:/var/lib/mysql networks: - my-network deploy: mode: global placement: constraints: - node.role == manager volumes: mysql-data: networks: my-network: driver: overlay
创建服务
docker stack deploy wordpress --compose-file docker-compose.yml docker stack ls docker stack ps wordpress docker stack services wordpress
复杂的服务
参考:https://docs.docker.com/compose/compose-file/#compose-file-structure-and-examples