本篇内容介绍了“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
# 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文件
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
此时已可联通;
注意iptables配置是否正确。
“Docker容器跨主机通信怎么实现”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注编程之家网站,小编将为大家输出更多高质量的实用文章!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。