All Products
Search
Document Center

Alibaba Cloud Service Mesh:Expose a localhost application to other pods

Last Updated:Aug 28, 2026

Configure a Sidecar resource so that other pods in the cluster can use a Service to access an application that listens on localhost.

Why other pods cannot access a localhost application

localhost is a loopback address that is reachable only from within the same pod. When an application deployed in a cluster listens on localhost, other pods in the cluster cannot access it, even if you expose the application's service port through a Service.

The following examples show how applications written in different languages listen on localhost:

  • Golang: net.Listen("tcp", "localhost:8080")

  • Node.js: http.createServer().listen(8080, "localhost")

  • Python: socket.socket().bind(("localhost", 8083))

Choose an option

Two options are available for exposing your application. Choose one based on whether you can modify the application code:

  • Option 1 — Change the network address that the application listens on. Choose this option if you can modify the application code.

  • Option 2 — Use Service Mesh (ASM) to expose an application that listens on localhost. Choose this option if you do not want to modify the application code.

Option 1: Change the network address that the application listens on

To expose your application, modify the application code to listen on 0.0.0.0 instead of localhost.

Option 2: Use ASM to expose an application that listens on localhost

Create a Sidecar resource in the ASM console. The Sidecar resource intercepts the inbound requests to the pods that you select and forwards the requests to your application on localhost.

Follow these steps:

  1. Log on to the ASM console. In the left-side navigation pane, choose Service Mesh > Mesh Management.

  2. On the Mesh Management page, click the name of the ASM instance. In the left-side navigation pane, choose Traffic Management Center > Sidecar Traffic Configuration. On the page that appears, click Create from YAML.

  3. On the Create page, select a Namespaces and any Scenario Template, configure the following YAML, and then click Create.

    apiVersion: networking.istio.io/v1beta1
    kind: Sidecar
    metadata:
      name: localhost-access
      namespace: {namespace}
    spec:
      ingress:
        - defaultEndpoint: '127.0.0.1:{container_port}'
          port:
            name: tcp
            number: {port}
            protocol: TCP
      workloadSelector:
        labels:
          {key}: {value}

    Replace the following parameters with your actual values:

    Parameter

    Description

    {namespace}

    The namespace in which the application is deployed.

    {container_port}

    The container port on which the application listens on localhost.

    {port}

    The Service port of the application.

    {key}: {value}

    The label that selects the pods of the application.

Note

The preceding configuration applies only to the pods that carry the label defined under workloadSelector, and it exposes the single TCP port defined under ingress.