How can I make a Google Kubernetes Engine Load Balancer serve different sets of pods?

1/21/2018

I have am using Google Kubernetes Engine 1.8.6 and have a load balancer configured as below:

apiVersion: v1
kind: Service
metadata:
  name: my-load-balancer
spec:
  ports:
  - port: 19222
    name: my-test-port
    protocol: TCP
    targetPort: 19222
  - port: 9222
    name: my-prod-port
    protocol: TCP
    targetPort: 9222
  selector:
    app: test-app-stateful-set
  type: LoadBalancer

This allows someone connecting to the load balancer's external IP to be routed to my test-app-stateful-set when they use port 19222. However I would like connections to port 9222 from the same external IP sent to my prod-app-stateful-set instead. How can I configure my service to do this?

-- Dan
google-kubernetes-engine
kubernetes

1 Answer

1/22/2018

You can't do this directly, because a Service has a single NodeSelector that is valid for all its ports.

One option you have is to run a third pod that acts as a proxy. It receives connections on both ports and forwards the traffic to your backend a based on the port the incoming traffic is coming through.

You could use nginx for this proxy pod.

A sample configuration for your nginx could be the following:

stream {
  server { 
    listen 19222;
    proxy_pass <test-service-name>:19222;
  }
  server { 
    listen 9222;
    proxy_pass <prod-service-name>:9222;
  }
}

Of course, your load balancer service has to be adjusted as well in order to make the selector match your new nginx pod and you need to create two different services for your production and test pods

-- whites11
Source: StackOverflow