README.adoc 15 KB

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