README.adoc 17 KB

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