ubuntu 16.04 下安裝kubernetes 1.6 之flannel網絡模型
1 準備環境
確保在集群環境中安裝了etcd,在各node中安裝了docker
2 安裝flannel
2.1 flannel節點IP地址段分配
flannel依賴etcd分配不沖突的集群IP,首先在etcd上設置flannel節點使用的IP地址段,本文的etcd節點的ip為192.168.100.20
etcdctl --endpoints "http://192.168.100.20:2379" set /coreos.com/network/config '{"NetWork":"172.17.0.0/16","SubnetMin":"172.17.0.0","SubnetMax":"172.17.20.0","Backend":{"Type":"vxlan"}}'
2.2 minion節點安裝flannel
下載flannel軟件包
wget https://github.com/coreos/flannel/releases/download/v0.9.0/flannel-v0.9.0-linux-amd64.tar.gz
解壓并將解壓出的flanneld,mk-docker-opts.sh復制到/usr/local/bin下
cp flanneld,mk-docker-opts.sh /usr/local/bin
2.3 創建flannel的systemd服務
cat /lib/systemd/system/flanneld.service
[Unit]
Description=Flanneld
Documentation=https://github.com/coreos/flannel
After=network.target
Before=docker.service
[Service]
ExecStart=/usr/local/bin/flanneld --etcd-endpoints=http://192.168.100.20:2379 --iface=enp0s3 --ip-masq=true
Restart=on-failure
Type=notify
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
啟動flannel服務
systemctl start flanneld.service
flannel服務啟動時主要做了以下幾步的工作:
- 從etcd中獲取network的配置信息。
- 劃分subnet,并在etcd中進行注冊。
- 將子網信息記錄到/run/flannel/subnet.env中
cat /run/flannel/subnet.env
FLANNEL_NETWORK=172.17.0.0/16
FLANNEL_SUBNET=172.17.2.1/24
FLANNEL_MTU=1450
FLANNEL_IPMASQ=true
3 配置docker啟動參數
使用flannel提供的腳本將subnet.env轉寫成Docker啟動參數,創建好的啟動參數位于/run/docker_opts.env文件中
root@wangpei-VM:~# sh /usr/local/bin/mk-docker-opts.sh -d /run/docker_opts.env -c
root@wangpei-VM:~# cat /run/docker_opts.env
DOCKER_OPTS=" --bip=172.17.2.1/24 --ip-masq=false --mtu=1450"
將這條語句寫入 /etc/default/docker,作為docker啟動的環境變量
cat /etc/default/docker |grep DOCKER_OPTS
DOCKER_OPTS=" --bip=172.17.2.1/24 --ip-masq=false --mtu=1450"
docker中的啟動參數
root@192:~# awk '!/#/{if($0!="")print}' /lib/systemd/system/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network.target docker.socket firewalld.service flanneld.service
Requires=docker.socket
[Service]
Type=notify
EnvironmentFile=-/etc/default/docker
ExecStartPre=/usr/local/bin/mk-docker-opts.sh -d /run/docker_opts.env -c
EnvironmentFile=-/run/docker_opts.env
ExecStart=/usr/bin/dockerd -H fd:// $DOCKER_OPTS
ExecReload=/bin/kill -s HUP $MAINPID
LimitNOFILE=1048576
LimitNPROC=infinity
LimitCORE=infinity
TasksMax=infinity
TimeoutStartSec=0
Delegate=yes
KillMode=process
[Install]
WantedBy=multi-user.target
加載配置,啟動docker
root@wangpei-VM:~# systemctl daemon-reload
root@wangpei-VM:~# systemctl restart docker