Find a way to route specific port to specific pod

Hello,
I have a load balancer deploying in GKE.
I want to rout 443 port to my Nginx pod, then other UDP port route to my other pod. Just like following figure. Pod 1 is Nginx. Pod 2 is other pod that receive UDP.


But I can’t find a way to use two selectors in one service.
Is there a way that satisfy my requirement?

Thank you.

You can’t use two selectors on a service, but there’s a trick you can do to fake this.

  • Set the selector to select both sets of pods.
  • Each pod exposes only the ports it handles.
  • Give all ports a name (e.g. “http” vs “udp”).
  • In your Service, declare both http and udp ports, and set the targetPort to the NAMEs of the ports.

Endpoint selection should remove pods that don’t have the named ports, leaving you with two sets of pods in the same selector.

Sir, I tried your suggestion.
But didn’t work well.
I can connect to the nginx pod but not UDP pod.
Here is my code:

spec:
  type: LoadBalancer
  ports:
    ## to nginx
    - name: https
      port: 443
      targetPort: https
    ## to freeswitch
    - name: udp-18874
      port: 18874
      protocol: UDP
      targetPort: 18874

In nginx pod:

          ports:
          - containerPort: 443
            name: https

In freeswitch pod:

          ports:
          - containerPort: 18874
            name: udp-18874

They both use the same labels.
I use netcat to verify UDP connection.
But netcat can’t connect to freeswitch pod.

Is there anything I did wrong?

Use names for all ports. Your UDP is still numeric.

I see, I change it to:

    ## to nginx
    - name: https
      port: 443
      targetPort: https
    ## to freeswitch
    - name: udp-18874
      port: 18874
      protocol: UDP
      targetPort: udp-18874

But I still can’t connect to UDP pod.
In the same pod, I can connect UDP using 0.0.0.0.
But can’t use load balancer’s IP to establish UDP connection from the outside.

Ahh, I don’t think GKE has multiple-protocol LB support implemented yet. :frowning:

Ok, still thank you for your help.