|
| 1 | +# Bare Metal Setup of Prometheus and cAdvisor |
| 2 | + |
| 3 | +## cAdvisor Configuration |
| 4 | +[cAdvisor](https://github.com/google/cadvisor) needs to be running on each host on which you want to monitor running containers. |
| 5 | +We are going to setup cAdvisor as a systemd service. |
| 6 | + |
| 7 | +Follow instructions [here](https://github.com/google/cadvisor/blob/master/docs/development/build.md) to build cAdvisor from source. |
| 8 | +Once built, copy the executable into _/usr/local/bin_. |
| 9 | + |
| 10 | +Create a systemd file named _cadvisor.service_ with the below contents in _/etc/systemd/system/_. |
| 11 | + |
| 12 | +**/etc/systemd/system/cadvisor.service** |
| 13 | +```shell |
| 14 | +[Unit] |
| 15 | +Description=cAdvisor |
| 16 | +Wants=network-online.target |
| 17 | +After=network-online.target docker.service |
| 18 | + |
| 19 | +[Service] |
| 20 | +Type=simple |
| 21 | +ExecStart=/usr/local/bin/cadvisor -port 9090 -store_container_labels=false -whitelisted_container_labels=LABEL, [LABEL, ...] -docker_only=true |
| 22 | + |
| 23 | +[Install] |
| 24 | +WantedBy=multi-user.target |
| 25 | +``` |
| 26 | +1. cAdvisor is being configured to run with root privileges to be able to whitelist docker labels. |
| 27 | +2. `-docker\_only=true` tells cAdvisor to report only docker container metrics. |
| 28 | +3. [-whitelisted\_container\_labels](https://github.com/google/cadvisor/blob/90f391fddf71801f76f408d8ed191ddc006df323/cmd/cadvisor.go#L72) are a comma separated list of container labels that you assign to containers that need to be monitored. You can then provide these labels are dedicated labels to Task Ranker that will use them as labels in the Prometheus query string. |
| 29 | + |
| 30 | +Run the below command on each host to launch cAdvisor. |
| 31 | +```commandline |
| 32 | +sudo systemctl start cadvisor |
| 33 | +``` |
| 34 | + |
| 35 | +## Prometheus Configuration |
| 36 | + |
| 37 | +Create a directory called _prometheus_ in _/etc_. |
| 38 | +On the host machine from which you will be collecting container metrics, store the below Prometheus configuration file in _/etc/prometheus/prometheus.yml_. |
| 39 | +Checkout [Prometheus configuration docs](https://github.com/prometheus/prometheus/blob/master/docs/configuration/configuration.md) to get more help on configuring Prometheus. |
| 40 | +```shell |
| 41 | +scrape_configs: |
| 42 | + - job_name: 'prometheus' |
| 43 | + scrape_interval: 1s |
| 44 | + static_configs: |
| 45 | + - targets: ['localhost:9090'] |
| 46 | + - job_name: '<host1>-cadvisor' |
| 47 | + scrape_interval: 1s |
| 48 | + static_configs: |
| 49 | + - targets: ['<host1-ip>:9090'] |
| 50 | + - job_name: '<host2>-cadvisor' |
| 51 | + scrape_interval: 1s |
| 52 | + static_configs: |
| 53 | + - targets: ['<host2-ip>:9090'] |
| 54 | + ... |
| 55 | +``` |
| 56 | + |
| 57 | +Use Prometheus [Makefile targets](https://github.com/prometheus/prometheus/blob/master/Makefile) to build from source.<br> |
| 58 | +Run the below command to create the directory in which Prometheus stores its time series database. |
| 59 | +```commandline |
| 60 | +mkdir /var/lib/prometheus |
| 61 | +``` |
| 62 | +We will now setup Prometheus as a systemd service. Create a systemd file named _prometheus.service_ with the below contents in _/etc/systemd/system_. |
| 63 | + |
| 64 | +**/etc/systemd/system/prometheus.service** |
| 65 | +```shell |
| 66 | +[Unit] |
| 67 | +Description=Prometheus |
| 68 | +Wants=network-online.target |
| 69 | +After=network-online.target |
| 70 | + |
| 71 | +[Service] |
| 72 | +User=prometheus |
| 73 | +Group=prometheus |
| 74 | +ExecStart=/usr/local/bin/prometheus \ |
| 75 | + --config.file /etc/prometheus/prometheus.yml \ |
| 76 | + --storage.tsdb.path /var/lib/prometheus/ \ |
| 77 | + --web.console.templates=/etc/prometheus/consoles \ |
| 78 | + --web.console.libraries=/etc/prometheus/console_libraries |
| 79 | + |
| 80 | +[Install] |
| 81 | +WantedBy=multi-user.target |
| 82 | +``` |
| 83 | + |
| 84 | +Run the below command on each host to launch prometheus. |
| 85 | +```commandline |
| 86 | +sudo systemctl start prometheus |
| 87 | +``` |
| 88 | + |
| 89 | +You should now be able to hit [http://localhost:9090](http://localhost:9090) to see the Prometheus UI. |
0 commit comments