Kubernetes different container args depending on number of pods in replica set

Viewed 2324

I want to scale an application with workers.
There could be 1 worker or 100, and I want to scale them seamlessly.
The idea is using replica set. However due to domain-specific reasons, the appropriate way to scale them is for each worker to know its: ID and the total number of workers.

For example, in case I have 3 workers, I'd have this:

id:0, num_workers:3
id:1, num_workers:3
id:2, num_workers:3

Is there a way of using kubernetes to do so?
I pass this information in command line arguments to the app, and I assume it would be fine having it in environment variables too.

It's ok on size changes for all workers to be killed and new ones spawned.

2 Answers

In the end, I've tried something different. I've used kubernetes API to get the number of running pods with the same label. This is python code utilizing kubernetes python client.

import socket
from kubernetes import client
from kubernetes import config

config.load_incluster_config()
v1 = client.CoreV1Api()
with open(
    '/var/run/secrets/kubernetes.io/serviceaccount/namespace',
    'r'
) as f:
    namespace = f.readline()
workers = []
for pod in v1.list_namespaced_pod(
    namespace,
    watch=False,
    label_selector="app=worker"
).items:
    workers.append(pod.metadata.name)
workers.sort()
num_workers = len(workers)
worker_id = workers.index(socket.gethostname())
Related