這篇文章主要講解了“docker如何在不同的Linux機器上實現(xiàn)容器通信”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“docker如何在不同的Linux機器上實現(xiàn)容器通信”吧!
創(chuàng)新互聯(lián)建站擁有十多年成都網(wǎng)站建設(shè)工作經(jīng)驗,為各大企業(yè)提供網(wǎng)站制作、成都網(wǎng)站建設(shè)服務(wù),對于網(wǎng)頁設(shè)計、PC網(wǎng)站建設(shè)(電腦版網(wǎng)站建設(shè))、成都App定制開發(fā)、wap網(wǎng)站建設(shè)(手機版網(wǎng)站建設(shè))、程序開發(fā)、網(wǎng)站優(yōu)化(SEO優(yōu)化)、微網(wǎng)站、主機域名等,憑借多年來在互聯(lián)網(wǎng)的打拼,我們在互聯(lián)網(wǎng)網(wǎng)站建設(shè)行業(yè)積累了很多網(wǎng)站制作、網(wǎng)站設(shè)計、網(wǎng)絡(luò)營銷經(jīng)驗,集策劃、開發(fā)、設(shè)計、營銷、管理等網(wǎng)站化運作于一體,具備承接各種規(guī)模類型的網(wǎng)站建設(shè)項目的能力。
分布式存儲有很多工具,今天選擇etcd工具,這個是開源的免費分布式存儲。
準(zhǔn)備實驗環(huán)境:
docker node1 ip: 192.168.0.109
docker node2 ip: 192.168.0.107
在docker node1 上,下載解壓https://github.com/etcd-io/etcd/releases/tag/v3.3.13
vincent@swarm-manager:~/etcd-v3.3.13-linux-amd64$ nohup ./etcd --name docker-node1 --initial-advertise-peer-urls http://192.168.0.109:2380 \ > --listen-peer-urls http://192.168.0.109:2380 \ > --listen-client-urls http://192.168.0.109:2379,http://127.0.0.1:2379 \ > --advertise-client-urls http://192.168.0.109:2379 \ > --initial-cluster-token etcd-cluster \ > --initial-cluster docker-node1=http://192.168.0.109:2380,docker-node2=http://192.168.0.107:2380 \ > --initial-cluster-state new & [1] 3870 vincent@swarm-manager:~/etcd-v3.3.13-linux-amd64$ nohup: ignoring input and appending output to 'nohup.out'
--name 表示這個名字是docker-node1
--initial-advertise-peer-urls 表示本地地址
./etcdctl cluster-health member 72400c6f5c5691f3 is healthy: got healthy result from http://172.16.247.131:2379 member dc0810ee9a06524d is healthy: got healthy result from http://172.16.247.132:2379 cluster is healthy
在docker node2上也去執(zhí)行:
wget https://github.com/coreos/etcd/releases/download/v3.0.12/etcd-v3.0.12-linux-amd64.tar.gz vincent@swarm-worker-1:~/etcd-v3.3.13-linux-amd64$ ./etcd --name docker-node2 --initial-advertise-peer-urls http://192.168.0.107:2380 \ > --listen-peer-urls http://192.168.0.107:2380 \ > --listen-client-urls http://192.168.0.107:2379,http://127.0.0.1:2379 \ > --advertise-client-urls http://192.168.0.107:2379 \ > --initial-cluster-token etcd-cluster \ > --initial-cluster docker-node1=http://192.168.0.109:2380,docker-node2=http://192.168.0.107:2380 \ > --initial-cluster-state new &
如何確定我們的cluster已經(jīng)成功建立了:
在node1上執(zhí)行:
vincent@swarm-manager:~/etcd-v3.3.13-linux-amd64$ ./etcdctl cluster-health member beb7fd3596aa26eb is healthy: got healthy result from http://192.168.0.109:2379 member e6bdc10e37172e00 is healthy: got healthy result from http://192.168.0.107:2379 cluster is healthy
然后在node2上執(zhí)行:
vincent@swarm-worker-1:~/etcd-v3.3.13-linux-amd64$ ./etcdctl cluster-health member beb7fd3596aa26eb is healthy: got healthy result from http://192.168.0.109:2379 member e6bdc10e37172e00 is healthy: got healthy result from http://192.168.0.107:2379 cluster is healthy
這樣我們就在兩臺機器上搭建了一個分布式存儲
因為我們要讓docker知道我們要去使用分布式存儲。
在docker-node1上執(zhí)行:
vincent@swarm-manager:~/etcd-v3.3.13-linux-amd64$ sudo service docker stop [sudo] password for vincent: vincent@swarm-manager:~/etcd-v3.3.13-linux-amd64$ docker --version Docker version 17.12.0-ce, build c97c6d6
然后手動啟動docker:
vincent@swarm-manager:~/etcd-v3.3.13-linux-amd64$ sudo /usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock --cluster-store=etcd://192.168.0.109:2379 --cluster-advertise=192.168.0.109:2375& [2] 4153
在docker-node2上執(zhí)行:
sudo service docker stop sudo /usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock --cluster-store=etcd://192.168.0.109:2379 --cluster-advertise=192.168.0.109:2375&
在docker-node1上創(chuàng)建一個demo的overlay network
sudo docker network ls NETWORK ID NAME DRIVER SCOPE f6acf1d69b7c bridge bridge local c051f46f8a15 host host local 4caf51fb3438 none null local
docker network create -d overlay demo 1607f5636b8515d7e06d2f13261d32d8370c72de99ffb688ccdce3f6d8bce898
docker network ls NETWORK ID NAME DRIVER SCOPE f6acf1d69b7c bridge bridge local 1607f5636b85 demo overlay global c051f46f8a15 host host local 4caf51fb3438 none null local
查看overlay網(wǎng)絡(luò)的詳細(xì)信息:
docker network inspect demo [ { "Name": "demo", "Id": "1607f5636b8515d7e06d2f13261d32d8370c72de99ffb688ccdce3f6d8bce898", "Created": "2019-07-01T07:52:43.137469208-07:00", "Scope": "global", "Driver": "overlay", "EnableIPv6": false, "IPAM": { "Driver": "default", "Options": {}, "Config": [ { "Subnet": "10.0.0.0/24", "Gateway": "10.0.0.1" } ] }, "Internal": false, "Attachable": false, "Ingress": false, "ConfigFrom": { "Network": "" }, "ConfigOnly": false, "Containers": {}, "Options": {}, "Labels": {} } ]
我們會看到在node2上,這個demo的overlay network會被同步創(chuàng)建:
docker network ls NETWORK ID NAME DRIVER SCOPE e944ecd3d81f bridge bridge local 1607f5636b85 demo overlay global ca2b5e91ee2f host host local cfb09007c0ce none null local
在docker-node1上創(chuàng)建容器:
docker run -d --name test1 --network demo vincent/ubuntu-base /bin/bash -c "while true; do sleep 3600; done" c86061fd856cca0d157cc602cd9b98edd9f0fa4db9a26aa77ae2b054d6d804f1
docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c86061fd856c vincent/ubuntu-base "/bin/bash -c 'while…" About a minute ago Up About a minute
查看test1的ip:
docker exec test1 ifconfig eth0 Link encap:Ethernet HWaddr 02:42:0a:00:00:02 inet addr:10.0.0.2 Bcast:10.0.0.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1450 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) eth2 Link encap:Ethernet HWaddr 02:42:ac:12:00:02 inet addr:172.18.0.2 Bcast:172.18.255.255 Mask:255.255.0.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:16 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:1308 (1.3 KB) TX bytes:0 (0.0 B) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
在docker-node2上新建容器test2:
docker run -d --name test2 --network demo vincent/ubuntu-base /bin/bash -c "while true; do sleep 3600; done" 31f87913be02db7a8033b407c559a7a213445384d735239ff6504318c5077e46
在docker-node2上測試連通性
docker exec -it test2 ifconfig eth0 Link encap:Ethernet HWaddr 02:42:0a:00:00:03 inet addr:10.0.0.3 Bcast:10.0.0.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1450 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) eth2 Link encap:Ethernet HWaddr 02:42:ac:12:00:02 inet addr:172.18.0.2 Bcast:172.18.255.255 Mask:255.255.0.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:16 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:1308 (1.3 KB) TX bytes:0 (0.0 B) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
這兩個container之間可以連通。
感謝各位的閱讀,以上就是“docker如何在不同的Linux機器上實現(xiàn)容器通信”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對docker如何在不同的Linux機器上實現(xiàn)容器通信這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!
網(wǎng)頁標(biāo)題:docker如何在不同的Linux機器上實現(xiàn)容器通信
本文網(wǎng)址:http://aaarwkj.com/article22/psoejc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、手機網(wǎng)站建設(shè)、面包屑導(dǎo)航、關(guān)鍵詞優(yōu)化、網(wǎng)站排名、動態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)