README.adoc 17 KB

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