Skip to content

Commit e51a0d0

Browse files
danielguedesbjzheaux
authored andcommitted
Spring Boot Actuator + Kubernetes
Issue: BAEL-2356
1 parent 5dec6f7 commit e51a0d0

17 files changed

Lines changed: 372 additions & 0 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM openjdk:8-jdk-alpine
2+
3+
# Create app directory
4+
RUN mkdir -p /usr/opt/service
5+
6+
# Copy app
7+
COPY target/*.jar /usr/opt/service/service.jar
8+
9+
EXPOSE 8080
10+
11+
ENTRYPOINT exec java -jar /usr/opt/service/service.jar
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
6+
<parent>
7+
<groupId>org.springframework.boot</groupId>
8+
<artifactId>spring-boot-starter-parent</artifactId>
9+
<version>1.5.17.RELEASE</version>
10+
<relativePath/> <!-- lookup parent from repository -->
11+
</parent>
12+
13+
<modelVersion>4.0.0</modelVersion>
14+
15+
<artifactId>liveness-example</artifactId>
16+
<version>1.0-SNAPSHOT</version>
17+
18+
<properties>
19+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
21+
<java.version>1.8</java.version>
22+
</properties>
23+
24+
<dependencies>
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter-web</artifactId>
28+
</dependency>
29+
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-actuator</artifactId>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>org.springframework.boot</groupId>
37+
<artifactId>spring-boot-starter-test</artifactId>
38+
<scope>test</scope>
39+
</dependency>
40+
</dependencies>
41+
42+
<build>
43+
<plugins>
44+
<plugin>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-maven-plugin</artifactId>
47+
</plugin>
48+
</plugins>
49+
</build>
50+
51+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.liveness;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(Application.class, args);
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.liveness.health;
2+
3+
import org.springframework.boot.actuate.health.Health;
4+
import org.springframework.boot.actuate.health.HealthIndicator;
5+
import org.springframework.stereotype.Component;
6+
7+
import java.util.concurrent.Executors;
8+
import java.util.concurrent.ScheduledExecutorService;
9+
import java.util.concurrent.TimeUnit;
10+
11+
@Component
12+
public class CustomHealthIndicator implements HealthIndicator {
13+
14+
private boolean isHealthy = true;
15+
16+
public CustomHealthIndicator() {
17+
ScheduledExecutorService scheduled = Executors.newSingleThreadScheduledExecutor();
18+
scheduled.schedule(() -> {
19+
isHealthy = false;
20+
}, 30, TimeUnit.SECONDS);
21+
}
22+
23+
@Override
24+
public Health health() {
25+
return isHealthy ? Health.up().build() : Health.down().build();
26+
}
27+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
server.port=8080
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
4+
<encoder>
5+
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
6+
</pattern>
7+
</encoder>
8+
</appender>
9+
10+
<root level="INFO">
11+
<appender-ref ref="STDOUT" />
12+
</root>
13+
</configuration>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung;
2+
3+
import com.baeldung.liveness.Application;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.boot.test.context.SpringBootTest;
7+
import org.springframework.test.context.junit4.SpringRunner;
8+
9+
@RunWith(SpringRunner.class)
10+
@SpringBootTest(classes = Application.class)
11+
public class SpringContextIntegrationTest {
12+
13+
@Test
14+
public void contextLoads() {
15+
}
16+
17+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: liveness-example
5+
spec:
6+
selector:
7+
app: liveness-example
8+
ports:
9+
- port: 8080
10+
11+
---
12+
apiVersion: apps/v1
13+
kind: Deployment
14+
metadata:
15+
name: liveness-example
16+
spec:
17+
selector:
18+
matchLabels:
19+
app: liveness-example
20+
replicas: 1
21+
strategy:
22+
rollingUpdate:
23+
maxUnavailable: 0
24+
template:
25+
metadata:
26+
labels:
27+
app: liveness-example
28+
spec:
29+
containers:
30+
- name: liveness-example
31+
image: dbdock/liveness-example:1.0.0
32+
imagePullPolicy: IfNotPresent
33+
resources:
34+
requests:
35+
memory: 400Mi
36+
cpu: 400m
37+
ports:
38+
- containerPort: 8080
39+
readinessProbe:
40+
httpGet:
41+
path: /health
42+
port: 8080
43+
initialDelaySeconds: 10
44+
timeoutSeconds: 2
45+
periodSeconds: 3
46+
failureThreshold: 1
47+
livenessProbe:
48+
httpGet:
49+
path: /health
50+
port: 8080
51+
initialDelaySeconds: 20
52+
timeoutSeconds: 2
53+
periodSeconds: 8
54+
failureThreshold: 1
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: readiness-example
5+
spec:
6+
selector:
7+
app: readiness-example
8+
ports:
9+
- port: 8080
10+
11+
---
12+
apiVersion: apps/v1
13+
kind: Deployment
14+
metadata:
15+
name: readiness-example
16+
spec:
17+
selector:
18+
matchLabels:
19+
app: readiness-example
20+
replicas: 1
21+
strategy:
22+
rollingUpdate:
23+
maxUnavailable: 0
24+
template:
25+
metadata:
26+
labels:
27+
app: readiness-example
28+
spec:
29+
containers:
30+
- name: readiness-example
31+
image: dbdock/readiness-example:1.0.0
32+
imagePullPolicy: IfNotPresent
33+
resources:
34+
requests:
35+
memory: 400Mi
36+
cpu: 400m
37+
ports:
38+
- containerPort: 8080
39+
readinessProbe:
40+
httpGet:
41+
path: /health
42+
port: 8080
43+
initialDelaySeconds: 40
44+
timeoutSeconds: 2
45+
periodSeconds: 3
46+
failureThreshold: 5
47+
livenessProbe:
48+
httpGet:
49+
path: /health
50+
port: 8080
51+
initialDelaySeconds: 100
52+
timeoutSeconds: 2
53+
periodSeconds: 8
54+
failureThreshold: 1

‎spring-cloud/spring-cloud-kubernetes/pom.xml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
<modules>
1212
<module>demo-frontend</module>
1313
<module>demo-backend</module>
14+
<module>liveness-example</module>
15+
<module>readiness-example</module>
1416
</modules>
1517

1618
<parent>

0 commit comments

Comments
 (0)