我在 AWS EC2 实例上运行 Docker 容器,Docker 容器通过 Docker 网络连接,该网络通过 etcd 集群进行复制。 但所有容器都无法与外部连接。例如,如果我 ping 或 curl 主机,则 100% 的包丢失。 etcd 网络通过 AWS 私有 IP 进行通信。
这是我的码头工人信息:
root@ip-10-0-127-34:/home# docker info
Containers: 3
Running: 3
Paused: 0
Stopped: 0
Images: 9
Server Version: 17.05.0-ce
Storage Driver: devicemapper
Pool Name: docker-202:1-2050903-pool
Pool Blocksize: 65.54kB
Base Device Size: 10.74GB
Backing Filesystem: xfs
Data file: /dev/loop0
Metadata file: /dev/loop1
Data Space Used: 4.554GB
Data Space Total: 107.4GB
Data Space Available: 102.8GB
Metadata Space Used:sattler 6.922MB
Metadata Space Total: 2.147GB
Metadata Space Available: 2.141GB
Thin Pool Minimum Free Space: 3.221GB
Udev Sync Supported: true
Deferred Removal Enabled: false
Deferred Deletion Enabled: false
Deferred Deleted Device Count: 0
Data loop file: /var/lib/docker/devicemapper/devicemapper/data
Metadata loop file: /var/lib/docker/devicemapper/devicemapper/metadata
Library Version: 1.02.110 (2015-10-30)
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host macvlan null overlay
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 9048e5e50717ea4497b757314bad98ea3763c145
runc version: 9c2d8d184e5da67c95d601382adf14862e4f2228
init version: 949e6fa
Security Options:
apparmor
seccomp
Profile: default
Kernel Version: 4.4.0-1028-aws
Operating System: Ubuntu 16.04.3 LTS
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 7.303GiB
Name: ip-10-0-127-34
ID: JFQO:C56I:VM22:UDKJ:QDKD:HUDT:KL3X:JCSR:WAPG:66JL:S4RM:4ENN
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Experimental: false
Cluster Store: etcd://127.0.0.1:2379
Cluster Advertise: 10.0.127.34:2376
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
WARNING: devicemapper: usage of loopback devices is strongly discouraged for production use.
Use `--storage-opt dm.thinpooldev` to specify a custom block storage device.
WARNING: No swap limit support
Docker 检查网络:
{
"Name": "df-global",
"Id": "59aef8ccc5d7464ee715428783b9b03ba7737d298f41cc30a21f4856e75b92c1",
"Created": "2017-08-14T09:51:31.650109966Z",
"Scope": "global",
"Driver": "overlay",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "192.168.1.0/14"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"Containers": {
"ep-f257d697653d801395f9154d086a6290694d6ded7aefe827c67dcd10808023eb": {
"Name": "elasticsearch-data-1",
"EndpointID": "f257d697653d801395f9154d086a6290694d6ded7aefe827c67dcd10808023eb",
"MacAddress": "02:42:c0:a8:00:0b",
"IPv4Address": "192.168.0.11/14",
"IPv6Address": ""
},
"ep-f5c1be7a768802154b58b33e67a3f009df1d4f1336297df9f914be9a720bd8ce": {
"Name": "postgresql-vg_internal01-prod",
"EndpointID": "f5c1be7a768802154b58b33e67a3f009df1d4f1336297df9f914be9a720bd8ce",
"MacAddress": "02:42:c0:a8:00:02",
"IPv4Address": "192.168.0.2/14",
"IPv6Address": ""
这里描述了同样的问题:https://forums.docker.com/t/ping- Between-containers-on- Different-docker-host-but-connected-by-overlay-networ-fails/9960 但没有答案
在主机上运行:
sudo ip addr show docker0
您将获得输出,其中包括:
inet 172.17.2.1/16 scope global docker0
docker 主机在 docker0 网络接口上的 IP 地址为 172.17.2.1。
然后启动容器:
docker run --rm -it ubuntu:trusty bash
然后运行
ip addr show eth0
输出将包括:
inet 172.17.1.29/16 scope global eth0
您的容器的 IP 地址为 172.17.1.29。现在查看路由表: 运行:
route
输出将包括:
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default 172.17.2.1 0.0.0.0 UG 0 0 0 eth0
这意味着 docker 主机的 IP 地址 172.17.2.1 被设置为默认路由,并且可以从您的容器访问。
现在尝试 ping 到您的主机 IP :
root@e21b5c211a0c:/# ping 172.17.2.1
PING 172.17.2.1 (172.17.2.1) 56(84) bytes of data.
64 bytes from 172.17.2.1: icmp_seq=1 ttl=64 time=0.071 ms
64 bytes from 172.17.2.1: icmp_seq=2 ttl=64 time=0.211 ms
64 bytes from 172.17.2.1: icmp_seq=3 ttl=64 time=0.166 ms
如果这有效,您很可能能够 ping 通任何公共 IP
希望对你有帮助!
我仍然能够
ping 172.17.2.1
,但这是我可以从容器内联系的唯一 IP 地址。 结果有人/东西清除了 iptables 防火墙,并用默认的 ACCEPT
规则替换了每个表中的每一条链! 所有 docker nat 和转发规则都丢失了。 快速重启解决了这个问题。
临时解决方案是使用 docker 命令行选项
--network 'host'
。
还有一个替代方案..如果你不想搞乱网络路由..所以在运行容器时尝试Docker支持从主机到容器的绑定安装文件。您可以绑定安装 Docker 控制套接字。
docker run -v /var/run/docker.sock:/var/run/docker.sock ...usual code here...
希望对你有帮助!
这让我很为难,所以我想分享我的解决方案,你必须在容器规则中将
network mode
定义为主机。
即如果您使用的是ecs,则在任务定义中,您可以为
network mode
选择awsvpc、bridge等。选择主持人。这将允许您的容器使用主机的网络堆栈!