1. Background
Node Exporter is a common component in the Prometheus ecosystem for collecting host metrics, but by default it does not provide access authentication. This article describes how to add basic authentication to Node Exporter in a Kubernetes environment to improve security.
2. Configuring Credentials for Node Exporter
2.1 Generating an Encrypted Password
Use the htpasswd tool to generate an encrypted password:
| |
You need to enter a password here, and the generated output will be an encrypted password string.
2.2 Creating the web-config.yml Configuration File
First, create a configuration file containing the basic authentication user information:
| |
Add the following content:
| |
Here default is the username, and the string after it is the encrypted password generated by htpasswd.
2.3 Creating a ConfigMap
Use web-config.yml to create a ConfigMap so that it can be used in the Node Exporter DaemonSet:
| |
2.4 Modifying the Node Exporter DaemonSet
Edit the Node Exporter DaemonSet configuration to load the authentication configuration:
| |
Add the startup argument and mount the ConfigMap:
| |
3. Adding Credentials on the Scrape Side
3.1 Creating the Authentication Credential Secret
Create a Secret containing the access credentials for Prometheus:
| |
3.2 Configuring the ServiceMonitor
Update the ServiceMonitor configuration to use the authentication credentials:
| |
Add the basicAuth configuration:
| |
