DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Implementing Health Checks With MicroProfile and Istio

When running cloud-native applications in orchestration frameworks, microservices need to report whether they are ready and live.

Niklas Heidloff user avatar by
Niklas Heidloff
CORE ·
Apr. 16, 19 · Tutorial
Like (4)
Save
Tweet
Share
7.85K Views

Join the DZone community and get the full member experience.

Join For Free

When running cloud-native applications in the orchestration framework Kubernetes and the service mesh Istio, microservices need to report whether they are ready and live. Kubernetes needs to know this to restart containers if necessary. Istio needs to know this information to define where to route requests to.

With the Kubernetes livenessProbe it is determined whether, as the name indicates, the pod is live. Pods that are live might not be ready though, for example, when they just started and still need to load data. That's why there is a second probe, the readinessProbe.

I've implemented some sample code that shows how to develop these health checks with Eclipse MicroProfile.

Get the code of cloud-native-starter from GitHub.

Here is the Java code which tells the 'articles' service that the microservice is ready.

import org.eclipse.microprofile.health.Health;
import org.eclipse.microprofile.health.HealthCheck;
import org.eclipse.microprofile.health.HealthCheckResponse;
import javax.enterprise.context.ApplicationScoped;

@Health
@ApplicationScoped
public class HealthEndpoint implements HealthCheck {

    @Override
    public HealthCheckResponse call() {
        return HealthCheckResponse.named("articles").withData("articles", "ok").up().build();
    }
}

In the Kubernetes YAML file the URLs of the livenessProbe and the readinessProbe are defined.

kind: Deployment
apiVersion: apps/v1beta1
metadata:
  name: articles
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: articles
        version: v1
    spec:
      containers:
      - name: articles
        image: articles:1
        ports:
        - containerPort: 8080
        livenessProbe:
          exec:
            command: ["sh", "-c", "curl -s http://localhost:8080/"]
          initialDelaySeconds: 20
        readinessProbe:
          exec:
            command: ["sh", "-c", "curl -s http://localhost:8080/health | grep -q articles"]
          initialDelaySeconds: 40
      restartPolicy: Always

If you want to run this demo yourself in Minikube, get the code from GitHub and invoke these commands:

$ git clone https://github.com/nheidloff/cloud-native-starter.git
$ scripts/check-prerequisites.sh
$ scripts/deploy-articles-java-jee.sh
$ kubectl get pods --watch
$ minikube dashboard

To learn more about health checks, take a look at these articles:

  • Open Liberty Guide: Adding Health Reports to Microservices
  • MicroProfile Health
  • MicroProfile Health Check in Istio
Health (Apple)

Published at DZone with permission of Niklas Heidloff, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Best Practices for Writing Clean and Maintainable Code
  • The 31 Flavors of Data Lineage and Why Vanilla Doesn’t Cut It
  • Distributed Stateful Edge Platforms
  • Project Hygiene

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: