Skip to content

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

License

Notifications You must be signed in to change notification settings

k8spatterns/random-generator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 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

# Write 100000 lines to /dev/random_seed, wait 30s, and then repeat endlessly.
java -c target/classes RandomRunner /dev/random_seed 100000 30

This command is used in the Batch Job and Periodic Job pattern example. The last example is used in Daemon Service 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.

Crossbuild the images

In order to compile the images for multiple architectures, use:

docker buildx create --use
docker buildx build --build-arg VERSION="v1" --platform linux/arm64/v8,linux/amd64 -t k8spatterns/random-generator:1.0 .
docker buildx build --build-arg VERSION="v2" --platform linux/arm64/v8,linux/amd64 -t k8spatterns/random-generator:2.0 .

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

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published