Liveness and Readiness for service at container —— kubernetes & .Net

1. how to coding?

  • reference:

https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/health-checks?view=aspnetcore-6.0#filter-health-checks 

 

 

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddHealthChecks();
    }

    public void Configure(IApplicationBuilder app)
    {
        app.UseRouting();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapHealthChecks("/healthz/ready");
            endpoints.MapHealthChecks("/healthz/live");;
        });
    }
}

ASP.Net  Core 6

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddHealthChecks();

var app = builder.Build();

app.MapHealthChecks("/healthz/ready");

app.MapHealthChecks("/healthz/live");

app.Run();




2. how to add liveness/Readiness check to Kubernetes

https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/#define-a-liveness-command 
https://istio.io/latest/docs/ops/configuration/mesh/app-health-check/#liveness-and-readiness-probes-using-the-http-request-approach 

The liveness probe is what you might expect—it indicates whether the container is alive or not. If a container fails its liveness probe, Kubernetes will kill the pod and restart another.

Readiness probes indicate whether your application is ready to handle requests. It could be that your application is alive, but that it just can't handle HTTP traffic. In that case, Kubernetes won't kill the container, but it will stop sending it requests. In practical terms, that means the pod is removed from an associated service's "pool" of pods that are handling requests, by marking the pod as "Unready".

 

add those to deployment yaml

readinessProbe:
    httpGet:
        path: /healthz/ready
        port: 80
        scheme: HTTP
    timeoutSeconds: 1
    periodSeconds: 10
    successThreshold: 1
    failureThreshold: 3
livenessProbe:
    httpGet:
        path: /healthz/live
        port: 80
        scheme: HTTP
    timeoutSeconds: 1
    periodSeconds: 10
    successThreshold: 1
    failureThreshold: 3

  

3. how to check service

1.

kubectl describe pods **** -n [namespace]

check 是否存在 liveness,readiness的异常 event

 

2. 观察一段时间,是否 pods启动正常 (是否正常runing)

kubectl get pods **** -n [namespace] |grep ***

posted @ 2022-11-29 10:16  PanPan003  阅读(18)  评论(0编辑  收藏  举报