1

I am trying to run a .NET Core console app on Kubernetes which should be kept alive with a Console.ReadLine.

Docker image is being run with a kubectl apply -f ....yaml command. Is there a way to run this command sort of interactively, forcing it now to close?

I did not find anything useful with the kubectl apply --help command.

If there is no way of doing this, what would be the best alternative? Running a CMD on the deployment yaml? I would rather not do an infite loop. There must be a better solution

Not that I am very proud of the Console.WriteLine way of doing it..

UPDATE:

Until I find a better solution, I implemented the following solution

Does it make sense to run a c# worker service in docker?

which is an infinite loop. Tested it and works on Kubernetes.

Sugafree
  • 631
  • 2
  • 14
  • 30
  • You can't usually interact with the standard input stream of a Kubernetes pod. What does "keep alive" mean here; why does the process exit, and why don't you want it to? – David Maze Nov 06 '20 at 14:51
  • it is just an email microservice listening to Redis for incoming messages to be sent out – Sugafree Nov 06 '20 at 14:57
  • The reason why it has to run continiously is because it uses Redis PubSub to listen to incoming messages and the code exits as soon as it finished running. This app only connects to redis and subscribes to a specific channel and this would not take more than a second probably, meaning it exits straight after that – Sugafree Nov 06 '20 at 15:05
  • 1
    It seems like your application code needs to sit in a loop receiving messages: if it sends a Redis `SUBSCRIBE` command and then immediately exits (or sits, waiting for user input, without contacting Redis) then you'll never receive a message. You might try ensuring this setup works with a local consumer, producer, and Redis (no Kubernetes, possibly no containers at all) before packaging it up. – David Maze Nov 06 '20 at 15:11
  • I have tested it without Docker and Kubernetes. It works perfectly. The console app has a Console.WriteLine as the last line of code, preveting it from exiting, but in Kubernetes, it just ignores this line and exits anyway. – Sugafree Nov 06 '20 at 15:24
  • If the last thing the program does is wait for the user to press a key, it is not receiving messages from Redis. I'd focus on fixing that over making the process "not exit". – David Maze Nov 06 '20 at 15:32
  • David I am not sure in this case which thread does what, but this solution has been tested and worked (running for hours and sending out emails as soon as it came through redis). – Sugafree Nov 06 '20 at 15:37
  • I think this depends on your c# version. This is very easy to create a daemon process that stays alive in dotnet core (non-OS specific), and pre-core, you would design this as a Windows Service instead of a command line utility. – ps2goat Nov 06 '20 at 15:58
  • @Sugafree How did you solve this? Having same issue. – Anonymous Creator Jan 02 '22 at 10:33

0 Answers0