# Kubernetes CNI Flannel GuideLine# 时间:20210721-[20:00-22:00]# 主题:Flannel基础架构# 文档:https://www.yuque.com/docs/share/b20749e7-eb7f-4dd0-8a46-81501ed0e4b2?# 《20210721-Flannel基础架构》# 作业:CNI设计思想
# FAQ:1.Kubernetes环境内核具体需要升级到哪一个版本?升级该内核的原因是为了更好的支持IPVLAN,通常的CentOS7.x 的发行版本内核一般为3.10。而稳定的IPVLNA 内核4.2+。所以原则上4.2+的内核版本都是推荐的。2.资料是否长期有效。通常情况下是的。env:[root@k8s-1 ~]# kubectl get nodes -o wideNAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIMEk8s-1 Ready control-plane,master 116d v1.20.5 172.12.1.11 <none> CentOS Linux 7 (Core) 5.11.10-1.el7.elrepo.x86_64 docker://20.10.5k8s-2 Ready <none> 116d v1.20.5 172.12.1.12 <none> CentOS Linux 7 (Core) 5.11.10-1.el7.elrepo.x86_64 docker://20.10.5[root@k8s-1 ~]#
Kubernetes CNI Flannel GuideLine.pdf
# 1.Flannel :# https://github.com/flannel-io/flannel1.1:How it worksFlannel is a simple and easy way to configure a layer 3 network fabric designed for Kubernetes.Flannel runs a small, single binary agent called flanneld on each host, and is responsible for allocating a subnet lease to each host out of a larger, preconfigured address space. Flannel uses either the Kubernetes API or etcd directly to store the network configuration, the allocated subnets, and any auxiliary data (such as the host's public IP). Packets are forwarded using one of several backend mechanisms including VXLAN and various cloud integrations.1.2:Networking detailsPlatforms like Kubernetes assume that each container (pod) has a unique, routable IP inside the cluster. The advantage of this model is that it removes the port mapping complexities that come from sharing a single host IP.Flannel is responsible for providing a layer 3 IPv4 network between multiple nodes in a cluster. Flannel does not control how containers are networked to the host, only how the traffic is transported between hosts. However, flannel does provide a CNI plugin for Kubernetes and a guidance on integrating with Docker.Flannel is focused on networking. For network policy, other projects such as Calico can be used.# kubeadmFor information on deploying flannel manually, using the Kubernetes installer toolkit kubeadm, see [Installing Kubernetes on Linux with kubeadm][kubeadm].NOTE: If `kubeadm` is used, then pass `--pod-network-cidr=10.244.0.0/16` to `kubeadm init` to ensure that the `podCIDR` is set.# kube-flannel.yamlThe `flannel` manifest defines four things:1. A ClusterRole and ClusterRoleBinding for role based acccess control (RBAC).2. A service account for `flannel` to use.3. A ConfigMap containing both a CNI configuration and a `flannel` configuration. The `network` in the `flannel` configuration should match the pod network CIDR. The choice of `backend` is also made here and defaults to VXLAN.4. A DaemonSet for every architecture to deploy the `flannel` pod on each Node. The pod has two containers 1) the `flannel` daemon itself, and 2) an initContainer for deploying the CNI configuration to a location that the `kubelet` can read.When you run pods, they will be allocated IP addresses from the pod network CIDR. No matter which node those pods end up on, they will be able to communicate with each other.# Annotations* `flannel.alpha.coreos.com/public-ip-overwrite`: Allows to overwrite the public IP of a node. Useful if the public IP can not determined from the node, e.G. because it is behind a NAT. It can be automatically set to a nodes `ExternalIP` using the [flannel-node-annotator](https://github.com/alvaroaleman/flannel-node-annotator)## Older versions of Kubernetes`kube-flannel.yaml` has some features that aren't compatible with older versions of Kubernetes, though flanneld itself should work with any version of Kubernetes.### For Kubernetes v1.6~v1.15If you see errors saying `found invalid field...` when you try to apply `kube-flannel.yaml` then you can try the "legacy" manifest file* `kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/k8s-manifests/kube-flannel-legacy.yml`This file does not bundle RBAC permissions. If you need those, run* `kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/k8s-manifests/kube-flannel-rbac.yml`If you didn't apply the `kube-flannel-rbac.yml` manifest and you need to, you'll see errors in your flanneld logs about failing to connect.* `Failed to create SubnetManager: error retrieving pod spec...`### For Kubernetes v1.16`kube-flannel.yaml` uses `ClusterRole` & `ClusterRoleBinding` of `rbac.authorization.k8s.io/v1`. When you use Kubernetes v1.16, you should replace `rbac.authorization.k8s.io/v1` to `rbac.authorization.k8s.io/v1beta1` because `rbac.authorization.k8s.io/v1` had become GA from Kubernetes v1.17.## The flannel CNI pluginThe flannel CNI plugin can be found in the CNI plugins [repository](https://github.com/containernetworking/plugins). For additional details, see the [README](https://github.com/containernetworking/plugins/tree/master/plugins/meta/flannel)Kubernetes 1.6 requires CNI plugin version 0.5.1 or later.# TroubleshootingSee [troubleshooting](troubleshooting.md)[kubeadm]: https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/
# 2.Flannel Dockerfile:FROM alpine:3.13LABEL maintainer="Tom Denham <tom@tigera.io>"ENV FLANNEL_ARCH=amd64RUN apk add --no-cache iproute2 net-tools ca-certificates iptables strongswan && update-ca-certificatesRUN apk add wireguard-tools --no-cache --repository http://dl-cdn.alpinelinux.org/alpine/edge/communityCOPY dist/flanneld-$FLANNEL_ARCH /opt/bin/flanneldCOPY dist/mk-docker-opts.sh /opt/bin/COPY dist/iptables-wrapper-installer.sh /RUN /iptables-wrapper-installer.shENTRYPOINT ["/opt/bin/flanneld"]
# 3.backend type:VXLANUse in-kernel VXLAN to encapsulate the packets.Type and options:Type (string): vxlanVNI (number): VXLAN Identifier (VNI) to be used. On Linux, defaults to 1. On Windows should be greater than or equal to 4096.Port (number): UDP port to use for sending encapsulated packets. On Linux, defaults to kernel default, currently 8472, but on Windows, must be 4789.GBP (Boolean): Enable VXLAN Group Based Policy. Defaults to false. GBP is not supported on WindowsDirectRouting (Boolean): Enable direct routes (like host-gw) when the hosts are on the same subnet. VXLAN will only be used to encapsulate packets to hosts on different subnets. Defaults to false. DirectRouting is not supported on Windows.MacPrefix (String): Only use on Windows, set to the MAC prefix. Defaults to 0E-2A.host-gwUse host-gw to create IP routes to subnets via remote machine IPs. Requires direct layer2 connectivity between hosts running flannel.host-gw provides good performance, with few dependencies, and easy set up.Type:Type (string): host-gwUDPUse UDP only for debugging if your network and kernel prevent you from using VXLAN or host-gw.Type and options:Type (string): udpPort (number): UDP port to use for sending encapsulated packets. Defaults to 8285.
yum -y install bridge-utilsbrctl show[root@k8s-1 ~]# brctl showbridge name bridge id STP enabled interfacescni0 8000.1edb12e1c079 no veth54a9e98adocker0 8000.0242da0ca579 noflannel 8000.000000000000 no[root@k8s-1 ~]#
# For Kubernetes v1.17+kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml---kind: ConfigMapapiVersion: v1metadata:name: kube-flannel-cfgnamespace: kube-systemlabels:tier: nodeapp: flanneldata:cni-conf.json: |{"name": "cbr0","cniVersion": "0.3.1","plugins": [{"type": "flannel","delegate": {"hairpinMode": true,"isDefaultGateway": true}},{"type": "portmap","capabilities": {"portMappings": true}}]}net-conf.json: |{"Network": "10.244.0.0/16","Backend": {"Type": "vxlan" # backend:ipip,vxlan,udp,host-gw#}}---在安装CNI之前,做一个快照。CNI:[root@k8s-1 ~]# cd /etc/cni/net.d/[root@k8s-1 net.d]# ls10-flannel.conflist[root@k8s-1 net.d]# lltotal 4-rw-r--r-- 1 root root 292 Jul 21 11:06 10-flannel.conflist[root@k8s-1 net.d]# cat 10-flannel.conflist{"name": "cbr0","cniVersion": "0.3.1","plugins": [{"type": "flannel","delegate": {"hairpinMode": true,"isDefaultGateway": true}},{"type": "portmap","capabilities": {"portMappings": true}}]}[root@k8s-1 net.d]#[root@k8s-1 net.d]# cd /opt/cni/bin/[root@k8s-1 bin]# lltotal 56484-rwxr-xr-x. 1 root root 3254624 Sep 10 2020 bandwidth-rwxr-xr-x. 1 root root 3581192 Sep 10 2020 bridge-rwxr-xr-x. 1 root root 9837552 Sep 10 2020 dhcp-rwxr-xr-x. 1 root root 4699824 Sep 10 2020 firewall-rwxr-xr-x. 1 root root 2650368 Sep 10 2020 flannel-rwxr-xr-x. 1 root root 3274160 Sep 10 2020 host-device-rwxr-xr-x. 1 root root 2847152 Sep 10 2020 host-local-rwxr-xr-x. 1 root root 3377272 Sep 10 2020 ipvlan-rwxr-xr-x. 1 root root 2715600 Sep 10 2020 loopback-rwxr-xr-x. 1 root root 3440168 Sep 10 2020 macvlan-rwxr-xr-x. 1 root root 3048528 Sep 10 2020 portmap-rwxr-xr-x. 1 root root 3528800 Sep 10 2020 ptp-rwxr-xr-x. 1 root root 2849328 Sep 10 2020 sbr-rwxr-xr-x. 1 root root 2503512 Sep 10 2020 static-rwxr-xr-x. 1 root root 2820128 Sep 10 2020 tuning-rwxr-xr-x. 1 root root 3377120 Sep 10 2020 vlan[root@k8s-1 bin]#
# Demo for the CNI Config:[root@master1 net.d]# cat 00-multus.conf{"cniVersion": "0.3.1","kubeconfig": "/etc/cni/net.d/multus.d/multus.kubeconfig","name": "multus-cni-network","type": "multus","confDir": "/etc/cni/net.d","binDir": "/opt/cni/bin","logFile": "/var/log/multus.log","logLevel": "debug","multusNamespace": "kube-system","capabilities": {"portMappings": true},"delegates": [{"cniVersion": "0.3.1","name": "calico","plugins": [{"datastore_type": "kubernetes", # this parameter.||||||kubernetes or etcd's address."ipam": {"type": "whereabouts"},"kubernetes": {"kubeconfig": "/etc/cni/net.d/calico-kubeconfig"},"log_level": "info","mtu": 1500,"nodename": "k8s-1","policy": {"type": "k8s"},"type": "calico"},{"type": "tuning","sysctl": {"net.ipv6.conf.all.accept_ra": "0","net.ipv6.conf.default.accept_ra": "0","net.ipv6.conf.lo.accept_ra": "0","net.ipv6.conf.all.accept_dad": "0","net.ipv6.conf.default.accept_dad": "0","net.ipv6.conf.all.ndisc_notify": "1","net.ipv6.conf.default.ndisc_notify": "1"}},{"capabilities": {"portMappings": true},"snat": true,"type": "portmap"}]}]}
