README.adoc 15 KB

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