二进制安装k8s集群-部署flannel(十一)

364次阅读
没有评论

共计 8199 个字符,预计需要花费 21 分钟才能阅读完成。

Flannel 是由 go 语言开发,是一种基于 Overlay 网络的跨主机容器网络解决方案,也就是将 TCP 数据包封装在另一种网络包里面进行路由转发和通信,Flannel 是 CoreOS 开发,专门用于 docker 多主机互联的一个工具,简单来说,它的功能是让集群中的不同节点主机创建的容器都具有全局唯一的虚拟 IP 地址。

在默认的 Docker 配置中,每个节点上的 Docker 服务会分别独立负责自己所在节点容器的 IP 地址分配,这样就导致一个问题,不同节点上容器获得的 IP 地址可能一样,容器之间无法直接通信,需要借助 nat 桥接的方式来进行通信。

安装过程比较简单,下载解压,建立软连接!

[root@hdss7-21 ~]# cd /data/soft/  && mkdir -p /usr/local/flannel-v0.11.0

[root@hdss7-21 soft]# wget https://github.com/coreos/flannel/releases/download/v0.11.0/flannel-v0.11.0-linux-amd64.tar.gz
[root@hdss7-21 soft]# tar xf flannel-v0.11.0-linux-amd64.tar.gz -C /usr/local/flannel-v0.11.0
[root@hdss7-21 ~]#cd /usr/loacal
[root@hdss7-21 local]# ln -sf flannel-v0.11.0/ flannel
[root@hdss7-21 local]# ll
total 0
lrwxrwxrwx  1 root root  25 Apr 20 14:46 etcd -> etcd-v3.1.20-linux-amd64/
drwxr-xr-x  4 etcd etcd 166 Apr 20 15:50 etcd-v3.1.20-linux-amd64
lrwxrwxrwx  1 root root  16 Apr 26 17:33 flannel -> flannel-v0.11.0/
drwxr-xr-x  2 root root  64 Apr 26 17:33 flannel-v0.11.0
lrwxrwxrwx  1 root root  18 Apr 21 11:30 kubernetes -> kubernetes-v1.15.2
drwxr-xr-x  4 root root  50 Apr 21 11:32 kubernetes-v1.15.2

拷贝证书,需要与 etcd 通信并配置,所以需要拷贝以下证书

[root@hdss7-21 flannel]# mkdir cert
[root@hdss7-21 cert]#scp hdss7-200:/opt/certs/ca.pem .
[root@hdss7-21 cert]#scp hdss7-200:/opt/certs/client.pem .
[root@hdss7-21 cert]#scp hdss7-200:/opt/certs/client-key.pem .

[root@hdss7-21 cert]#cd ../
[root@hdss7-21 flannel]# vim subnet.env
FLANNEL_NETWORK=172.7.0.0/16
FLANNEL_SUBNET=172.7.21.1/24
FLANNEL_MTU=1500
FLANNEL_IPMASQ=false

[root@hdss7-21 flannel]# vim flanneld.sh
#!/bin/sh
./flanneld \
  --public-ip=10.4.7.21 \
  --etcd-endpoints=https://10.4.7.12:2379,https://10.4.7.21:2379,https://10.4.7.22:2379 \
  --etcd-keyfile=./cert/client-key.pem \
  --etcd-certfile=./cert/client.pem \
  --etcd-cafile=./cert/ca.pem \
  --iface=ens33 \
  --subnet-file=./subnet.env \
  --healthz-port=2401

[root@hdss7-21 flannel]# chmod +x flanneld.sh
[root@hdss7-21 flannel]# mkdir -p /data/logs/flanneld


[root@hdss7-21 flannel]# vim /etc/supervisord.d/flannel.ini
[program:flanneld-7-21]
command=/usr/local/flannel/flanneld.sh                       ; the program (relative uses PATH, can take args)
numprocs=1                                                   ; number of processes copies to start (def 1)
directory=/usr/local/flannel                                ; directory to cwd to before exec (def no cwd)
autostart=true                                               ; start at supervisord start (default: true)
autorestart=true                                             ; retstart at unexpected quit (default: true)
startsecs=30                                                 ; number of secs prog must stay running (def. 1)
startretries=3                                               ; max # of serial start failures (default 3)
exitcodes=0,2                                                ; 'expected' exit codes for process (default 0,2)
stopsignal=QUIT                                              ; signal used to kill process (default TERM)
stopwaitsecs=10                                              ; max num secs to wait b4 SIGKILL (default 10)
user=root                                                    ; setuid to this UNIX account to run the program
redirect_stderr=true                                         ; redirect proc stderr to stdout (default false)
stdout_logfile=/data/logs/flanneld/flanneld.stdout.log       ; stderr log path, NONE for none; default AUTO
stdout_logfile_maxbytes=64MB                                 ; max # logfile bytes b4 rotation (default 50MB)
stdout_logfile_backups=4                                     ; # of stdout logfile backups (default 10)
stdout_capture_maxbytes=1MB                                  ; number of bytes in 'capturemode' (default 0)
stdout_events_enabled=false                                  ; emit events on stdout writes (default false)

