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.

2
  • did you find any answer? Commented Dec 15, 2022 at 23:26
  • 1
    @moeinrahimi Yes, I've added the answer. Commented Dec 20, 2022 at 11:59

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.