InfluxDB+TDengine+Telegraf+Grafana 搭建监控平台

发布时间 2023-08-01 11:13:30作者: freshfresh
Linux CentOS环境

1.Install InfluxDB

(1)首先访问 InfluxDB 官网(点击访问),找到选择合适的版本

参考文档:https://docs.influxdata.com/influxdb/v2.7/install/?t=Docker
下载并运行 InfluxDBv 2.7
docker run --name influxdb -p 8086:8086 influxdb:2.7.0
将数据持久化到 InfluxDB 容器之外
  1. 创建一个新目录以将数据存储在其中并导航到该目录。
mkdir path/to/influxdb-docker-data-volume && cd $_
例如:mkdir /root/influxdb-docker-data-volume && cd $_
在新目录中,运行带有标志的 InfluxDB Docker 容器 将容器内部的数据保存到当前工作目录 主机文件系统。--volume/var/lib/influxdb2
docker run \
--name influxdb \
-p 8086:8086 \
--volume $PWD:/var/lib/influxdb2 \
influxdb:2.7.0
然后进入界面进行配置
 
 
 
 

2.Install TDengine

参考:https://docs.taosdata.com/get-started/docker/

通过 Docker 快速体验 TDengine

启动 TDengine
如果已经安装了 Docker,首先拉取最新的 TDengine 容器镜像:
docker pull tdengine/tdengine:latest
或者指定版本的容器镜像:
docker pull tdengine/tdengine:3.0.1.4
docker run -d -p 6030:6030 -p 6041:6041 -p 6043-6049:6043-6049 -p 6043-6049:6043-6049/udp tdengine/tdengine
注意:TDengine 3.0 服务端仅使用 6030 TCP 端口。6041 为 taosAdapter 所使用提供 REST 服务端口。6043-6049 为 taosAdapter 提供第三方应用接入所使用端口,可根据需要选择是否打开。
确定该容器已经启动并且在正常运行。
docker ps
进入该容器并执行 bash
docker exec -it <container name> bash
然后就可以执行相关的 Linux 命令操作和访问 TDengine。

3.Install Telegraf

参考:https://docs.influxdata.com/telegraf/v1.27/install/
RedHatCentOS使用包管理器安装最新的稳定版本的 Telegraf:yum
cat <<EOF | sudo tee /etc/yum.repos.d/influxdb.repo
[influxdb]
name = InfluxData Repository - Stable
baseurl = https://repos.influxdata.com/stable/\$basearch/main
enabled = 1
gpgcheck = 1
gpgkey = https://repos.influxdata.com/influxdata-archive_compat.key
EOF
将存储库添加到配置后安装 telegraf:yum
sudo yum install telegraf
1.运行以下命令以创建配置文件:
telegraf --sample-config > telegraf.conf
2.找到配置文件。位置因系统而异:
  • Linux debian 和 RPM 软件包:/etc/telegraf/telegraf.conf
 
3.使用 或文本编辑器编辑配置文件。由于此示例使用 InfluxDB V2 输出插件,因此我们需要将 InfluxDB URL、身份验证令牌、组织和存储桶详细信息添加到配置文件的此部分。vim
  1. 对于此示例,使用标志指定两个输入( 和 )。 指定 InfluxDB 作为带有标志的输出。cpumem--input-filter--output-filter
telegraf --sample-config --input-filter cpu:mem --output-filter influxdb_v2 > telegraf.conf
生成的配置将收集 CPU 和内存数据并将其发送到 InfluxDB V2。
设置环境变量
在 Telegraf 环境变量文件 ():/etc/default/telegraf
USER="root"
INFLUX_URL="http://127.0.0.1:8086"
INFLUX_SKIP_DATABASE_CREATION="true"
INFLUX_PASSWORD="12345678"
在 Telegraf 配置文件 ():/etc/telegraf.conf
[global_tags]  user = "${USER}"
[[inputs.mem]]
[[outputs.influxdb]] 
 urls = ["${INFLUX_URL}"]  
 skip_database_creation = ${INFLUX_SKIP_DATABASE_CREATION}  
 password = "${INFLUX_PASSWORD}"
上面的环境变量将以下配置设置添加到 Telegraf:
[global_tags]  user = "root"
[[outputs.influxdb]]  
urls = "http://127.0.0.1:8086"  
skip_database_creation = true  
password = "11111111"
接下来,您需要启动 Telegraf 服务并将其定向到您的配置文件:
Linux(systemd installs)
systemctl start telegraf
systemctl restart telegraf 启动
systemctl status telegraf.service 查看状态
systemctl restart telegraf 重启

4.Install Grafana