设置 flannel 的网络插件模式为 host-gw

[root@hdss7-21 etcd]# ./etcdctl set /coreos.com/network/config '{"Network": "172.7.0.0/16", "Backend": {"Type": "host-gw"}}'
{"Network": "172.7.0.0/16", "Backend": {"Type": "host-gw"}}

查看
[root@hdss7-21 etcd]# ./etcdctl get /coreos.com/network/config
{"Network": "172.7.0.0/16", "Backend": {"Type": "host-gw"}}

其他节点基本和 hdss7-21 相同,注意修改一下文件:

[root@hdss7-22 flannel]# vim subnet.env
FLANNEL_NETWORK=172.7.0.0/16
FLANNEL_SUBNET=172.7.22.1/24
FLANNEL_MTU=1500
FLANNEL_IPMASQ=false

flanneld.sh
#!/bin/sh
./flanneld \
  --public-ip=10.4.7.22 \
  --etcd-endpoints=https://10.4.7.12:2379,https://10.4.7.21:2379,https://10.4.7.22:2379 \
  --etcd-keyfile=./cert/client-key.pem \
  --etcd-certfile=./cert/client.pem \
  --etcd-cafile=./cert/ca.pem \
  --iface=ens33 \
  --subnet-file=./subnet.env \
  --healthz-port=2401

chmod +x flanneld.sh

vim /etc/supervisord.d/flannel.ini
[program:flanneld-7-22]
command=/usr/local/flannel/flanneld.sh                       ; the program (relative uses PATH, can take args)
numprocs=1                                                   ; number of processes copies to start (def 1)
directory=/usr/local/flannel                                ; directory to cwd to before exec (def no cwd)
autostart=true                                               ; start at supervisord start (default: true)
autorestart=true                                             ; retstart at unexpected quit (default: true)
startsecs=30                                                 ; number of secs prog must stay running (def. 1)
startretries=3                                               ; max # of serial start failures (default 3)
exitcodes=0,2                                                ; 'expected' exit codes for process (default 0,2)
stopsignal=QUIT                                              ; signal used to kill process (default TERM)
stopwaitsecs=10                                              ; max num secs to wait b4 SIGKILL (default 10)
user=root                                                    ; setuid to this UNIX account to run the program
redirect_stderr=true                                         ; redirect proc stderr to stdout (default false)
stdout_logfile=/data/logs/flanneld/flanneld.stdout.log       ; stderr log path, NONE for none; default AUTO
stdout_logfile_maxbytes=64MB                                 ; max # logfile bytes b4 rotation (default 50MB)
stdout_logfile_backups=4                                     ; # of stdout logfile backups (default 10)
stdout_capture_maxbytes=1MB                                  ; number of bytes in 'capturemode' (default 0)
stdout_events_enabled=false                                  ; emit events on stdout writes (default false)

[root@hdss7-22 flannel]# mkdir -p /data/logs/flanneld/

其他方式(没有实验过 VxLAN 模式)。

使用方法:

1、先停止 flennel.sh --- 通过 supervisor stop flanneld-7-[21.22]
2、删除 host-gw 模型创建的路由
route del -net 172.7.21.0/24 gw 10.4.7.21     hdss7-22 上
route del -net 172.7.22.0/24 gw 10.4.7.22     hdss7-21 上
3、在 etcd 节点修改
./etcdctl get /coreos.com/network/config
./etcdctl rm /coreos.com/network/config
etcd]# ./etcdctl set /coreos.com/network/config '{"Network": "172.7.0.0/16", "Backend": {"Type": "VxLAN"}}'
4、supervisorctl start flanneld-7-21
    supervisorctl start flanneld-7-22
5、查看 ifconfig 会多了一个 flannel 1 的设备,route - n 是没有路由的


2.1.17.flannel 直接路由模型(智能判定)类似与 mysql 日志的 mixed 模式

./etcdctl set /coreos.com/network/config '{"Network": "172.7.0.0/16", "Backend": {"Type": "VxLAN","Directrouting": true}}'


