×

附加网络的 IP 地址分配配置

IP 地址管理 (IPAM) 容器网络接口 (CNI) 插件为其他 CNI 插件提供 IP 地址。

您可以使用以下 IP 地址分配类型

  • 静态分配。

  • 通过 DHCP 服务器进行动态分配。您指定的 DHCP 服务器必须可以从附加网络访问。

  • 通过 Whereabouts IPAM CNI 插件进行动态分配。

静态 IP 地址分配配置

下表描述了静态 IP 地址分配的配置

表 1. ipam 静态配置对象
字段 类型 描述

type

字符串

IPAM 地址类型。需要值为 static

addresses

数组

指定要分配给虚拟接口的 IP 地址的对象数组。支持 IPv4 和 IPv6 IP 地址。

routes

数组

指定要在 Pod 内配置的路由的对象数组。

dns

数组

可选:指定 DNS 配置的对象数组。

addresses 数组需要具有以下字段的对象

表 2. ipam.addresses[] 数组
字段 类型 描述

address

字符串

您指定的 IP 地址和网络前缀。例如,如果您指定 10.10.21.10/24,则附加网络将分配 10.10.21.10 的 IP 地址,掩码为 255.255.255.0

gateway

字符串

将出站网络流量路由到的默认网关。

表 3. ipam.routes[] 数组
字段 类型 描述

dst

字符串

CIDR 格式的 IP 地址范围,例如 192.168.17.0/240.0.0.0/0(用于默认路由)。

gw

字符串

网络流量路由到的网关。

表 4. ipam.dns 对象
字段 类型 描述

nameservers

数组

一个或多个用于发送 DNS 查询的 IP 地址数组。

domain

数组

要附加到主机名的默认域名。例如,如果域名设置为 example.com,则对 example-host 的 DNS 查询将被改写为 example-host.example.com

search

数组

在 DNS 查询期间要附加到非限定主机名(例如 example-host)的域名数组。

静态 IP 地址分配配置示例
{
  "ipam": {
    "type": "static",
      "addresses": [
        {
          "address": "191.168.1.7/24"
        }
      ]
  }
}

动态 IP 地址 (DHCP) 分配配置

以下 JSON 描述了使用 DHCP 进行动态 IP 地址分配的配置。

DHCP 租约续期

Pod 在创建时获取其原始 DHCP 租约。该租约必须由在集群上运行的最小 DHCP 服务器部署定期续期。

要触发 DHCP 服务器的部署,您必须通过编辑集群网络操作员配置来创建一个 shim 网络附件,如下例所示

shim 网络附件定义示例
apiVersion: operator.openshift.io/v1
kind: Network
metadata:
  name: cluster
spec:
  additionalNetworks:
  - name: dhcp-shim
    namespace: default
    type: Raw
    rawCNIConfig: |-
      {
        "name": "dhcp-shim",
        "cniVersion": "0.3.1",
        "type": "bridge",
        "ipam": {
          "type": "dhcp"
        }
      }
  # ...
表 5. ipam DHCP 配置对象
字段 类型 描述

type

字符串

IPAM 地址类型。需要值为 dhcp

动态 IP 地址 (DHCP) 分配配置示例
{
  "ipam": {
    "type": "dhcp"
  }
}

使用 Whereabouts 的动态 IP 地址分配配置

Whereabouts CNI 插件允许将 IP 地址动态分配给附加网络,而无需使用 DHCP 服务器。

Whereabouts CNI 插件还支持重叠的 IP 地址范围以及在单独的 NetworkAttachmentDefinition CRD 中多次配置相同的 CIDR 范围。这在多租户环境中提供了更大的灵活性和管理能力。

动态 IP 地址配置对象

下表描述了使用 Whereabouts 进行动态 IP 地址分配的配置对象

表 6. ipam whereabouts 配置对象
字段 类型 描述

type

字符串

IPAM 地址类型。需要值为 whereabouts

range

字符串

CIDR 表示法中的 IP 地址和范围。IP 地址将从此地址范围内分配。

exclude

数组

可选:零个或多个 CIDR 表示法中的 IP 地址和范围列表。不会分配排除的地址范围内的 IP 地址。

network_name

字符串

可选:帮助确保每个 Pod 组或域都获得其自己的一组 IP 地址,即使它们共享相同的 IP 地址范围。设置此字段对于保持网络的独立性和组织性非常重要,尤其是在多租户环境中。

使用 Whereabouts 的动态 IP 地址分配配置

以下示例显示了使用 Whereabouts 的动态地址分配配置

Whereabouts 动态 IP 地址分配
{
  "ipam": {
    "type": "whereabouts",
    "range": "192.0.2.192/27",
    "exclude": [
       "192.0.2.192/30",
       "192.0.2.196/32"
    ]
  }
}

使用 Whereabouts 和重叠 IP 地址范围的动态 IP 地址分配

以下示例显示了使用重叠 IP 地址范围为多租户网络进行动态 IP 地址分配。

NetworkAttachmentDefinition 1
{
  "ipam": {
    "type": "whereabouts",
    "range": "192.0.2.192/29",
    "network_name": "example_net_common", (1)
  }
}
1 可选。如果设置,则必须与NetworkAttachmentDefinition 2network_name匹配。
NetworkAttachmentDefinition 2
{
  "ipam": {
    "type": "whereabouts",
    "range": "192.0.2.192/24",
    "network_name": "example_net_common", (1)
  }
}
1 可选。如果设置,则必须与NetworkAttachmentDefinition 1network_name匹配。

创建 whereabouts-reconciler 守护程序集

Whereabouts 调解器负责使用 Whereabouts IP 地址管理 (IPAM) 解决方案管理集群内 Pod 的动态 IP 地址分配。它确保每个 Pod 从指定的 IP 地址范围获得唯一的 IP 地址。它还在 Pod 被删除或缩减规模时处理 IP 地址释放。

您还可以使用NetworkAttachmentDefinition自定义资源定义 (CRD) 进行动态 IP 地址分配。

当您通过集群网络运营商配置附加网络时,whereabouts-reconciler守护程序集会自动创建。当您从 YAML 清单配置附加网络时,它不会自动创建。

要触发whereabouts-reconciler守护程序集的部署,您必须通过编辑集群网络运营商自定义资源 (CR) 文件手动创建一个whereabouts-shim网络附件。

请使用以下步骤部署whereabouts-reconciler守护程序集。

步骤
  1. 通过运行以下命令编辑Network.operator.openshift.io自定义资源 (CR)

    $ oc edit network.operator.openshift.io cluster
  2. 在自定义资源 (CR) 的spec定义中包含此示例 YAML 片段中所示的additionalNetworks部分

    apiVersion: operator.openshift.io/v1
    kind: Network
    metadata:
      name: cluster
    # ...
    spec:
      additionalNetworks:
      - name: whereabouts-shim
        namespace: default
        rawCNIConfig: |-
          {
           "name": "whereabouts-shim",
           "cniVersion": "0.3.1",
           "type": "bridge",
           "ipam": {
             "type": "whereabouts"
           }
          }
        type: Raw
    # ...
  3. 保存文件并退出文本编辑器。

  4. 通过运行以下命令验证whereabouts-reconciler守护程序集是否已成功部署

    $ oc get all -n openshift-multus | grep whereabouts-reconciler
    示例输出
    pod/whereabouts-reconciler-jnp6g 1/1 Running 0 6s
    pod/whereabouts-reconciler-k76gg 1/1 Running 0 6s
    pod/whereabouts-reconciler-k86t9 1/1 Running 0 6s
    pod/whereabouts-reconciler-p4sxw 1/1 Running 0 6s
    pod/whereabouts-reconciler-rvfdv 1/1 Running 0 6s
    pod/whereabouts-reconciler-svzw9 1/1 Running 0 6s
    daemonset.apps/whereabouts-reconciler 6 6 6 6 6 kubernetes.io/os=linux 6s

配置 Whereabouts IP 调解器计划

Whereabouts IPAM CNI 插件每天运行 IP 调解器。此过程会清理任何可能导致 IP 资源耗尽并因此阻止为新 Pod 分配 IP 的闲置 IP 分配。

使用此步骤更改 IP 调解器的运行频率。

先决条件
  • 您已安装 OpenShift CLI (oc)。

  • 您可以作为具有cluster-admin角色的用户访问集群。

  • 您已部署whereabouts-reconciler守护程序集,并且whereabouts-reconciler Pod 正在运行。

步骤
  1. 运行以下命令以在openshift-multus命名空间中创建名为whereabouts-configConfigMap对象,其中包含 IP 调解器的特定 cron 表达式

    $ oc create configmap whereabouts-config -n openshift-multus --from-literal=reconciler_cron_expression="*/15 * * * *"

    此 cron 表达式表示 IP 调解器每 15 分钟运行一次。根据您的具体要求调整表达式。

    whereabouts-reconciler守护程序集只能使用包含五个星号的 cron 表达式模式。第六个(用于表示秒)目前不支持。

  2. 通过运行以下命令检索与openshift-multus命名空间中的whereabouts-reconciler守护程序集和 Pod 相关的资源信息

    $ oc get all -n openshift-multus | grep whereabouts-reconciler
    示例输出
    pod/whereabouts-reconciler-2p7hw                   1/1     Running   0             4m14s
    pod/whereabouts-reconciler-76jk7                   1/1     Running   0             4m14s
    pod/whereabouts-reconciler-94zw6                   1/1     Running   0             4m14s
    pod/whereabouts-reconciler-mfh68                   1/1     Running   0             4m14s
    pod/whereabouts-reconciler-pgshz                   1/1     Running   0             4m14s
    pod/whereabouts-reconciler-xn5xz                   1/1     Running   0             4m14s
    daemonset.apps/whereabouts-reconciler          6         6         6       6            6           kubernetes.io/os=linux   4m16s
  3. 运行以下命令以验证whereabouts-reconciler Pod 是否以配置的间隔运行 IP 调解器

    $ oc -n openshift-multus logs whereabouts-reconciler-2p7hw
    示例输出
    2024-02-02T16:33:54Z [debug] event not relevant: "/cron-schedule/..2024_02_02_16_33_54.1375928161": CREATE
    2024-02-02T16:33:54Z [debug] event not relevant: "/cron-schedule/..2024_02_02_16_33_54.1375928161": CHMOD
    2024-02-02T16:33:54Z [debug] event not relevant: "/cron-schedule/..data_tmp": RENAME
    2024-02-02T16:33:54Z [verbose] using expression: */15 * * * *
    2024-02-02T16:33:54Z [verbose] configuration updated to file "/cron-schedule/..data". New cron expression: */15 * * * *
    2024-02-02T16:33:54Z [verbose] successfully updated CRON configuration id "00c2d1c9-631d-403f-bb86-73ad104a6817" - new cron expression: */15 * * * *
    2024-02-02T16:33:54Z [debug] event not relevant: "/cron-schedule/config": CREATE
    2024-02-02T16:33:54Z [debug] event not relevant: "/cron-schedule/..2024_02_02_16_26_17.3874177937": REMOVE
    2024-02-02T16:45:00Z [verbose] starting reconciler run
    2024-02-02T16:45:00Z [debug] NewReconcileLooper - inferred connection data
    2024-02-02T16:45:00Z [debug] listing IP pools
    2024-02-02T16:45:00Z [debug] no IP addresses to cleanup
    2024-02-02T16:45:00Z [verbose] reconciler success

创建动态分配双栈 IP 地址的配置

可以使用ipRanges参数为以下情况配置双栈 IP 地址分配:

  • IPv4 地址

  • IPv6 地址

  • 多个 IP 地址分配

步骤
  1. type设置为whereabouts

  2. 使用ipRanges分配 IP 地址,如下例所示

    cniVersion: operator.openshift.io/v1
    kind: Network
    =metadata:
      name: cluster
    spec:
      additionalNetworks:
      - name: whereabouts-shim
        namespace: default
        type: Raw
        rawCNIConfig: |-
          {
           "name": "whereabouts-dual-stack",
           "cniVersion": "0.3.1,
           "type": "bridge",
           "ipam": {
             "type": "whereabouts",
             "ipRanges": [
                      {"range": "192.168.10.0/24"},
                      {"range": "2001:db8::/64"}
                  ]
           }
          }
  3. 将网络附加到 Pod。有关更多信息,请参见“将 Pod 添加到附加网络”。

  4. 验证所有 IP 地址是否已分配。

  5. 运行以下命令以确保 IP 地址已作为元数据分配。

    $ oc exec -it mypod -- ip a