Skip to content

Commit 7dfe07c

Browse files
committed
chore(Adapter): Completed Adapter description
1 parent 3f615c8 commit 7dfe07c

File tree

5 files changed

+73
-20
lines changed

5 files changed

+73
-20
lines changed

README.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ link:structural/InitContainer/README.adoc[Init Container]::
5656
link:structural/Sidecar/README.adoc[Sidecar]::
5757
Git polling example for a sidecar
5858
link:structural/Adapter/README.adoc[Adapter]::
59-
Adapter for exporting timing information from the sample random-generator application in a Prometheus format [*]
59+
Adapter for exporting timing information from the sample random-generator application in a Prometheus format
6060
link:structural/Ambassador/README.adoc[Ambassador]::
6161
Ambassador for moving on the log of the random-generator [*]
6262

foundational/HealthProbe/README.adoc

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,18 @@
22

33
For this example we need a Kubernetes installation as described in link:../../INSTALL.adoc[INSTALLATION.adoc].
44

5-
For our example for this pattern we are reusing our https://github.com/k8spatterns/random-generator[random-generator] which also includes support for health checks.
5+
Here we are reusing our https://github.com/k8spatterns/random-generator[random-generator] which also includes support for health checks.
66

7-
To apply a Deployment with liveness and readiness check enables, use
7+
To apply a Deployment with liveness and readiness check enabled, use
88

99
[source, bash]
1010
----
11-
kubectl create -f deployment.yml
11+
kubectl create -f https://k8spatterns.io/HealthProbe/deployment.yml
1212
----
1313

1414
This deployment introduces an artificial pause of 20 seconds before the application becomes ready.
1515
To monitor the readiness and liveness states you best open an extra terminal with running
1616

17-
1817
[source, bash]
1918
----
2019
kubectl get pods -w
@@ -24,7 +23,7 @@ random-generator-5856b5f774-54h6b 1/1 Running 0 38s
2423
----
2524

2625
The example apps expose to endpoints with which you can switch the state of the readiness and liveness checks.
27-
For simplicity reasons, we haven't installed a Service or Ingress (but of course, you are free to do so of course !)
26+
For simplicity reasons, we haven't installed a Service or Ingress (but of course, you are free to do so!)
2827

2928
Instead we are using a simple port-forwarding directly to the pod to trigger the toggles:
3029

@@ -34,29 +33,23 @@ Instead we are using a simple port-forwarding directly to the pod to trigger the
3433
kubectl port-forward $(pod random-generator) 8080:8080 &
3534
----
3635

37-
Now you can switch the readiness/liveness checks:
36+
Now you can switch on/off the readiness/liveness checks and see how the cluster manages your pods:
3837

3938
[source, bash]
4039
----
41-
# Check liveness
40+
# Check liveness probe by querying the actuator
4241
curl -s http://localhost:8080/actuator/health | jq .
43-
{
44-
"status": "UP"
45-
}
4642
47-
# Switch liveness checks
43+
# Toggle liveness check to off
4844
curl -s http://localhost:8080/toggle-live
4945
50-
# Check liveness again
46+
# Check liveness probe again
5147
curl -s http://localhost:8080/actuator/health | jq .
52-
{
53-
"status": "DOWN"
54-
}
5548
56-
# Wait a bit and watch the pods. What happens after 2-3 mins ?
49+
# Watch the pods and wait a bit. What happens after 2-3 mins ?
5750
kubectl get pods -w
5851
59-
# Switch readiness off
52+
# Now switch readiness off
6053
curl -s http://localhost:8080/toggle-ready
6154
6255
# Watch the pods for 1-2 mins

structural/Adapter/README.adoc

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,65 @@
22

33
IMPORTANT: The instructions have not been written/finished, but the resource file has been verified. Instructions will be added soon.
44

5+
In this example we will see how we can expose a the metrics created by the random generator REST service in a Prometheus conformant format.
6+
7+
By intention the metrics created by the the random-generator service is written is an own format which can't be used by Prometheus directly.
8+
Also the metrics are not exposed via a network port but is written into the filesystem.
9+
We are using an _Adapter_ sidecar container to convert and expose data written in that custom proprietary format over HTTP so that it can be scraped by Prometheus.
10+
11+
Let's see how this works.
12+
13+
We are using a bare Pod for simplicities sake (in real world scenarios this would be of course e.g. a Deployment).
14+
15+
Please have a look into the `pod.yml` descriptor which
16+
17+
* Create two containers: One is our `random-generator` the other is the _Adapter_, `k8spatterns/random-generator-exporter`. This is a simple Perl script (yeah, Perl ;) which does the transformation. You find the Dockerfile and script for this image in the link::image/[image] directory.
18+
* Create a volume which is shared between the main application container and the adapter. The main container writes the metrics file into this shared directory, and the Prometheus _Adapter_ picks it up when queried over HTTP
19+
20+
The Pod can be created with
21+
22+
[source, bash]
23+
----
24+
kubectl create -f https://k8spatterns.io/Adapter/pod.yml
25+
----
26+
27+
For easy access of random generator, let's create a Service:
28+
29+
[source, bash]
30+
----
31+
kubectl create -f https://k8spatterns.io/Adapter/service.yml
32+
----
33+
34+
This Service uses a `nodePort` to expose it on every node of the cluster.
35+
For Minikube this works nicely.
36+
However, if your cluster nodes are not exposed, you might either use our `kubectl port-forward` hack used in other examples or use a full ingress or loadbalancer exposed route as described in the _Service Discovery_ pattern.
37+
38+
Let's assume that you are using Minikube for now, so that we can access the
39+
Then let's access our Service with
40+
41+
[source, bash]
42+
----
43+
port=$(kubectl get svc random-generator -o jsonpath={.spec.ports[0].nodePort})
44+
curl http://$(minikube ip):$port
45+
----
46+
47+
When we now jump into the Pod we can examine the generated metrics file
48+
49+
[source, bash]
50+
----
51+
kubectl exec -it random-generator -c main bash
52+
cat /logs/random.log
53+
----
54+
55+
NOTE: You could also have entered the `adapter` container which mounts the same directory at `/logs/`, too
56+
57+
The metrics extracted from that data can now be accessed in a Prometheus conformant way:
58+
59+
[source, bash]
60+
----
61+
prom_port=$(kubectl get svc random-generator -o jsonpath={.spec.ports[1].nodePort})
62+
curl http://$(minikube ip):$prom_port
63+
----
564

665
=== More Information
766

structural/Adapter/image/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Imagename: k8spatterns/random-generator-exporter
1+
# Imagename: k8spatterns/random-generator-exporter:1.0
22
# Simple image for exposing the log file created by random-exporter
33

44
# Yes, I know. But still the best language for quick and small system related tasks

structural/Adapter/pod.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ spec:
2525
- mountPath: /logs
2626
name: log-volume
2727
# ------------------------------------------------
28-
# Prometheus adapter
28+
# Prometheus adapter. You find the source to this image
29+
# in the "image/" directory.
2930
- image: k8spatterns/random-generator-exporter
3031
name: adapter
3132
env:

0 commit comments

Comments
 (0)