tencent cloud

Feedback

NGINX Exporter Integration

Last updated: 2024-10-30 16:00:58

    Overview

    Nginx exposes some monitoring metrics through the stub_status page. Nginx Prometheus Exporter collects metrics from a single Nginx instance, converts them into Prometheus-compatible monitoring data, and exposes such data to the Prometheus service through the HTTP protocol for collection. Through Exporter, key monitoring metrics can be reported for exception alarming and dashboard display.

    Prerequisites

    Enabling the NGINX stub_status Feature

    Note:
    1. The following example is for Nginx deployed in Tencent Kubernetes Engine (TKE). For other deployment methods, adjust the login and configuration modification methods accordingly.
    2. For TKE-related operations, see the TKE documentation.
    Because Nginx Prometheus Exporter monitors Nginx through the stub_status module of Nginx, you need to ensure that the stub_status module has been enabled for the Nginx service. The steps for enabling this module are as follows:
    1. Log in to the TKE console.
    2. Click Cluster in the left sidebar, find the cluster where the Nginx server is located, enter the cluster, and find the Nginx server.
    3. Log in to the Nginx server and execute the following command to check whether this module has been enabled for Nginx:
    nginx -V 2>&1 | grep -o with-http_stub_status_module
    If with-http_stub_status_module is output in the terminal, the stub_status module has been enabled for Nginx.
    If no result is output, you can use the --with-http_stub_status_module parameter to configure and compile Nginx again from the source code. The example is as follows:
    ./configure \\
    \\ ## Command required to compile nginx previously.
    --with-http_stub_status_module
    make
    sudo make install
    4. If the Nginx service-related ConfigMap is not added, you can log in to the Nginx server, copy the default.conf configuration information in the configuration directory (/etc/nginx/conf.d for the official image), create a ConfigMap, and add the configuration information to the ConfigMap. For the ConfigMap operation guide, see ConfigMap Management.
    5. After you confirm that the stub_status module is enabled, add the following configuration to default.conf of ConfigMap. The example is as follows:
    server {
    listen 8080; # Adjust the configuration based on the business situation.
    listen [::]:8080; # Adjust the configuration based on the business situation.
    server_name localhost; # Adjust the configuration based on the business situation.
    location = /stub_status { # Adjust the specific path based on the business situation.
    stub_status;
    }
    }
    The configuration example in ConfigMap is as follows:
    Search for the required CAM policy as needed, and click to complete policy association.
    
    
    
    6. After modifying the configuration, find the Nginx server and click More > Redeployment to reload the configuration. For non-TKE environments, execute the following commands to reload the configuration:
    nginx -t
    nginx -s reload
    7. After the preceding steps are completed, log in to the Nginx server and execute the following command to view the statistical results of Nginx's working status since the last startup.
    curl http://localhost:8080/stub_status ## Adjust the command based on the configuration content in the configuration file.
    Active connections: 45
    server accepts handled requests
    1056958 1156958 4491319
    Reading: 0 Writing: 25 Waiting : 7

    Connection Method

    Method 1: One-Click Installation (Recommended)

    Directions

    1. Log in to TMP Console.
    2. Select the corresponding Prometheus instance from the instance list.
    3. Go to the instance details page, select Data Collection > Integration Center.
    4. Search for Nginx in the integration center, and click it to pop up an installation window.
    5. On the Installation tab of the pop-up window, fill in the metric name, address, path, and other information, and click Save.
    Search for the required CAM policy as needed, and click to complete policy association.
    
    
    

    Configuration Instructions

    Parameters
    Description
    name
    Exporter name, which should meet the following requirements:
    The name should be unique.
    The name should conform to the following regular expression: '^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$'.
    address
    Nginx service connection address.
    path
    Nginx service status path, which is specified in the configuration.
    user name
    Username for HTTP authentication of the Nginx service.
    password
    Password for HTTP authentication of the Nginx service.
    tag
    Custom labels for metrics.

    Method 2: Custom Installation

    Note:
    TKE is recommended for convenient installation and management of the Exporter.

    Prerequisites

    A TKE cluster has been created in the region and VPC of the corresponding Prometheus instance, and a namespace has been created for the cluster.
    In the TMP Console > select the corresponding Prometheus instance > Data Collection > Integrate with TKE to find the corresponding container cluster and complete the cluster association operation. See the guide Associate Cluster for reference.

    Directions

    Step 1: Deploying the Exporter
    1. Log in to the TKE console.
    2. Click Cluster in the left sidebar.
    3. Click the ID/name of the cluster whose access credential is required to go to the management page of the cluster.
    4. Follow the steps below to deploy Nginx Exporter and verify the deployment status.
    Step 2: Deploying the Nginx Exporter
    1. Choose Workload > Deployment in the left sidebar to enter the Deployment page.
    2. Click Create via YAML in the upper right corner of the page to create a YAML file, and select the corresponding namespace for server deployment. The following part shows how to deploy the Exporter by using a YAML file. Sample configurations are as follows:
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    labels:
    k8s-app: nginx-exporter # Use the actual name based on business needs. It is recommended to include the information on the corresponding Nginx instance.
    name: nginx-exporter # Use the actual name based on business needs. It is recommended to include the information on the corresponding Nginx instance.
    namespace: nginx-demo # Use the actual namespace based on business needs.
    spec:
    replicas: 1
    selector:
    matchLabels:
    k8s-app: nginx-exporter # Use the actual name based on business needs. It is recommended to include the information on the corresponding Nginx instance.
    template:
    metadata:
    labels:
    k8s-app: nginx-exporter # Use the actual name based on business needs. It is recommended to include the information on the corresponding Nginx instance.
    spec:
    containers:
    - args:
    - --web.listen-address=:8080
    - --nginx.scrape-uri=http://127.0.0.1:8080/stub_status # Use the actual address corresponding to the Nginx instance based on business needs.
    image: ccr.ccs.tencentyun.com/rig-agent/common-image:nginx-exporter-v1.1.0
    name: nginx-exporter
    ports:
    - containerPort: 9113
    name: metric-port
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    dnsPolicy: ClusterFirst
    imagePullSecrets:
    - name: qcloudregistrykey
    restartPolicy: Always
    schedulerName: default-scheduler
    securityContext: {}
    terminationGracePeriodSeconds: 30
    
    Validation
    1. Click the Deployment created in the previous step on the Deployment page to go to the Deployment management page.
    2. Click the Log tab. The Exporter is started, and the corresponding access address is exposed, as shown below:
    Search for the required CAM policy as needed, and click to complete policy association.
    
    
    
    3. Click the Pod tab to enter the Pod page.
    4. Click Remote login to in the operation bar to log in to the Pod. Execute the following wget command on the command line interface to access the exposed Exporter address. In this way, data of corresponding Nginx metrics can be collected. If no data is collected, check whether the connection string is correct. The command is as follows:
    wget -qO- http://localhost:8080/metrics
    The successful outcome is shown in the following figure:
    Search for the required CAM policy as needed, and click to complete policy association.
    
    
    Step 4: Adding a Collection Task
    1. Log in to the TMP console and select the corresponding Prometheus instance to go to the management page.
    2. Choose Data Collection > Integrate with TKE, select the associated cluster, and choose Data Collection Configuration > Customize Monitoring Configuration > Via YAML to add a collection task.
    3. Add a PodMonitor via service discovery to define the collection task. The YAML example is as follows:
    apiVersion: monitoring.coreos.com/v1
    kind: PodMonitor
    metadata:
    name: nginx-exporter # Enter a unique name.
    namespace: cm-prometheus # Pay-as-you-go instance: Use the namespace of the cluster. Monthly subscription instance (no longer available): The namespace is fixed. Do not change it.
    spec:
    podMetricsEndpoints:
    - interval: 30s
    port: metric-port # Enter the port of the Prometheus Exporter in the Pod YAML file.
    path: /metrics # Enter the path of the Prometheus Exporter. Default value: /metrics.
    relabelings:
    - action: replace
    sourceLabels:
    - instance
    regex: (.*)
    targetLabel: instance
    replacement: 'crs-xxxxxx' # Enter the information on the corresponding Nginx instance.
    namespaceSelector: # Select the namespace where the Pod to be monitored is located.
    matchNames:
    - nginx-demo
    selector: # Enter the labels of the Pod to be monitored to locate the target Pod.
    matchLabels:
    k8s-app: nginx-exporter
    

    Viewing Monitoring Information

    Prerequisites

    The Prometheus instance has been bound to a Grafana instance.

    Directions

    1. Log in to the TMP console and select the corresponding Prometheus instance to go to the management page.
    2. On the Basic Info page of the instance, find the bound Grafana address, open it, and log in to Grafana. Then, find the Nginx instance monitoring panel in the middleware folder to view relevant monitoring data of the instance, as shown below:
    
    

    Configure Alarm

    TMP supports configuring alerting rules based on the actual business situation. For details, see Creating Alerting Rules.

    Appendix: Data Collection Parameters of Nginx Exporter

    Global Configuration Parameters

    Name
    Description
    web.telemetry-path
    Path for exposing metrics. Default value /metrics.
    nginx.scrape-uri
    URL for Nginx metric collection. Default value: http://127.0.0.1:8080/stub_status.
    [no-]nginx.plus
    Whether to enable Nginx Plus. Default value: enabled.
    [no-]nginx.ssl-verify
    Whether to verify the SSL certificate.
    nginx.ssl-ca-cert
    SSL certificate path.
    nginx.ssl-client-cert
    SSL certificate path.
    nginx.ssl-client-key
    SSL certificate path.
    nginx.timeout
    Nginx metric collection timeout interval.
    prometheus.const-label
    Tag to be used for each metric, which is in the format of label=value. One tag can be used multiple times.
    [no-]web.systemd-socket
    Use a systemd socket listener instead of a port listener (Linux only).
    web.listen-address
    Listening address. Default value: 9113.
    web.config.file
    Configuration file path. TLS or authentication can be enabled. (This parameter is used for testing.)
    log.level
    Log level. Default value: info.
    log.format
    Log message output format. Valid values: logfmt and json. Default value: logfmt.
    version
    Printed Apache version information.
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support