How to install node exporter in a different host and connect to prometheus

December 29, 2023
I have another device that I needed to start adding to my monitoring stack. ( Previous link ) . So that I can build another dashboard later to monitor all three major devices I use as servers for my home lab. 
Now there are several Boxes that you can chose from as metric exporter to install in the different machine. You can learn more about these here. https://prometheus.io/download/#node_exporter
Choose the URL on the basis of the device architecture and operating system that you are using . For my specific need. I need to install node exporter in my "Nakhwahp" node with Ubuntu Operating system and 64 architecture. 
Hence, My download link is https://github.com/prometheus/node_exporter/releases/download/v1.7.0/node_exporter-1.7.0.linux-amd64.tar.gz
So let's ssh into Nakhwahp 
$ ssh xecutioner@nakhwahp
wget the node exporter tarball
xecutioner@nakhwahp:~$ wget https://github.com/prometheus/node_exporter/releases/download/v1.7.0/node_exporter-1.7.0.linux-amd64.tar.gz
--2023-12-29 07:24:05--  https://github.com/prometheus/node_exporter/releases/download/v1.7.0/node_exporter-1.7.0.linux-amd64.tar.gz
Resolving github.com (github.com)... 20.205.243.166
Connecting to github.com (github.com)|20.205.243.166|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://objects.githubusercontent.com/github-production-release-asset-2e65be/9524057/01323270-6ecb-47aa-813f-52a2a89cdc64?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAVCODYLSA53PQK4ZA%2F20231229%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20231229T072406Z&X-Amz-Expires=300&X-Amz-Signature=c526aff225cc6fe14310043299076c501f25aa9b3f0ff0f119805613c1175e1d&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=9524057&response-content-disposition=attachment%3B%20filename%3Dnode_exporter-1.7.0.linux-amd64.tar.gz&response-content-type=application%2Foctet-stream [following]
--2023-12-29 07:24:06--  https://objects.githubusercontent.com/github-production-release-asset-2e65be/9524057/01323270-6ecb-47aa-813f-52a2a89cdc64?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAVCODYLSA53PQK4ZA%2F20231229%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20231229T072406Z&X-Amz-Expires=300&X-Amz-Signature=c526aff225cc6fe14310043299076c501f25aa9b3f0ff0f119805613c1175e1d&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=9524057&response-content-disposition=attachment%3B%20filename%3Dnode_exporter-1.7.0.linux-amd64.tar.gz&response-content-type=application%2Foctet-stream
Resolving objects.githubusercontent.com (objects.githubusercontent.com)... 185.199.111.133, 185.199.108.133, 185.199.109.133, ...
Connecting to objects.githubusercontent.com (objects.githubusercontent.com)|185.199.111.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 10419253 (9.9M) [application/octet-stream]
Saving to: ‘node_exporter-1.7.0.linux-amd64.tar.gz’

node_exporter-1.7 100%[=============>]   9.94M  3.68MB/s    in 2.7s

2023-12-29 07:24:11 (3.68 MB/s) - ‘node_exporter-1.7.0.linux-amd64.tar.gz’ saved [10419253/10419253]
untar the tarball 
xecutioner@nakhwahp:~$ tar -zxvf node_exporter-1.7.0.linux-amd64.tar.gz
node_exporter-1.7.0.linux-amd64/
node_exporter-1.7.0.linux-amd64/LICENSE
node_exporter-1.7.0.linux-amd64/node_exporter
node_exporter-1.7.0.linux-amd64/NOTICE
xecutioner@nakhwahp:~$ cd node_exporter-1.7.0.linux-amd64
xecutioner@nakhwahp:~/node_exporter-1.7.0.linux-amd64$ ls
LICENSE  node_exporter  NOTICE
move the tarball
xecutioner@nakhwahp:~/node_exporter-1.7.0.linux-amd64$ sudo mv node_exporter /usr/local/bin
switch to home directory and remove downloaded files 
ecutioner@nakhwahp:~/node_exporter-1.7.0.linux-amd64$ cd ..
xecutioner@nakhwahp:~$ cd ..
xecutioner@nakhwahp:~$ rm -rf  node_exporter-1.7.0.linux-amd64*
Create the user for node_exporter and have the user own node exporter binary
xecutioner@nakhwahp:~$ sudo useradd --no-create-home --shell /bin/false node_exporter
xecutioner@nakhwahp:~$ sudo chown node_exporter:node_exporter /usr/local/bin/node_exporter


Create a service in systemd 
xecutioner@nakhwahp:~$ sudo vi /etc/systemd/system/node_exporter.service
Paste the following 
[Unit]
Description=Node Exporter
Wants=network-online.target
After=network-online.target

[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/bin/node_exporter
Restart=always
RestartSec=3

[Install]
WantedBy=multi-user.target
reload the systemd 
xecutioner@nakhwahp:~$ sudo systemctl daemon-reload
Enable and start our brand new node exporter 
xecutioner@nakhwahp:~$ sudo systemctl enable node_exporter
Created symlink /etc/systemd/system/multi-user.target.wants/node_exporter.service → /etc/systemd/system/node_exporter.service.
xecutioner@nakhwahp:~$ sudo systemctl start node_exporter
Test the service
got to <IP>:<9001>/metrics
Screenshot from 2023-12-29 13-31-10.png 172 KB

Great now let's head over to our prometheus instance 
[I] ^^>>> ssh xecutioner@xecutioner-maccy                             13:32:01
xecutioner@xecutioner-maccy:~$ cd dev/portainers/promethues/

To add the recently added node exporter as target
Let's stop the running compose we had set up earlier https://kapil.codes/blog/posts/prometheus-and-grafana-to-monitoring-my-own-home-servers
xecutioner@xecutioner-maccy:~/dev/portainers/promethues$ docker-compose down
Stopping node-exporter           ... done
Stopping promethues_grafana_1    ... done
Stopping promethues_prometheus_1 ... done
Removing node-exporter           ... done
Removing promethues_grafana_1    ... done
Removing promethues_prometheus_1 ... done
Removing network promethues_default
xecutioner@xecutioner-maccy:~/dev/portainers/promethues$

Add the new host as target 
xecutioner@xecutioner-maccy:~/dev/portainers/promethues$ nvim prometheus/prometheus.yml

Screenshot from 2023-12-29 13-36-17.png 216 KB


Start the compose stack again 
xecutioner@xecutioner-maccy:~/dev/portainers/promethues$ docker-compose up -d
Creating network "promethues_default" with the default driver
Creating promethues_prometheus_1 ... done
Creating promethues_grafana_1    ... done
Creating node-exporter           ... done
Go over to the prometheus at http://100.96.226.84:9090/targets and we should see it added as data sources
Screenshot from 2023-12-29 13-57-43.png 76.1 KB

Now over to grafana and we should the new host listed
Screenshot from 2023-12-29 14-00-26.png 86.9 KB