我有一个关于在Docker容器中运行的应用程序和UUID生成的问题。
下面是我们的场景:
在我们的分析中,这个场景似乎归结为每个Docker容器上mac地址的独特性。
所以坦率地说:
发布于 2014-10-27 09:52:01
从我阅读的函数 (编辑:reading 1.3.0-dev
__,但对于https://github.com/moby/moby/blob/17.05.x/vendor/github.com/docker/libnetwork/netutils/utils.go#L97-L125仍然正确)来看,docker
生成的MAC地址实质上是docker0
桥上容器接口的IPv4
地址:它们保证与IP地址一致。
您必须操作的docker0
桥子网(通常是按照这个例子 of 172.17.42.1/16
操作的255.255.0.0
)有65,534个可路由地址。这确实降低了UUID生成的熵,但MAC地址冲突是不可能的,因为in必须是唯一的,在同一个码头服务器/CoreOS主机上的两个容器中出现相同的MAC、PID、时间和计数器的情况不应该是可能的。
但是,两个CoreOS主机(每个运行一个docker
服务器)可能会选择相同的随机子网,从而可能在不同主机上为容器复制MACs。您可以通过在每个主机上为固定CIDR服务器设置一个docker
来避免这种情况:
--fixed-cidr=CIDR
-使用标准的CIDR符号(如172.167.1.0/28
)限制来自docker0子网的IP范围。此范围必须为固定IP (例如: IPv4 )和10.20.0.0/16
范围,并且必须是桥IP范围(docker0
或使用--bridge
设置)的子集。例如,对于--fixed-cidr=192.168.1.0/25
,您的容器的IP将从192.168.1.0/24
子网的前半部分中选择。
这将确保整个集群都有唯一的MAC地址。
原有的IEEE 802 MAC地址来源于原来的Xerox以太网寻址方案。这个48位地址空间可能包含248或281,474,976,710,656个可能的MAC地址.
如果您担心缺乏熵( IP到MAC映射大大减少了熵),更好的选择可能是使用不同的机制生成UUID。考虑到UUID版本3、4和5 不要取MAC地址。或者,您可以在UUID生成中包括主机的MAC。
当然,这种“相当大的MAC空间减少”是否会对UUID的生成产生任何影响,应该在更改任何代码之前进行测试。
与上述有关的资料来源:
// Generate a IEEE802 compliant MAC address from the given IP address.
//
// The generator is guaranteed to be consistent: the same IP will always yield the same
// MAC address. This is to avoid ARP cache issues.
func generateMacAddr(ip net.IP) net.HardwareAddr {
hw := make(net.HardwareAddr, 6)
// The first byte of the MAC address has to comply with these rules:
// 1. Unicast: Set the least-significant bit to 0.
// 2. Address is locally administered: Set the second-least-significant bit (U/L) to 1.
// 3. As "small" as possible: The veth address has to be "smaller" than the bridge address.
hw[0] = 0x02
// The first 24 bits of the MAC represent the Organizationally Unique Identifier (OUI).
// Since this address is locally administered, we can do whatever we want as long as
// it doesn't conflict with other addresses.
hw[1] = 0x42
// Insert the IP address into the last 32 bits of the MAC address.
// This is a simple way to guarantee the address will be consistent and unique.
copy(hw[2:], ip.To4())
return hw
}
https://stackoverflow.com/questions/26385259
复制相似问题