Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to make clientid dynamic in StatefulSets replicas: 3 in kubernetes #335

Closed
alifpay opened this issue Jan 7, 2021 · 3 comments
Closed

Comments

@alifpay
Copy link

alifpay commented Jan 7, 2021

Hi,
I have issue clientID in Kubernetes replicas.
How to make clientID in dynamic for stan connection if i have replicas more than 1 in Kubernetes?


sc, err := stan.Connect(clusterid, clientID, stan.NatsConn(nc))

durableOpt := stan.DurableName("servicename")

_, err = sc.QueueSubscribe("pay", "groupName", fnPay, durableOpt)

DurableName must be different or same for each connection in QueueSubscribe ?

@kozlovic
Copy link
Member

kozlovic commented Jan 7, 2021

DurableName must be different or same for each connection in QueueSubscribe ?

My answer would be no, but not sure what you are trying to do. Here is some info on durables and queue (durables or not):
https://docs.nats.io/nats-streaming-concepts/channels/subscriptions/durable
https://docs.nats.io/nats-streaming-concepts/channels/subscriptions/queue-group

For queue groups, the clientID does not have to be the same when you restart an application. Since a single connection with the same clientID can exist at one point it time, it would prevent several applications to participate in the same group.
So you could use a generated unique ID for this application.

@wallyqs
Copy link
Member

wallyqs commented Jan 7, 2021

Hi @alifpay I might be misunderstanding but what you can do is to use the POD_NAME environment variable from K8S to get a predictable name for the clientID.

sc, err := stan.Connect(clusterid, os.Getenv("POD_NAME"), stan.NatsConn(nc))

This will work better if you use a StatefulSet instead of a Deployment, so that when your pod gets restarted it will use the same name.

@alifpay
Copy link
Author

alifpay commented Jan 8, 2021

thank you! very much

@alifpay alifpay closed this as completed Jan 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants