问题描述
项目在测试环境使用docker-compose方式部署微服务,刚开始一切正常。某次重新部署后突然出现内网172.31网段服务器无法访问。
问题定位
- 虚机访问非172.31网段一切正常;
- 同网段其他虚机访问172.31网段一切正常;
- 虚机防火墙设置正常;
ip addr查看网址时发现存在docker虚拟网卡使用了172.31网段地址,与内网172.31网段地址冲突造成服务器无法访问。
解决方案
When user creates a network without specifying a --subnet, docker will pick a subnet for the network from the static set 172.[17-31].0.0/16 and 192.168.[0-240].0/20 for the local scope networks and from the static set 10.[0-255].[0-255].0/24 for the global scope networks.
- 修改docker相关配置,调整默认的IP范围(推荐方式);
- 修改docker-compose配置文件,指定网络配置;
这里主要介绍方案1.
- 停用docker;
systemctl stop docker
- 删除之前docker创建的网络
# 查看docker网络信息
docker network ls
# 删除未使用的网络,主要是把发生网络冲突的那个网段给清掉
docker network prune
- 修改docker配置文件
vi /etc/docker/daemon.json
在配置文件中添加以下内容,其中default-address-pools的base表示CIDR地址,size表示docker创建的网络的掩码长度,CIDR的掩码长度应该小于size,否则docker将会出现网络失败。这里使用192.168网段地址,其中CIDR为16为掩码,划分的网络子网掩码24位,理论可以划分出2(32-16)-(32-24)=28=256个子网。
{
"registry-mirrors": [
"https://docker.mirrors.ustc.edu.cn/"
],
"debug": true,
"default-address-pools": [
{
"base": "192.168.1.1/16",
"size": 24
}
]
}
4)重新启动docker
# 请再次确认已将docker之前创建的网络删除
# 启动docker
systemctl start docker
设置bip无效
在daemon配置文件中单纯配置bip只对docker启动的容器有效,但是对docker-compose启动的容器无效。
Docker engine is started with --bip argument, but it is ignored by docker-compose, which set up an additional bridge interface in addition to the docker0 interface. It is named br-f4a912f7750b and has the typical Docker 172.17.x.x format, which happens to conflict with the network I’m currently using.
参考链接:
[1]: https://github.com/docker/compose/issues/4336
[2]: https://github.com/moby/moby/pull/29376