# cat /etc/hosts 127.0.0.1 localhost 192.168.139.175 swarm01 192.168.139.176 swarm02 192.168.139.177 swarm03
# ssh-keygen -t rsa -P '' # ssh-copy-id -i .ssh/id_rsa.pub root@192.168.139.176 # ssh-copy-id -i .ssh/id_rsa.pub root@192.168.139.177
# yum -y install ansible # cat /etc/ansible/hosts | grep -v ^# | grep -v ^$ [node] 192.168.139.176 192.168.139.177 # sed -i "s/SELINUX=enforcing/SELINUX=disabled" /etc/selinux/config # ansible node -m copy -a 'src=/etc/selinux/config dest=/etc/selinux/' # systemctl stop firewalld # systemctl disable firewalld # ansible node -a 'systemctl stop firewalld' # ansible node -a 'systemctl disable firewalld'
注:這裏選擇關閉防火牆,實際環境中可自行開放端口。node
在manager節點安裝dockerlinux
# yum install -y yum-utils device-mapper-persistent-data lvm2 # yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo # yum list docker-ce --showduplicates | sort -r # yum -y install docker-ce # docker --version Docker version 17.06.0-ce, build 02c1d87 # systemctl start docker # systemctl status docker # systemctl enable docker
使用ansible在worker節點安裝dockernginx
# ansible node -m copy -a 'src=/etc/yum.repos.d/docker-ce.repo dest=/etc/yum.repos.d/' # ansible node -m yum -a "state=present name=docker-ce" # ansible node -a 'docker --version' 192.168.139.173 | SUCCESS | rc=0 >> Docker version 17.06.0-ce, build 02c1d87 192.168.139.174 | SUCCESS | rc=0 >> Docker version 17.06.0-ce, build 02c1d87 # ansible node -a 'systemctl start docker' # ansible node -a 'systemctl status docker' # ansible node -a 'systemctl enable docker'
# docker swarm init --listen-addr 0.0.0.0 Swarm initialized: current node (a1tno675d14sm6bqlc512vf10) is now a manager. To add a worker to this swarm, run the following command: docker swarm join --token SWMTKN-1-3sp9uxzokgr252u1jauoowv74930s7f8f5tsmm5mlk5oim359e-dk52k5uul50w49gbq4j1y7zzb 192.168.139.175:2377 To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
# docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS a1tno675d14sm6bqlc512vf10 * swarm01 Ready Active Leader
# docker swarm join-token manager To add a manager to this swarm, run the following command: docker swarm join --token SWMTKN-1-3sp9uxzokgr252u1jauoowv74930s7f8f5tsmm5mlk5oim359e-7tdlpdnkyfl1bnq34ftik9wxw 192.168.139.175:2377
# docker swarm join-token worker To add a worker to this swarm, run the following command: docker swarm join --token SWMTKN-1-3sp9uxzokgr252u1jauoowv74930s7f8f5tsmm5mlk5oim359e-dk52k5uul50w49gbq4j1y7zzb 192.168.139.175:2377
# docker swarm join --token SWMTKN-1-3sp9uxzokgr252u1jauoowv74930s7f8f5tsmm5mlk5oim359e-dk52k5uul50w49gbq4j1y7zzb 192.168.139.175:2377 This node joined a swarm as a worker.
# docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS 7zkbqgrjlsn8c09l3fagtfwre swarm02 Ready Active a1tno675d14sm6bqlc512vf10 * swarm01 Ready Active Leader apy9zys2ch4dlwbmgdqwc0pn3 swarm03 Ready Active
# docker network ls NETWORK ID NAME DRIVER SCOPE 05efca714d2f bridge bridge local c9cd9c37edd7 docker_gwbridge bridge local 10ac9e48d81b host host local n60tdenc5jy7 ingress overlay swarm a9284277dc18 none null local
這裏,一個docker swarm集羣就搭建好了web
Portainer地址:https://portainer.io/。docker
# docker service create \ --name portainer \ --publish 9000:9000 \ --constraint 'node.role == manager' \ --mount type=bind,src=//var/run/docker.sock,dst=/var/run/docker.sock \ portainer/portainer \ -H unix:///var/run/docker.sock # docker images |grep portainer portainer/portainer latest 07cde96d4789 2 weeks ago 10.4MB # docker service ls ###查看集羣列表 ID NAME MODE REPLICAS IMAGE PORTS p5bo3n0fmqgz portainer replicated 1/1 portainer/portainer:latest *:9000->9000/tcp
這就部署好了shell
密碼修改完成後點擊「validate」驗證centos
以下圖所示,輸入admin用戶名和密碼進入Portainer瀏覽器
首頁以下bash
查看swarm節點模塊網絡
這裏能夠在images模塊pull鏡像,在這裏我pull了nginx
在Services模塊下建立nginx服務,Services > Add service,這裏建立三個副本,並將80端口映射出去,最後點擊「Create Service」建立服務
刷新服務列表,查看是否建立成功
# docker images | grep nginxnginx latest b8efb18f159b 7 days ago 107MB # ansible node -m shell -a 'docker images|grep nginx' 192.168.139.177 | SUCCESS | rc=0 >> nginx latest b8efb18f159b 8 days ago 107MB 192.168.139.176 | SUCCESS | rc=0 >> nginx latest b8efb18f159b 8 days ago 107MB # docker service ls ###查看服務的任務列表 ID NAME MODE REPLICAS IMAGE PORTS emrs3rj73bwh Nginx replicated 3/3 nginx:latest *:80->80/tcp p5bo3n0fmqgz portainer replicated 1/1 portainer/portainer:latest *:9000->9000/tcp # docker service ps Nginx ID NAME IMAGE NODE 0smpndfx0bwc Nginx.1 nginx:latest swarm03 werrrzlyfbf1 Nginx.2 nginx:latest swarm01 l7puro0787cj Nginx.3 nginx:latest swarm02 DESIRED STATE CURRENT STATE ERROR PORTS Running Running 15 minutes ago Running Running 15 minutes ago Running Running 15 minutes ago
Shipyard的UI也是比較簡單的,可是比較反覆,它須要在每一個節點都pull相應鏡像才能加入Shipyard的UI。
# docker pull hub.c.163.com/library/alpine:latest # docker pull hub.c.163.com/library/rethinkdb:latest # docker pull hub.c.163.com/longjuxu/microbox/etcd:latest # docker pull hub.c.163.com/wangjiaen/shipyard/docker.io/shipyard/docker-proxy:latest # docker pull hub.c.163.com/library/swarm:latest # docker pull hub.c.163.com/wangjiaen/shipyard/docker.io/shipyard/shipyard:latest
# docker tag 7328f6f8b418 alpine # docker tag 4a511141860c rethinkdb # docker tag 6aef84b9ec5a microbox/etcd # docker tag cfee14e5d6f2 shipyard/docker-proxy # docker tag 0198d9ac25d1 swarm # docker tag 36fb3dc0907d shipyard/shipyard
# curl -sSL https://shipyard-project.com/deploy | bash -s Deploying Shipyard -> Starting Database -> Starting Discovery -> Starting Cert Volume -> Starting Proxy -> Starting Swarm Manager -> Starting Swarm Agent -> Starting Controller Waiting for Shipyard on 192.168.139.175:8080 .. Shipyard available at http://192.168.139.175:8080 Username: admin Password: shipyard
# curl -sSL https://shipyard-project.com/deploy | ACTION=node DISCOVERY=etcd://192.168.139.175:4001 bash -s Adding Node -> Starting Cert Volume -> Starting Proxy -> Starting Swarm Manager -> Starting Swarm Agent Node added to Swarm: 192.168.139.176
其餘節點同理。
對比兩種UI,其實都是比較簡單的,我的認爲Portainer較好,在manager節點pull一個鏡像便可搭建UI。
問題:
manager:
# docker swarm init --advertise-addr 192.168.139.175
worker:
# docker swarm join --token SWMTKN-1-4dwtfbdvjmuf3limglbpy66k85ply2cn66hd0ugsaxfed5fj1d-3rp33pedt9k7ewpfizbzc9bvi 192.168.139.175:2377 Error response from daemon: Timeout was reached before node was joined. The attempt to join the swarm will continue in the background. Use the "docker info" command to see the current swarm status of your node.
出現worker節點沒法加入集羣的問題,這裏須要設置監聽地址全零。
本文出自https://www.centos.bz/2017/08/docker-swarm-cluster-shipyard-ui-manager/