README.adoc 17 KB

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