1. # Kubernetes CNI Flannel GuideLine
    2. # 时间:20210721-[20:00-22:00]
    3. # 主题:Flannel基础架构
    4. # 文档:https://www.yuque.com/docs/share/b20749e7-eb7f-4dd0-8a46-81501ed0e4b2?# 《20210721-Flannel基础架构》
    5. # 作业:CNI设计思想
    1. # FAQ:
    2. 1.Kubernetes环境内核具体需要升级到哪一个版本?
    3. 升级该内核的原因是为了更好的支持IPVLAN,通常的CentOS7.x 的发行版本内核一般为3.10。而稳定的IPVLNA 内核4.2+。所以原则上4.2+的内核版本都是推荐的。
    4. 2.资料是否长期有效。
    5. 通常情况下是的。
    6. env:
    7. [root@k8s-1 ~]# kubectl get nodes -o wide
    8. NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
    9. k8s-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.5
    10. k8s-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
    11. [root@k8s-1 ~]#

    Kubernetes CNI Flannel GuideLine.pdf

    1. # 1.Flannel :
    2. # https://github.com/flannel-io/flannel
    3. 1.1:How it works
    4. Flannel is a simple and easy way to configure a layer 3 network fabric designed for Kubernetes.
    5. 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.
    6. 1.2:Networking details
    7. Platforms 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.
    8. 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.
    9. Flannel is focused on networking. For network policy, other projects such as Calico can be used.
    10. # kubeadm
    11. For information on deploying flannel manually, using the Kubernetes installer toolkit kubeadm, see [Installing Kubernetes on Linux with kubeadm][kubeadm].
    12. 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.
    13. # kube-flannel.yaml
    14. The `flannel` manifest defines four things:
    15. 1. A ClusterRole and ClusterRoleBinding for role based acccess control (RBAC).
    16. 2. A service account for `flannel` to use.
    17. 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.
    18. 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.
    19. 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.
    20. # Annotations
    21. * `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)
    22. ## Older versions of Kubernetes
    23. `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.
    24. ### For Kubernetes v1.6~v1.15
    25. If you see errors saying `found invalid field...` when you try to apply `kube-flannel.yaml` then you can try the "legacy" manifest file
    26. * `kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/k8s-manifests/kube-flannel-legacy.yml`
    27. This file does not bundle RBAC permissions. If you need those, run
    28. * `kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/k8s-manifests/kube-flannel-rbac.yml`
    29. 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.
    30. * `Failed to create SubnetManager: error retrieving pod spec...`
    31. ### For Kubernetes v1.16
    32. `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.
    33. ## The flannel CNI plugin
    34. The 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)
    35. Kubernetes 1.6 requires CNI plugin version 0.5.1 or later.
    36. # Troubleshooting
    37. See [troubleshooting](troubleshooting.md)
    38. [kubeadm]: https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/
    1. # 2.Flannel Dockerfile:
    2. FROM alpine:3.13
    3. LABEL maintainer="Tom Denham <tom@tigera.io>"
    4. ENV FLANNEL_ARCH=amd64
    5. RUN apk add --no-cache iproute2 net-tools ca-certificates iptables strongswan && update-ca-certificates
    6. RUN apk add wireguard-tools --no-cache --repository http://dl-cdn.alpinelinux.org/alpine/edge/community
    7. COPY dist/flanneld-$FLANNEL_ARCH /opt/bin/flanneld
    8. COPY dist/mk-docker-opts.sh /opt/bin/
    9. COPY dist/iptables-wrapper-installer.sh /
    10. RUN /iptables-wrapper-installer.sh
    11. ENTRYPOINT ["/opt/bin/flanneld"]
    1. # 3.backend type:
    2. VXLAN
    3. Use in-kernel VXLAN to encapsulate the packets.
    4. Type and options:
    5. Type (string): vxlan
    6. VNI (number): VXLAN Identifier (VNI) to be used. On Linux, defaults to 1. On Windows should be greater than or equal to 4096.
    7. Port (number): UDP port to use for sending encapsulated packets. On Linux, defaults to kernel default, currently 8472, but on Windows, must be 4789.
    8. GBP (Boolean): Enable VXLAN Group Based Policy. Defaults to false. GBP is not supported on Windows
    9. DirectRouting (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.
    10. MacPrefix (String): Only use on Windows, set to the MAC prefix. Defaults to 0E-2A.
    11. host-gw
    12. Use host-gw to create IP routes to subnets via remote machine IPs. Requires direct layer2 connectivity between hosts running flannel.
    13. host-gw provides good performance, with few dependencies, and easy set up.
    14. Type:
    15. Type (string): host-gw
    16. UDP
    17. Use UDP only for debugging if your network and kernel prevent you from using VXLAN or host-gw.
    18. Type and options:
    19. Type (string): udp
    20. Port (number): UDP port to use for sending encapsulated packets. Defaults to 8285.

    Flannel.pdf

    1. yum -y install bridge-utils
    2. brctl show
    3. [root@k8s-1 ~]# brctl show
    4. bridge name bridge id STP enabled interfaces
    5. cni0 8000.1edb12e1c079 no veth54a9e98a
    6. docker0 8000.0242da0ca579 no
    7. flannel 8000.000000000000 no
    8. [root@k8s-1 ~]#

    flannel.yaml

    1. # For Kubernetes v1.17+
    2. kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
    3. ---
    4. kind: ConfigMap
    5. apiVersion: v1
    6. metadata:
    7. name: kube-flannel-cfg
    8. namespace: kube-system
    9. labels:
    10. tier: node
    11. app: flannel
    12. data:
    13. cni-conf.json: |
    14. {
    15. "name": "cbr0",
    16. "cniVersion": "0.3.1",
    17. "plugins": [
    18. {
    19. "type": "flannel",
    20. "delegate": {
    21. "hairpinMode": true,
    22. "isDefaultGateway": true
    23. }
    24. },
    25. {
    26. "type": "portmap",
    27. "capabilities": {
    28. "portMappings": true
    29. }
    30. }
    31. ]
    32. }
    33. net-conf.json: |
    34. {
    35. "Network": "10.244.0.0/16",
    36. "Backend": {
    37. "Type": "vxlan" # backend:ipip,vxlan,udp,host-gw#
    38. }
    39. }
    40. ---
    41. 在安装CNI之前,做一个快照。
    42. CNI:
    43. [root@k8s-1 ~]# cd /etc/cni/net.d/
    44. [root@k8s-1 net.d]# ls
    45. 10-flannel.conflist
    46. [root@k8s-1 net.d]# ll
    47. total 4
    48. -rw-r--r-- 1 root root 292 Jul 21 11:06 10-flannel.conflist
    49. [root@k8s-1 net.d]# cat 10-flannel.conflist
    50. {
    51. "name": "cbr0",
    52. "cniVersion": "0.3.1",
    53. "plugins": [
    54. {
    55. "type": "flannel",
    56. "delegate": {
    57. "hairpinMode": true,
    58. "isDefaultGateway": true
    59. }
    60. },
    61. {
    62. "type": "portmap",
    63. "capabilities": {
    64. "portMappings": true
    65. }
    66. }
    67. ]
    68. }
    69. [root@k8s-1 net.d]#
    70. [root@k8s-1 net.d]# cd /opt/cni/bin/
    71. [root@k8s-1 bin]# ll
    72. total 56484
    73. -rwxr-xr-x. 1 root root 3254624 Sep 10 2020 bandwidth
    74. -rwxr-xr-x. 1 root root 3581192 Sep 10 2020 bridge
    75. -rwxr-xr-x. 1 root root 9837552 Sep 10 2020 dhcp
    76. -rwxr-xr-x. 1 root root 4699824 Sep 10 2020 firewall
    77. -rwxr-xr-x. 1 root root 2650368 Sep 10 2020 flannel
    78. -rwxr-xr-x. 1 root root 3274160 Sep 10 2020 host-device
    79. -rwxr-xr-x. 1 root root 2847152 Sep 10 2020 host-local
    80. -rwxr-xr-x. 1 root root 3377272 Sep 10 2020 ipvlan
    81. -rwxr-xr-x. 1 root root 2715600 Sep 10 2020 loopback
    82. -rwxr-xr-x. 1 root root 3440168 Sep 10 2020 macvlan
    83. -rwxr-xr-x. 1 root root 3048528 Sep 10 2020 portmap
    84. -rwxr-xr-x. 1 root root 3528800 Sep 10 2020 ptp
    85. -rwxr-xr-x. 1 root root 2849328 Sep 10 2020 sbr
    86. -rwxr-xr-x. 1 root root 2503512 Sep 10 2020 static
    87. -rwxr-xr-x. 1 root root 2820128 Sep 10 2020 tuning
    88. -rwxr-xr-x. 1 root root 3377120 Sep 10 2020 vlan
    89. [root@k8s-1 bin]#
    1. # Demo for the CNI Config:
    2. [root@master1 net.d]# cat 00-multus.conf
    3. {
    4. "cniVersion": "0.3.1",
    5. "kubeconfig": "/etc/cni/net.d/multus.d/multus.kubeconfig",
    6. "name": "multus-cni-network",
    7. "type": "multus",
    8. "confDir": "/etc/cni/net.d",
    9. "binDir": "/opt/cni/bin",
    10. "logFile": "/var/log/multus.log",
    11. "logLevel": "debug",
    12. "multusNamespace": "kube-system",
    13. "capabilities": {
    14. "portMappings": true
    15. },
    16. "delegates": [
    17. {
    18. "cniVersion": "0.3.1",
    19. "name": "calico",
    20. "plugins": [
    21. {
    22. "datastore_type": "kubernetes", # this parameter.||||||kubernetes or etcd's address.
    23. "ipam": {
    24. "type": "whereabouts"
    25. },
    26. "kubernetes": {
    27. "kubeconfig": "/etc/cni/net.d/calico-kubeconfig"
    28. },
    29. "log_level": "info",
    30. "mtu": 1500,
    31. "nodename": "k8s-1",
    32. "policy": {
    33. "type": "k8s"
    34. },
    35. "type": "calico"
    36. },
    37. {
    38. "type": "tuning",
    39. "sysctl": {
    40. "net.ipv6.conf.all.accept_ra": "0",
    41. "net.ipv6.conf.default.accept_ra": "0",
    42. "net.ipv6.conf.lo.accept_ra": "0",
    43. "net.ipv6.conf.all.accept_dad": "0",
    44. "net.ipv6.conf.default.accept_dad": "0",
    45. "net.ipv6.conf.all.ndisc_notify": "1",
    46. "net.ipv6.conf.default.ndisc_notify": "1"
    47. }
    48. },
    49. {
    50. "capabilities": {
    51. "portMappings": true
    52. },
    53. "snat": true,
    54. "type": "portmap"
    55. }
    56. ]
    57. }
    58. ]
    59. }