Skip to content

Instantly share code, notes, and snippets.

@mrocabado
Created August 8, 2020 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrocabado/0cfd36d2a3b67b85e5960ae65dbaee62 to your computer and use it in GitHub Desktop.
Save mrocabado/0cfd36d2a3b67b85e5960ae65dbaee62 to your computer and use it in GitHub Desktop.
Java application deployment with Docker and Kubernetes

#Java application deployment with Docker and Kubernetes

Stop the service

POST http://localhost:8080/actuator/shutdown

Get environment details

GET http://localhost:8080/actuator/info curl http://localhost:80/actuator/info

Build the image

  • cd [pom.xml folder]
  • mvn clean package
  • docker image build -t /k8s-demo:1.0 .

Run the image

Creates and run the container

docker container run -d --name k8s-demo -p 8080:8080 mrocabado/k8s-demo:1.0

Starts the container

docker container start k8s-demo

Clean up

docker container rm $(docker container ls -aq) -f
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class K8sDemoApplication {
public static void main(String[] args) {
SpringApplication.run(K8sDemoApplication.class, args);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.mindwaresrl</groupId>
<artifactId>k8s-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>k8s-demo</name>
<description>K8s Demo project</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.github.oshi</groupId>
<artifactId>oshi-core</artifactId>
<version>5.2.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.example.demo.k8s;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.boot.actuate.info.Info.Builder;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.stereotype.Component;
import oshi.SystemInfo;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.hardware.NetworkIF;
import oshi.software.os.OperatingSystem;
@Component
public class SystemInfoContributor implements InfoContributor {
private static SystemInfo systemInfo = new SystemInfo();
@Override
public void contribute(Builder builder) {
HardwareAbstractionLayer hal = systemInfo.getHardware();
OperatingSystem os = systemInfo.getOperatingSystem();
List<NetworkIF> networkInterfaces = hal.getNetworkIFs();
Map<String, Object> details = new HashMap<>();
details.put("processor", String.format("Physical: %d, Logical: %d", hal.getProcessor().getPhysicalProcessorCount(), hal.getProcessor().getLogicalProcessorCount() ));
details.put("memory", hal.getMemory().toString());
details.put("network interfaces", buildReport(networkInterfaces));
details.put("network parameters", os.getNetworkParams().toString());
builder.withDetails(details);
}
private String buildReport(List<NetworkIF> networkInterfaces) {
if (networkInterfaces.isEmpty()) {
return "no network interfaces found";
}
return networkInterfaces.stream()
.filter(networkIF -> networkIF.getIPv4addr().length>0)
.map( this::buildReport)
.collect(Collectors.joining(" | "));
}
private String buildReport(NetworkIF networkIF) {
String ips = Arrays.asList(networkIF.getIPv4addr()).stream().collect(Collectors.joining(" "));
return String.format("%s: %s", networkIF.getName(), ips);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment