从 4.0 版开始,Contrail 使用 Kubernetes 编排为容器提供网络支持。您可以将网络接口分配给使用标准容器网络接口(CNI 插件)创建的每个容器。有关 Contrail 容器网络的更多信息,请参阅 Contrail 与 Kubernetes 集成。
在 Contrail 5.1 版中,您可以为容器分配多个网络接口(多网络),以使容器能够连接到多个网络。可以指定容器可以连接到的网络。网络接口是物理接口或虚拟接口,连接到 Linux 网络命名空间。网络命名空间是 Linux 内核中的网络堆栈。多个容器可以共享同一个网络命名空间。
The following limitations and caveats apply when you create multi-net interfaces:
您无法在容器仍在运行时添加或移除挎斗网络。
管理员负责先移除相应的 Contrail Pod,然后再从 Kubernetes API 服务器中删除网络附件定义。
除了自定义网络之外,Contrail 还会创建默认值 cluster-wide-network
。
Contrail CNI 插件不是委托插件。它不支持委派 Kubernetes 网络自定义资源定义事实上的标准版本 1 中提供的插件的规范。有关详细信息,请从 https://github.com/K8sNetworkPlumbingWG/multi-net-spec 页面查看 [v1] Kubernetes 网络自定义资源定义事实上的 Standard.md。
Contrail 多网络支持基于 Kubernetes 多网络模型。Kubernetes 多网模型具有特定的设计和构造,可以扩展到非 Kubernetes 模型,如 Contrail 多网络。Contrail 多网络模型不需要更改 Kubernetes API 和 Kubernetes CNI 驱动程序。与 Kubernetes 多网络模型一样,Contrail 多网络模型不会改变现有的集群范围网络行为。
Creating Multi-Net Interfaces
按照以下步骤创建多网络接口。
- 创建网络对象模型。
如果集群不支持网络对象模型,则可以创建该模型。
容器编排平台的对象模型表示网络并将网络附加到容器。如果模型默认不支持网络对象,则可以使用扩展来表示网络。
使用 Kubernetes 创建网络对象模型 NetworkAttachmentDefinition CRD 对象
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
# name must match the spec fields below, and be in the form: <plural>.<group>
name: network-attachment-definitions.k8s.cni.cncf.io
spec:
# group name to use for REST API: /apis/<group>/<version>
group: k8s.cni.cncf.io
# version name to use for REST API: /apis/<group>/<version>
version: v1
# either Namespaced or Cluster
scope: Namespaced
names:
# plural name to be used in the URL: /apis/<group>/<version>/<plural>
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) 功能有助于支持自定义扩展。
注意:
安装 Contrail 时会自动创建 CRD。CRD 指定的网络是 Kubernetes 无法识别的挎斗。未指定其他 Pod 网络附件与 Kubernetes API 及其对象(如服务、端点、代理等)的交互。Kubernetes 无法识别这些对象与任何 Pod 的关联。
- 创建网络。
您可以在群集中创建网络:
创建网络的命令:
kubectl apply -f right-network.yaml
- 将网络分配给 Pod。
您将在步骤 2 中创建的网络分配给 Pod。每个容器还分配有一个默认网络。因此,将为每个容器分配以下网络:
默认网络(由 Kubernetes 分配)
注意:
Contrail 会在内部创建一个名为 cluster-wide-network
的默认网络。此接口是容器的默认接口
您在步骤 2 中创建的网络
使用 k8s 语义将网络分配给 Pod:
Option 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
Option 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