k8s設計了網絡模型,可是把實現交給了網絡插件,而CNI網絡插件實現的最主要的功能就是POD跨宿主機資源互相訪問linux
flannel安裝:
nginx
hdss7-21和hdss7-22 兩個幾點操做:git
wget https://github.com/coreos/flannel/releases/download/v0.11.0/flannel-v0.11.0-linux-amd64.tar.gz mkdir /opt/flannel-v0.11.0 tar xf flannel-v0.11.0-linux-amd64.tar.gz -C /opt/flannel-v0.11.0/ ln -s /opt/flannel-v0.11.0/ flannel mkdir cert
拷貝證書:github
cert]# scp dc2-user@hdss7-200:/opt/certs/ca.pem . scp dc2-user@hdss7-200:/opt/certs/client.pem .
編輯配置文件和啓動腳本
docker
[root@hdss7-21api
flannel]# cat 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]# cat 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=eth0 \ --subnet-file=./subnet.env \ --healthz-port=2401
建立log目錄:
bash
mkdir -p /data/logs/flanneld
在etcd中建立配置,聲明flannel使用的網絡模型:
網絡
etcd]# ./etcdctl set /coreos.com/netwoide
rk/config '{"Network": "172.7.0.0/16", "Backend": {"Type": "host-gw"}}' etcd]# ./etcdctl get /coreos.com/network/config {"Network": "172.7.0.0/16", "Backend": {"Type": "host-gw"}}
配置supervisior配置文件並重載配置:post
[root@hdss7-21 ~]# cat /etc/supervisord.d/flannel.ini [program:flanneld-7-21] command=/opt/flannel/flanneld.sh ; the program (relative uses PATH, can take args) numprocs=1 ; number of processes copies to start (def 1) directory=/opt/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 cert]# supervisorctl update flanneld-7-22: added process group [root@hdss7-22 cert]# supervisorctl status etcd-server-7-22 RUNNING pid 3093, uptime 13:12:04 flanneld-7-22 RUNNING pid 379, uptime 0:00:36 kube-apiserver-7-22 RUNNING pid 3090, uptime 13:12:04 kube-controller-manager-7-22 RUNNING pid 3092, uptime 13:12:04 kube-kubelet-7-22 RUNNING pid 3089, uptime 13:12:04 kube-proxy-7-22 RUNNING pid 3091, uptime 13:12:04 kube-scheduler-7-22 RUNNING pid 3095, uptime 13:12:04
此時你能夠跨節點ping通pod地址了
[root@hdss7-21 ~]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES nginx-ds-rxfqd 1/1 Running 0 129m 172.7.22.2 hdss7-22.host.com <none> <none> nginx-ds-xm5l2 1/1 Running 0 151m 172.7.21.2 hdss7-21.host.com <none> <none> [root@hdss7-21 ~]# ping 172.7.22.2 PING 172.7.22.2 (172.7.22.2) 56(84) bytes of data. 64 bytes from 172.7.22.2: icmp_seq=1 ttl=63 time=2.39 ms 64 bytes from 172.7.22.2: icmp_seq=2 ttl=63 time=1.19 ms
查看路由規則能夠看到,flannel幫咱們添加了靜態路由,實際上,在flannel的host-gw網絡模型中,flannel僅僅只是幫咱們在每一臺宿主機上作了這樣一件事情而已,因此他的效率也是很是高的,可是host-gw網絡模型,僅支持咱們的宿主機在同一個2層網絡下(即宿主機的網關指向同一地址),若是不通的2層網絡打通的話就須要用到Vxlan模型的網絡了:
[root@hdss7-21 ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 10.4.0.1 0.0.0.0 UG 0 0 0 eth0 10.4.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 1002 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
flannel三種網絡模型設置以下:
宿主機在同一網絡
'{"Network": "172.7.0.0/16", "Backend": {"Type": "host-gw"}}'
不在同一網絡
'{"Network": "172.7.0.0/16", "Backend": {"Type": "VxLAN"}}'
由flannel來判斷宿主機是否在同一2層網絡下,直接路由模型
'{"Network": "172.7.0.0/16", "Backend": {"Type": "VxLAN","Directrouting": true}}'
flannel的SNAT規則優化:
默認狀況下,咱們的跨宿主機pod以前容器的互相通訊,會通過iptables的原地址轉換,這就致使一個問題,咱們在相同的二層網絡下,POD以前互相通訊,殊不知道究竟是哪個pod來訪問個人,可想而知,也不利於問題的排查
安裝iptables-services
~]# yum install iptables-services -y [root@hdss7-21 ~]# systemctl start iptables [root@hdss7-21 ~]# systemctl enable iptables Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
刪除snat規則,從新添加:
[root@hdss7-21 ~]# iptables-save |grep POSTROUTING :POSTROUTING ACCEPT [72:3710] :KUBE-POSTROUTING - [0:0] -A POSTROUTING -s 172.7.21.0/24 ! -o docker0 -j MASQUERADE ~]# iptables -t nat -D POSTROUTING -s 172.7.21.0/24 ! -o docker0 -j MASQUERADE ~]# iptables -t nat -I POSTROUTING -s 172.7.21.0/24 ! -d 172.7.0.0/16 ! -o docker0 -j MASQUERADE ~]# iptables-save |grep -i postrouting :POSTROUTING ACCEPT [32:1647] :KUBE-POSTROUTING - [0:0] -A POSTROUTING -s 172.7.21.0/24 ! -d 172.7.0.0/16 ! -o docker0 -j MASQUERADE
保存iptables規則:
~]# iptables -t filter -D INPUT -j REJECT --reject-with icmp-host-prohibited ~]# iptables -t filter -D FORWARD -j REJECT --reject-with icmp-host-prohibited ~]# iptables-save > /etc/sysconfig/iptables