本篇內(nèi)容介紹了“Docker容器跨主機(jī)通信怎么實(shí)現(xiàn)”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
創(chuàng)新互聯(lián)公司是專業(yè)的庫車網(wǎng)站建設(shè)公司,庫車接單;提供網(wǎng)站設(shè)計(jì)、網(wǎng)站制作,網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行庫車網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
默認(rèn)情況下docker容器需要跨主機(jī)通信兩個(gè)主機(jī)節(jié)點(diǎn)都需要在同一個(gè)網(wǎng)段下,這時(shí)只要兩個(gè)docker容器的宿主機(jī)能相互通信并且該容器使用net網(wǎng)絡(luò)模式,改實(shí)現(xiàn)方式為網(wǎng)橋模式通信;
除此之外我們還可以通過使用第三方工具為不同主機(jī)間創(chuàng)建一個(gè)覆蓋網(wǎng)絡(luò),使之能夠 跨節(jié)點(diǎn)通信 ,這里將使用flanneld實(shí)現(xiàn);
安裝etcd
創(chuàng)建 cat /etc/etcd/etcd.conf文件
# [member] etcd_name=infra1 etcd_data_dir="/var/lib/etcd" etcd_listen_peer_urls="http://192.168.2.150:2380" etcd_listen_client_urls="http://192.168.2.150:2379" #[cluster] etcd_initial_advertise_peer_urls="http://192.168.2.150:2380" etcd_initial_cluster_token="etcd-cluster" etcd_advertise_client_urls="http://192.168.2.150:2379"
創(chuàng)建/etc/systemd/system/etcd.service文件
[unit] description=etcd server after=network.target after=network-online.target wants=network-online.target documentation=https://github.com/coreos [service] type=notify workingdirectory=/var/lib/etcd/ environmentfile=-/etc/etcd/etcd.conf execstart=/usr/local/bin/etcd \ --name ${etcd_name} \ --initial-advertise-peer-urls ${etcd_initial_advertise_peer_urls} \ --listen-peer-urls ${etcd_listen_peer_urls} \ --listen-client-urls ${etcd_listen_client_urls},http://127.0.0.1:2379 \ --advertise-client-urls ${etcd_advertise_client_urls} \ --initial-cluster-token ${etcd_initial_cluster_token} \ --initial-cluster infra1=http://192.168.2.150:2380,infra2=http://192.168.2.151:2380 \ --initial-cluster-state new \ --data-dir=${etcd_data_dir} restart=on-failure restartsec=5 limitnofile=65536 [install] wantedby=multi-user.target
啟動(dòng)systemctl start etcd
在etcd中創(chuàng)建目錄:etcdctl --endpoints=http://192.168.2.150:2379,http://192.168.5.151:2379
mkdir /kube-centos/network
創(chuàng)建config節(jié)點(diǎn)并寫入網(wǎng)絡(luò)配置信息:
etcdctl --endpoints=http://172.20.0.113:2379,http://172.20.0.114:2379 mk /kube-centos/network/config '{"network":"192.167.0.0/16","subnetlen":24,"backend":{"type":"vxlan"}}'
flanneld
創(chuàng)建 /etc/sysconfig/flanneld文件
# flanneld configuration options # etcd url location. point this to the server where etcd runs flannel_etcd_endpoints="http://127.0.0.1:2379" # etcd config key. this is the configuration key that flannel queries # for address range assignment # flannel_etcd_prefix="/kube-centos/network" flannel_etcd_prefix="/coreos.com/network" # any additional options that you want to pass flannel_options="-iface=eth0"
創(chuàng)建/usr/lib/systemd/system/flanneld.service文件
[unit] description=flanneld overlay address etcd agent after=network.target after=network-online.target wants=network-online.target after=etcd.service before=docker.service [service] type=notify environmentfile=/etc/sysconfig/flanneld environmentfile=-/etc/sysconfig/docker-network #execstart=/usr/bin/flanneld-start $flannel_options execstart=/usr/bin/flanneld-start -etcd-endpoints=http://192.168.2.150:2379,http://192.168.2.151:2379 - iface=ens33 #execstart=/usr/bin/flanneld-start -etcd-endpoints=http://192.168.2.150:2379,http://192.168.2.151:2379 -etcd- prefix=/kube-centos/network execstartpost=/usr/libexec/flannel/mk-docker-opts.sh -k docker_network_options -d /run/flannel/docker restart=on-failure [install] wantedby=multi-user.target requiredby=docker.service
啟動(dòng)systemctl start flanneld
flannled啟動(dòng)后會(huì)生產(chǎn)/run/flannel/subnet.env文件
修改docker啟動(dòng)參數(shù)配置加上:
environmentfile=/run/flannel/subnet.env --bip=${flannel_subnet} --ip-masq=${flannel_ipmasq} --mtu=${flannel_mtu}
重啟docker,此時(shí)docker將使用flanneld配置的網(wǎng)段為container分配ip;
在兩個(gè)節(jié)點(diǎn)分別啟動(dòng)容器:docker run -it –rm busybox sh
查看其中一個(gè)主機(jī)節(jié)點(diǎn)的容器ip,ping另一個(gè)主機(jī)節(jié)點(diǎn)ip
查看其中一個(gè)主機(jī)節(jié)點(diǎn)的容器ip,ping另一個(gè)主機(jī)節(jié)點(diǎn)ip
此時(shí)已可聯(lián)通;
注意iptables配置是否正確。
“Docker容器跨主機(jī)通信怎么實(shí)現(xiàn)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
文章名稱:Docker容器跨主機(jī)通信怎么實(shí)現(xiàn)
分享地址:http://muchs.cn/article44/ghhjee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、自適應(yīng)網(wǎng)站、網(wǎng)站內(nèi)鏈、企業(yè)建站、網(wǎng)站排名、動(dòng)態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)