Flannel主要的作用就是将不同节点上的docker容器互相打通。
主要功能:
1.它能协助kubernetes,给每一个node上的docker容器分配互相不冲突的ip地址
2.它能在这些ip地址之间建立一个覆盖网络(overlay network),通过这个覆盖网络,将数据包原封不动的传递到目标容器
Flannel 二进制安装
1、下载fannel组件
wget https://github.com/coreos/flannel/releases/dow
2、安装Flannel 网络组件
tar xf flannel-v0.10.0-linux-amd64.tar.gz cp flanneld /usr/bin/ cp mk-docker-opts.sh /usr/bin/
nload/v0.10.0/flannel-v0.10.0-linux-amd64.tar.gz
3、由于是二进制的文件,将flanneld拷贝到有系统环境变量的可执行路径就完成了安装。下面讲解具体的配置过程和原理。
Kubernetes Cluster中的几个“网络”
node network自不必多说,node间通过你的本地局域网(无论是物理的还是虚拟的)通信。
service network比较特殊,每个新创建的service会被分配一个service IP,在当前集群中,这个IP的分配范围是192.168.3.0/24。不过这个IP并不“真实”,更像一个“占位符”并且只有入口流量,所谓的“network”也是“名不符实”的,后续我们会详尽说明。
flannel network是我们要理解的重点,cluster中各个Pod要实现相互通信,必须走这个网络,无论是在同一node上的Pod还是跨node的Pod。我们的cluster中,flannel net的分配范围是:172.16.0.0/16。
remove-docker0.sh脚本内容如下:
# cat /usr/bin/remove-docker0.sh
#!/usr/bin/env bash
set -e
rc=0
ip link show docker0 >/dev/null 2>&1 || rc="$?"
if [[ "$rc" -eq "0" ]]; then
ip link set dev docker0 down
ip link delete docker0
fi
1、这里编写flanneld的systemd文件(需要在所有节点安装包括master):
# cat /usr/lib/systemd/system/flanneld.service
[Unit]
Description=Flanneld overlay address etcd agent
After=network.target
Before=docker.service
[Service]
#EnvironmentFile=-/run/flannel/docker
EnvironmentFile=/etc/kubernetes/flanneld
ExecStartPre=/usr/bin/remove-docker0.sh
ExecStart=/usr/bin/flanneld ${FLANNEL_ETCD} $FLANNEL_OPTIONS
ExecStartPost=/usr/bin/mk-docker-opts.sh -d /run/flannel/docker
Type=notify
[Install]
WantedBy=multi-user.target
RequiredBy=docker.service
对上面的文件做一下解释:
2、配置fannel参数文件:
# cat /etc/kubernetes/flanneld
FLANNEL_ETCD="-etcd-endpoints=http://10.1.31.205:2379"
FLANNEL_ETCD_KEY="/coreos.com/network"
# etcdctl set /coreos.com/network/config '{ "Network": "10.1.0.0/16" }'
4、启动fannel:
systemctl daemon-reload systemctl start flanneld
配置docker 服务:
# vim /usr/lib/systemd/system/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service flanneld.service
Wants=network-online.target
Requires=flanneld.service
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd --registry-mirror=https://qxx96o44.mirror.aliyuncs.com --bip=10.5.100.1/24
ExecReload=/bin/kill -s HUP $MAINPID
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
# restart the docker process if it exits prematurely
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
EnvironmentFile=-/run/flannel/docker
[Install]
WantedBy=multi-user.target
修改配置说明:
之后需要先重启flanneld后再重启docker
后续创建好两个pod测试下
如下
[root@docker1 ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE
mysql-zth7l 1/1 Running 3 104m 10.5.100.5 docker2
myweb-9rz7r 1/1 Running 0 44m 10.5.92.2 docker3
myweb-klvvw 1/1 Running 0 44m 10.5.100.4 docker2
static-web-docker2 1/1 Running 13 3h14m 10.5.100.2 docker2
[root@docker1 ~]# kubectl exec -it myweb-9rz7r bash
root@myweb-9rz7r:/usr/local/tomcat# ping 10.5.100.5
PING 10.5.100.5 (10.5.100.5): 56 data bytes
64 bytes from 10.5.100.5: icmp_seq=0 ttl=60 time=1.092 ms
^C--- 10.5.100.5 ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max/stddev = 1.092/1.092/1.092/0.000 ms
root@myweb-9rz7r:/usr/local/tomcat#
ps:也可以在etcd中查看flannel设置的flannel地址与物理机ip地址的对应规则
# etcdctl ls /coreos.com/network/subnets
/coreos.com/network/subnets/10.5.92.0-24
/coreos.com/network/subnets/10.5.100.0-24
/coreos.com/network/subnets/10.5.101.0-24
[root@docker1 ~]# etcdctl get /coreos.com/network/subnets/10.5.92.0-24
{"PublicIP":"10.1.1.207"}
[root@docker1 ~]# etcdctl get /coreos.com/network/subnets/10.5.100.0-24
{"PublicIP":"10.1.1.206"}
[root@docker1 ~]# etcdctl get /coreos.com/network/subnets/10.5.101.0-24
{"PublicIP":"10.1.31.205"}