您好,登錄后才能下訂單哦!
本篇內容介紹了“Docker容器跨主機通信怎么實現”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
默認情況下docker容器需要跨主機通信兩個主機節點都需要在同一個網段下,這時只要兩個docker容器的宿主機能相互通信并且該容器使用net網絡模式,改實現方式為網橋模式通信;
除此之外我們還可以通過使用第三方工具為不同主機間創建一個覆蓋網絡,使之能夠 跨節點通信 ,這里將使用flanneld實現;
安裝etcd
創建 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"
創建/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
啟動systemctl start etcd
在etcd中創建目錄:etcdctl --endpoints=http://192.168.2.150:2379,http://192.168.5.151:2379
mkdir /kube-centos/network
創建config節點并寫入網絡配置信息:
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
創建 /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"
創建/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
啟動systemctl start flanneld
flannled啟動后會生產/run/flannel/subnet.env文件
修改docker啟動參數配置加上:
environmentfile=/run/flannel/subnet.env --bip=${flannel_subnet} --ip-masq=${flannel_ipmasq} --mtu=${flannel_mtu}
重啟docker,此時docker將使用flanneld配置的網段為container分配ip;
在兩個節點分別啟動容器:docker run -it –rm busybox sh
查看其中一個主機節點的容器ip,ping另一個主機節點ip
查看其中一個主機節點的容器ip,ping另一個主機節點ip
此時已可聯通;
注意iptables配置是否正確。
“Docker容器跨主機通信怎么實現”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。