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