Help Center> Cloud Container Engine> Best Practices> Networking> Retaining the Original IP Address of a Pod
Updated on 2024-07-04 GMT+08:00

Retaining the Original IP Address of a Pod

SNAT Overview

In a CCE cluster that uses a VPC network, to enable pods to communicate with external systems, the system automatically translates the source IP addresses of the pods into the IP addresses of the nodes that are running them. This allows pods to communicate with external systems using the node IP addresses. This process is known as pod IP address masquerading or Source Network Address Translation (SNAT).

Prerequisites

You have a cluster that uses the VPC network and whose version is v1.23.14-r0, v1.25.9-r0, v1.27.6-r0, v1.28.4-r0, or later.

Default Non-Masqueraded CIDR Block Settings in a CCE Cluster

By default, CCE uses the following well-known private CIDR blocks as non-masqueraded CIDRs in each cluster:

  • 10.0.0.0/8
  • 172.16.0.0/12
  • 192.168.0.0/16

Additionally, in a CCE cluster that uses a secondary VPC CIDR block, adding or resetting a node will automatically include the secondary CIDR block in the non-masqueraded CIDR blocks.

This means that when a pod initiates communication from these CIDR blocks with external resources, the source IP address of the pod data packet remains unchanged, and it will not be replaced with the IP address of the node running the pod.

Scenarios Where the Default Non-Masqueraded CIDR Blocks Cannot Meet Requirements

The default non-masqueraded CIDR block settings in CCE clusters apply to typical scenarios, but in certain specific scenarios, these default settings may not be sufficient to meet user requirements. The following shows typical examples:

  • Cross-node access to pods in a cluster

    When a node in a Kubernetes cluster needs to access a pod on another node, the response data packet sent from the pod is automatically subjected to SNAT. This changes the source IP address from the pod IP address to the IP address of the node that runs the pod. However, this automatic IP address translation can sometimes lead to communication issues, making cross-node access impossible.

    To enable a node to access pods on other nodes, you can add the CIDR block of the subnet where the node is located to the nonMasqueradeCIDRs parameter. This will prevent SNAT and allow the original IP addresses of pods in this node to be retained.

  • Access from other resources in the same VPC as a cluster to pods in the cluster

    In certain scenarios, it may be necessary to access the original IP addresses of pods on different nodes in a CCE cluster directly from other resources (such as ECSs) in the same VPC as the cluster. However, with SNAT enabled by default, the source IP addresses of the pod data packets are replaced with the IP addresses of the nodes that run these pods when the data packets pass through the nodes. This makes it difficult for these resources to access pods directly.

    To enable direct access from resources in the same VPC as the cluster to pods, you can add the CIDR blocks of the subnets where these resources are located to the nonMasqueradeCIDRs parameter. This will prevent SNAT and ensure that the source IP addresses of the data packets remain the same as the original IP addresses of pods.

Precautions

If you have set up a security group or ACL for a cloud service that only allows access from IP addresses of nodes running pods, there is no need to perform pod IP address masquerading and configure the nonMasqueradeCIDRs parameter.

The default behavior of pod IP address masquerading (SNAT) is usually sufficient. However, if you need to retain the original IP addresses of pods in specific scenarios, you can configure the nonMasqueradeCIDRs parameter.

Before doing so, make sure to evaluate your application scenario and understand the potential risks of improper configuration, because it may block access within clusters. If you are unsure whether to configure this parameter, it is recommended to keep the default settings and adjust the configuration later once the requirement is clarified.

Procedure

To reserve the source IP address of a pod when the pod accesses a CIDR block, you can configure nonMasqueradeCIDRs to specify the CIDR blocks that do not need to be masqueraded.

  1. Log in to the CCE console and click the name of the target cluster to access the cluster console.
  2. In the navigation pane, choose Settings and click the Network tab.
  3. Modify the parameters under Retain the non-spoofed CIDR block of the original pod IP address. Make sure the parameter configuration complies with the following rules:

    • Each CIDR block must be in CIDR format and must be a valid IPv4 CIDR block.

      Example of a correct CIDR block: 192.168.1.0/24

      Example of an incorrect CIDR block: 192.168.1.1/24 (incompliant with the CIDR format)

    • The CIDR blocks you configured do not overlap with each other.

      Example of correct CIDR blocks: 192.168.1.0/24 and 192.168.2.0/24

      Example of incorrect CIDR blocks: 192.168.1.0/24 and 192.168.1.128/25 (The two CIDR blocks overlap.)

    • The nonMasqueradeCIDRs parameter must contain all destination CIDR blocks that you want them to use the original pod IP addresses for communications.

  4. After the modification, click Confirm configuration. The setting takes effect within 1 minute.