Flannel for Docker Overlay Network

Kubernetes使用etcd/flannel构建虚拟网络, 实现主机/容器跨机访问.
参考这篇文章, 使用2台树莓派做实验, 为后续搭建完整k8s集群作准备.

$ curl https://get.docker.com/ | sh
$ apt-get install bridge-utils
$ systemctl stop docker
$ ifconfig docker0 down
$ brctl delbr docker0
$ systemctl start etcd flannel docker
$ ifconfig

其中: etcd仅在master运行.

  • master:
    • eth0: 192.168.31.231/24
    • docker0: 10.1.55.1/24
  • worker:
    • eth0: 192.168.31.156/24
    • docker0: 10.1.74.1/24

etcd.service (master)

[Unit]
Description=etcd key-value store  
Documentation=https://github.com/coreos/etcd  
After=network.target

[Service]
Type=notify  
Environment=ETCD_UNSUPPORTED_ARCH=arm  
ExecStart=/usr/bin/etcd \  
    --data-dir=/var/lib/etcd \
    --advertise-client-urls=http://192.168.31.231:2379 \
    --listen-client-urls=http://0.0.0.0:2379 \
    --listen-peer-urls=http://0.0.0.0:2380 \
    --name=etcd
Restart=always  
RestartSec=10s  
LimitNOFILE=40000

[Install]
WantedBy=multi-user.target  

flannel.service (master)

[Unit]
Description=Flannel Overlay Network for Kubernetes  
After=network.target etcd.service  
Before=docker.service  
Requires=etcd.service

[Service]
ExecStartPre=/usr/bin/etcdctl set /coreos.com/network/config '{"Network": "10.1.0.0/16"}'  
ExecStart=/usr/bin/flanneld \  
     --etcd-endpoints=http://192.168.31.231:2379 \
     --subnet-file=/var/lib/flannel/subnet.env
Restart=on-failure  
RestartSec=5

[Install]
WantedBy=multi-user.target  

flannel.service (worker)

[Unit]
Description=Flannel Overlay Network for Kubernetes  
After=network.target

[Service]
ExecStart=/usr/bin/flanneld \  
     --etcd-endpoints=http://192.168.31.231:2379 \
     --subnet-file=/var/lib/flannel/subnet.env
Restart=on-failure  
RestartSec=5

[Install]
WantedBy=multi-user.target  

docker.service.d/override.conf

[Unit]
After=network.target docker.socket flannel.service  
Requires=docker.socket flannel.service

[Service]
EnvironmentFile=/var/lib/flannel/subnet.env  
ExecStartPre=-/sbin/ifconfig docker0 down  
ExecStartPre=-/sbin/brctl delbr docker0  
ExecStart=  
ExecStart=/usr/bin/docker daemon \  
    --bip=${FLANNEL_SUBNET} \
    --mtu=${FLANNEL_MTU} \
    -H tcp://0.0.0.0:2375 \
    -H unix:///var/run/docker.sock \
    --storage-driver=overlay \
    --exec-opt native.cgroupdriver=cgroupfs