Configure a custom CNI Chain

Updated at:

CNI (Container Network Interface) is the interface standard for implementing network plugins in Kubernetes. CNI Chain is a plugin mode that combines multiple CNI plugins in a chain, with each plugin handling a task such as IP allocation or routing. This makes network configuration more flexible.

Prerequisites

  • You have created an ACK managed cluster whose network plugin is Terway. For more information, see Create an ACK managed cluster.

  • The Terway version is v1.5.6 or later, and the cluster does not use exclusive elastic network interface (ENI) mode.

Warning

ACK does not guarantee that multiple CNI plugins can work together. Configuring a custom CNI Chain is a high-risk operation. Configure it only after you fully understand the functions of the multiple CNIs. Proceed with caution to prevent configuration errors from causing service interruptions.

Configure a custom CNI Chain

When you need to use CNI Chain, add the plugins that you require to the Terway configuration file.

Configure by using cni_chain

Modify the Terway configuration file eni-config.

kubectl edit cm -nkube-system eni-config

Configuration parameter

Description

10-terway.conf

The Terway CNI configuration. Do not modify its content.

Important

Do not modify the existing configuration.

cni_chain

Additional CNI rules. These rules are appended after the existing Terway rules. This configuration can be set on a per-node basis.

Important
  • cni_chain and 10-terway.conflist are mutually exclusive.

  • cni_chain uses the YAML format.

  • The terway-eniip version must be 1.18.0 or later.

Configuration file example

apiVersion: v1
kind: ConfigMap
metadata:
  name: eni-config
  namespace: kube-system
data:
  10-terway.conf: |
    # If no configuration is required, keep it unchanged
  cni_chain: |
    - type: tuning
      sysctl:
        net.ipv6.conf.all.disable_ipv6: "1"

Use kubectl rollout restart -n kube-system daemonset.apps/terway-eniip to restart the Terway pods.

If the configuration is correct, you can view your custom plugin configuration in the /etc/cni/net.d/10-terway.conflist configuration file on the node.

Configure by using 10-terway.conflist (not recommended)

  1. Modify the eni-config configuration file:

    kubectl edit cm -nkube-system eni-config

    Configuration parameter

    Description

    10-terway.conf

    The Terway CNI configuration. Do not modify its content.

    Important

    Do not modify the existing configuration.

    10-terway.conflist

    The custom CNI Chain configuration.

    plugins must start with the CNI configuration in 10-terway.conf.

    Important

    The configurations in this document are examples for illustration only. Do not copy them directly to avoid configuration errors.

    The content of the configuration block must be in JSON format. Make sure that the format is correct.

    Configuration file example

    kind: ConfigMap
    apiVersion: v1
    metadata:
      name: eni-config
      namespace: kube-system
    data:
      10-terway.conflist: |
          {
            "plugins": [
              {
                "cniVersion": "0.4.0",
                "name": "terway",
                "type": "terway",
                "capabilities": {"bandwidth": true}
              },
              {
                "type": "portmap",
                "capabilities": {"portMappings": true},
                "externalSetMarkChain":"KUBE-MARK-MASQ"
              }
            ]
          }
      10-terway.conf: |
        {
          "cniVersion": "0.4.0",
          "name": "terway",
          "type": "terway",
          "capabilities": {"bandwidth": true}
        }
  2. Use kubectl rollout restart -n kube-system daemonset.apps/terway-eniip to restart the Terway pods.

    If the configuration is correct, you can view your custom plugin configuration in the /etc/cni/net.d/10-terway.conflist configuration file on the node.

Use cases

Configure portmap

The portmap plugin maps the internal ports of a Pod to ports on the host, so that external networks can access specific services in the Pod.

Note

If you require public network access, make sure that the corresponding ports are opened in the inbound direction of the security group.

The policy rules generated by portmap may be incompatible with network policies. Test them thoroughly before use.

Configuration example

  • Access by using the private IP of the node + port:

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: eni-config
      namespace: kube-system
    data:
      10-terway.conf: |
        {
          "cniVersion": "0.4.0",
          "name": "terway",
          "type": "terway",
          "capabilities": {"bandwidth": true}
        }
      cni_chain: |
        - type: portmap
          capabilities:
            portMappings: true
          externalSetMarkChain: KUBE-MARK-MASQ
  • Access by using the public IP + port:

    For public network access, the following configuration is required:

    Configuration item

    Non-Datapath V2

    Datapath V2

    masqAll

    Not required

    Required

    symmetric_routing

    Required

    Required

    • masqAll: a parameter of the portmap plugin, which requires portmap version >= 1.7.1

    • symmetric_routing: a parameter of the Terway plugin, which requires Terway version >= 1.15.0

    • For clusters that use Datapath V2 and have Kube-proxy-replacement enabled, port mapping is supported without configuring the portmap plugin.

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: eni-config
      namespace: kube-system
    data:
      10-terway.conf: |
        {
          "cniVersion": "0.4.0",
          "name": "terway",
          "type": "terway",
          "symmetric_routing": true,
          "capabilities": {
            "bandwidth": true
          }
        }
      cni_chain: |
        - type: portmap
          capabilities:
            portMappings: true
          masqAll: false

Disable IPv6 in containers

Inside a container, the network interface is automatically assigned an IPv6 link-local address, even if your cluster does not have the IPv6 dual-stack feature enabled. This address belongs to the fe80::/64 range. This is the default behavior of the operating system kernel. It is usually harmless and does not disrupt normal business operations.

However, if your application handles network addresses incorrectly, it may mistakenly identify this IPv6 link-local address as the IP of the Pod and try to use it for communication. But a link-local address is intended for communication between devices on the same link and cannot be used for network communication, which may cause your application to run abnormally. If your application uses a link-local address to communicate, its code has a defect. Report the defect to the application vendor first and seek a solution.

If you still need to disable the IPv6 link-local address inside containers, you can achieve this by configuring the tuning plugin.

Configuration example

kind: ConfigMap
apiVersion: v1
metadata:
  name: eni-config
  namespace: kube-system
data:
  10-terway.conf: |
    {
      "cniVersion": "0.4.0",
      "name": "terway",
      "type": "terway",
      "capabilities": {"bandwidth": true}
    }
  cni_chain: |
    - type: tuning
      sysctl:
        net.ipv6.conf.all.disable_ipv6: "1"