我正在尝试在应用程序上处理kuberenetes的Prestop
lifecycle:
postStart:
tcpSocket:
port: 13000
preStop:
tcpSocket:
port: 13001
事件
FailedPrestophook Pod/podname-795764DB56-9Q9PG无法运行
处理程序:无效处理程序:
& lifecyclehandler {exec:nil,httpget:nil,tcpsocket:& tcpsocketAction {port:{0
13001},主机:,},}
我尝试了另一种解决方案来开始使用本机函数
[DllImport("Kernel32")]
private static extern bool SetConsoleCtrlHandler(SetConsoleCtrlEventHandler handler, bool add);
,但我只能在Windows环境上运行它,但是一旦我去Linux容器,我会收到错误
未经治疗的例外。 System.DllNotFoundException:无法加载
共享库“ kernel32”或其依赖项之一。为了帮助
诊断加载问题,考虑设置LD_DEBUG环境
变量:libkernel32:无法打开共享对象文件:没有这样的文件或
目录
请建议如果有其他解决方案可以解决Linux Contianer环境上的Consol应用程序的关闭。
I am trying to handle prestop event of kuberenetes on my application but I didn't receive any events on my .net core console application
lifecycle:
postStart:
tcpSocket:
port: 13000
preStop:
tcpSocket:
port: 13001
I am receiving on event log
FailedPreStopHook pod/podname-795764db56-9q9pg Cannot run
handler: invalid handler:
&LifecycleHandler{Exec:nil,HTTPGet:nil,TCPSocket:&TCPSocketAction{Port:{0
13001 },Host:,},}
I have tried another solution to start working with native functions like
[DllImport("Kernel32")]
private static extern bool SetConsoleCtrlHandler(SetConsoleCtrlEventHandler handler, bool add);
But I am able only to run it on windows environment but once I go to linux container I receive error
Unhandled exception. System.DllNotFoundException: Unable to load
shared library 'Kernel32' or one of its dependencies. In order to help
diagnose loading problems, consider setting the LD_DEBUG environment
variable: libKernel32: cannot open shared object file: No such file or
directory
Please advice if there is any other solution to this issue to handle closing greacefully for consol application on linux contianer environment.
发布评论
评论(1)
consolectrleventhandler
仅在Windows下可用。您应该在Linux容器和Windows下使用system.runtime.interopservices.posixSignAlregistration
:您必须保护事件处理程序免受垃圾收集器的收集,请参见 this Anders 。
使用.NET Core 6.0引入posixSignAlregsistration。
对于较旧的版本,
appdomain.currentdomain.processexit
可用,并在 sigterm 和console.cancelkeypress
上升高。强>。posixSignalRegistration
可以取消 sigterm ,因此您可以清理事件处理程序的外部外部。使用ProcessExit
您必须可以清理内部事件处理程序:结果:
ConsoleCtrlEventHandler
is only available under windows. You should useSystem.Runtime.InteropServices.PosixSignalRegistration
in a Linux container and under Windows:You have to protect the event handler from being collected by the garbage collector, see this Answer.
PosixSignalRegistration was introduced with .NET core 6.0.
For older versions,
AppDomain.CurrentDomain.ProcessExit
is availavle and is raised on SIGTERM andConsole.CancelKeyPress
is raised on SIGINT.PosixSignalRegistration
can cancel SIGTERM so you can clean up outside the event handler. WithProcessExit
you have to can clean up inside the event handler:The results: