使用flannel配置docker网络
这篇文章主要总结一下如何在使用flannel进行docker的配置,介绍docker尚未安装或者是已经安装的情况下,如何与flannel进行连接,并以一个具体的例子介绍一下如何在docker和flannel分别安装的情况下进行设定。
主要思路
主要需要确认的是docker0是否在flannel的设定下统一进行容器IP的管理和分配。
全新docker安装
flannel在安装时,一般会在systemd的service文件的ExecStartPost中设定如下执行内容:
mk-docker-opts.sh -k DOCKER_NETWORK_OPTIONS -d /run/flannel/docker
这样则会生成/run/flannel/docker文件, 在这个文件中一般会给docker提供一个设定参数DOCKER_NETWORK_OPTIONS,DOCKER_NETWORK_OPTIONS中会包含如下设定信息:
- --bip
- --ip-masq
- --mtu
而在docker的systemd的service中,只需要将DOCKER_NETWORK_OPTIONS传递给dockerd即可。
已有安装
事前准备
docker详细信息如下所示(centos下的yum install docker的版本1.13)
[root@host131 shell]# docker version Client: Version: 1.13.1 API version: 1.26 Package version: docker-1.13.1-91.git07f3374.el7.centos.x86_64 Go version: go1.10.3 Git commit: 07f3374/1.13.1 Built: Wed Feb 13 17:10:12 2019 OS/Arch: linux/amd64 Server: Version: 1.13.1 API version: 1.26 (minimum version 1.12) Package version: docker-1.13.1-91.git07f3374.el7.centos.x86_64 Go version: go1.10.3 Git commit: 07f3374/1.13.1 Built: Wed Feb 13 17:10:12 2019 OS/Arch: linux/amd64 Experimental: false [root@host131 shell]#
flannel的版本为v0.10.0
[root@host131 shell]# flanneld --version v0.10.0 [root@host131 shell]#
flannel 和docker分别安装,可以看到flannel.1和docker0所在网段不同
[root@host131 shell]# ip addr show flannel.1 5: flannel.1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UNKNOWN group default link/ether 22:22:02:92:1e:74 brd ff:ff:ff:ff:ff:ff inet 172.200.240.0/32 scope global flannel.1 valid_lft forever preferred_lft forever inet6 fe80::2022:2ff:fe92:1e74/64 scope link valid_lft forever preferred_lft forever [root@host131 shell]# [root@host131 shell]# ip addr show docker0 6: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default link/ether 02:42:5f:2b:90:6b brd ff:ff:ff:ff:ff:ff inet 172.17.0.1/16 scope global docker0 valid_lft forever preferred_lft forever [root@host131 shell]#
确定设定文件
确认如下文件是否存在
[root@host131 shell]# cat /run/flannel/docker DOCKER_OPT_BIP="--bip=172.200.240.1/21" DOCKER_OPT_IPMASQ="--ip-masq=false" DOCKER_OPT_MTU="--mtu=1450" DOCKER_NETWORK_OPTIONS=" --bip=172.200.240.1/21 --ip-masq=false --mtu=1450" [root@host131 shell]# cat /run/flannel/subnet.env FLANNEL_NETWORK=172.200.0.0/16 FLANNEL_SUBNET=172.200.240.1/21 FLANNEL_MTU=1450 FLANNEL_IPMASQ=true [root@host131 shell]#
如果不存在的情况下,考虑如下操作:
- 重启flanneld:systemctl restart flanneld
- 执行命令:mk-docker-opts.sh -k DOCKER_NETWORK_OPTIONS -d /run/flannel/docker
设定docker0
使用subnet.env的内容进行docker0设定
[root@host131 shell]# source /run/flannel/subnet.env [root@host131 shell]# echo ${FLANNEL_SUBNET} 172.200.240.1/21 [root@host131 shell]# ifconfig docker0 ${FLANNEL_SUBNET} [root@host131 shell]#
重启docker
重启docker后即可
[root@host131 shell]# ip ad s docker0 7: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default link/ether 02:42:6f:f5:6e:a2 brd ff:ff:ff:ff:ff:ff inet 172.200.240.1/21 scope global docker0 valid_lft forever preferred_lft forever [root@host131 shell]#
启动后恢复缺省的ip的对应方法
经常会碰到的问题是ifconfig 命令之后修改成功,但是重启之后又恢复原装,可以考虑如下方式进行解决。
删除docker0设备并重启
执行示例如下所示
[root@host131 shell]# systemctl stop docker [root@host131 shell]# ip link set dev docker0 down [root@host131 shell]# brctl delbr docker0 [root@host131 shell]# ip a s docker0 Device "docker0" does not exist. [root@host131 shell]# systemctl restart docker [root@host131 shell]#
修改docker的service文件并重启docker
centos下缺省安装的docker,由于环境变量文件中将DOCKER_NETWORK_OPTIONS进行了设定,即使你在外部进行了source操作,docker在重启的时候仍然会将你读入的正确设定设成缺省的"",所以会恢复原装。
[root@host131 shell]# cat /usr/lib/systemd/system/docker.service [Unit] Description=Docker Application Container Engine Documentation=http://docs.docker.com After=network.target Wants=docker-storage-setup.service Requires=docker-cleanup.timer [Service] Type=notify NotifyAccess=main EnvironmentFile=-/run/containers/registries.conf EnvironmentFile=-/etc/sysconfig/docker EnvironmentFile=-/etc/sysconfig/docker-storage EnvironmentFile=-/etc/sysconfig/docker-network ...省略 [root@host131 shell]#
- /etc/sysconfig/docker-network
[root@host131 shell]# cat /etc/sysconfig/docker-network # /etc/sysconfig/docker-network DOCKER_NETWORK_OPTIONS= [root@host131 shell]#
- 修改方法
按照本文提示的第一种方式,设定此参数,示例如下
EnvironmentFile=-/etc/sysconfig/docker-network EnvironmentFile=-/run/flannel/docker
- 重启并确认
[root@host131 shell]# systemctl daemon-reload [root@host131 shell]# systemctl restart docker [root@host131 shell]# ip addr show docker0 7: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default link/ether 02:42:6f:f5:6e:a2 brd ff:ff:ff:ff:ff:ff inet 172.200.240.1/21 scope global docker0 valid_lft forever preferred_lft forever [root@host131 shell]#
- Docker 使用指南 (三)—— 网络配置
- Docker 配置网络 - 使用 Macvlan 网络
- 使用Calico配置Docker网络
- Docker 使用Calico插件配置网络
- Docker 配置网络 - 使用 bridge 网络
- Docker:使用pipework配置docker网络
- Docker 配置网络 - 使用 bridge 网络
- Docker 配置守护进程和容器的网络 - 配置 Docker 使用代理服务器
- Docker 配置网络 - 使用 overlay 网络
- docker深入2-使用自定义的网络来配置zookeeper集群
- Docker 使用指南 (三)—— 网络配置
- Docker 配置Flannel网络过程及原理
- Docker 配置网络 - 使用 overlay 网络
- 14. 使用脚本创建Docker及配置网络
- Docker 配置网络 - 使用 host 网络
- Docker 配置网络使用bridge网络的方法
- Docker 使用指南 (三)—— 网络配置
- 02docker简单使用和配置(网络、存储和Hub)
- Docker 使用指南 (三)—— 网络配置
- ORACLE-PLSQL Developer远程访问:使用Netca 配置网络服务