TF+K8s部署指南丨容器的多网络接口(multi-net)功能支持
-
从4.0版开始,Tungsten Fabric为使用Kubernetes编排器的容器提供网络支持。你可以使用标准容器网络接口(CNI插件)为创建的每个容器分配一个网络接口。有关Tungsten Fabric容器联网的更多信息,请参阅已发表文章。
从5.1版本开始,Tungsten Fabric支持为容器分配多个网络接口(multi-net),使容器能够连接到多个网络,并且可以指定容器能连接到的网络。网络接口可以是物理接口,也可以是虚拟接口,并连接到Linux网络命名空间。网络命名空间是Linux内核中的网络栈。一个以上的容器可以共享同一个网络命名空间。
Tungsten Fabric多网络支持是基于Kubernetes多网模型的。Kubernetes多网模型有特定的设计和结构,可以扩展到TF多网络等非kubernetes的模型当中。TF多网模型不需要修改Kubernetes API和Kubernetes CNI驱动。TF多网模型与Kubernetes多网模型一样,也不改变已有的集群范围网络行为。
创建多网络接口时,请注意以下限制和注意事项:
当pod仍在运行时,不能添加或删除sidecar 网络。
在从Kubernetes API服务器中删除网络附件定义之前,由管理员负责删除相应的TF pod。
除了自定义网络外,TF还创建了一个默认的cluster-wide-network。
TF CNI插件不是一个委托插件。它不支持Kubernetes Network Custom Resource Definition De Facto Standard Version 1中提供的委托插件规范。有关更多信息,请从以下页面中查看:
https://github.com/K8sNetworkPlumbingWG/multi-net-spec创建多网络接口
按照这些步骤来创建多网络接口。
1.创建网络对象模型
如果集群不支持网络对象模型,你就创建一个网络对象模型。
容器编排平台的对象模型表示网络,并将网络连接到容器。如果模型默认不支持网络对象,你可以使用扩展来表示网络。
使用Kubernetes NetworkAttachmentDefinition CRD对象创建网络对象模型。
apiVersion: apiextensions.k8s.io/v1beta1 kind: CustomResourceDefinition metadata: # name must match the spec fields below, and be in the form: . name: network-attachment-definitions.k8s.cni.cncf.io spec: # group name to use for REST API: /apis// group: k8s.cni.cncf.io # version name to use for REST API: /apis// version: v1 # either Namespaced or Cluster scope: Namespaced names: # plural name to be used in the URL: /apis/// plural: network-attachment-definitions # singular name to be used as an alias on the CLI and for display singular: network-attachment-definition # kind is normally the CamelCased singular type. Your resource manifests use this. kind: NetworkAttachmentDefinition # shortNames allow shorter string to match your resource on the CLI shortNames: - net-attach-def validation: openAPIV3Schema: properties: spec: properties: config: type: string
Kubernetes在其对象模型中使用自定义扩展来表示网络。Kubernetes的CustomResourceDefinition(CRD)功能有助于支持自定义扩展。
注意:安装Tungsten Fabric时,会自动创建一个CRD。CRD指定的网络是不被Kubernetes识别的sidecars。附加的pod网络附件与Kubernetes API及其对象(如服务、端点、代理等)的交互没有被指定。Kubernetes不能识别这些对象与任何pod的关联。
2.建立网络
在集群中创建网络:
通过API服务器创建
apiVersion: k8s.cni.cncf.io/v1 kind: NetworkAttachmentDefinition metadata: annotations: opencontrail.org/cidr: "/24" opencontrail.org/ip_fabric_forwarding: "false" opencontrail.org/ip_fabric_snat: "false" name: right-network namespace: default spec: config: '{ "cniVersion": "0.3.0", "type": "contrail-k8s-cni" }'
创建一个right-network.yaml文件。
通过映射到从Tungsten Fabric Web用户界面或从Command用户界面创建的现有网络。
apiVersion: "k8s.cni.cncf.io/v1" kind: NetworkAttachmentDefinition metadata: name: extns-network annotations: "opencontrail.org/network" : '{"domain":"default-domain", "project": "k8s-extns", "name":"k8s-extns-pod-network"}' spec: config: '{ "cniVersion": "0.3.1", "type": "contrail-k8s-cni" }'
创建网络的命令:
kubectl apply -f right-network.yaml3.将网络分配给pod
可以将步骤2中创建的网络分配给 pod。每个pod也有一个默认网络分配给它。因此,每个pod都将有以下网络分配:
默认网络(由Kubernetes分配)
注意:Tungsten Fabric内部创建了一个名为cluster-wide-network的默认网络。这个接口是pod的默认接口。
在步骤2中创建的网络
使用k8s-semantics将网络分配给pod:
方案1
apiVersion: v1 kind: Pod metadata: name: multiNetworkPod annotations: k8s.v1.cni.cncf.io/networks: '[ { "name": "network-a" }, { "name": "network-b" } ]' spec: containers: - image: busybox command: - sleep - "3600" imagePullPolicy: IfNotPresent name: busybox stdin: true tty: true restartPolicy: Always
方案2
apiVersion: v1 kind: Pod metadata: name: ubuntu-pod-3 annotations: k8s.v1.cni.cncf.io/networks: left-network,blue-network,right-network,extns/data-network spec: containers: - name: ubuntuapp image: ubuntu-upstart securityContext: capabilities: add: - NET_ADMIN
(注:原文出现Contrail或Contrail networking的地方,本文都以Tungsten Fabric替代,绝大多数情况下两者功能一致。)