Voiced by Amazon Polly |
Node Exporter is a Prometheus exporter for server level and OS level metrics with configurable metric collectors. It helps us in measuring various server resources such as RAM, disk space, and CPU utilization. Node exporter is a good solution to collect all the Linux server related metrics and statistics for monitoring.
Here, you will learn how to install the node exporter manually as well by Ansible playbook on a Linux server to export all node level metrics to the Prometheus server.
Customized Cloud Solutions to Drive your Business Success
- Cloud Migration
- Devops
- AIML & IoT
Prerequisites
- Prometheus server to be up and running to receive Node Exporter metrics.
- In the inbound rules of the server firewall, port 9100 should be opened for Prometheus server IP as Prometheus reads metrics on this port.
Setup Node Exporter Binary
Step 1: Go to the official release page of Prometheus Node Exporter and copy the link of the latest version of the Node Exporter package according to your OS type. Then update the command to get that package.
1 2 |
cd /tmp curl -LO https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz |
Step 2: Unpack the tar file
1 |
tar -xvf node_exporter-0.18.1.linux-amd64.tar.gz |
Step 3: Move the binary file of node exporter to /usr/local/bin location.
1 |
sudo mv node_exporter-0.18.1.linux-amd64/node_exporter /usr/local/bin/ |
Create a Custom Node Exporter Service
Step 1: Create a node_exporter service file in the /etc/systemd/system directory.
1 |
sudo vi /etc/systemd/system/node_exporter.service |
Step 2: Edit the file and add the below content in it. This will allow us to start node exporter as a service.
1 2 3 4 5 6 7 8 9 10 11 12 |
[Unit] Description=Node Exporter After=network.target [Service] User=node_exporter Group=node_exporter Type=simple ExecStart=/usr/local/bin/node_exporter [Install] WantedBy=multi-user.target |
Step 3: Register the node exporter as a service in systemctl by reloading the system daemon and start the node exporter service.
1 2 |
sudo systemctl daemon-reload sudo systemctl start node_exporter |
Step 4: check the status of node exporter service to make sure it is active, and service is running.
1 |
sudo systemctl status node_exporter |
Step 5: Finally, enable the node exporter service to the system startup so that service will be up and running even after the reboot of the machine.
1 |
sudo systemctl enable node_exporter |
Now, after starting the service, node exporter is ready to export metrics on port 9100.
To view all the server metrics, you can curl your server URL on /metrics, as shown below.
1 |
curl http://<server-IP>:9100/metrics |
Configure the Server as Target on Prometheus Server
Now that we have successfully configured the node exporter on the server and it is ready to export metrics, we must add this server as a target on the Prometheus server configuration. This configuration we will do on the Prometheus server.
Step 1: SSH into the Prometheus machine and open the prometheus.yml file which is at location /etc/prometheus.
1 |
sudo vi /etc/prometheus/prometheus.yml |
Step 2: Now, we need to create a new job for Node Exporter, which will fetch the required metrics. Add the new job under the scrape config segment as shown below. In the targets section, add the IP of your Node Exporter machine. Job name can be anything like your server hostname or IP for identification purposes but here we are using Node_Exporter.
1 2 3 4 |
- job_name: 'Node_Exporter' scrape_interval: 5s static_configs: - targets: ['<Server_IP_of_Node_Exporter_Machine>:9100'] |
Step 3: Now, restart the prometheus service to make our changes effective in the configuration using the below command.
1 |
sudo systemctl restart prometheus.service |
Now, we have configured the node exporter job in the Prometheus configuration file; you can check the targets on the Prometheus web UI (http://<prometheus-IP>:9090/targets), The updated target will be reflected there under the Node_Exporter job. Please make sure the status of the target is active. If the target status is Down, then please check the connectivity between your Prometheus server and the target server.
Also, you can use the Prometheus graph (http://<prometheus-IP>:9090/graph) to query the metrics on the web UI and get the corresponding output in the form of graphs over time as well as the value. You can try with the below query metrics.
1 2 3 4 5 |
node_memory_MemFree_bytes node_cpu_seconds_total node_filesystem_avail_bytes rate(node_cpu_seconds_total{mode="system"}[1m]) rate(node_network_receive_bytes_total[1m]) |
To install Node Exporter using an Ansible playbook, there are some prerequisites.
- There should be Ansible installed on your machine in which you want to install Node Exporter, and you must run the playbook on your localhost.
- If you are using a far Ansible workstation, the IP of your Ansible workstation should be whitelisted for SSH ( Port 22 ) in the firewall of the machine where you want to install Node Exporter.
Node_Exporter.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
--- - hosts: node-exporter become: yes vars_files: - vars.yml tasks: - name: Creating node_exporter user group group: name="{{groupId}}" become: true - name: Creating node_exporter user user: name: "{{userId}}" group: "{{groupId}}" system: yes shell: "/sbin/nologin" comment: "{{userId}} nologin User" createhome: "no" state: present - name: Install prometheus node exporter unarchive: src: "https://github.com/prometheus/node_exporter/releases/download/v{{version}}/node_exporter-{{version}}.linux-amd64.tar.gz" dest: /tmp/ remote_src: yes - name: Copy prometheus node exporter file to bin copy: src: "/tmp/node_exporter-{{ version }}.linux-amd64/node_exporter" dest: "/usr/local/bin/node_exporter" owner: "{{userId}}" group: "{{groupId}}" remote_src: yes mode: 0755 - name: Delete node exporter tmp folder file: path: '/tmp/node_exporter-{{ version }}.linux-amd64' state: absent - name: Copy systemd init file template: src: /home/ec2-user/playbook/init.service.j2 dest: /etc/systemd/system/node_exporter.service - name: Start node_exporter service service: name: node_exporter state: started enabled: yes ... |
Here, Two files are used, which are vars.yml and init.service.j2.
vars.yml: It is having variables like userId, groupId, and version of the node exporter. Location: /home/ec2-user/vars.yml (for CentOS machines)
init.service.j2: It is the service file of node exporter. Location: /home/ec2-user/playbook/init.service.j2. (If the playbook directory is not there, then it needs to be created.)
vars.yml
1 2 3 4 5 6 7 |
--- groupId: node_grp userId: node_user_1 version: "1.0.0-rc.0" serviceName: "node_exporter" exec_command: /usr/local/bin/node_exporter ... |
init.service.j2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[Unit] Description={{serviceName}} Wants=network-online.target After=network-online.target [Service] User={{ userId }} Group={{ groupId }} Restart=on-failure Type=simple ExecStart={{ exec_command }} [Install] WantedBy=multi-user.target |
To install Node Exporter with Ansible, add the host IP of the destination machine in the hosts file located at /etc/ansible/hosts and use the below command.
1 |
ansible-playbook Node_Exporter.yml |
Now, you can use this playbook to install Node Exporter in as many servers as you want at a time. Hope the steps mentioned above are clear. Watch this space for more blogs on monitoring and alerting.
f you want to learn more about Prometheus go to our website and check more courses on DevOps.
Please comment if you have any questions.
Get your new hires billable within 1-60 days. Experience our Capability Development Framework today.
- Cloud Training
- Customized Training
- Experiential Learning
WRITTEN BY Saurabh Jain
Saurabh Kumar Jain is a Subject Matter Expert working with CloudThat. He has experience in AWS, Microsoft Azure, and DevOps technologies. He is specialized in cloud security and architecture design. He also holds experience on various cloud and DevOps projects based on the cloud maturity model. He is an AWS Certified Security Speciality, AWS Certified Solutions Architect - Associate, Microsoft certified Azure Administrator and a certified Terraform Associate.
Eli
Dec 30, 2022
I want to use node_rxporter as a metrics scraper but the target location is HTTPS and has a user/password. node_exporter only uses HTTP still, I don’t find any place ussing https?
Sanket
Oct 5, 2021
I think you will need to also create node_exporter user since the service is defined to start with User / Group as node_exporter
Click to Comment