3

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.

Vahid Alimohamadi
  • 4,900
  • 2
  • 22
  • 37

1 Answers1

1

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.

Vahid Alimohamadi
  • 4,900
  • 2
  • 22
  • 37