Skip to content

Commit 8ca3fe6

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

File tree

2 files changed

+179
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)