Docker 跨主机容器之间的通信macvlan

默认一个物理网卡,只有一个物理mac地址,虚拟多个mac地址

缺点:每次需要手动配置ip地址,容易ip地址冲突。类似于保存到execl表格里面。

两台物理机: docker-01和docker-02

创建macvlan网络

bash 复制代码
[root@docker-01 ~]# docker network create --driver macvlan --subnet 10.0.0.0/24 --gateway 10.0.0.254 -o parent=eth0 macvlan_1 
a2e790b4ac802c659e216b23096bb2f761361828f41498bf28392e8261fb7ea2
[root@docker-02 ~]# docker network create --driver macvlan --subnet 10.0.0.0/24 --gateway 10.0.0.254 -o parent=eth0 macvlan_1 
9c7839f953ba6f619d195872bc2a34791efa12b37c96754063041eadd876654e

创建使用macvlan网络的容器

bash 复制代码
[root@docker-01 ~]# docker run -itd --network macvlan_1 --ip=10.0.0.201 centos6_9_ssh_nginx:v1
[root@docker-02 ~]# docker run -itd --network macvlan_1 --ip=10.0.0.202 centos6_9_ssh_nginx:v1

互ping测试

bash 复制代码
[root@docker-01 ~]# docker exec -it lucid_mestorf /bin/bash
[root@cbeaf45ad4da /]# ifconfig |grep 10.0.0.
          inet addr:10.0.0.201  Bcast:10.0.0.255  Mask:255.255.255.0
[root@docker-02 ~]# docker exec -it 1e14992e9209 /bin/bash
[root@1e14992e9209 /]# ifconfig |grep 10.0.0.2
          inet addr:10.0.0.202  Bcast:10.0.0.255  Mask:255.255.255.0

在docker01的容器ping docker02容器的ip地址

在docker02的容器ping docker01容器的ip地址

bash 复制代码
[root@cbeaf45ad4da /]# ping 10.0.0.202 -c4
PING 10.0.0.202 (10.0.0.202) 56(84) bytes of data.
64 bytes from 10.0.0.202: icmp_seq=1 ttl=64 time=0.289 ms
64 bytes from 10.0.0.202: icmp_seq=2 ttl=64 time=1.44 ms
64 bytes from 10.0.0.202: icmp_seq=3 ttl=64 time=1.52 ms
64 bytes from 10.0.0.202: icmp_seq=4 ttl=64 time=1.31 ms

[root@1e14992e9209 /]# ping 10.0.0.201 -c4
PING 10.0.0.201 (10.0.0.201) 56(84) bytes of data.
64 bytes from 10.0.0.201: icmp_seq=1 ttl=64 time=2.64 ms
64 bytes from 10.0.0.201: icmp_seq=2 ttl=64 time=1.40 ms
64 bytes from 10.0.0.201: icmp_seq=3 ttl=64 time=0.521 ms
64 bytes from 10.0.0.201: icmp_seq=4 ttl=64 time=0.601 ms

设置eth0的网卡为混杂模式,ubuntu需要设置

bash 复制代码
ip link set eth1 promisc on
相关推荐
fetasty10 小时前
rustfs加picgo图床搭建
docker
蝎子莱莱爱打怪1 天前
GitLab CI/CD + Docker Registry + K8s 部署完整实战指南
后端·docker·kubernetes
碳基沙盒1 天前
OpenClaw 多 Agent 配置实战指南
运维
小p2 天前
docker学习7:docker 容器的通信方式
docker
小p2 天前
docker学习5:提升Dockerfile水平的5个技巧
docker
小p2 天前
docker学习3:docker是怎么实现的?
docker
小p3 天前
docker学习: 2. 构建镜像Dockerfile
docker
小p4 天前
docker学习: 1. docker基本使用
docker
蝎子莱莱爱打怪4 天前
Centos7中一键安装K8s集群以及Rancher安装记录
运维·后端·kubernetes
崔小汤呀4 天前
Docker部署Nacos
docker·容器