• CentOS安装etcd和flannel实现Docker跨物理机通信


    1、安装etcd

    yum install etcd
    
    systemctl stop etcd
    systemctl start etcd
    systemctl status etcd
    systemctl enable etcd ##开机自启动

    查看etcd是否启动成功,etcd默认端口为2379
    $ lsof -i:2379

    测试etcd可用性
    $ etcd --version
    $ etcdctl mkdir /test
    $ etcdctl ls /

    2、安装flannel
    $ wget https://github.com/coreos/flannel/releases/download/v0.5.5/flannel-0.5.5-linux-amd64.tar.gz
    $ tar xvf flannel-0.5.5-linux-amd64.tar.gz
    $ cd flannel-0.5.5
    $ mv flanneld /usr/bin
    $ mv mk-docker-opts.sh /usr/bin

    启动flannel方法一:
    $ flanneld &

    查看flannel是否启动
    $ ps -ef|grep flannel

    启动flannel方法二(推荐):添加到systemd服务中
    a、创建配置/etc/flannel/flanneld.conf
    $ mkdir -p /etc/flannel/
    $ vim /etc/flannel/flanneld.conf

    # Flanneld configuration options
    
    # etcd url location. Point this to the server where etcd runs
    FLANNEL_ETCD_ENDPOINTS="-etcd-endpoints=http://localhost:2379"
    # etcd集群配置
    FLANNEL_ETCD_ENDPOINTS="-etcd-endpoints=http://172.16.0.6:2379,http://172.16.0.7:2379" # etcd config key. This is the configuration key that flannel queries # For address range assignment FLANNEL_ETCD_PREFIX="-etcd-prefix=/coreos.com/network" # Any additional options that you want to pass #FLANNEL_OPTIONS=""

     

    创建flanneld服务文件/usr/lib/systemd/system/flanneld.service

    $ vim /usr/lib/systemd/system/flanneld.service
    [Unit]
    Description=Flanneld overlay address etcd agent
    After=network.target
    After=network-online.target
    Wants=network-online.target
    After=etcd.service
    Before=docker.service
    
    [Service]
    Type=notify
    EnvironmentFile=-/etc/flannel/flanneld.conf
    ExecStart=/usr/bin/flanneld $FLANNEL_ETCD_ENDPOINTS $FLANNEL_ETCD_PREFIX $FLANNEL_OPTIONS
    Restart=on-failure
    
    [Install]
    WantedBy=multi-user.target
    RequiredBy=docker.service

     

    启动flanneld
    $ systemctl daemon-reload
    $ systemctl start flanneld
    $ systemctl restart flanneld

    $ systemctl enable flanneld ##开机自启动

    查看启动日志:
    $ systemctl status flanneld -l
    发现报错了:Couldn't fetch network config: 100: Key not found

    想起来etcd里还没写入网络配置信息呢;

    flanneld网络配置
    docker默认用172.17.0.0/16这个网段,我想换个网段,以明确知道是在使用flanneld提供的网络,于是就用了172.10.0.0/16。

    $ etcdctl mk /coreos.com/network/config '{"Network":"172.10.0.0/16", "SubnetLen": 24, "SubnetMin": "172.10.1.0", "SubnetMax": "172.10.254.0", "Backend": {"Type": "vxlan"}}'
    $ etcdctl get /coreos.com/network/config
    {"Network":"172.10.0.0/16", "SubnetLen": 24, "SubnetMin": "172.10.1.0", "SubnetMax": "172.10.254.0", "Backend": {"Type": "vxlan"}}

    因为flannel一直在尝试从etcd读配置信息,所以不需要重启flanneld,再看启动日志,flanneld已经获取到想要的子网地址了;

    此时,在/run/flannel/subnet.env位置会生成这个host所分配到的子网信息:

    FLANNEL_NETWORK=172.10.0.0/16
    FLANNEL_SUBNET=172.10.60.1/24
    FLANNEL_MTU=1450
    FLANNEL_IPMASQ=false

     

    正常情况下,执行ifconfig或ip a可以看到flannel0桥接网卡了,etcd上执行etcdctl ls /coreos.com/network/subnets也能够看到flannel申请的网段。

    而这个文件正是mk-docker-opts.sh脚本的输入文件:
    $ /usr/bin/mk-docker-opts.sh -c
    $ cat /run/docker_opts.env
    DOCKER_OPTS=" --bip=172.10.60.1/24 --ip-masq=true --mtu=1450 "

    这时看一下etcd,多了一条信息:
    $ etcdctl get /coreos.com/network/subnets/172.10.60.0-24
    {"PublicIP":"172.16.92.255","BackendType":"vxlan","BackendData":{"VtepMAC":"1e:36:0a:72:6b:c7"}}

    看一下主机网卡
    $ ip a |grep flannel
    2036: flannel.1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UNKNOWN group default
    inet 172.10.60.0/16 scope global flannel.1

    看一下网卡详情
    $ ip -d link show dev flannel.1
    2036: flannel.1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UNKNOWN mode DEFAULT group default
    link/ether 1e:36:0a:72:6b:c7 brd ff:ff:ff:ff:ff:ff promiscuity 0
    vxlan id 1 local 172.16.92.255 dev eth0 srcport 0 0 dstport 8472 nolearning ageing 300 noudpcsum noudp6zerocsumtx noudp6zerocsumrx addrgenmode eui64 numtxqueues 1 numrxqueues 1 gso_max_size 65536 gso_max_segs 65535

    3、Docker使用flannel网络

    再重新生成一次docker_opts.env:

    # /usr/bin/mk-docker-opts.sh -c
    # cat /run/docker_opts.env 
    DOCKER_OPTS=" --bip=172.10.60.1/24 --ip-masq=true --mtu=1450 "

     

    修改vim /lib/systemd/system/docker.service:

    EnvironmentFile=/run/docker_opts.env # 在ExecStart参数之前新增这句话
    ExecStart=/usr/bin/dockerd $DOCKER_OPTS #在ExecStart末尾添加$DOCKER_OPTS,$DOCKER_OPTS添加网桥的参数


    重启docker服务:
    # systemctl daemon-reload
    # systemctl restart docker

    查看bridge网络的信息:
    # docker network ls |grep bridge
    8d4e33066eb0 bridge bridge local
    db61b72ccaa7 metabase_default bridge local

    # docker network inspect 8d4e33066eb0 | grep Subnet
    "Subnet": "172.18.0.0/16"
    这就已经是在flannel提供的网段内了;

    测试
    然后在另一台主机上也照做一遍,分别启动一个container,互ping一下,测试通过。

     

    docker run -itd --name mycentos  docker.io/centos  /bin/bash

     

    4、flannel生成的vxlan设备的删除方法

    停止flanneld服务并确认
    # systemctl stop flanneld
    # status flanneld

    使用ifconfig将设备停止
    # ip addr s flannel.1
    # ifconfig flannel.1 down

    使用ip link del删除vxlan设备
    # ip link del flannel.1
    # ip addr s flannel.1

    ==========================================================================
    温馨提示:
    如上面操作后,发现各容器内分配的ip之间相互ping不通,基本就是由于防火墙问题引起的!
    可是明明已经在前面部署的时候,通过"systemctl stop firewalld.service"关闭了防火墙,为什么还有防火墙问题??
    这是因为linux还有底层的iptables,所以解决办法是在各节点上执行下面操作:

    $ systemctl stop firewalld.service ##只关闭一次防火墙
    $ systemctl disable firewalld.service ##永久关闭一次防火墙
    [root@node-1 ~]# iptables -P INPUT ACCEPT [root@node-1 ~]# iptables -P FORWARD ACCEPT [root@node-1 ~]# iptables -F

    执行上面操作后,基本各容器间就能相互ping通了。

    docker通过Flannel可以实现各容器间的相互通信,即宿主机和容器,容器和容器之间都能相互通信。

    如果服务器重启,这时docker是基于flannel网络的,docker自启动会失败,此时我们可以把etcd、flannel设置自启动,然后执行如下命令进行docker重启:

    /usr/bin/mk-docker-opts.sh -c
    systemctl daemon-reload
    systemctl restart docker
  • 相关阅读:
    poj 1265 Area(Pick定理)
    poj 2954 Triangle(Pick定理)
    poj 1654 Area(多边形面积)
    bzoj 1069 [SCOI2007]最大土地面积(旋转卡壳)
    227 用栈模拟汉诺塔问题
    213 字符串压缩
    212 空格替换
    211 字符串置换
    209 First Unique Character in a String
    197 排列序号
  • 原文地址:https://www.cnblogs.com/linjiqin/p/11549329.html
Copyright © 2020-2023  润新知