[root@hdss7-21 script]# iptables-save |grep -i postrouting
:POSTROUTING ACCEPT [68:4092]
:KUBE-POSTROUTING - [0:0]
-A POSTROUTING -m comment --comment "kubernetes postrouting rules" -j KUBE-POSTROUTING
-A POSTROUTING -s 172.7.21.0/24 ! -o docker0 -j MASQUERADE
-A KUBE-POSTROUTING -m comment --comment "kubernetes service traffic requiring SNAT" -m mark --mark 0x4000/0x4000 -j MASQUERADE


flennel 优化
yum install iptables-services  -y
[root@hdss7-21 ~]# systemctl start iptables
[root@hdss7-21 ~]# systemctl enable  iptables

[root@hdss7-21 ~]# iptables -t nat -D POSTROUTING -s 172.7.21.0/24 ! -o docker0 -j MASQUERADE
[root@hdss7-21 ~]# iptables -t nat -I POSTROUTING -s 172.7.21.0/24 ! -d 172.7.0.0/16 ! -o docker0 -j MASQUERADE
[root@hdss7-21 ~]# iptables-save |grep -i postrouting

 iptables -t filter -D INPUT -j REJECT --reject-with icmp-host-prohibited
 iptables -t filter -D FORWARD -j REJECT --reject-with icmp-host-prohibited
########## 规则定义 #########
10.4.7.21 主机上的,来源是 172.7.21.0/24 段的 docker 的 ip,目标 ip 不是 172.7.0.0/16 段,网络发包不从 docker0 桥设备出站的,才进行 SNAT 转换

~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[OK]

各自访问对方节点,并查看 nginx-access 日志,可看到现在暴露的都是容器 ip
[root@hdss7-21 ~]#  kubectl  logs -f nginx-ds-jtn62
172.7.22.2 - - [23/Nov/2019:17:46:48 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.38.0" "-"
[root@hdss7-22 ~]# kubectl  logs -f nginx-ds-d5kl8
10.4.7.21 - - [23/Nov/2019:17:01:37 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.38.0" "-"

172.7.21.2 - - [23/Nov/2019:17:43:34 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.38.0" "-"


[root@hdss7-21 ~}#route add -net 172.7.22.0/24 gw 10.4.7.22 dev eth0
[root@hdss7-22~}#route add -net 172.7.21.0/24 gw 10.4.7.21 dev eth0
[root@hdss7-21 flannel]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.4.7.254      0.0.0.0         UG    100    0        0 eth0
10.4.7.0        0.0.0.0         255.255.255.0   U     100    0        0 eth0
172.7.21.0      0.0.0.0         255.255.255.0   U     0      0        0 docker0
172.7.22.0      10.4.7.22       255.255.255.0   UG    0      0        0 eth0

[root@hdss7-22 flannel]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.4.7.254      0.0.0.0         UG    100    0        0 eth0
10.4.7.0        0.0.0.0         255.255.255.0   U     100    0        0 eth0
172.7.21.0      10.4.7.21       255.255.255.0   UG    0      0        0 eth0
172.7.22.0      0.0.0.0         255.255.255.0   U     0      0        0 docker0

注意还要优化一条 iptables 规则:~]#  iptables -t filter -I FORWARD -d 172.7.21.0/24 -j ACCEPT



flennel 优化
yum install iptables-services  -y
[root@hdss7-22 ~]# systemctl start iptables
[root@hdss7-22 ~]# systemctl enable  iptables

[root@hdss7-22 ~]# iptables -t nat -D POSTROUTING -s 172.7.22.0/24 ! -o docker0 -j MASQUERADE
[root@hdss7-22 ~]# iptables -t nat -I POSTROUTING -s 172.7.22.0/24 ! -d 172.7.0.0/16 ! -o docker0 -j MASQUERADE
[root@hdss7-22 ~]# iptables-save |grep -i postrouting

 iptables -t filter -D INPUT -j REJECT --reject-with icmp-host-prohibited
 iptables -t filter -D FORWARD -j REJECT --reject-with icmp-host-prohibited
########## 规则定义 #########
10.4.7.22 主机上的,来源是 172.7.22.0/24 段的 docker 的 ip,目标 ip 不是 172.7.0.0/16 段,网络发包不从 docker0 桥设备出站的,才进行 SNAT 转换

~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[OK]

各自访问对方节点,并查看 nginx-access 日志,可看到现在暴露的都是容器 ip
这里的需要注意防火墙的放通问题。

正文完
 0
yx
版权声明:本站原创文章,由 yx 于2022-05-06发表,共计8199字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
验证码