Skip to content

Commit eb66bff

Browse files
committed
blog: add scaphandre blogpost
Signed-off-by: Mathieu Tortuyaux <[email protected]>
1 parent d66705f commit eb66bff

File tree

2 files changed

+178
-0
lines changed

2 files changed

+178
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
+++
2+
tags = ["flatcar", "energy", "monitoring"]
3+
topics = ["Linux", "monitoring"]
4+
authors = ["mathieu-tortuyaux"]
5+
title = "Measuring energy consumption"
6+
draft = false
7+
aliases = ["/blog/2022/10/flatcar-and-scaphandre"]
8+
description = "Let's dive into Scaphandre to measure power consumption of infrastructure"
9+
date = "2022-10-10T20:00:00+02:00"
10+
+++
11+
12+
In the last decade, with the increase of components inside Ops
13+
infrastructures, monitoring has started to be more than ever a central
14+
component. Having a monitored infrastructure helps to not navigate
15+
blindly inside a constellation of services and to be pro-active on
16+
issues. Monitoring can be defined as a shaped form of metrics and these
17+
metrics are often divided into two categories: applicative and system.
18+
In this blogpost, let's put on our *Scaphandre* to dive into an unusual
19+
system metric: the energy.
20+
21+
Energy is a concept widely spread across science -- in that case, we
22+
will focus on electrical energy. This one can be defined with Joules (J)
23+
or Watt (W) units. With a simple relation we can pass from one to the
24+
other:
25+
26+
$$ 1 W = 1 J / s $$
27+
28+
In 2011, a [patch](https://lwn.net/Articles/444887/) landed on the Linux
29+
kernel to introduce "intel_rapl" driver. It provides a unified interface
30+
to read the energy consumed by different [power
31+
zones](https://helda.helsinki.fi/bitstream/handle/10138/321707/RAPL_in_Action_Experiences_in_Using_RAPL_for_Power_Measurements.pd)
32+
of the CPU using powercap framework:
33+
34+
```bash
35+
cat /sys/class/powercap/intel-rapl:0*/name
36+
core # CPU cores
37+
uncore # integrated graphics, ...
38+
dram # RAM attached to the integrated memory controller
39+
package-0 # CPU entire socket
40+
```
41+
42+
Leaving the kernel to the user space, it's now possible to use
43+
high-level software like
44+
[Scaphandre](https://github.com/hubblo-org/scaphandre) to fetch and
45+
serve these metrics through various exporters (JSON, stdout, Prometheus,
46+
etc.).
47+
As seen previously, it's first required to configure the kernel to build
48+
"intel_rapl" driver and "powercap" framework -- it has been done for
49+
Flatcar in this PR:
50+
[flatcar/coreos-overlay#1801](https://github.com/flatcar-linux/coreos-overlay/pull/1801)
51+
and it's available since the release 3227.0.0.
52+
53+
Let's see how to deploy and provision Scaphandre on a Flatcar instance
54+
with QEMU/KVM. We first need to generate an Ignition configuration --
55+
It's possible to run Scaphandre directly from a Docker image. Here's a
56+
Butane configuration:
57+
58+
```yaml
59+
---
60+
variant: flatcar
61+
version: 1.0.0
62+
passwd:
63+
users:
64+
- name: core
65+
ssh_authorized_keys:
66+
- ssh-rsa ...
67+
systemd:
68+
units:
69+
- name: scaphandre-prometheus.service
70+
enabled: true
71+
contents: |
72+
[Unit]
73+
Description=Scaphandre Prometheus exporter
74+
[Service]
75+
Type=fork
76+
ExecStartPre=-/usr/bin/mkdir --parents /var/scaphandre
77+
ExecStartPre=-/usr/bin/docker stop scaphandre
78+
ExecStartPre=-/usr/bin/docker rm scaphandre
79+
ExecStartPre=/usr/bin/docker pull hubblo/scaphandre:latest
80+
ExecStart=/usr/bin/docker run \
81+
--name scaphandre \
82+
--network=host \
83+
--volume="/proc:/proc" \
84+
--volume="/var/scaphandre:/var/scaphandre" \
85+
--volume="/sys/class/powercap:/sys/class/powercap" \
86+
hubblo/scaphandre:latest \
87+
--vm prometheus
88+
[Install]
89+
WantedBy=multi-user.target
90+
- name: var-scaphandre.mount
91+
enabled: true
92+
contents: |
93+
[Unit]
94+
Description=Mount scaphandre filesystem
95+
Conflicts=umount.target
96+
Before=umount.target
97+
98+
[Mount]
99+
What=scaphandre
100+
Where=/var/scaphandre
101+
Type=9p
102+
Options=trans=virtio
103+
104+
[Install]
105+
WantedBy=multi-user.target
106+
```
107+
108+
Ignition configuration can be generated with the following command:
109+
110+
```bash
111+
butane ./config.yml -o ./ignition.json
112+
```
113+
114+
Now we have an Ignition configuration, we can follow the official
115+
documentation: [https://www.flatcar.org/docs/latest/installing/vms/libvirt/](https://www.flatcar.org/docs/latest/installing/vms/libvirt/)
116+
117+
It's required to drift a bit from the documentation to generate the XML
118+
*before* defining the domain (`--print-xml > flatcar-linux1.xml`) to
119+
add the following filesystem config to the `<devices>` block:
120+
121+
```xml
122+
<devices>
123+
<filesystem type='mount' accessmode='passthrough'>
124+
<source dir='/var/lib/libvirt/scaphandre/flatcar-linux1'/>
125+
<target dir='scaphandre'/>
126+
<readonly/>
127+
<address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/>
128+
</filesystem>
129+
...
130+
```
131+
132+
This is mandatory to bridge the guest with the host: the host will
133+
compute the energy consumption metrics for each running VM.
134+
135+
On the host, the following command needs to be run. It will create a
136+
filetree compatible with powercap for each VM found:
137+
138+
```bash
139+
$ sudo scaphandre qemu
140+
```
141+
142+
(For Gentoo users, an `app-metrics/scaphandre` package is available on
143+
`::guru` overlay)
144+
145+
Default filetree is created in the following location:
146+
`/var/lib/libvirt/scaphandre/${DOMAIN_NAME}` - and with the current
147+
example, we have the following:
148+
149+
```bash
150+
$ tree /var/lib/libvirt/scaphandre/flatcar-linux1
151+
/var/lib/libvirt/scaphandre/flatcar-linux1
152+
├── intel-rapl:0
153+
│   └── energy_uj
154+
└── intel-rapl:0:0
155+
2 directories, 1 file
156+
```
157+
158+
All components are now in place, we can define and start the domain:
159+
160+
```bash
161+
$ virsh define --file flatcar-linux1.xml
162+
$ virsh start flatcar-linux1
163+
```
164+
165+
Once the instance up, Prometheus metrics should be available at
166+
<http://DOMAIN_IP:8080/metrics> and they can be interfaced with usual
167+
tools like Grafana. Voilà !
168+
169+
![](/media/scaphandre-2022/grafana.png)
170+
171+
Displaying metrics and graphs is "easy": give time, datasources and
172+
dashboards to someone creative, you'll get beautiful graphs, histograms,
173+
and table by the end of the day but certainly without any sense.
174+
Complexity resides in the understanding of this data and actions you can
175+
take based on it: infrastructure energy consumption is one of these
176+
metrics that reveal the thin border between digital and *real* worlds
177+
and how it's possible to impact one from the other by taking the right
178+
actions.
75.6 KB
Loading

0 commit comments

Comments
 (0)