微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在kubernetes中实现C#/NET gRPC Healthcheck

如何解决在kubernetes中实现C#/NET gRPC Healthcheck

目标

我想使用gRPC health probe protocol向.NET微服务添加运行状况检查。

微服务只不过是用Greeter模板创建的dotnet new grpc服务。我已经让它直接在Docker上本地运行和构建,最后在我的开发K8s集群上运行。

问题

this post之后,我将NuGet包添加到项目中,并像使用greeter服务一样映射了端点。但是,活动/就绪探测器由于超时而失败。我是C#/。NET的新手,所以我希望我在该领域犯了一个错误,但是问题也可能出在kubernetes资源上。

代码

Startup.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Grpc.HealthCheck;

namespace Company.Project.GRPC
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application,visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddGrpc();
            services.AddSingleton<HealthServiceImpl>(); // <-- makes sense to me generally though the details are hazy 
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app,IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                // Healthcheck services
                endpoints.MapGrpcService<HealthServiceImpl>(); // <-- what besides this is needed?
                
                // App gRPC services
                endpoints.MapGrpcService<GreeterService>();

                endpoints.MapGet("/",async context =>
                {
                    await context.Response.WriteAsync("Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client,visit: https://go.microsoft.com/fwlink/?linkid=2086909");
                });
            });

        }
    }
}

Program.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Server.Kestrel.Core;

namespace Company.Project.GRPC
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
            
        }

        // Additional configuration is required to successfully run gRPC on macOS.
        // For instructions on how to configure Kestrel and gRPC clients on macOS,visit https://go.microsoft.com/fwlink/?linkid=2099682
       public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.ConfigureKestrel(options =>
                {
                    // Setup a HTTP/2 endpoint without TLS.
                    // Todo: Review security. May in fact be ok as an internal-only service
                    // options.ListenLocalhost(5000,o => o.Protocols = 
                    //     HttpProtocols.Http2);
                    options.Listen( System.Net.IPAddress.Parse("0.0.0.0"),5000);
                    options.Listen( System.Net.IPAddress.Parse("0.0.0.0"),50051);
                });
                webBuilder.UseStartup<Startup>();

            });
    }
}

Deployment.yaml

...
          ports:
            - containerPort: 5000
            - containerPort: 50051
          readinessProbe:
            exec:
              command:
                - /bin/grpc_health_probe
                - -addr=:50051
            initialDelaySeconds: 15
            periodSeconds: 5
            timeoutSeconds: 5
            successthreshold: 1
            failureThreshold: 1
          livenessProbe:
            exec:
              command:
                - /bin/grpc_health_probe
                - -addr=:50051   # <-- doesn't matter which port I use
            initialDelaySeconds: 15
            periodSeconds: 5
            timeoutSeconds: 5
            successthreshold: 1
            failureThreshold: 1

行为

当我拖尾该Pod的日志时,我看到服务器启动正常,但是在活动性探针失败后被杀死。 “事件”窗格显示Container myservice Failed liveness probe,will be restarted

探测客户端是从顶部链接分配的正式客户端。我检查了文件是否存在,并且可以按照指示执行该文件

除了服务器可以访问并运行的基本Check之外,我不需要任何功能,因此我相信,将HealthCheckImpl的子类化步骤包括在内我引用的帖子是多余的。

要获得对此简单服务的正常运行状况检查,我需要做什么/应该理解什么?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。