Terway CNI Plugin

repository·main·Indexed 20 days ago

https://github.com/aliyuncontainerservice/terway

A high-performance CNI plugin for Alibaba Cloud Kubernetes (ACK) that utilizes Elastic Network Interfaces (ENI) to provide Pods with dedicated network stacks and IP addresses. It bypasses traditional encapsulation overhead to optimize network performance and cloud resource usage. The project includes the terway-cli for managing network resources, ENI deletion, and metadata inspection, as well as Terraform modules for provisioning ACK E2E clusters.

Tokens
32.2K
Snippets
75
Records
135
Agent score
67%

What's inside terway

  1. Overview of Terway CNI Plugin

    main

    Terway is a Container Network Interface (CNI) plugin developed by Alibaba Cloud for ACK (Alibaba Cloud Container Service for Kubernetes). It builds networking based on Alibaba Cloud's Elastic Network Interfaces (ENI) to fully utilize cloud resources.

    Key Networking Model:

    • Each Pod has its own network stack and IP address.
    • Intra-node communication: Pods on the same ECS communicate via internal machine forwarding.
    • Inter-node communication: Pods across different ECS instances communicate via VPC Elastic Network Interfaces (ENI) directly.
    • Performance: Because it avoids encapsulation technologies like VxLAN, Terway provides high communication performance and low latency.
  2. What is the IP Prefix mode in Terway?

    main

    IP Prefix mode is a prefix-based IP Address Management (IPAM) mode provided by Terway. Unlike the traditional mode that allocates individual IP addresses, Prefix mode allocates CIDR prefixes (e.g., /28) to Elastic Network Interfaces (ENIs). Each /28 prefix contains a continuous block of 16 IP addresses.

    Key Differences

    FeatureTraditional IP ModeIP Prefix Mode
    Allocation UnitSingle IP address (e.g., 10.244.17.119)CIDR prefix (e.g., 10.244.126.48/28)
    IP QuantityFixed number per ENIEach prefix contains 16 IPs (/28)
    ManagementController manages Pod↔IP bindingController allocates prefixes; Daemon manages binding

    Use Cases

    • Large-scale clusters: When a massive number of Pods need IP addresses.
    • High-density deployment: When running many Pods per node.
    • Fast startup: Reduces API calls to improve Pod startup speed.
    • IPv6 Dual-stack: Scenarios requiring both IPv4 and IPv6 addresses.
  3. What is Terway CNI Plugin

    main

    Terway is a Container Network Interface (CNI) plugin specifically designed for Alibaba Cloud VPC/ENI. It is built for Alibaba Cloud Kubernetes (ACK) and leverages Alibaba Cloud's Elastic Network Interface (ENI) technology to optimize cloud resource usage and network performance.

    Key Networking Model:

    • Each Pod receives its own dedicated network stack and IP address.
    • Intra-node communication: Pods residing on the same ECS (Elastic Compute Service) instance communicate directly.
    • Inter-node communication: Pods on different ECS instances communicate directly through VPC ENIs. This avoids the overhead of encapsulation technologies like VxLAN, resulting in higher performance.
  4. Network Policy implementation modes

    main

    Terway implements Kubernetes NetworkPolicy differently depending on the selected networking mode:

    • VPC Mode and ENI Multi-IP (Veth Policy Routing): Uses the Calico Felix component to enforce policies.
    • ENI Multi-IP (IPvlan): Uses the Cilium component to enforce policies.
  5. Key Features of Terway CNI

    main

    Terway provides several advanced networking capabilities:

    • ENI Network Mode: Assigns Elastic Network Interfaces (ENIs) to Pods to optimize resource utilization and network performance.
    • Trunking: Allows Pods to configure independent ENIs, supporting flexible Security Group and VSwitch configurations.
    • Node Pool Configuration: Supports configuring network modes (such as exclusive ENI) at the Node Pool dimension.
    • Security Policies: Supports both Kubernetes standard NetworkPolicy and traditional Alibaba Cloud Security Groups for multi-dimensional network security control.
    • High Performance: Utilizes eBPF to accelerate the protocol stack, ensuring low latency and high throughput.
    • IPv6 Support: Supports IPv4/IPv6 dual-stack networking.
    • Lingjun Support: Supports Alibaba Cloud's Lingjun intelligent computing resources.
  6. How the ENI Blacklist and Unschedulable mechanism works

    main

    Terway implements a protection mechanism to ensure HighPerformance (HP) ENIs are reserved for RDMA Pods. This involves two main components:

    1. ENI Blacklist Tag Mechanism

    Terway identifies ENIs to skip using a blacklist system:

    • Default Hardcoded Blacklist: Automatically recognizes creator=alibabacloud-erdma-controller and terway.alibabacloud.com/excluded=true.
    • ConfigMap Supplement: Users can extend the blacklist via ConfigMap.

    2. The Unschedulable State

    When an ENI matches the blacklist, Terway performs the following:

    • Marking: The ENI is retained in the Custom Resource (CR) but marked as Unschedulable.
    • Scheduling: During IP allocation, Terway skips any ENI marked Unschedulable.
    • Lifecycle Management:
      • Unschedulable ENIs are skipped during IP release/deletion processes (the card and its IPs are not deleted automatically).
      • Unschedulable ENIs are excluded from IP water level calculations and deletion quotas.
      • If a node only has Unschedulable ENIs with idle IPs, the node will report InsufficientIP.
  7. How PodNetworking works for multi-network configuration

    main

    A PodNetworking resource is a cluster-level Custom Resource (CR) used to describe a network plane configuration, including settings like vSwitch and SecurityGroup.

    Key Concepts:

    • Relationship: Pods and PodNetworking resources have a many-to-many relationship. A Pod can reference 1 or more network planes.
    • Lifecycle: PodNetworking configurations are applied only at the time of Pod creation. Modifying an existing PodNetworking resource will not affect Pods that have already been created.
    • Routing: In a multi-network setup, only one network plane can be set as the default route; other planes can be configured with specific detailed routes.
    • Limitations:
      • ACS (Alibaba Cloud Serverless) does not support custom route configurations (defaultRoute and routes).
      • Multi-network functionality currently supports Trunk ENI or Dedicated ENI, but does not support Lingjun nodes.
    apiVersion: network.alibabacloud.com/v1beta1
    kind: PodNetworking
    metadata:
      name: example
    spec:
      allocationType:
        type: Fixed
        releaseStrategy: TTL
        releaseAfter: "1h"
      securityGroupIDs:
      - sg-bpxxxx
      vSwitchOptions:
      - vsw-bpxxxx
  8. Understand the `Unschedulable` ENI lifecycle

    main

    When an ENI is marked as Unschedulable (due to blacklist tags or being a HighPerformance ENI without RDMA requirements), Terway follows a "show but don't manage" policy to ensure coexistence with other controllers:

    • IP Allocation: Skipped in assignIPFromLocalPool and assignEniPrefixWithOptions.
    • Capacity/Quota: Skipped in validateENI (no new IPs added) and countTotalIdleIPs (not counted in watermarks).
    • Reclamation/GC: The releaseUnUsedIP and handleStatus paths explicitly skip Unschedulable ENIs. Terway will never Detach, Delete, or UnAssign IPs from these cards.
    • Evacuation: Existing Pods on these ENIs are left untouched. The ENI is naturally evacuated as existing Pods exit.

    Note: If you have a HighPerformance ENI that is not managed by a controller and you want Terway to use it, you must manually unbind it from the node via the ECS Console, as Terway will not automatically claim unowned HighPerformance ENIs to avoid race conditions.

  9. Compare Terway network modes: VPC, ENI, and ENI Multi-IP

    main

    Terway supports three primary networking modes depending on your connectivity and scale requirements:

    1. VPC Mode (Overlay)

    • Mechanism: Uses an overlay network where the Pod CIDR is different from the host VPC CIDR. Connectivity is achieved via Aliyun VPC routing tables.
    • Connectivity: Pods connect to the host via veth devices. Traffic is routed through the host's network namespace using iptables and tc for load balancing and traffic control.
    • Best for: Standard overlay networking where Pod IPs are distinct from host IPs.

    2. ENI Mode (Direct VPC Access)

    • Mechanism: Binds an Aliyun Elastic Network Interface (ENI) directly to the node. Pods use this ENI to communicate.
    • Connectivity: The Pod's network interface is essentially the ENI moved into the Pod's network namespace. Pods can communicate directly with VPC and public resources.
    • Constraint: Limited by the number of ENIs supported by the specific ECS instance type.

    3. ENI Multi-IP Mode (High Density)

    • Mechanism: Leverages multiple secondary IP addresses on a single ENI to increase Pod density.
    • Implementation Options:
      • Veth Policy Routing: Uses veth pair to connect the Pod to the host. Requires policy routing on the host to ensure traffic for secondary IPs exits through the correct ENI.
      • IPvlan L3/L2: Uses Linux ipvlan (available in kernels 4.2+) to create virtual sub-interfaces from the ENI. This mode is simpler and offers better performance than veth policy routing.
    • Best for: High-density Pod deployments requiring direct VPC connectivity.
  10. Protocol between Terway and erdma-controller

    main

    For seamless coexistence, the erdma-controller and Terway follow a specific tagging and attribute protocol. Terway uses these markers to identify ENIs that should be excluded from general Pod scheduling.

    ResponsibilityTechnical Token / Attribute
    erdma-controller (Tagging)creator=alibabacloud-erdma-controller
    erdma-controller (Tagging)terway.alibabacloud.com/excluded=true
    erdma-controller (Attribute)NetworkInterfaceTrafficMode=HighPerformance
    Terway (Action)Recognizes the above tags in the default blacklist.
    Terway (Action)Skips ENIs with HighPerformance mode during assignIPFromLocalPool for non-RDMA Pods.
  11. How Terway components interact

    main

    Terway follows a standard CNI design pattern consisting of two main components: a daemon and a binary.

    • daemon: Responsible for allocating and managing network resources. It is called by the binary and interacts with Aliyun OpenAPIs to manage underlying cloud resources.
    • binary: The CNI plugin that interacts with kubelet and the daemon. It configures the network namespace for Pods.

    Lifecycle of a Pod network setup:

    1. Kubelet detects a new Pod and creates a sandbox container.
    2. Kubelet calls the terway binary via the CNI interface.
    3. terway binary requests network resources from the terway daemon.
    4. terway daemon calls Aliyun OpenAPIs to allocate resources and returns them to the binary.
    5. terway binary configures the network and connectivity within the container's network namespace.
  12. How Terway merges dynamic and default configurations

    main

    Terway uses the MergePatch mechanism (RFC 7396) via the json-patch library to merge dynamic node configurations into the default configuration.

    Key Behaviors:

    • Partial Merging: For nested objects like vswitches, Terway performs a partial merge. If a dynamic config specifies a subset of keys in a nested object, the other keys from the default config are preserved.
    • Removing Keys: To remove a key from a nested object during a merge, set its value to null in the dynamic configuration.

    Examples:

    Partial Merge Example: Default vswitches:

    {
      "vswitches": {
        "cn-hangzhou-g": ["vsw-xxx"],
        "cn-hangzhou-i": ["vsw-yyy"]
      }
    }

    Dynamic vswitches:

    {
      "vswitches": {
        "cn-hangzhou-g": ["vsw-10000"]
      }
    }

    Resulting configuration:

    {
      "vswitches": {
        "cn-hangzhou-g": ["vsw-10000"],
        "cn-hangzhou-i": ["vsw-yyy"]
      }
    }

    Removing a Key Example: To remove cn-hangzhou-i from the resulting configuration, use null:

    {
      "vswitches": {
        "cn-hangzhou-g": ["vsw-10000"],
        "cn-hangzhou-i": null
      }
    }