Distribute ssh connections made by Django App (multi-threaded python function) among n number of Kubernetes Replicas

2/27/2018

My Django App makes SSH connections to n number of machines (using a multithreaded python function). When replica=n is set in kubernetes deployment.yaml file then I want my app to distribute the connections among the n replicas.

I mean 1 replica should connect to k number machines, another to next k number of machines and so on. When all the replicas are done then it should take the connections in cyclic fashion i.e. next k connections to first machine and another next k to other machine.

I tried with 2 replicas but all the connections are getting established by both the pods (replicas).

I want those connections to be distributed among the pods. How can I achieve this?

-- Priyanka Gupta
django
kubernetes
multithreading
python
ssh

1 Answer

2/27/2018

If the replica count will not change after launch you can get this info from kube api and then have your pods run from statefulset so they have sequential ids, then use the good old trick of mod N = 0..N-1 to divide work from the list in an even fashion and you should be fine.

-- Radek 'Goblin' Pieczonka
Source: StackOverflow