您好,登錄后才能下訂單哦!
這篇文章主要介紹“怎么用docker1.12搭建多主機docker swarm集群”,在日常操作中,相信很多人在怎么用docker1.12搭建多主機docker swarm集群問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么用docker1.12搭建多主機docker swarm集群”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
準備
準備至少兩臺的centos 7 主機(全新最小安裝, 可以使用虛擬機安裝)
開放端口2377 tcp端口, 7946 4789 tcp udp 端口
本文使用192.168.99.101(hostname:centos-node4) 作為swarm manager
192.168.99.102(hostname:centos-node5) 作為swarm agent1
安裝docker engine 1.12
在每臺機器上執行以下命令
# sudo tee /etc/yum.repos.d/docker.repo <<-'EOF' [dockerrepo] name=Docker Repository baseurl=https://yum.dockerproject.org/repo/main/centos/7/ enabled=1 gpgcheck=1 gpgkey=https://yum.dockerproject.org/gpg EOF # sudo yum install docker-engine # sudo systemctl enable docker # sudo systemctl start docker
安裝完后查看Docker 版本
[root@centos-node4 ~]# docker version Client: Version: 1.12.0 API version: 1.24 Go version: go1.6.3 Git commit: 8eab29e Built: OS/Arch: linux/amd64 Server: Version: 1.12.0 API version: 1.24 Go version: go1.6.3 Git commit: 8eab29e Built: OS/Arch: linux/amd64
開放端口相關命令
firewall-cmd --zone=public --add-port=2377/tcp --permanent firewall-cmd --zone=public --add-port=7946/tcp --permanent firewall-cmd --zone=public --add-port=7946/udp --permanent firewall-cmd --zone=public --add-port=4789/tcp --permanent firewall-cmd --zone=public --add-port=4789/udp --permanent firewall-cmd --reload
新版docker swarm 命令詳情
有關集群的docker命令如下:
docker swarm:集群管理,子命令有init, join,join-token, leave, update
docker node:節點管理,子命令有demote, inspect,ls, promote, rm, ps, update
docker service:服務管理,子命令有create, inspect, ps, ls ,rm , scale, update
docker stack/deploy:試驗特性,用于多應用部署
創建swarm 集群
查看docker swarm 命令說明
[root@centos-node4 ~]# docker swarm -h Flag shorthand -h has been deprecated, please use --help Usage: docker swarm COMMAND Manage Docker Swarm Options: --help Print usage Commands: init Initialize a swarm join Join a swarm as a node and/or manager join-token Manage join tokens update Update the swarm leave Leave a swarm Run 'docker swarm COMMAND --help' for more information on a command.
在swarm manager(centos-node4:192.168.99.101)初始化swarm集群
用--listen-addr
指定監聽的ip與端口
#命令格式: docker swarm init --listen-addr <MANAGER-IP>:<PORT> [root@centos-node4 ~]# docker swarm init --listen-addr 192.168.99.101:2377 Swarm initialized: current node (a60d5c3ttymvtozr46uvk17q4) is now a manager.
查看結果
[root@centos-node4 ~]# docker node ls ID HOSTNAME MEMBERSHIP STATUS AVAILABILITY MANAGER STATUS a60d5c3ttymvtozr46uvk17q4 * centos-node4 Accepted Ready Active Leader
把swarm-agent1(centos-node5: 192.168.99.102)添加到swarm集群
在swarm-agent1上執行:
#命令格式: docker swarm join <MANAGER-IP>:<PORT> [root@centos-node5 ~]# docker swarm join 192.168.99.101:2377 This node joined a Swarm as a worker.
在swarm manager查看結果
[root@centos-node4 ~]# docker node ls ID HOSTNAME MEMBERSHIP STATUS AVAILABILITY MANAGER STATUS 0ypcw58hjlcvr0xqbtizmau62 centos-node5 Accepted Ready Active a60d5c3ttymvtozr46uvk17q4 * centos-node4 Accepted Ready Active Leader
創建一個overlay 跨主機網絡
查看原有網絡
[root@centos-node4 ~]# docker network ls NETWORK ID NAME DRIVER SCOPE abec77415f48 bridge bridge local e2fff9d572a6 docker_gwbridge bridge local 166bd71f7d0e host host local 9gr6bfff1rv9 ingress overlay swarm 1d2bfc590294 none null local
可以看到在swarm上默認已有一個名為ingress的overlay 網絡,默認在swarm里使用,本文會創建一個新的
創建一個新的overlay網絡
[root@centos-node4 ~]# docker network create --driver overlay docker-net aoqs3p835s5glx69hi46ou2dw [root@centos-node4 ~]# docker network ls NETWORK ID NAME DRIVER SCOPE abec77415f48 bridge bridge local aoqs3p835s5g docker-net overlay swarm e2fff9d572a6 docker_gwbridge bridge local 166bd71f7d0e host host local 9gr6bfff1rv9 ingress overlay swarm 1d2bfc590294 none null local
新的網絡(docker-net)已創建
在新的跨主機overlay 網絡(docker-net)上創建應用
部署
用alpine鏡像在docker-net網絡上啟動兩個實例, 并編排為一組服務
[root@centos-node4 ~]# docker service create --replicas 2 --name helloworld --network=docker-net alpine ping docker.com 5lgdq3ihiez0o7h3uegu4fgd3
查看部署結果
[root@centos-node4 ~]# docker service ls ID NAME REPLICAS IMAGE COMMAND 5lgdq3ihiez0 helloworld 0/2 alpine ping docker.com [root@centos-node4 ~]# docker service tasks helloworld ID NAME SERVICE IMAGE LAST STATE DESIRED STATE NODE eul3bus45qz3b555wekotdmo5 helloworld.1 helloworld alpine Running 14 seconds Running centos-node5 55uhq6xxcv53xlkqv2f0be9b9 helloworld.2 helloworld alpine Running 14 seconds Running centos-node4
可以看到兩個實例分別運行在兩個節點上
測試兩個主機的網絡是否能互通
分別查看兩個實例的名稱
# 在swarm-manager 上執行 [root@centos-node4 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f4a197abdb0b alpine:latest "ping docker.com" 42 minutes ago Up 42 minutes helloworld.2.55uhq6xxcv53xlkqv2f0be9b9 # 在swarm-agnet1 上執行 [root@centos-node5 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 39cc35cd54b5 alpine:latest "ping docker.com" 50 seconds ago Up 49 seconds helloworld.1.eul3bus45qz3b555wekotdmo5
在swarm-manager 上測試
[root@centos-node4 ~]# docker exec -ti helloworld.2.55uhq6xxcv53xlkqv2f0be9b9 sh / # ping helloworld.1.eul3bus45qz3b555wekotdmo5 PING helloworld.1.eul3bus45qz3b555wekotdmo5 (10.0.9.3): 56 data bytes 64 bytes from 10.0.9.3: seq=0 ttl=64 time=0.514 ms 64 bytes from 10.0.9.3: seq=1 ttl=64 time=0.508 ms 64 bytes from 10.0.9.3: seq=2 ttl=64 time=0.381 ms 64 bytes from 10.0.9.3: seq=3 ttl=64 time=0.408 ms ^C --- helloworld.1.eul3bus45qz3b555wekotdmo5 ping statistics --- 4 packets transmitted, 4 packets received, 0% packet loss round-trip min/avg/max = 0.381/0.452/0.514 ms
在swarm-agent1 上測試
[root@centos-node5 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 39cc35cd54b5 alpine:latest "ping docker.com" 50 seconds ago Up 49 seconds helloworld.1.eul3bus45qz3b555wekotdmo5 [root@centos-node5 ~]# docker exec -ti helloworld.1.eul3bus45qz3b555wekotdmo5 sh / # ping helloworld.2.55uhq6xxcv53xlkqv2f0be9b9 PING helloworld.2.55uhq6xxcv53xlkqv2f0be9b9 (10.0.9.4): 56 data bytes 64 bytes from 10.0.9.4: seq=0 ttl=64 time=0.892 ms 64 bytes from 10.0.9.4: seq=1 ttl=64 time=0.463 ms 64 bytes from 10.0.9.4: seq=2 ttl=64 time=0.462 ms 64 bytes from 10.0.9.4: seq=3 ttl=64 time=0.478 ms 64 bytes from 10.0.9.4: seq=4 ttl=64 time=0.468 ms 64 bytes from 10.0.9.4: seq=5 ttl=64 time=0.459 ms ^C --- helloworld.2.55uhq6xxcv53xlkqv2f0be9b9 ping statistics --- 6 packets transmitted, 6 packets received, 0% packet loss round-trip min/avg/max = 0.459/0.537/0.892 ms
現在新版的docker swarm 管理非常簡單, 可以快速的搭建起一個跨主機的集群并部署應用
dokcer swarm自帶的負載均衡
創建一組服務
復制代碼 代碼如下:
docker service create --replicas 2 --name whoami -p 8080:80 daocloud.io/nginx:alpine
訪問服務(可以多執行幾次)
# 訪問地址格式: swarm-manager.ip + (-p映射的端口) curl -v 192.168.99.101:8080
然后用docker logs 查看容器中nginx的訪問日志, 可以現兩個容器都有訪問記錄
到此,關于“怎么用docker1.12搭建多主機docker swarm集群”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。