I'm wondering if it's possible to assign different selectors for different ports in a Kubernetes Service. I know that Kubernetes allows you to specify multiple ports in the Service spec, but can I route traffic based on both the port and a unique selector? My goal is to use a single Network Load Balancer (NLB) to manage traffic for two proxies, with the routing determined by the port number and specific selectors without adding any extra layers like a sidecar. Any insights?
5 Answers
Directly answering your question: no, different selectors for different ports aren't possible on a single service. However, consider using an ingress controller, like ingress-nginx, which can manage port-based routing more effectively. This could give you that flexibility you're missing.
Short answer: no, you can't use different selectors for different ports in a single Kubernetes Service. However, you can create multiple services that share the same selector, each specifying different ports. That might be the best approach for your situation.
While you can't set different selectors per port, there's a neat trick with targetPorts! You can run two deployments with overlapping labels like app=foo and use targetPorts in your service spec to direct traffic. Just keep in mind this method isn't very common, so testing is a must before going live.
If you're using Cilium as your CNI, you could potentially set up multiple services with a shared load balancer IP. By annotating the services properly, you can have different ports redirecting to distinct proxies while maintaining the same IP. Just remember about the annotations for cross-namespace use!
Honestly, the selector in Kubernetes Services applies to the entire service, not on a port-by-port basis. For complex routing scenarios, consider leveraging an Ingress controller or a service mesh. They provide more advanced routing capabilities and can simplify your traffic management without adding too much complexity.
Totally agree, using service meshes can really streamline things.
Yeah, that sounds like a straightforward route!