Can I Use Different Selectors for Different Ports in a Kubernetes Service?

0
5
Asked By GamerDude42 On

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

Answered By KubeGuru On

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.

Answered By TechieTom123 On

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.

ReplyGuy99 -

Yeah, that sounds like a straightforward route!

Answered By CloudWizard88 On

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.

Answered By CNI_Dude On

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!

Answered By K8sNinja On

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.

HelpfulHank -

Totally agree, using service meshes can really streamline things.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.