1. 程式人生 > >Docker容器_Swarm叢集構建_學習記錄 (3)

Docker容器_Swarm叢集構建_學習記錄 (3)

Docker容器_學習記錄 (3)

swarm叢集構建:

官方文件:

https://docs.docker.com/swarm/install-manual/#step-5-create-swarm-cluster  

環境:centos7
A: 192.168.183.129(node01)
B: 192.168.183.130(node02)
C: 192.168.183.128(mg01)


1.  A/B/C:
#yum install docker

編輯配置檔案
Edit /etc/docker/daemon.json. Create it if it does not exist. Assuming the file was empty, its contents should be:
{
  "hosts": ["tcp://0.0.0.0:2375", "unix:///var/run/docker.sock"]
}

#systemctl restart docker

2. 分別給A、B節點安裝swarm
#docker pull swarm

 

3. C節點:
#docker pull progrium/consul
#docker run -d -p 8500:8500 --name=consul progrium/consul -server -bootstrap -advertise=<consul0_ip>
#docker run -d -p 8500:8500 --name=consul progrium/consul -server -bootstrap -advertise=192.168.183.128

為保證伺服器或docker服務重啟後容器能自動啟動,建議使用如下命令:
#docker run -d --restart unless-stopped -p 8500:8500 --name=consul progrium/consul -server -bootstrap -advertise=192.168.183.128


5. create swarm cluster:


#docker run -d -p 4000:4000 swarm manage -H :4000 --replication --advertise <manager0_ip>:4000 consul://<consul0_ip>:8500

#docker run -d -p 4000:4000 swarm manage -H :4000 --replication --advertise 192.168.183.128:4000 consul://192.168.183.128:8500

#docker run -d  --restart unless-stopped -p 4000:4000 swarm manage -H :4000 --replication --advertise 192.168.183.128:4000 consul://192.168.183.128:8500


6. Connect to node0 and node1 in turn and join them to the cluster
node01 
#docker run -d swarm join --advertise=192.168.183.130:2375 consul://192.168.183.128:8500

#docker run -d  --restart unless-stopped swarm join --advertise=192.168.183.130:2375 consul://192.168.183.128:8500

node02 
#docker run -d swarm join --advertise=192.168.183.129:2375 consul://192.168.183.128:8500

#docker run -d  --restart unless-stopped swarm join --advertise=192.168.183.129:2375 consul://192.168.183.128:8500

Start the swarm manager
#docker swarm init 然後在manager節點執行提示
然後根據提示在其他節點內執行

在其他節點內執行如下:

(注意)如果出現下列錯誤:

docker swarm:Error response from daemon: rpc error: code = Unavailable desc = grpc: the connection is unavailable

可能是防火牆沒關的問題。

#docker node ls (檢查節點)

#docker service create --replicas 2 -p 81:80 --name httpd docker.io/httpd:latest