Help Center/ Application Service Mesh/ Best Practices/ Reserving Source IP Address for Gateway Access
Updated on 2024-09-24 GMT+08:00

Reserving Source IP Address for Gateway Access

If node affinity is configured, the ELB address cannot be used to access workloads in some scenarios. For details, see Why the ELB Address Cannot Be Used to Access Workloads in a Cluster?

Scenario

When a service is accessed through a gateway, the source IP address displayed in the target container is not the source IP address of the client by default. To retain the source IP address, perform the operations described in this section.

Configuration

Log in to the CCE console. On the Networking page, select the istio-system namespace, update the gateway service associated with the service, and change Service Affinity to Node-level. The prerequisite is that the function of obtaining the client IP address of the ELB has been enabled (this function is enabled by default).

externalTrafficPolicy: indicates whether the Service wants to route external traffic to the local nodes or cluster-wide endpoints. Two options are available: Cluster (default) and Local. Cluster hides the client IP address, which may cause the second hop to another node, but has a good overall load distribution. Local retains the source IP address of the client and avoids the second hop of LoadBalancer and NodePort services. However, there is a potential risk of unbalanced traffic transmission.

Authentication Method

The x-forward-for field of the httpbin image displays the source IP address. The httpbin service is an HTTP Request & Response Service that can send requests to the httpbin image. The httpbin image will return the requests based on the specified rules. You can search for the httpbin image in SWR. Before using the httpbin image for verification, ensure that the mesh function has been enabled for the cluster.

  1. Log in to the ASM console and click the name of an available test mesh to go to its details page.
  2. In the navigation pane, choose Mesh Configuration. Then, view the associated cluster.

  3. Click the cluster name to go to the cluster console. In the navigation pane, choose Workloads. On the Deployments tab, click Create Workload in the upper right corner.
  4. Configure workload information.

    Basic Information

    • Workload Type: Select Deployment.
    • Workload Name: Enter httpbin.
    • Namespace: Select the namespace of the workload. The default value is default.
    • Retain the default values of other parameters.

    Container Configuration

    • Basic Information
      • Container Name: Customize a name.
      • Image Name: Click Select Image, search for the httpbin image, select the image, and click OK.
      • Image Tag: Select an image tag.
      • Retain the default values of other parameters.

    Service Settings

    A Service solves the pod access problems. With a fixed IP address, a Service forwards access traffic to pods and performs load balancing for these pods.

    Click + under the service configuration parameter to go to the Create Service page.
    • Service Name: Enter the workload name.
    • Service Type: Select ClusterIP.
    • Port parameters:
      • Protocol: Select TCP.
      • Container Port: 80 (Use the actual port number.)
      • Service Port: 80 (Use the actual port number.)

  5. Click OK in the lower right corner.
  6. Click Create Workload in the lower right corner.
  7. On the cluster console, click Networking in the navigation pane. The created httpbin service is displayed in the service list.

  8. Return to the ASM console and click Service Management. The Configuration Diagnosis Result of the httpbin service is Abnormal.

  9. Click Fix. In Configuration Diagnosis Result, rectify the fault by following the solution.

    Take "The Service port name complies with the Istio specifications" as an example. Select http and click Auto Fix.

  10. In the navigation pane, choose Gateway Management. Then, click Add Gateway in the upper right corner. Configure the parameters in the window that slides out from the right.
    Configuration Information
    • Gateway Name: Enter httpbin.
    • Cluster: Select the cluster associated with the mesh.
    • Load Balancer: Select Public network and select a load balancer.
    • External Protocol: Select HTTP.
    • External Port: Specify a port.
    • External Access Address: Enter the public IP address of the load balancer selected for the Load Balancer.

    • Click + under Routing. In the Add Route dialog box displayed, add a route.
      • URL: Select Full match and enter a mapping.
      • Namespace: Select the namespace to which the service belongs.
      • Target Service: Retain the default value.

    When the configuration is complete, click OK.

  11. Click OK.
  12. Click Service Management on the left. You can view the external access address of the created route in the Access Address column.

  13. Add ?show_env=1 to the mapped external access address, for example, http://xxx.xxx.xxx:80/get?show_env=1. You can view the value of the x-forward-for field. The IP address obtained by the gateway is the container IP address.
    ...
    ...
    ...
    x-forward-for: xxxx
  14. Return to the cluster console, click Networking in the navigation pane, and modify the configuration of the gateway service associated with the service as follows:

    Select the istio-system namespace.

    Click More > Update in the Operation column. On the Update Service page displayed, change Service Affinity to Node-level, select I have read Notes on Using Load Balancers, and click OK.

  15. Return and refresh the external IP address accessed in 13. If the IP address obtained by the gateway in the x-forward-for field is the source IP address of the local host, the verification is complete.
    ...
    ...
    ...
    x-forward-for: xxxx