本篇内容介绍了“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容器跨主机通信怎么实现”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/u/4593000/blog/4419699