您好,登錄后才能下訂單哦!
默認(rèn)情況下Docker容器需要跨主機通信兩個主機節(jié)點都需要在同一個網(wǎng)段下,這時只要兩個Docker容器的宿主機能相互通信并且該容器使用net網(wǎng)絡(luò)模式,改實現(xiàn)方式為網(wǎng)橋模式通信;
除此之外我們還可以通過使用第三方工具為不同主機間創(chuàng)建一個覆蓋網(wǎng)絡(luò),使之能夠 跨節(jié)點通信 ,這里將使用Flanneld實現(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
啟動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é)點并寫入網(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
啟動systemctl start flanneld
flannled啟動后會生產(chǎn)/run/flannel/subnet.env文件
修改docker啟動參數(shù)配置加上:
EnvironmentFile=/run/flannel/subnet.env --bip=${FLANNEL_SUBNET} --ip-masq=${FLANNEL_IPMASQ} --mtu=${FLANNEL_MTU}
重啟docker,此時docker將使用flanneld配置的網(wǎng)段為container分配ip;
在兩個節(jié)點分別啟動容器:docker run -it –rm busybox sh
查看其中一個主機節(jié)點的容器IP,ping另一個主機節(jié)點IP
查看其中一個主機節(jié)點的容器IP,ping另一個主機節(jié)點IP
此時已可聯(lián)通;
注意iptables配置是否正確;
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。