Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

Using Kubernetes readiness and liveness probes for health checks with ASP.NET Core 2.2 on OpenShift

<p>&nbsp;</p> <quillbot-extension-portal></quillbot-extension-portal>

December 21, 2018
Takayoshi Tanaka
Related topics:
.NETKubernetes
Related products:
Red Hat OpenShiftRed Hat OpenShift Container Platform

    .NET Core 2.2 has been released. You can try it on Red Hat Enterprise Linux (RHEL) and OpenShift. One of the new features of ASP.NET Core is the Health Checks API.

    In this article, which was written for C# Advent Calendar 2018, I show an example of how the API works with OpenShift by implementing two health checks for the Kubernetes liveness and readiness probes. Since OpenShift includes Kubernetes, this example also works well with Kubernetes.

    Example code

    This new Health Checks API quite works well with Kubernetes liveness and readiness probes. These probes are available in OpenShift, too.

    Here is my example application to explain the Health Checks API.

    And below is an example of configuring two health checks: one for the liveness probe and one for the readiness probe.

    public void ConfigureServices(IServiceCollection services)
    {
    ...
    
        services.AddHealthChecks()
                .AddCheck("Liveness", failureStatus: null)
                .AddCheck("Readiness", failureStatus: null);
    }
    
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
    ...
        app.UseHealthChecks("/health/live", new HealthCheckOptions()
        {
            Predicate = check => check.Name == "Liveness"
        });
    
        app.UseHealthChecks("/health/ready", new HealthCheckOptions()
        {
            Predicate = check => check.Name == "Readiness",
    
        });
    ...
    }
    

    In the ConfigureServices method, you can enable health checks and add as many checks as you want. LivenessHealthCheck and ReadinessHealthCheck are the classes that define how to check the application's health in this project. I'll show you these classes later.

    In the Configure method, you can define which health checks are responsible for the specified path. In my example, the health check whose name is Liveness is executed for /health/live and the health check whose name is Readiness is executed for /health/ready.

    Below is an example implementation of the LivenessHealthCheck class. You can return the application's health status in the CheckHealthAsync method. My example returns Unhealth when the application status defined in HealthStatusData.IsLiveness is false. This status can be updated by the POST method.

    Example implementation of the LivenessHealthCheck class

    For more details, please refer to the code.

    internal class LivenessHealthCheck : IHealthCheck
    {
        private HealthStatusData data;
    
        public LivenessHealthCheck(HealthStatusData data)
        {
            this.data = data;
        }
    
        public Task CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (data.IsLiveness)
            {
                return Task.FromResult(HealthCheckResult.Healthy());
            }
            else
            {
                return Task.FromResult(HealthCheckResult.Unhealthy("Error"));
            }
        }
    }
    

    Working with OpenShift

    My example code can be run on a local machine. However, you can try it with Kubernetes or OpenShift. If you want to try it with OpenShift, please import the ASP.NET Core 2.2 s2i image by following this document. After importing the .NET Core 2.2 s2i image, you can deploy my app to your cluster, as follows:

    $ oc new-project aspnetcore22
    $ oc new-app --name=healthcheckexample 'dotnet:2.2~https://github.com/tanaka-takayoshi/HealthCheckExample.git' --build-env DOTNET_STARTUP_PROJECT=HealthCheckExample
    $ oc expose svc/healthcheckexample
    

    After the deployment configuration is created, you can enable health checks in the OpenShift web console.

    Enable health checks in the OpenShift web console

    If you edit the deploymentconfig YAML file, the probes should be the following.

    spec:
    ...
      template:
      ...
        spec:
          containers:
            - image: >-
                ...
              livenessProbe:
                failureThreshold: 3
                httpGet:
                  path: /health/live
                  port: 8080
                  scheme: HTTP
                periodSeconds: 10
                successThreshold: 1
                timeoutSeconds: 1
              readinessProbe:
                failureThreshold: 3
                httpGet:
                  path: /health/ready
                  port: 8080
                  scheme: HTTP
                periodSeconds: 10
                successThreshold: 1
                timeoutSeconds: 1
              ...
    
    

    Now, here is the application status in the web console.

    Application status in the web console

    First, let's make the readiness probe unhealthy. Soon after that, you can see in the monitoring page of the web console that the readiness check has failed.

    Web console showing that the Readiness check failed
    Web console sowing that the Readiness check failed

    You can see the same thing by using the oc get event -w command.

    oc get event -w command showing that the Readiness check failed

    When the readiness check fails, the pod is still running but it stops routing the traffic. Since this application has only one pod, you can't access the application.

    Message showing the application is not available

    You can easily scale up this application by clicking the upper arrow next to the number of the pods.

    You can scale up the application

    Now, since one of the two pods is healthy, you can access the application.

    One of the pods is healthy, so you can access the application

    Next, scale down the app to one pod and make the liveness probe unhealthy.

    Scale down the app to one pod and make the Liveness probe unhealthy

    When the liveness probe fails, the pod is killed and a new one is created. You can see the sequence of events.

    Sequence of events after Liveness probe fails

    Summary

    In this article, I described how the Health Checks API in ASP.NET Core 2.2 works with OpenShift (Kubernetes) probes. You can define your appropriate logic in a class that implements the IHealthCheck interface. The readiness probe can be used when the pod is ready to serve the request. The liveness probe can be used when the pod is running.

    Other resources

    • Announcing .NET Core 2.2 for Red Hat Platforms
    • Using OpenShift to deploy .NET Core applications
    • Building .NET Core container images using S2I
    • Other .Net Core articles on the Red Hat Developer blog
    Last updated: January 12, 2024

    Recent Posts

    • Debugging image mode with Red Hat OpenShift 4.20: A practical guide

    • EvalHub: Because "looks good to me" isn't a benchmark

    • SQL Server HA on RHEL: Meet Pacemaker HA Agent v2 (tech preview)

    • Deploy with confidence: Continuous integration and continuous delivery for agentic AI

    • Every layer counts: Defense in depth for AI agents with Red Hat AI

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Chat Support

    Please log in with your Red Hat account to access chat support.