配置项说明

etcd配置项说明

flannel从etcd读取配置项。默认从/coreos.com/network/config读取配置项(可以通过--etcd-prefix重写)。可以使用etcdctl设置etcd中的值。JSON字典中,包含以下关键字:

  • Network(字符串):整个flannel网络所使用的CIDR的IPv4网络。唯一一个必要的关键字。
  • SubnetLen(整形):分配子网的长度,默认是24(例如/24)。
  • SubnetMin(字符串):子网分配IP的起始地址,默认是子网的第一个地址。
  • SubnetMax(字符串):子网分配IP的末端地址,默认是子网的最后一个地址。
  • Backend(字典):后台所使用的类型和特定配置。可位于该字典中的关键字如下,默认是“UDP”:

Backends

  • udp:使用udp封装数据包:
    • type(字符串):udp
    • Port(数字):发送被封装数据包的UDP端口号,默认端口号为8285。
  • vxlan:使用内核VXLAN封装数据包:
    • type(字符串):vxlan
    • VNI(数字):VXLAN所使用的标识符(VNI),默认是1
    • Port(数字):发送被封装数据包的UDP端口号,默认是内核的默认值,通常是8472。
  • host-gw:通过远程主机IPs创建子网的IP路由。注意,这需要运行flannel主机层2的直接连通。
    • type(字符串):host-gw
  • aws-vpc:使用亚马逊的VPC路由表创建IP路由。
    • 要求:
      • 运行在使用亚马逊VPC的EC2实例上
      • 权限要求:CreateRoute, DeleteRoute,DescribeRouteTables, ModifyInstanceAttribute, DescribeInstances 【可选】
    • Type(字符串):aws-vpc
    • RouteTableID(字符串):【可选】VPC路由表所添加路由的ID。路由表必须位于运行flannel的EC2实例的相同区域。如果赋予了EC2实例DescribeInstances权限,flannel可以自动发现Id。

身份认证通过环境变量或节点的IAM角色处理。如果节点没有足够的权限去修改VPC路由表,应保证在运行flanneld时,环境变量AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, 和可选的AWS_SECURITY_TOKEN设置正确。

注意:目前,AWS限制路由表的记录数为50。

  • gce:使用Google处理引擎网络创建IP路由
    • 要求:
      • 为实例启用网络转发
      • 服务实例账户应有读写的处理权限。
    • Type(字符串):gce

创建一个有正确权限和启用IP转发功能的处理实例的命令如下:

$ gcloud compute instances create INSTANCE --can-ip-forward --scopes compute-rw

注意:目前,GCE为每个工程限制了路由数量为100。

  • alloc:只分配子网(不转发数据包)
    • Type(字符串):alloc

JSON配置举例

下面的配置列举了使用udp后台的大多数选项:

{
    "Network": "10.0.0.0/8",
    "SubnetLen": 20,
    "SubnetMin": "10.10.0.0",
    "SubnetMax": "10.99.0.0",
    "Backend": {
        "Type": "udp",
        "Port": 7890
    }
}

防火墙

当使用udp后台时,flannel使用UDP端口8285发送封装的数据包。使用vxlan后台,内核使用UDP端口8472发送封装的数据包。确保网络中的所有主机的防火墙规则允许这些端口通信。

C/S模式(客户端/服务器模式)

flannel运行默认是没有中心控制器的,使用etcd进行配置。然而,可将flannel配置为C/S模式,指定一个特定的flannel进程实例(服务器)与etcd通信,该flannel实例是唯一一个与etcd进行通信的。只有服务器与etcd进行通信,其他的flannel(客户端)通过服务器与etcd进行通信。

以服务器模式运行flannel,只要提供--listen标签:

$ flanneld --listen=0.0.0.0:8888

以客户端模式运行flannel,使用--remote指定flannel服务器的实例:

$ flanneld --remote=10.0.0.3:8888

注意:服务器本身是不加入flannel网络的(比如,不会为服务器分配子网)。因此,若果一台主机运行了flannel服务器,同时也要加入flannel网络,应该在这台主机上运行两个flannel实例——一个是服务器模式,另一个是客户端模式。

多网络模式

多网络模式允许一个flannel进程加入多个网络。每个网络之间相互独立,都有自己的配置、IP空间、接口。举例,配置三个网络,名称分别为:bluegreered。首先将他们的配置发布至etcd的三个不同位置:

$ etcdctl set /coreos.com/network/blue/config  '{ "Network": "10.1.0.0/16", "Backend": { "Type": "vxlan", "VNI": 1 } }'
$ etcdctl set /coreos.com/network/green/config '{ "Network": "10.2.0.0/16", "Backend": { "Type": "vxlan", "VNI": 2 } }'
$ etcdctl set /coreos.com/network/red/config   '{ "Network": "10.3.0.0/16", "Backend": { "Type": "vxlan", "VNI": 3 } }'

下一步,开启flannel进程,指定要加入的网络:

$ flanneld --networks=blue,green,red

flannel参数不是写入一个单独的/run/flannel/subnet.env文件。在目录/run/flannel/networks下,将为每一个网络创建一个.env文件:

$ ls /run/flannel/networks/
blue.env  green.env  red.env

注意:多网络模式可以和C/S模式混合使用。--networks标签只在客户端使用:

# Server daemon
$ flanneld --listen=0.0.0.0:8888

# Client daemon
$ flanneld --remote=10.0.0.3:8888 --networks=blue,green

关键字命令行选项

--public-ip="": IP accessible by other nodes for inter-host communication. Defaults to the IP of the interface being used for communication.
--etcd-endpoints=http://127.0.0.1:4001: a comma-delimited list of etcd endpoints.
--etcd-prefix=/coreos.com/network: etcd prefix.
--etcd-keyfile="": SSL key file used to secure etcd communication.
--etcd-certfile="": SSL certification file used to secure etcd communication.
--etcd-cafile="": SSL Certificate Authority file used to secure etcd communication.
--iface="": interface to use (IP or name) for inter-host communication. Defaults to the interface for the default route on the machine.
--subnet-file=/run/flannel/subnet.env: filename where env variables (subnet and MTU values) will be written to.
--ip-masq=false: setup IP masquerade for traffic destined for outside the flannel network.
--listen="": if specified, will run in server mode. Value is IP and port (e.g. `0.0.0.0:8888`) to listen on or `fd://` for [socket activation](http://www.freedesktop.org/software/systemd/man/systemd.socket.html).
--remote="": if specified, will run in client mode. Value is IP and port of the server.
--remote-keyfile="": SSL key file used to secure client/server communication.
--remote-certfile="": SSL certification file used to secure client/server communication.
--remote-cafile="": SSL Certificate Authority file used to secure client/server communication.
--networks="": if specified, will run in multi-network mode. Value is comma separate list of networks to join.
-v=0: log level for V logs. Set to 1 to see messages related to data path.
--version: print version and exit

环境变量

上面列出的命令行选项,也可以通过环境变量指定。例如,--etcd-endpoints=http://10.0.0.2:2379,等效于环境变量FLANNELD_ETCD_ENDPOINTS=http://10.0.0.2:2379。任何一个命令行选项都可以转变为环境变量,转变方式:加上前缀FLANNELD_、转化为大写并将“-”换成“_”。

Docker集成

Docker daemon接收--bip参数配置docker0网桥的子网。通过--mtu,设置docker0和将创建的veth设备的MTU。 flannel将子网和MTU信息写入文件中,启动Docker时,首先source这些值,即可将他们传入Docker daemon中:

source /run/flannel/subnet.env
docker -d --bip=${FLANNEL_SUBNET} --mtu=${FLANNEL_MTU}