简体   繁体   中英

How do I send commands to an specific pod deployed on Kubernetes?

I have a microservice deployed in a container with 2 replicas. So I have 2 pods. This microservice receive commands from another service through the Azure Service Bus. My issue is that i want to retrieve the commands with one of the two pods. I've tryed to enable Sessions and use SessionId but I still receive commands in the wrong pod. Is there any way to make this?

Here i leave how do i configure the bus (using masstransit library)

var bus = Bus.Factory.CreateUsingAzureServiceBus(sbc =>
        {
            var host = sbc.Host(_configuration["myConnectionString"], h => { });
            sbc.ConfigureSend(x => x.UseExecute(context =>
            {
                var sessionId = EnvConstants.SessionId;
                context.SetSessionId(sessionId);
            }));
            sbc.ReceiveEndpoint(host, _configuration["myQueueName"], e =>
            {
                e.RequiresSession = true;
                e.Instance(consumer);
                e.UseContextFilter(c =>
        {
               //I tryed this filter but sends my command to the skipped queue. 
            var sameSessionId = c.SessionId() == EnvConstants.SessionId;
            return Task.FromResult(sameSessionId);
        });
            });
        });

I don't believe its best practice to differentiate replicas in any way, as they are meant to be stateless with no specific identifier at the pod level, so you can't tell them apart in a guaranteed way.

For scenarios such as yours, you should opt for Stateful Sets , which provides an ordinal identifier to the pods that you can use to tell them apart (and guarantee it). And depending on the identifier, you could have special queues (or subscriptions to a single topic) to address a specific pod.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM