# Prometheus 将数据远程写入 InfluxDB 存储

## **Prometheus 将数据远程写入 InfluxDB 存储** <a href="#prometheus-e5-b0-86-e6-95-b0-e6-8d-ae-e8-bf-9c-e7-a8-8b-e5-86-99-e5-85-a5-influxdb-e5-a-d-98-e5-82-a" id="prometheus-e5-b0-86-e6-95-b0-e6-8d-ae-e8-bf-9c-e7-a8-8b-e5-86-99-e5-85-a5-influxdb-e5-a-d-98-e5-82-a"></a>

* InfluxDB 1.x 提供了直接写入数据的 http 接口，Prometheus 可以直接写入，但是 InfluxDB 2.x 删除了这个接口。
* InfluxDB 2.x 引入了新的组件 Telegraf，必须借助 Telegraf 才可以将 Prometheus 的数据写入到 InfluxDB 2.x 中。

### **InfluxDB** 2.0 使用 Prometheus 架构图 <a href="#influxdb-2.0-e4-bd-bf-e7-94-a8-prometheus-e6-9e-b6-e6-9e-84-e5-9b-be" id="influxdb-2.0-e4-bd-bf-e7-94-a8-prometheus-e6-9e-b6-e6-9e-84-e5-9b-be"></a>

<figure><img src="https://2134947750-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvILwbD2PrkBCkSitM3m4%2Fuploads%2FqzUXwkR2dAtXWI6efL8k%2Fimage.png?alt=media&#x26;token=47f437ef-ac84-44fe-9f54-4dcce653a396" alt=""><figcaption></figcaption></figure>

### **InfluxDB** 2.0 不使用 Prometheus 架构图 <a href="#influxdb-2.0-e4-b8-8d-e4-bd-bf-e7-94-a8-prometheus-e6-9e-b6-e6-9e-84-e5-9b-be" id="influxdb-2.0-e4-b8-8d-e4-bd-bf-e7-94-a8-prometheus-e6-9e-b6-e6-9e-84-e5-9b-be"></a>

<figure><img src="https://2134947750-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvILwbD2PrkBCkSitM3m4%2Fuploads%2Fp1h6sqbKbZbQJvfJvEdi%2Fimage.png?alt=media&#x26;token=8f163764-b4e8-49e2-ad60-1d3baeb3f741" alt=""><figcaption></figcaption></figure>

### **InfluxDB** 2.0 与 **Telegraf 如何结合配置** <a href="#influxdb-2.0-e4-b8-8e-telegraf-e5-a6-82-e4-bd-95-e7-bb-93-e5-90-88-e9-85-8d-e7-bd-ae" id="influxdb-2.0-e4-b8-8e-telegraf-e5-a6-82-e4-bd-95-e7-bb-93-e5-90-88-e9-85-8d-e7-bd-ae"></a>

> 参考： <https://blog.csdn.net/catoop/article/details/127533069>

## 实操 InfluxDB 1.x 版本 <a href="#e5-ae-9e-e6-93-8d-influxdb-1.x-e7-89-88-e6-9c-ac" id="e5-ae-9e-e6-93-8d-influxdb-1.x-e7-89-88-e6-9c-ac"></a>

```yml
version: '3'

services:
  influxdb:
    image: 'influxdb:1.8'
    container_name: influxdb
    network_mode: host
    volumes:
      - './influxdb:/var/lib/influxdb'
    #ports:
    #  - '8086:8086'

  mysql:
    image: mysql:8.0
    restart: always
    container_name: mysql
    network_mode: host
    environment:
      TZ: Asia/Shanghai
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: grafana
      MYSQL_USER: grafana
      MYSQL_PASSWORD: grafana
      MYSQL_ROOT_HOST: '%'
      MYSQL_DEFAULT_AUTHENTICATION_PLUGIN: mysql_native_password  # 设置使用 MySQL 5.7 的加密算法
    volumes:
      - ./mysql:/var/lib/mysql
    #ports:
    #  - "3306:3306

  prometheus:
    image: prom/prometheus
    container_name: prometheus
    network_mode: host
    environment:
      TZ: Asia/Shanghai
    #ports:
    #  - "9090:9090"
    volumes:
      #- ./prometheus/k8s.token:/etc/prometheus/k8s.token 
      - ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'

  grafana:
    image: grafana/grafana
    #image: grafana/grafana:8.5.27
    container_name: grafana
    network_mode: host
    volumes:
      - ./grafana/grafana.ini:/etc/grafana/grafana.ini
    depends_on:
      - mysql
    #ports:
    #  - "53000:3000"
```

```bash
[root@worker1 aaa]# cat prometheus/prometheus.yml 
global:
  scrape_interval: 15s

remote_write:
  - url: "http://<influxdb>:8086/api/v1/prom/write?db=prometheus"

remote_read:
  - url: "http://<influxdb>:8086/api/v1/prom/read?db=prometheus"

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']

  - job_name: 'test'
    static_configs:
      - targets: ['8.8.8.8:9100']
```

```bash
[root@worker1 aaa]# cat grafana/grafana.ini
......略
[database]
type = mysql
host = you_mysql_adderss:3306
name = grafana
user = grafana
password = grafana

[paths]
data = mysql
```

> <http://xxxx:3000>
>
> admin/admin
>
> 测试: 启动，收集一点数据，再卸载容器，再启动容器，查看刚刚收集的数据是否存在，存在就说明持久化成功
