Skip to main content
Redhat Developers  Logo
  • Products

    Platforms

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat AI
      Red Hat AI
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • View All Red Hat Products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat Developer Hub
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat OpenShift Local
    • Red Hat Developer Sandbox

      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Secure Development & Architectures

      • Security
      • Secure coding
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • Product Documentation
    • API Catalog
    • Legacy Documentation
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

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

 

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

Share:

    .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

    • One model is not enough, too many models is hard: Technical deep dive

    • What's new in Ansible Automation Platform 2.6

    • Quantum computing 101 for developers

    • LLM Compressor 0.8.0: Extended support for Qwen3 and more

    • Master KV cache aware routing with llm-d for efficient AI inference

    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
    © 2025 Red Hat

    Red Hat legal and privacy links

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

    Report a website issue