Skip to content

Commit

Permalink
Add healthcheck and probes
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewlock committed Aug 23, 2020
1 parent 58533fb commit 6f4a280
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,23 @@ spec:
protocol: TCP
livenessProbe:
httpGet:
path: /
path: /healthz
port: http
initialDelaySeconds: 0
periodSeconds: 10
timeoutSeconds: 1
failureThreshold: 3
startupProbe:
httpGet:
path: /health/startup
port: http
failureThreshold: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /
path: /ready
port: http
successThreshold: 3
resources:
{{- toYaml .Values.resources | nindent 12 }}
{{- with .Values.nodeSelector }}
Expand Down
21 changes: 21 additions & 0 deletions helmcharts/TestApp/src/TestApp.Api/RandomHealthCheck.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Diagnostics.HealthChecks;

namespace TestApp.Api
{
public class RandomHealthCheck : IHealthCheck
{
private static readonly Random _rnd = new Random();

public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
var result = _rnd.Next(5) == 0
? HealthCheckResult.Healthy()
: HealthCheckResult.Unhealthy("Failed random");

return Task.FromResult(result);
}
}
}
7 changes: 6 additions & 1 deletion helmcharts/TestApp/src/TestApp.Api/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -26,6 +27,8 @@ public Startup(IConfiguration configuration)
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddHealthChecks()
.AddCheck<RandomHealthCheck>("Random check");
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand All @@ -44,7 +47,9 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", _ => Task.CompletedTask);
endpoints.MapHealthChecks("/health/startup");
endpoints.MapHealthChecks("/healthz", new HealthCheckOptions { Predicate = _ => false });
endpoints.MapHealthChecks("/ready", new HealthCheckOptions { Predicate = _ => false });
endpoints.MapControllers();
});
}
Expand Down

0 comments on commit 6f4a280

Please sign in to comment.