README.adoc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. = Linux Metrics Exporter for OpenShift Nodes =
  2. == Components ==
  3. . Container Image for SAR
  4. . Container Image for PSACCT
  5. . Container Image for Exporter
  6. == How It All Works ==
  7. Very simple: two sidecar containers, `collector-sysstat` and
  8. `collector-psacct`, produce data on a shared ephemeral volume, and the third
  9. container, `metrics-exporter`, consumes the data and exposes it on the
  10. `/q/metrics` endpoint where Prometheus can pick them up.
  11. The specific thing about how the entire composition works is that care has been
  12. taken, especially with `psacct` (which can grow excessively during periods of
  13. high activity), that accounting files are regularly truncated or moved out of
  14. the way in order to keep the disk space utilisation as low as possible.
  15. == OpenShift Deployment ==
  16. TBD
  17. == Standalone Containers ==
  18. Start the composition.
  19. // TODO: podman pod
  20. [subs=+quotes]
  21. ------
  22. $ *podman volume create metrics*
  23. metrics
  24. $ *podman run -d --rm -v metrics:/var/account --cap-add SYS_PACCT --pid=host collector-psacct:latest*
  25. dd9f4825d23614df2acefdcd70ec1e6c3ea18a58b86c9d17ddc4f91038487919
  26. $ *podman run -d --rm -v metrics:/var/log/sa collector-sysstat*
  27. ec3d0957525cc907023956a185b15123c20947460a48d37196d511ae42de2e27
  28. $ *podman run --name exporter -d --rm -v metrics:/metrics -p 8080:8080 metrics-exporter*
  29. d4840ad57bfffd4b069e7c2357721ff7aaa6b6ee77f90ad4866a76a1ceb6adb7
  30. ------
  31. Configure prometheus with a data source from the `exporter` container.
  32. [subs=+quotes]
  33. ------
  34. $ *podman inspect -f '{{.NetworkSettings.IPAddress}}' exporter
  35. 10.88.0.8
  36. $ *tail -n15 tmp-test/prometheus.yml*
  37. scrape_configs:
  38. # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  39. - job_name: "prometheus"
  40. static_configs:
  41. - targets: ["localhost:9090"]
  42. **- job_name: "exporter"
  43. metrics_path: "/q/metrics"
  44. scheme: "http"
  45. static_configs:
  46. - targets: ["10.88.0.8:8080"]
  47. scrape_interval: 10s
  48. scrape_timeout: 5s**
  49. ------
  50. Add prometheus and grafana.
  51. [subs=+quotes]
  52. ------
  53. $ *podman run --name prometheus \*
  54. *-d --rm \*
  55. *-v ./test/prometheus.yml:/etc/prometheus/prometheus.yml*
  56. *-v prometheus:/prometheus \*
  57. *-p 9090:9090 \*
  58. *registry.redhat.io/openshift4/ose-prometheus:v4.11*
  59. 6eae04677fcded65bbe1cb7f66aa887d94587977a0616f7ec838f9453702474c
  60. $ *podman run --name grafana -d --rm -p 3000:3000 \*
  61. *-v ./test/grafana.ini:/etc/grafana/grafana.ini \*
  62. *registry.redhat.io/openshift4/ose-grafana:v4.11*
  63. 78d5bfa7977923b828c1818bb877fa87bdd96086cc8c875fbc46073489f6760e
  64. ------
  65. Configure grafana with prometheus as the datasource and dashboard away!
  66. .Process Accounting Graphs from a Single Host
  67. image::pics/psacct-sample.png[scaledwidth="95%" width="95%"]
  68. .Sysstat Scheduler Information, Single Host
  69. image::pics/sysstat-sample-sched.png[scaledwidth="95%" width="95%"]
  70. .Sysstat I/O Information, Single Host
  71. image::pics/sysstat-sample-io.png[scaledwidth="95%" width="95%"]
  72. == Images ==
  73. This set of images requires a valid entitlement for RHEL (and consequently
  74. either a RHEL system to build on or a RHEL system to create an entitlement
  75. secret from).
  76. IMPORTANT: You do not have to build the images, I have built them for `x86_64` and made them available on `quay.io/benko/`.
  77. === SAR ===
  78. The _system activity reporting_ image is based on `ubi-minimal` and includes
  79. just the `sysstat` package.
  80. It expects a volume to be attached at `/var/log/sa`.
  81. Entrypoint takes care of initialising the `saXX` files.
  82. // TODO: and rotating any old files out of the way.
  83. It *requires* to be executed under `root` UID (can be rootless, but that may
  84. affect your data depending on host and container configuration).
  85. It also *requires* access to host's network namespace if you want to measure
  86. global network statistics.
  87. // NOTE: When running in a pod, the below is irrelevant as the exporter sets
  88. // the hostname, and you can override it there. It does however obtain
  89. // the default hostname from data sources.
  90. //
  91. // If aggregating from several hosts, you also need to use the hosts' UTS
  92. // namespace to report correct hostnames, or set the corresponding environment
  93. // variables to override detection.
  94. ==== Parameters ====
  95. `PERIOD`::
  96. Sampling period in seconds. Defaults to `10`. Increase this to something
  97. like `30` (or more) for hosts with many network interfaces, block devices,
  98. and/or CPUs.
  99. `STARTUP_SCRATCH`::
  100. Whether to scratch existing `sa1` data at startup. Defaults to `0`, but
  101. could be anything except `1`, `yes`, or `true`, which activates it.
  102. `STARTUP_ROTATE`::
  103. Whether to mark data as rotated at startup. Basically just writes a marker
  104. in the previous `sadc` data file. Defaults to `0`, but could be anything
  105. except `1`, `yes`, or `true`, which activates it.
  106. === PSACCT ===
  107. The _process accounting_ image is based on `ubi-minimal` and includes just the
  108. `psacct` package.
  109. It expects a volume to be attached at `/var/account`.
  110. Entrypoint takes care of rotating any old `pacct` files out of the way.
  111. In addition to *requiring* execution under a *real* `root` UID (i.e. *NOT* a
  112. rootless container), it also *requires* the `CAP_SYS_PACCT` capability
  113. (`--cap-add=SYS_PACCT`) and access to host's PID namespace (`--pid=host`).
  114. // NOTE: When running in a pod, the below is irrelevant as the exporter sets
  115. // the hostname, and you can override it there. It does however obtain
  116. // the default hostname from data sources.
  117. //
  118. // If aggregating from several hosts, you also need to use the hosts' UTS
  119. // namespace to report correct hostnames, or set the corresponding environment
  120. // variables to override detection.
  121. ==== Parameters ====
  122. `PERIOD`::
  123. Sampling period in seconds. Defaults to `10`. Increase this to something
  124. like `30` (or more) for hosts with many network interfaces, block devices,
  125. and/or CPUs.
  126. `CUMULATIVE`::
  127. Tells the collection process to never reset the `pacct` file and just keep
  128. it growing, thus reporting cumulative stats since container start. Beware
  129. that the `pacct` file will grow correspondinly large as time goes by.
  130. `STARTUP_SCRATCH`::
  131. Whether to scratch existing `pacct` data at startup. Defaults to `0`, but
  132. could be anything except `1`, `yes`, or `true`, which activates it.
  133. === Exporter ===
  134. The brain of the group.
  135. // TODO: Add support for hostname overrides in app.
  136. // run a maven registry.access.redhat.com/ubi9/openjdk-17 container:
  137. //
  138. // podman volume create maven
  139. //
  140. // podman run -it \
  141. // --name exporter \
  142. // -v maven:/home/default/.m2/repository \
  143. // -v metrics:/metrics \
  144. // -v /Users/johndoe/Documents/workspaces/projects/p0f/linux-metrics-exporter/exporter:/exporter \
  145. // -p 8080:8080 \
  146. // registry.access.redhat.com/ubi9/openjdk-17 bash
  147. //
  148. // $ cd /exporter
  149. // $ mvn quarkus:dev
  150. ==== Parameters ====
  151. In `application.properties` or as Java system properties:
  152. `exporter.data.path`::
  153. Override the location where the metrics files are expected to show up.
  154. Defaults to `/metrics` but obviously can't be that for testing outside of a
  155. container.
  156. ==== Debugging ====
  157. There are a couple of logger categories that might help you see what's going on.
  158. By default, the routes are fairly noisy, as apparently `TRACE` level logging
  159. doesn't work for some reason, so I had to bump everything up a level, so at
  160. `INFO` you already see a note about every record that's been processed - you
  161. will see their unmarshaled bodies (completely shameless, I know).
  162. These can be bumped up to `DEBUG` if you need more info:
  163. `psacct-reader`::
  164. The route reading process accounting files from `psacct-dump-all` file.
  165. Pretty much all the logic is here, but since there can be a large number of
  166. process records in the file it is split and each record is processed
  167. asynchronously by the dispatch route.
  168. `psacct-dispatch`::
  169. The route dispatching the records to the registration service.
  170. `psacct-reset`::
  171. To be able to work with instantaneous data, rather than cumulative, all
  172. previously registered records are synchronously reset to zero upon the
  173. arrival of a new snapshot. This prevents metrics for previously registered
  174. processes from disappearing.
  175. `sysstat-reader`::
  176. The route that reads `sysstat-dump.json` file. All the logic is here.
  177. `net.p0f.openshift.metrics`::
  178. Non-camel stuff is all logged in this category.
  179. === Building with Podman ===
  180. If building the images using `podman` on an entitled host, no extra steps need
  181. to be performed as host entitlements will automatically be imported into the
  182. build container.
  183. NOTE: When building for an architecture without the `ubi-minimal` image or on a
  184. host that can not be entitled (f.e. Fedora CoreOS), you can choose a different
  185. base image by using the `--from` option in `podman build`.
  186. [subs=+quotes]
  187. -------------------------------
  188. $ *podman build --from=registry.fedoraproject.org/fedora-minimal:36 -f ./images/Containerfile-sysstat -t collector-sysstat:latest*
  189. -------------------------------
  190. You will have noticed there is no `Containerfile` for exporter. That is because
  191. `quarkus-maven-plugin` can do just fine
  192. https://quarkus.io/guides/container-image[building an image on its own]. Just
  193. add the `jib` extension and tell it to push the image somewhere.
  194. [subs=+quotes]
  195. -------------------------------
  196. $ *mvn package -Dquarkus.container-image.build=true -Dquarkus.container-image.push=true -Dquarkus.container-image.registry=foo*
  197. -------------------------------
  198. === Building in OpenShift ===
  199. ==== Collector Images ====
  200. If building the images in OpenShift Container Platform, you must make sure an
  201. entitlement secret and corresponding RHSM certificate secret are mounted inside
  202. the build pod in order for packages to be found and installed.
  203. NOTE: The entitled system architecture needs to match the container host!
  204. The process is as follows.
  205. .Verify access to host entitlement data.
  206. [subs=+quotes]
  207. -------------------------------
  208. $ **ls -l /etc/pki/entitlement/*.pem /etc/rhsm/ca/*.pem**
  209. -rw-r--r--. 1 root root 3272 Oct 31 06:09 /etc/pki/entitlement/_6028779042203586857_-key.pem
  210. -rw-r--r--. 1 root root 149007 Oct 31 06:09 /etc/pki/entitlement/_6028779042203586857_.pem
  211. -rw-r--r--. 1 root root 2305 Sep 2 2021 /etc/rhsm/ca/redhat-entitlement-authority.pem
  212. -rw-r--r--. 1 root root 7411 Sep 2 2021 /etc/rhsm/ca/redhat-uep.pem
  213. -------------------------------
  214. .Create corresponding secrets.
  215. [subs=+quotes]
  216. -------------------------------
  217. $ *oc create secret generic etc-pki-entitlement \*
  218. *--from-file=/etc/pki/entitlement/_6028779042203586857_-key.pem \*
  219. *--from-file=/etc/pki/entitlement/_6028779042203586857_.pem*
  220. secret/etc-pki-entitlement created
  221. $ *oc create secret generic rhsm-ca \*
  222. *--from-file=/etc/rhsm/ca/redhat-entitlement-authority.pem \*
  223. *--from-file=/etc/rhsm/ca/redhat-uep.pem*
  224. secret/rhsm-ca created
  225. -------------------------------
  226. .Make sure the BuildConfig mounts those secrets.
  227. [subs=+quotes]
  228. -------------------------------
  229. apiVersion: build.openshift.io/v1
  230. kind: BuildConfig
  231. ...
  232. strategy:
  233. type: Docker
  234. dockerStrategy:
  235. dockerfilePath: Containerfile-psacct
  236. from:
  237. kind: ImageStreamTag
  238. name: ubi-minimal:latest
  239. **volumes:
  240. - source:
  241. type: Secret
  242. secret:
  243. secretName: etc-pki-entitlement
  244. name: etc-pki-entitlement
  245. mounts:
  246. - destinationPath: /etc/pki/entitlement
  247. - source:
  248. type: Secret
  249. secret:
  250. secretName: rhsm-ca
  251. name: rhsm-ca
  252. mounts:
  253. - destinationPath: /etc/rhsm/ca**
  254. -------------------------------
  255. `Containerfile` instructions are written such that they should work without
  256. modification regardless of whether the build is running in `podman` on an
  257. entitled host or inside a correctly configured OpenShift builder pod.
  258. NOTE: Key thing in `Containerfile` steps is to remove `/etc/rhsm-host` at some
  259. point unless `/etc/pki/entitlement-host` contains something (such as for
  260. example, valid entitlemets). Both are symlinks to `/run/secrets`.
  261. ==== Exporter Image ====
  262. ===== Java Build =====
  263. Java build is relatively simple.
  264. Figure out what OpenJDK image is available in the cluster and create a new build.
  265. [subs=+quotes]
  266. -------------------------------
  267. $ *oc new-build openjdk-11-rhel8:1.0~https://github.com/benko/linux-metrics-exporter.git --context-dir=exporter*
  268. -------------------------------
  269. Wait for the build to complete (it's going to take quite some time to download all deps) and that's it!
  270. If you're experimenting with the code, don't forget to mark the build as incremental.
  271. [subs=+quotes]
  272. -------------------------------
  273. $ *oc patch bc/linux-metrics-exporter -p '{"spec": {"strategy": {"sourceStrategy": {"incremental": true}}}}'*
  274. -------------------------------
  275. ===== Native Build =====
  276. TBD
  277. // For the native build, you need a specific Mandrel image. Import it first.
  278. //
  279. // $ oc import-image mandrel --from=registry.redhat.io/quarkus/mandrel-21-rhel8:latest --confirm
  280. // imagestream.image.openshift.io/mandrel imported
  281. // ...
  282. ===== Publishing Image =====
  283. Make sure the internal OpenShift image registry is exposed if you want to copy the image somewhere else.
  284. [subs=+quotes]
  285. -------------------------------
  286. $ *oc patch config.imageregistry/cluster --type=merge -p '{"spec": {"defaultRoute": true}}'*
  287. -------------------------------
  288. Login to both source and target registries.
  289. [subs=+quotes]
  290. -------------------------------
  291. $ *podman login quay.io*
  292. Username: *youruser*
  293. Password: *yourpassword*
  294. Login Succeeded!
  295. $ *oc whoami -t*
  296. sha256~8tIizkcLNroDEcWXJgoPMsVYUriK1sGnJ6N94WSveEU
  297. $ podman login default-route-openshift-image-registry.apps.your.openshift.cluster
  298. Username: _this-is-irrelevant_
  299. Password: *token-pasted-here*
  300. Login Succeeded!
  301. -------------------------------
  302. Then simply copy the image using `skopeo`.
  303. [subs=+quotes]
  304. -------------------------------
  305. $ *skopeo copy \*
  306. *docker://default-route-openshift-image-registry.apps.your.openshift.cluster/project/linux-metrics-exporter:latest \*
  307. *docker://quay.io/youruser/yourimage:latest*
  308. -------------------------------