我无法从.NET Core Consol应用程序中处理Kubernets的申请生命周期活动

发布于 2025-01-31 22:19:39 字数 849 浏览 2 评论 0 原文

我正在尝试在应用程序上处理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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云淡风轻 2025-02-07 22:19:39

consolectrleventhandler 仅在Windows下可用。您应该在Linux容器和Windows下使用 system.runtime.interopservices.posixSignAlregistration

var isCancelRequested = false;
var signal = "none";
using (var protectSIGINTfromGC = PosixSignalRegistration.Create(PosixSignal.SIGINT, (signalContext) =>
{
    signal = "SIGINT";
    signalContext.Cancel = true;
    isCancelRequested = true;
}))
{
    /* Inovke your Root command handler here */
}

您必须保护事件处理程序免受垃圾收集器的收集,请参见 this Anders

使用.NET Core 6.0引入posixSignAlregsistration。
对于较旧的版本, appdomain.currentdomain.processexit 可用,并在 sigterm console.cancelkeypress 上升高。强>。

posixSignalRegistration 可以取消 sigterm ,因此您可以清理事件处理程序的外部外部。使用 ProcessExit 您必须可以清理内部事件处理程序:

static void Main(string[] args)
{
    Console.WriteLine();
    Console.WriteLine($"Process ID = {Environment.ProcessId}");
    var cancel = false;
    Console.CancelKeyPress += new ConsoleCancelEventHandler((s, e) =>
    {
        Console.WriteLine($"CancelKeyPress");
        e.Cancel = true; // not terminate immediately
        cancel = true;
    });
    AppDomain.CurrentDomain.ProcessExit += new EventHandler((s, e) =>
    {
        Console.WriteLine($"ProcessExit");
        Console.WriteLine($"Process {Environment.ProcessId} exited gracefully (SIGTERM).");
    });
    do
    {
        Thread.Sleep(10);
    } while (!cancel);
    Console.WriteLine($"Process {Environment.ProcessId} exited gracefully (SIGINT).");
}

结果:

$ dotnet run &
Process ID = 4113
$ kill -s INT 4113
CancelKeyPress
Process 4113 exited gracefully (SIGINT).
$ dotnet run &
Process ID = 4395
$ kill -s TERM 4395
ProcessExit
Process 4395 exited gracefully (SIGTERM).

ConsoleCtrlEventHandler is only available under windows. You should use System.Runtime.InteropServices.PosixSignalRegistration in a Linux container and under Windows:

var isCancelRequested = false;
var signal = "none";
using (var protectSIGINTfromGC = PosixSignalRegistration.Create(PosixSignal.SIGINT, (signalContext) =>
{
    signal = "SIGINT";
    signalContext.Cancel = true;
    isCancelRequested = true;
}))
{
    /* Inovke your Root command handler here */
}

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 and Console.CancelKeyPress is raised on SIGINT.

PosixSignalRegistration can cancel SIGTERM so you can clean up outside the event handler. With ProcessExit you have to can clean up inside the event handler:

static void Main(string[] args)
{
    Console.WriteLine();
    Console.WriteLine(
quot;Process ID = {Environment.ProcessId}");
    var cancel = false;
    Console.CancelKeyPress += new ConsoleCancelEventHandler((s, e) =>
    {
        Console.WriteLine(
quot;CancelKeyPress");
        e.Cancel = true; // not terminate immediately
        cancel = true;
    });
    AppDomain.CurrentDomain.ProcessExit += new EventHandler((s, e) =>
    {
        Console.WriteLine(
quot;ProcessExit");
        Console.WriteLine(
quot;Process {Environment.ProcessId} exited gracefully (SIGTERM).");
    });
    do
    {
        Thread.Sleep(10);
    } while (!cancel);
    Console.WriteLine(
quot;Process {Environment.ProcessId} exited gracefully (SIGINT).");
}

The results:

$ dotnet run &
Process ID = 4113
$ kill -s INT 4113
CancelKeyPress
Process 4113 exited gracefully (SIGINT).
$ dotnet run &
Process ID = 4395
$ kill -s TERM 4395
ProcessExit
Process 4395 exited gracefully (SIGTERM).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文