這篇文章主要介紹“怎么用docker1.12搭建多主機(jī)docker swarm集群”,在日常操作中,相信很多人在怎么用docker1.12搭建多主機(jī)docker swarm集群?jiǎn)栴}上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”怎么用docker1.12搭建多主機(jī)docker swarm集群”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
目前創(chuàng)新互聯(lián)已為成百上千的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬空間、網(wǎng)站托管運(yùn)營(yíng)、企業(yè)網(wǎng)站設(shè)計(jì)、瓊山網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
準(zhǔn)備
準(zhǔn)備至少兩臺(tái)的centos 7 主機(jī)(全新最小安裝, 可以使用虛擬機(jī)安裝)
開(kāi)放端口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
在每臺(tái)機(jī)器上執(zhí)行以下命令
# 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
開(kāi)放端口相關(guān)命令
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 命令詳情
有關(guān)集群的docker命令如下:
docker swarm:集群管理,子命令有init, join,join-token, leave, update
docker node:節(jié)點(diǎn)管理,子命令有demote, inspect,ls, promote, rm, ps, update
docker service:服務(wù)管理,子命令有create, inspect, ps, ls ,rm , scale, update
docker stack/deploy:試驗(yàn)特性,用于多應(yīng)用部署
創(chuàng)建swarm 集群
查看docker swarm 命令說(shuō)明
[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
指定監(jiān)聽(tīng)的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.
查看結(jié)果
[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上執(zhí)行:
#命令格式: 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查看結(jié)果
[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
創(chuàng)建一個(gè)overlay 跨主機(jī)網(wǎng)絡(luò)
查看原有網(wǎng)絡(luò)
[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上默認(rèn)已有一個(gè)名為ingress的overlay 網(wǎng)絡(luò),默認(rèn)在swarm里使用,本文會(huì)創(chuàng)建一個(gè)新的
創(chuàng)建一個(gè)新的overlay網(wǎng)絡(luò)
[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
新的網(wǎng)絡(luò)(docker-net)已創(chuàng)建
在新的跨主機(jī)overlay 網(wǎng)絡(luò)(docker-net)上創(chuàng)建應(yīng)用
部署
用alpine鏡像在docker-net網(wǎng)絡(luò)上啟動(dòng)兩個(gè)實(shí)例, 并編排為一組服務(wù)
[root@centos-node4 ~]# docker service create --replicas 2 --name helloworld --network=docker-net alpine ping docker.com 5lgdq3ihiez0o7h3uegu4fgd3
查看部署結(jié)果
[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
可以看到兩個(gè)實(shí)例分別運(yùn)行在兩個(gè)節(jié)點(diǎn)上
測(cè)試兩個(gè)主機(jī)的網(wǎng)絡(luò)是否能互通
分別查看兩個(gè)實(shí)例的名稱
# 在swarm-manager 上執(zhí)行 [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 上執(zhí)行 [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 上測(cè)試
[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 上測(cè)試
[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
現(xiàn)在新版的docker swarm 管理非常簡(jiǎn)單, 可以快速的搭建起一個(gè)跨主機(jī)的集群并部署應(yīng)用
dokcer swarm自帶的負(fù)載均衡
創(chuàng)建一組服務(wù)
復(fù)制代碼 代碼如下:
docker service create --replicas 2 --name whoami -p 8080:80 daocloud.io/nginx:alpine
訪問(wèn)服務(wù)(可以多執(zhí)行幾次)
# 訪問(wèn)地址格式: swarm-manager.ip + (-p映射的端口) curl -v 192.168.99.101:8080
然后用docker logs 查看容器中nginx的訪問(wèn)日志, 可以現(xiàn)兩個(gè)容器都有訪問(wèn)記錄
到此,關(guān)于“怎么用docker1.12搭建多主機(jī)docker swarm集群”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
本文標(biāo)題:怎么用docker1.12搭建多主機(jī)dockerswarm集群
本文來(lái)源:http://www.rwnh.cn/article2/jipooc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開(kāi)發(fā)、動(dòng)態(tài)網(wǎng)站、企業(yè)建站、外貿(mào)網(wǎng)站建設(shè)、面包屑導(dǎo)航、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容