Docker Community Forums

Share and learn in the Docker community.

.Net Core/Linux Run Hosted Service on only one of the same running images


(Jradke) #1

I have a .NET Core docker application we run in linux. This consists of several web api’s and one hosted service created inheriting from IHostedService.

I want to set everythiing up so I can run multiple containers with my image to handle surges in our web api traffic - BUT, I only want one of the launched containers to run the hosted service (this is a timed service that checks every x minutes for new things to send out to customers).

Is there any way from within my c# application, that I can do this?

I was originally thinking that I could post an entry to a table in our database under the application, with the System.environment.MachineName to show that machine is handling the hosted service. As new containers are launched I could look at this table, see if anything is already set to handle the hosted service, and if not, update the table and set a flag in the container to ‘activate’ the hosted service if there is see if it is to be handled by the current container.

As a container is shutdown, I am hoping I could then find a way to check the closing system.environment.machine name to see if it is being used to for the hosted service, and if it is clear out that it is being used to run the service that way the next time the Hosted Service hits the timer, it would check to see who is responsible for running the code and set it up again.

But, I think this could have lots of issues.

Can anyone think of a better way, short of splitting the application into multiple separate applications?

Thanks,

Jim


(Jradke) #2

I forgot to mention, that we are running in a Rancher/Kubernetes environment, if that helps!