Vahid Alimohamadi
Vahid Alimohamadi

Reputation: 5868

NODE_APP_INSTANCE similar variable inside kubernetes node app

When we use PM2 in cluster mode, we can find out the instance number inside node app using process.env.NODE_APP_INSTANCE, but how we can find that inside a kubernetes cluster without pm2. I'm looking for similar like find replica instance number or etc.

Imagine a node app with 2 or more replicas and we need to run node-cron scheduler only inside one of pods.

Upvotes: 3

Views: 414

Answers (1)

Vahid Alimohamadi
Vahid Alimohamadi

Reputation: 5868

I found that when use Statefulset instead of the Deployment then it's possible to inject the determinable POD name as an environment variable.

  ...
  containers:
     ...
     env:
     - name: "POD_NAME"
       valueFrom:
         fieldRef:
           fieldPath: metadata.name

And then POD_NAME variable has the value like this: pod-0 or pod-1 and so on. So we can find out the instance number with that ordinal number.

Upvotes: 1

Related Questions