042.集群网络-flannel及calico
一 Flannel组件
1.1 flannel介绍
- 它能协助Kubernetes,给每一个Node上的Docker容器都分配互相不冲突的IP地址。
- 它能在这些IP地址之间建立一个覆盖网络(Overlay Network),通过这个覆盖网络,将数据包原封不动地传递到目标容器内。
二 Calico组件
2.1 calco组件简介
2.2 Calico架构
2.3 Calico步骤过程
- 修改Kubernetes服务的启动参数,并重启服务。
- 设置Master上kube-apiserver服务的启动参数:–allow-privileged=true(因为calico-node需要以特权模式运行在各Node上)。
- 设置各Node上kubelet服务的启动参数:–networkplugin=cni(使用CNI网络插件)。
- 创建Calico服务,主要包括calico-node和calico policy controller。需要创建的资源对象如下:
- 创建ConfigMap calico-config,包含Calico所需的配置参数。
- 创建Secret calico-etcd-secrets,用于使用TLS方式连接etcd。
- 在每个Node上都运行calico/node容器,部署为DaemonSet。
- 在每个Node上都安装Calico CNI二进制文件和网络配置参数(由install-cni容器完成)。
- 部署一个名为calico/kube-policy-controller的Deployment,以对接Kubernetes集群中为Pod设置的Network Policy。
2.4 正式部署
- ConfigMap解析
1 kind: ConfigMap 2 apiVersion: v1 3 metadata: 4 name: calico-config 5 namespace: kube-system 6 data: 7 typha_service_name: "none" 8 calico_backend: "bird" 9 veth_mtu: "1440" 10 cni_network_config: |- 11 { 12 "name": "k8s-pod-network", 13 "cniVersion": "0.3.0", 14 "plugins": [ 15 { 16 "type": "calico", 17 "log_level": "info", 18 "datastore_type": "kubernetes", 19 "nodename": "__KUBERNETES_NODE_NAME__", 20 "mtu": __CNI_MTU__, 21 "ipam": { 22 "type": "calico-ipam" 23 }, 24 "policy": { 25 "type": "k8s" 26 }, 27 "kubernetes": { 28 "kubeconfig": "__KUBECONFIG_FILEPATH__" 29 } 30 }, 31 { 32 "type": "portmap", 33 "snat": true, 34 "capabilities": {"portMappings": true} 35 } 36 ] 37 }
- Secret解析
1 apiVersion: v1 2 kind: Secret 3 type: Opaque 4 metadata: 5 name: calico-etcd-secrets 6 namespace: kube-system 7 data: 8 # Populate the following with etcd TLS configuration if desired, but leave blank if 9 # not using TLS for etcd. 10 # The keys below should be uncommented and the values populated with the base64 11 # encoded contents of each file that would be associated with the TLS data. 12 # Example command for encoding a file contents: cat <file> | base64 -w 0 13 # 如果配置了TLS ,则需要设置相应的证书和密钥文件路径 14 # etcd-key: null 15 # etcd-cert: null 16 # etcd-ca: null
- DaemonSet解析
1 kind: DaemonSet 2 apiVersion: extensions/v1beta1 3 metadata: 4 name: calico-node 5 namespace: kube-system 6 labels: 7 k8s-app: calico-node 8 spec: 9 selector: 10 matchLabels: 11 k8s-app: calico-node 12 updateStrategy: 13 type: RollingUpdate 14 rollingUpdate: 15 maxUnavailable: 1 16 template: 17 metadata: 18 labels: 19 k8s-app: calico-node 20 annotations: 21 scheduler.alpha.kubernetes.io/critical-pod: '' 22 spec: 23 nodeSelector: 24 beta.kubernetes.io/os: linux 25 hostNetwork: true 26 tolerations: 27 - effect: NoSchedule 28 operator: Exists 29 - key: CriticalAddonsOnly 30 operator: Exists 31 - effect: NoExecute 32 operator: Exists 33 serviceAccountName: calico-node 34 terminationGracePeriodSeconds: 0 35 initContainers: 36 - name: upgrade-ipam 37 image: calico/cni:v3.6.0 38 command: ["/opt/cni/bin/calico-ipam", "-upgrade"] 39 env: 40 - name: KUBERNETES_NODE_NAME 41 valueFrom: 42 fieldRef: 43 fieldPath: spec.nodeName 44 - name: CALICO_NETWORKING_BACKEND 45 valueFrom: 46 configMapKeyRef: 47 name: calico-config 48 key: calico_backend 49 volumeMounts: 50 - mountPath: /var/lib/cni/networks 51 name: host-local-net-dir 52 - mountPath: /host/opt/cni/bin 53 name: cni-bin-dir 54 - name: install-cni 55 image: calico/cni:v3.6.0 56 command: ["/install-cni.sh"] 57 env: 58 - name: CNI_CONF_NAME 59 value: "10-calico.conflist" 60 - name: CNI_NETWORK_CONFIG 61 valueFrom: 62 configMapKeyRef: 63 name: calico-config 64 key: cni_network_config 65 - name: KUBERNETES_NODE_NAME 66 valueFrom: 67 fieldRef: 68 fieldPath: spec.nodeName 69 - name: CNI_MTU 70 valueFrom: 71 configMapKeyRef: 72 name: calico-config 73 key: veth_mtu 74 - name: SLEEP 75 value: "false" 76 volumeMounts: 77 - mountPath: /host/opt/cni/bin 78 name: cni-bin-dir 79 - mountPath: /host/etc/cni/net.d 80 name: cni-net-dir 81 containers: 82 - name: calico-node 83 image: calico/node:v3.6.0 84 env: 85 - name: DATASTORE_TYPE 86 value: "kubernetes" 87 - name: WAIT_FOR_DATASTORE 88 value: "true" 89 - name: NODENAME 90 valueFrom: 91 fieldRef: 92 fieldPath: spec.nodeName 93 - name: CALICO_NETWORKING_BACKEND 94 valueFrom: 95 configMapKeyRef: 96 name: calico-config 97 key: calico_backend 98 - name: CLUSTER_TYPE 99 value: "k8s,bgp" 100 - name: IP 101 value: "autodetect" 102 - name: IP_AUTODETECTION_METHOD 103 value: "can-reach=172.24.8.2" 104 - name: CALICO_IPV4POOL_IPIP 105 value: "Always" 106 - name: FELIX_IPINIPMTU 107 valueFrom: 108 configMapKeyRef: 109 name: calico-config 110 key: veth_mtu 111 - name: CALICO_IPV4POOL_CIDR 112 value: "10.10.0.0/16" 113 - name: CALICO_DISABLE_FILE_LOGGING 114 value: "true" 115 - name: FELIX_DEFAULTENDPOINTTOHOSTACTION 116 value: "ACCEPT" 117 - name: FELIX_IPV6SUPPORT 118 value: "false" 119 - name: FELIX_LOGSEVERITYSCREEN 120 value: "info" 121 - name: FELIX_HEALTHENABLED 122 value: "true" 123 securityContext: 124 privileged: true 125 resources: 126 requests: 127 cpu: 250m 128 livenessProbe: 129 httpGet: 130 path: /liveness 131 port: 9099 132 host: localhost 133 periodSeconds: 10 134 initialDelaySeconds: 10 135 failureThreshold: 6 136 readinessProbe: 137 exec: 138 command: 139 - /bin/calico-node 140 - -bird-ready 141 - -felix-ready 142 periodSeconds: 10 143 volumeMounts: 144 - mountPath: /lib/modules 145 name: lib-modules 146 readOnly: true 147 - mountPath: /run/xtables.lock 148 name: xtables-lock 149 readOnly: false 150 - mountPath: /var/run/calico 151 name: var-run-calico 152 readOnly: false 153 - mountPath: /var/lib/calico 154 name: var-lib-calico 155 readOnly: false 156 volumes: 157 - name: lib-modules 158 hostPath: 159 path: /lib/modules 160 - name: var-run-calico 161 hostPath: 162 path: /var/run/calico 163 - name: var-lib-calico 164 hostPath: 165 path: /var/lib/calico 166 - name: xtables-lock 167 hostPath: 168 path: /run/xtables.lock 169 type: FileOrCreate 170 - name: cni-bin-dir 171 hostPath: 172 path: /opt/cni/bin 173 - name: cni-net-dir 174 hostPath: 175 path: /etc/cni/net.d 176 - name: host-local-net-dir 177 hostPath: 178 path: /var/lib/cni/networks
- install-cni:在Node上安装CNI二进制文件到/opt/cni/bin目录下,并安装相应的网络配置文件到/etc/cni/net.d目录下,设置为initContainers并在运行完成后退出。
- calico-node:Calico服务程序,用于设置Pod的网络资源,保证Pod的网络与各Node互联互通。它还需要以hostNetwork模式运行,直接使用宿主机网络。
- CALICO_IPV4POOL_CIDR:Calico IPAM的IP地址池,Pod的IP地址将从该池中进行分配。
- CALICO_IPV4POOL_IPIP:是否启用IPIP模式。启用IPIP模式时,Calico将在Node上创建一个名为tunl0的虚拟隧道。
- IP_AUTODETECTION_METHOD:获取Node IP地址的方式,默认使用第1个网络接口的IP地址,对于安装了多块网卡的Node,可以使用正则表达式选择正确的网卡,例如”interface=eth.*”表示选择名称以eth开头的网卡的IP地址。
- FELIX_IPV6SUPPORT:是否启用IPv6。
- FELIX_LOGSEVERITYSCREEN:日志级别。
- securityContext.privileged=true:以特权模式运行。
- calico-kube-controllers解析
1 apiVersion: extensions/v1beta1 2 kind: Deployment 3 metadata: 4 name: calico-kube-controllers 5 namespace: kube-system 6 labels: 7 k8s-app: calico-kube-controllers 8 annotations: 9 scheduler.alpha.kubernetes.io/critical-pod: '' 10 spec: 11 replicas: 1 12 strategy: 13 type: Recreate 14 template: 15 metadata: 16 name: calico-kube-controllers 17 namespace: kube-system 18 labels: 19 k8s-app: calico-kube-controllers 20 spec: 21 nodeSelector: 22 beta.kubernetes.io/os: linux 23 tolerations: 24 - key: CriticalAddonsOnly 25 operator: Exists 26 - key: node-role.kubernetes.io/master 27 effect: NoSchedule 28 serviceAccountName: calico-kube-controllers 29 containers: 30 - name: calico-kube-controllers 31 image: calico/kube-controllers:v3.6.0 32 env: 33 - name: ENABLED_CONTROLLERS 34 value: node 35 - name: DATASTORE_TYPE 36 value: kubernetes 37 readinessProbe: 38 exec: 39 command: 40 - /usr/bin/check-status 41 - -r
2.5 查看验证
- 10-calico.conf:符合CNI规范的网络配置,其中type=calico表示该插件的二进制文件名为calico。
- calico-kubeconfig:Calico所需的kubeconfig文件。
- calico-tls目录:以TLS方式连接etcd的相关文件。
2.6 运行测试