/random-generator

Random Generator Service (example used in "Kubernetes Patterns" examples)

Primary LanguageJavaOtherNOASSERTION

Random Number Generator Service

This repository holds a sample REST service used in most of the examples for the book "Kubernetes Patterns" by Bilgin Ibryam and Roland Huß.

It’s a very simple Spring Boot service with a handful of endpoints used to demonstrate Kubernetes concepts.

You can start it locally with with ./mvnw -pl spring spring-boot:run [1] and get a random number:

curl -sL http://localhost:8080/ | jq .
{
  "random": 838265052,
  "id": "58da4c5b-ba06-438e-84f8-1a30581baeb8"
}

In order to create a Docker image, just use ./mvnw -pl spring package docker:build. You can easly change the Docker repository and push it to your own account with -Dimage.repo:

./mvnw -Dimage.repo="rhuss" package docker:build push

Configuration

Property Env Var Description Pattern

version

---

Version of this application, created during build

Declarative Deployment

log.file

LOG_FILE

Path to a log file. If set, the write out the generated response values into a CSV file

many

log.url

LOG_URL

If given, send out an HTTP request with the results of a random number generation

Ambassador

build.type

BUILD_TYPE

Add some informations about how the image has been built

Image Builder

pattern

PATTERN

Descriptive label used in environment variables

Envvar Configuration, Configuration Resource

seed

SEED

Seed number to initialize the random number generator. Must be a long.

Configuration Resource

Endpoints

Endpoint Description Pattern

/

Return a random number and a unique id of this service

many

/info

Return information about configuration and system

many

/memory-eater

Allocate an array for eating up heap memory. Use parameter "mb" to specify the size in MegaByte

Predictable Demands

/toggle-live

Toggle the status of the Sprint Boot’s actuator health check between UP and DOWN

Health Probe

/toggle-ready

Add or remove the local file which indicates readiness of this service

Health Probe

/shutdown

Shutdown the Spring Boot service

Managed Lifecycle

/logs

Return the content of the logs

Stateful Service, Init Container

Batch

There is also a single class RandomRunner included which creates a list of random numbers into a file:

# Compile stuff first
./mvnw -pl spring compile

# Create a file with 100000 lines in /tmp/random_numbers.txt
java -cp target/classes RandomRunner /tmp/random_numbers.txt 100000

This command is used in the Batch Job and Periodic Job pattern example.

Building

Refer to the Image Builder pattern how this example is build from within a Kubernetes cluster.

For creating a tag 2.0 just use the profile -Pv2. This is used for creating images to be used the Declarative Deployment pattern.


1. Instead of the Maven wrapper mvnw you can also use a locally installed mvn