-
- 1、Calico概述
- 2、Calico架构及BGP实现
- 3、Calico部署
- 4、Calico管理工具
- 5、Calico BGP模式
- 6、Calico Route Reflector 模式(RR)
- 7、Calico IPIP模式
- 8、Calico网络策略
1、Calico概述
Calico
是Kubernetes
生态系统中另一种流行的网络选择。虽然Flannel
被公认为是最简单的选择,但Calico
以其性能、灵活性而闻名。Calico
的功能更为全面,不仅提供主机和pod
之间的网络连接,还涉及网络安全和管理。Calico CNI
插件在CNI
框架内封装了Calico
的功能。
Calico
是一个基于BGP
的纯三层的网络方案,与OpenStack
、Kubernetes
、AWS
、GCE
等云平台都能够良好地集成。Calico
在每个计算节点都利用Linux Kernel
实现了一个高效的虚拟路由器vRouter
来负责数据转发。每个vRouter
都通过BGP1
协议把在本节点上运行的容器的路由信息向整个Calico
网络广播,并自动设置到达其他节点的路由转发规则。Calico
保证所有容器之间的数据流量都是通过IP
路由的方式完成互联互通的。Calico
节点组网时可以直接利用数据中心的网络结构(L2或者L3),不需要额外的NAT
、隧道或者Overlay Network
,没有额外的封包解包,能够节约CPU
运算,提高网络效率。
Calico
在小规模集群中可以直接互联,在大规模集群中可以通过额外的BGP route reflector
来完成。
此外,Calico
基于iptables
还提供了丰富的网络策略,实现了Kubernetes
的Network Policy
策略,提供容器间网络可达性限制的功能。
2、Calico架构及BGP实现
BGP
是互联网上一个核心的去中心化自治路由协议,它通过维护IP
路由表或“前缀”表来实现自治系统AS
之间的可达性,属于矢量路由协议。不过,考虑到并非所有的网络都能支持BGP
,以及Calico控制平面的设计要求物理网络必须是二层网络,以确保 vRouter
间均直接可达,路由不能够将物理设备当作下一跳等原因,为了支持三层网络,Calico
还推出了IP-in-IP
叠加的模型,它也使用Overlay的方式来传输数据。IPIP
的包头非常小,而且也是内置在内核中,因此理论上它的速度要比VxLAN
快一点 ,但安全性更差。Calico 3.x
的默认配置使用的是IPIP
类型的传输方案而非BGP
。
Calico
的系统架构如图所示
Calico
主要由Felix
、Orchestrator Plugin
、etcd
、BIRD
和BGP Router Reflector
等组件组成。
Felix
: Calico Agent,运行于每个节点。Orchestrator Plugi
:编排系统(如 Kubernetes 、 OpenStack 等)以将Calico
整合进系统中的插件,例如Kubernetes
的CNI
。etcd
:持久存储Calico
数据的存储管理系统。BIRD
:用于分发路由信息的BGP
客户端。BGP Route Reflector
:BGP
路由反射器,可选组件,用于较大规模的网络场景。
3、Calico部署
在Kubernetes
中部署Calico
的主要步骤如下:
- 修改
Kubernetes
服务的启动参数,并重启服务- 设置Master上kube-apiserver服务的启动参数:--allowprivileged=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(由install-cni容器完成)
- 部署一个名为calico/kube-policy-controller的Deployment,以对 接Kubernetes集群中为Pod设置的Network Policy
具体部署的步骤如下 下载yaml
代码语言:javascript复制curl https://docs.projectcalico.org/v3.11/manifests/calico-etcd.yaml -o calico-etcd.yaml
下载完后修改配置项
- 配置连接etcd地址,如果使用https,还需要配置证书。(ConfigMap,Secret)
# cat /opt/etcd/ssl/ca.pem | base64 -w 0
LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURlakNDQW1LZ0F3SUJBZ0lVRHQrZ21iYnhzWmoxRGNrbGl3K240MkI5YW5Nd0RRWUpLb1pJaHZjTkFRRUwKQlFBd1F6RUxNQWtHQTFVRUJoTUNRMDR4RURBT0JnTlZCQWdUQjBKbGFXcHBibWN4RURBT0JnTlZCQWNUQjBKbAphV3BwYm1jeEVEQU9CZ05WQkFNVEIyVjBZMlFnUTBFd0hoY05NVGt4TWpBeE1UQXdNREF3V2hjTk1qUXhNVEk1Ck1UQXdNREF3V2pCRE1Rc3dDUVlEVlFRR0V3S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VAwYzRKL1FTTHNMNjZZRTh5YnhvZ2M2L3JTYzBIblladkNZbXc5MFZxY05oU3hkT2liaWtPUy9SdDAKZHVRSnU3cmdkM3pldys3Y05CaTIwTFZrbzc3dDNRZWRZK0c3dUxVZ21qNWJudkU9Ci0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0K
将上述base64
加密的字符串修改至文件中声明:ca.pem
对应etcd-ca
、server-key.pem
对应etcd-key
、server.pem
对应etcd-cert
;修改etcd
证书的位置;修改etcd
的连接地址(与api-server中配置/opt/kubernetes/cfg/kube-apiserver.conf中相同)
# vim calico-etcd.yaml
...
apiVersion: v1
kind: Secret
type: Opaque
metadata:
name: calico-etcd-secrets
namespace: kube-system
data:
# Populate the following with etcd TLS configuration if desired, but leave blank if
# not using TLS for etcd.
# The keys below should be uncommented and the values populated with the base64
# encoded contents of each file that would be associated with the TLS data.
# Example command for encoding a file contents: cat <file> | base64 -w 0
etcd-key: 填写上面的加密字符串
etcd-cert: 填写上面的加密字符串
etcd-ca: 填写上面的加密字符串
...
kind: ConfigMap
apiVersion: v1
metadata:
name: calico-config
namespace: kube-system
data:
# Configure this with the location of your etcd cluster.
etcd_endpoints: "https://192.168.2.61:2379,https://192.168.2.62:2379,https://192.168.2.63:2379"
# If you're using TLS enabled etcd uncomment the following.
# You must also populate the Secret below with these files.
etcd_ca: "/calico-secrets/etcd-ca"
etcd_cert: "/calico-secrets/etcd-cert"
etcd_key: "/calico-secrets/etcd-key"
根据实际网络规划修改Pod CIDR(CALICO_IPV4POOL_CIDR),与controller-manager配置/opt/kubernetes/cfg/kube-controller-manager.conf中相同
代码语言:javascript复制# vim calico-etcd.yaml
...
320 - name: CALICO_IPV4POOL_CIDR
321 value: "10.244.0.0/16"
...
选择工作模式(CALICO_IPV4POOL_IPIP),支持BGP,IPIP,此处先关闭IPIP模式
代码语言:javascript复制# vim calico-etcd.yaml
...
309 - name: CALICO_IPV4POOL_IPIP
310 value: "Never"
...
修改完后应用清单
代码语言:javascript复制# kubectl apply -f calico-etcd.yaml
secret/calico-etcd-secrets created
configmap/calico-config created
clusterrole.rbac.authorization.k8s.io/calico-kube-controllers created
clusterrolebinding.rbac.authorization.k8s.io/calico-kube-controllers created
clusterrole.rbac.authorization.k8s.io/calico-node created
clusterrolebinding.rbac.authorization.k8s.io/calico-node created
daemonset.apps/calico-node created
serviceaccount/calico-node created
deployment.apps/calico-kube-controllers created
serviceaccount/calico-kube-controllers created
# kubectl get pods -n kube-system
如果事先部署了fannel
网络组件,需要先卸载和删除flannel
,在每个节点均需要操作
# kubectl delete -f kube-flannel.yaml
# ip link delete cni0
# ip link delete flannel.1
# ip route
default via 192.168.2.2 dev eth0
10.244.1.0/24 via 192.168.2.63 dev eth0
10.244.2.0/24 via 192.168.2.62 dev eth0
169.254.0.0/16 dev eth0 scope link metric 1002
172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1
192.168.2.0/24 dev eth0 proto kernel scope link src 192.168.2.61
# ip route del 10.244.1.0/24 via 192.168.2.63 dev eth0
# ip route del 10.244.2.0/24 via 192.168.2.62 dev eth0
# ip route
default via 192.168.2.2 dev eth0
169.254.0.0/16 dev eth0 scope link metric 1002
172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1
192.168.2.0/24 dev eth0 proto kernel scope link src 192.168.2.61
4、Calico管理工具
下载工具:https://github.com/projectcalico/calicoctl/releases
代码语言:javascript复制# wget -O /usr/local/bin/calicoctl https://github.com/projectcalico/calicoctl/releases/download/v3.11.1/calicoctl
# chmod x /usr/local/bin/calicoctl
使用calicoctl
查看服务状态
# ./calicoctl node status
Calico process is running.
IPv4 BGP status
-------------- ------------------- ------- ---------- -------------
| PEER ADDRESS | PEER TYPE | STATE | SINCE | INFO |
-------------- ------------------- ------- ---------- -------------
| 192.168.2.62 | node-to-node mesh | up | 02:58:05 | Established |
| 192.168.2.63 | node-to-node mesh | up | 03:08:46 | Established |
-------------- ------------------- ------- ---------- -------------
IPv6 BGP status
No IPv6 peers found.
实际上,使用calicoctl
查看node
状态就是调用系统查看的,与netstat
效果一样
# netstat -antp|grep bird
tcp 0 0 0.0.0.0:179 0.0.0.0:* LISTEN 62709/bird
tcp 0 0 192.168.2.61:179 192.168.2.63:58963 ESTABLISHED 62709/bird
tcp 0 0 192.168.2.61:179 192.168.2.62:37390 ESTABLISHED 62709/bird
想要查看更多的信息,需要指定配置查看etcd
中的数据
创建配置文件
# mkdir /etc/calico
# vim /etc/calico/calicoctl.cfg
apiVersion: projectcalico.org/v3
kind: CalicoAPIConfig
metadata:
spec:
datastoreType: "etcdv3"
etcdEndpoints: "https://192.168.2.61:2379,https://192.168.2.62:2379,https://192.168.2.63:2379"
etcdKeyFile: "/opt/etcd/ssl/server-key.pem"
etcdCertFile: "/opt/etcd/ssl/server.pem"
etcdCACertFile: "/opt/etcd/ssl/ca.pem"
查看数据等操作
代码语言:javascript复制# calicoctl get node
NAME
k8s-master-01
k8s-node-01
k8s-node-02
查看IPAM的IP地址池:
代码语言:javascript复制# ./calicoctl get ippool
NAME CIDR SELECTOR
default-ipv4-ippool 10.244.0.0/16 all()
# ./calicoctl get ippool -o wide
NAME CIDR NAT IPIPMODE VXLANMODE DISABLED SELECTOR
default-ipv4-ippool 10.244.0.0/16 true Never Never false all()
5、Calico BGP模式
Pod 1
访问Pod 2
大致流程如下:
- 数据包从容器1出到达
Veth Pair
另一端(宿主机上,以cali
前缀开头); - 宿主机根据路由规则,将数据包转发给下一跳(网关);
- 到达
Node2
,根据路由规则将数据包转发给cali
设备,从而到达容器2。
路由表:
代码语言:javascript复制# node1
10.244.36.65 dev cali4f18ce2c9a1 scope link
10.244.169.128/26 via 192.168.31.63 dev ens33 proto bird
10.244.235.192/26 via 192.168.31.61 dev ens33 proto bird
# node2
10.244.169.129 dev calia4d5b2258bb scope link
10.244.36.64/26 via 192.168.31.62 dev ens33 proto bird
10.244.235.192/26 via 192.168.31.61 dev ens33 proto bird
其中,这里最核心的“下一跳”路由规则,就是由Calico
的Felix
进程负责维护的。这些路由规则信息,则是通过BGP Client
也就是BIRD
组件,使用BGP
协议传输而来的。
不难发现,Calico
项目实际上将集群里的所有节点,都当作是边界路由器来处理,它们一起组成了一个全连通的网络,互相之间通过BGP
协议交换路由规则。这些节点,我们称为BGP Peer
。
calico相关文件
代码语言:javascript复制# ls /opt/cni/bin/calico-ipam
/opt/cni/bin/calico-ipam
# cat /etc/cni/net.d/
10-calico.conflist calico-kubeconfig calico-tls/
# cat /etc/cni/net.d/10-calico.conflist
{
"name": "k8s-pod-network",
"cniVersion": "0.3.1",
"plugins": [
{
"type": "calico",
"log_level": "info",
"etcd_endpoints": "https://192.168.2.61:2379,https://192.168.2.62:2379,https://192.168.2.63:2379",
"etcd_key_file": "/etc/cni/net.d/calico-tls/etcd-key",
"etcd_cert_file": "/etc/cni/net.d/calico-tls/etcd-cert",
"etcd_ca_cert_file": "/etc/cni/net.d/calico-tls/etcd-ca",
"mtu": 1440,
"ipam": {
"type": "calico-ipam"
},
"policy": {
"type": "k8s"
},
"kubernetes": {
"kubeconfig": "/etc/cni/net.d/calico-kubeconfig"
}
},
{
"type": "portmap",
"snat": true,
"capabilities": {"portMappings": true}
}
]
}
6、Calico Route Reflector 模式(RR)
https://docs.projectcalico.org/master/networking/bgp
Calico
维护的网络在默认是(Node-to-Node Mesh)全互联模式,Calico
集群中的节点之间都会相互建立连接,用于路由交换。但是随着集群规模的扩大,mesh
模式将形成一个巨大服务网格,连接数成倍增加。
这时就需要使用Route Reflector
(路由器反射)模式解决这个问题。
确定一个或多个Calico
节点充当路由反射器(一般配置两个以上),让其他节点从这个RR
节点获取路由信息。
具体步骤如下:
1、关闭 node-to-node BGP网格
默认node to node
模式最好在100个节点以下
添加default BGP
配置,调整nodeToNodeMeshEnabled
和asNumber
:bgp.yaml
# cat << EOF | calicoctl create -f -
apiVersion: projectcalico.org/v3
kind: BGPConfiguration
metadata:
name: default
spec:
logSeverityScreen: Info
nodeToNodeMeshEnabled: false
asNumber: 63400
EOF
# calicoctl apply -f bgp.yaml # 一旦执行,集群会立即断网
Successfully applied 1 'BGPConfiguration' resource(s)
# calicoctl get bgpconfig
NAME LOGSEVERITY MESHENABLED ASNUMBER
default Info false 63400
# calicoctl node status
Calico process is running.
IPv4 BGP status
No IPv4 peers found.
IPv6 BGP status
No IPv6 peers found.
ASN号可以通过获取
代码语言:javascript复制# calicoctl get nodes --output=wide
NAME ASN IPV4 IPV6
k8s-master-01 (63400) 192.168.2.61/24
k8s-node-01 (63400) 192.168.2.62/24
k8s-node-02 (63400) 192.168.2.63/24
2、配置指定节点充当路由反射器
为方便让BGPPeer
轻松选择节点,通过标签选择器匹配。
给路由器反射器节点打标签:
增加第二个路由反射器时,给新的node
打标签并配置成反射器节点即可
# kubectl label node k8s-node-02 route-reflector=true
node/k8s-node-02 labeled
然后配置路由器反射器节点routeReflectorClusterID
:
# calicoctl get nodes k8s-node-02 -o yaml> node.yaml
# vim node.yaml
apiVersion: projectcalico.org/v3
kind: Node
metadata:
annotations:
projectcalico.org/kube-labels: '{"beta.kubernetes.io/arch":"amd64","beta.kubernetes.io/os":"linux","kubernetes.io/arch":"amd64","kubernetes.io/hostname":"k8s-node2","kubernetes.io/os":"linux"}'
creationTimestamp: null
labels:
beta.kubernetes.io/arch: amd64
beta.kubernetes.io/os: linux
kubernetes.io/arch: amd64
kubernetes.io/hostname: k8s-node2
kubernetes.io/os: linux
name: k8s-node2
spec:
bgp:
ipv4Address: 192.168.31.63/24
routeReflectorClusterID: 244.0.0.1 # 增加集群ID
orchRefs:
- nodeName: k8s-node2
orchestrator: k8s
# ./calicoctl apply -f node.yaml
Successfully applied 1 'Node' resource(s)
现在,很容易使用标签选择器将路由反射器节点与其他非路由反射器节点配置为对等:
表示所有节点都连接路由反射器节点
代码语言:javascript复制# vim peer-with-route-reflectors.yaml
apiVersion: projectcalico.org/v3
kind: BGPPeer
metadata:
name: peer-with-route-reflectors
spec:
nodeSelector: all()
peerSelector: route-reflector == 'true'
# calicoctl apply -f peer-with-route-reflectors.yaml
Successfully applied 1 'BGPPeer' resource(s)
# calicoctl get bgppeer
NAME PEERIP NODE ASN
peer-with-route-reflectors all() 0
查看节点的BGP
连接状态,只有本节点与路由反射器节点的连接:
# calicoctl node status
Calico process is running.
IPv4 BGP status
-------------- --------------- ------- ---------- -------------
| PEER ADDRESS | PEER TYPE | STATE | SINCE | INFO |
-------------- --------------- ------- ---------- -------------
| 192.168.2.63 | node specific | up | 04:17:14 | Established |
-------------- --------------- ------- ---------- -------------
IPv6 BGP status
No IPv6 peers found.
7、Calico IPIP模式
Flannel host-gw
模式最主要的限制,就是要求集群宿主机之间是二层连通的。而这个限制对于Calico
来说,也同样存在。
修改为IPIP
模式:
也可以直接在部署calico
的时候直接修改
# calicoctl get ipPool -o yaml > ipip.yaml
# vi ipip.yaml
apiVersion: projectcalico.org/v3
kind: IPPool
metadata:
name: default-ipv4-ippool
spec:
blockSize: 26
cidr: 10.244.0.0/16
ipipMode: Always # 启动ipip模式
natOutgoing: true
# calicoctl apply -f ipip.yaml
# calicoctl get ippool -o wide
NAME CIDR NAT IPIPMODE VXLANMODE DISABLED SELECTOR
default-ipv4-ippool 10.244.0.0/16 true Always Never false all()
# ip route # 会增加tunl0网卡
default via 192.168.2.2 dev eth0
10.244.44.192/26 via 192.168.2.63 dev tunl0 proto bird onlink
blackhole 10.244.151.128/26 proto bird
10.244.154.192/26 via 192.168.2.62 dev tunl0 proto bird onlink
169.254.0.0/16 dev eth0 scope link metric 1002
172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1
192.168.2.0/24 dev eth0 proto kernel scope link src 192.168.2.61
IPIP示意图:
Pod 1
访问Pod 2
大致流程如下:
- 数据包从容器1出到达
Veth Pair
另一端(宿主机上,以cali
前缀开头); - 进入IP隧道设备(tunl0),由
Linux
内核IPIP
驱动封装在宿主机网络的IP
包中(新的IP
包目的地之是原IP
包的下一跳地址,即192.168.31.63),这样,就成了Node1
到Node2
的数据包; - 数据包经过路由器三层转发到
Node2
; Node2
收到数据包后,网络协议栈会使用IPIP
驱动进行解包,从中拿到原始IP
包;- 然后根据路由规则,根据路由规则将数据包转发给
cali
设备,从而到达容器2。
路由表:
代码语言:javascript复制# node1
10.244.36.65 dev cali4f18ce2c9a1 scope link
10.244.169.128/26 via 192.168.31.63 dev tunl0 proto bird onlink
# node2
10.244.169.129 dev calia4d5b2258bb scope link
10.244.36.64/26 via 192.168.31.62 dev tunl0 proto bird onlink
不难看到,当Calico
使用IPIP
模式的时候,集群的网络性能会因为额外的封包和解包工作而下降。所以建议你将所有宿主机节点放在一个子网里,避免使用IPIP
。
8、Calico网络策略
部署完成Calico后,就可以实现k8s中的网络策略NetworkPolicy
,对于网络策略在前面的文章使用flannel canal实现k8s的NetworkPolicy有详细描述,这里不再赘述。?
文章参考来源: https://docs.projectcalico.org/