You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否用同一K8s LB服务(公网IP)的不同端口访问9个不同Pod?

Can I use a single K8s LoadBalancer Service with different ports to access Pods with unique selectors?

Absolutely yes, this is totally doable! You can use one LoadBalancer Service (with a single public IP) and map different ports to your 9 distinct Pods—even though each Pod has its own unique selector. Here are two practical methods to make this work:

1. Selector-less LoadBalancer + Manual Endpoints (Direct Approach)

Since your Pods don't share common labels, a regular Service with a selector won't work (it would route all ports to the same set of Pods). Instead, create a LoadBalancer Service without a built-in selector, then manually define Endpoints to map each port to the right Pod.

How to implement:

First, create the LoadBalancer Service. Notice there's no selector field here—we'll handle backend mapping manually:

apiVersion: v1
kind: Service
metadata:
  name: multi-app-lb
spec:
  type: LoadBalancer
  ports:
    - name: app1
      port: 80          # External port on the LB
      targetPort: 80    # Port your first Pod is listening on
      protocol: TCP
    - name: app2
      port: 8080        # Second external port
      targetPort: 8080  # Port your second Pod uses
      protocol: TCP
    # Add 7 more port entries for your remaining Pods—give each a unique name!

Next, create an Endpoints resource for each Pod. Critical: the Endpoints name must match the Service name so Kubernetes links them together.

For your first Pod (replace <POD1_IP> with the actual IP of the Pod):

apiVersion: v1
kind: Endpoints
metadata:
  name: multi-app-lb  # Must match the Service name
subsets:
  - addresses:
      - ip: <POD1_IP>
    ports:
      - name: app1  # Must match the port name in the Service
        port: 80
        protocol: TCP

For your second Pod:

apiVersion: v1
kind: Endpoints
metadata:
  name: multi-app-lb
subsets:
  - addresses:
      - ip: <POD2_IP>
    ports:
      - name: app2
        port: 8080
        protocol: TCP

Heads up: If a Pod restarts, its IP will change, so you'll need to update the corresponding Endpoints. If you want to avoid manual updates, you can write a small script or use a custom operator to watch Pod events and sync Endpoints automatically.

2. Ingress Controller with TCP Port Mapping (Scalable Option)

If you're already running an Ingress Controller (like NGINX), this is a cleaner long-term solution. You can configure it to expose multiple TCP ports through the same LoadBalancer IP, and it will handle backend routing automatically using ClusterIP Services for each Pod.

Quick setup for NGINX Ingress:

  1. Create a ClusterIP Service for each Pod—each with its own selector pointing to the respective Pod. For example:
apiVersion: v1
kind: Service
metadata:
  name: app1-service
spec:
  type: ClusterIP
  selector:
    app: app1  # Unique selector for your first Pod
  ports:
    - port: 80
      targetPort: 80
  1. Update the NGINX TCP ConfigMap to map external ports to your ClusterIP Services:
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-ingress-tcp
  namespace: ingress-nginx  # Match your Ingress Controller's namespace
data:
  80: "default/app1-service:80"  # Format: <external-port>: <namespace>/<service-name>:<target-port>
  8080: "default/app2-service:8080"
  # Add entries for all 9 of your Services
  1. Modify the NGINX Ingress Controller Service to expose these ports:
    Add the following to the ports section of the LoadBalancer Service for the Ingress Controller:
ports:
  - name: tcp-80
    port: 80
    targetPort: 80
    protocol: TCP
  - name: tcp-8080
    port: 8080
    targetPort: 8080
    protocol: TCP
  # Add entries for all your external ports

Either method will let you use the same LoadBalancer IP to access each Pod via a unique port. Pick the one that fits your existing setup best!

内容的提问来源于stack exchange,提问作者Saurabh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:01:45