参考:https://grafana.com/docs/grafana/latest/setup-grafana/installation/docker/
Run Grafana Docker image
Run Grafana via Docker CLI
To run the latest stable version of Grafana, run the following command:
docker run -d -p 3000:3000 --name=grafana grafana/grafana-enterprise
Where:
run = run directly from the command line
d = run in the background
p = assign the port number, which in this case is 3000
name = assign a logical name to the container, for example, grafana
grafana/grafana-enterprise = the image to run in the container
# The `docker ps` command shows the processes running in Dockerdocker ps# This will display a list of containers that looks like the following:
CONTAINER ID   IMAGE  COMMAND   CREATED  STATUS   PORTS    NAMES
cd48d3994968   grafana/grafana-enterprise   "/run.sh"   8 seconds ago   Up 7 seconds   0.0.0.0:3000->3000/tcp   grafana

# To stop the grafana container run the command# docker stop CONTAINER-ID or use# docker stop NAME, which is `grafana` as previously defined
docker stop grafana

保存您的grafana数据

使用 Docker 容器时,默认情况下,其数据是临时的。如果未指定存储信息的位置,则在停止 Docker 容器时,所有 Grafana 数据都将丢失。为避免丢失数据,您可以为容器设置持久存储绑定挂载
注意:虽然这两种方法相似,但略有不同。如果您希望存储完全由 Docker 管理,并且只能通过 Docker 容器和 Docker CLI 访问,则应选择使用持久存储。但是,如果您需要完全控制存储,并希望允许 Docker 以外的其他进程访问或修改存储层,那么绑定挂载是您环境的正确选择。

使用持久存储(推荐)

建议使用持久存储,因为如果没有它,一旦容器关闭,所有数据都将丢失。如果您希望 Docker 服务完全管理存储卷,请使用此方法。
要使用持久存储,请完成以下步骤:
1.通过运行以下命令创建 Grafana Docker 卷:grafana-storage
# create a persistent volume for your data
docker volume create grafana-storage

# verify that the volume was created correctly# you should see a json output
docker volume inspect grafana-storage
2.通过运行以下命令启动 Grafana 容器:
# start grafana
docker run -d -p 3000:3000 --name=grafana \
--volume grafana-storage:/var/lib/grafana \
grafana/grafana-enterprise
通过 Docker Compose 运行 Grafana
Docker Compose 是一种软件工具,可以轻松定义和共享由多个容器组成的应用程序。它通过使用 YAML 文件(通常称为 )来工作,该文件列出了组成应用程序的所有服务。您可以使用单个命令以正确的顺序启动容器,并使用另一个命令关闭它们。有关使用 Docker Compose 的好处以及如何使用它的详细信息,请参阅使用 Docker Composedocker-compose.yaml
开始之前
要通过 Docker Compose 运行 Grafana,请在您的计算机上安装撰写工具。若要确定撰写工具是否可用,请运行以下命令:
docker compose version
运行 Grafana 的最新稳定版本
要使用 Docker Compose 运行 Grafana 的最新稳定版本,请完成以下步骤:
1.创建一个文件。docker-compose.yaml
# first go into the directory where you have created this docker-compose.yaml file
cd /path/to/docker-compose-folder
例如:cd /root/桌面/docker-compose-folder
# now create the docker-compose.yaml file
touch docker-compose.yaml
2.现在,将以下代码添加到文件中。docker-compose.yaml
version: "3.8"
services:
  grafana:
    image: grafana/grafana-enterprise
    container_name: grafana
    restart: unless-stopped
    ports:
     - '3000:3000'
3.要运行 ,请运行以下命令:docker-compose.yaml
# start the grafana container
docker compose up -d
where:
d = 分离模式
up = 启动并运行容器
要确定 Grafana 是否正在运行,请打开浏览器窗口并键入 。应显示登录屏幕。IP_ADDRESS:3000
若要停止 Grafana 容器,请运行以下命令:
docker compose down
使用持久存储(推荐)
建议使用持久存储,因为没有它,一旦容器关闭,所有数据都将丢失。如果您希望 Docker 服务完全管理存储卷,请使用此方法。
完成以下步骤以使用持久存储。
创建文件docker-compose.yaml
# first go into the directory where you have created this docker-compose.yaml file
cd /path/to/docker-compose-folder

# now create the docker-compose.yaml file
touch docker-compose.yaml
将以下代码添加到文件中。docker-compose.yaml
version: '3.8'
services:
  grafana:
    image: grafana/grafana-enterprise
    container_name: grafana
    restart: unless-stopped
    ports:
       - '3000:3000'
     volumes:
     - grafana_data:/var/lib/grafana
volumes:
grafana_data: {}
保存文件并运行以下命令:
docker compose up -d