k8spatterns / random-generator

Random Generator Service (example used in "Kubernetes Patterns" examples)
https://k8spatterns.io
Other
16 stars 18 forks source link

Random Number Generator Service

This repository holds a sample REST service used in most of the https://github.com/k8spatterns/examples/[examples] for the book "Kubernetes Patterns" by https://github.com/bibryam[Bilgin Ibryam] and https://github.com/rhuss[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 footnote:[Instead of the Maven wrapper mvnw you can also use a locally installed mvn] and get a random number:

[source, bash]

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:

[source, bash]

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

Configuration

[cols="1,1,5,2", options="header"] |=== | 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

[cols="1,5,2", options="header"] |=== | 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:

[source, bash]

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:

[source, bash]

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 .