简体   繁体   中英

How to forward Traffic to specific pod?

I have an app which using in memory database. I created statefulset using the Pod with let's say 3 replica. used PVC for storing the database related files.

I used a Loabalancer to expose the statefulset.

So when the traffic hits loadbalancer each time it's forwarded to different pods.

Is there anyway I can control the loadbalacing to the Pod based on some condition ( like if Client IP is X go to pod Y) ?

The very fact that you have a leader/follower topology, the ask to direct traffic to a said nome (master node) is flawed for a couple of reasons:

  1. What happens when the current leader fails over and there is a new election to select a new leader
  2. The fact that pods are ephemeral they should not have major roles to play in production, instead work with deployments and their replicas. What you are trying to achieve is an anti-pattern

In any case, if this is what you want, maybe you want to read about gateways in istio which can be found here

You can use the K8s ingress resource to solve this case. I think what you're trying to do is host-based routing. Put your pods behind different services using labels. In your case 1 - 100 behind service 1, 100 - 200 behind service 2 and so on. Then create an ingress resource to redirect incoming traffic to different services based on host or path, whatever you require. You may have to use a proxy like Nginx to get this working in a public cloud platform. The YAML manifest would be something like this:

apiVersion: extensions/v1beta1 kind: Ingress metadata: name: example-ingress annotations: kubernetes.io/ingress.class: nginx spec: rules: - host: host1.com http: paths: - path: /web1 backend: serviceName: service1 servicePort: 443 - path: /api/v1/a backend: serviceName: service2 servicePort: 80 - path: /api/v1/b backend: serviceName: service3 servicePort: 80 - host: host2.com http: paths: - path: /web2 backend: serviceName: service4 servicePort: 443 - path: /api/v2/a backend: serviceName: service5 servicePort: 80

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM