2

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) ?

Karthik
  • 744
  • 2
  • 7
  • 23

2 Answers2

3

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

Raunak Jhawar
  • 1,541
  • 1
  • 12
  • 21
  • 1
    I dont have any Cluster configuration done on my inmeomory database or app. There is no master/slave config enabled. In my staefulset I will store my application info about say 1 to 100 in POD 1, info about 100=200 in POD2, 200-300 POD3. In case any POD goes down K8s will reschedule the POD. If need increased I will create one more pod in statefulset. Concern is while retrieving data it should read from corresponsding pod. that is why I asked how to forward traffic based on some condition – Karthik Mar 04 '19 at 09:57
  • Ok, have you considered building your logic around labels in k8s. Given this a case of static routing, you may very well be off with labels. You may also want to consider reading about Network Policies to create new rules for label based routing. – Raunak Jhawar Mar 04 '19 at 09:58
1

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

rohanmehto2
  • 910
  • 1
  • 7
  • 19