【telegraf安装】centos主机安装和docker安装

目标

  • 在服务器以centos和docker安装telegraf

一、centos主机安装

  1. In your terminal, enter the following command to add the InfluxData repository to the yum configuration:
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
  1. Enter the following command to install telegraf from the repository.
sudo yum install telegraf
  1. The telegraf configuration file is installed at /etc/telegraf/telegraf.conf. 并修改配置,注意hostname为变量,后面操作中会进行替换,这里不用管。下面配置中的ip和port改为自己的。token和organization也需要替换。
  • 这个 token 是从 InfluxDB 中生成的,代表授权的用户或服务,允许 Telegraf 将数据写入或读取指定的 bucket。
  • organization 是 InfluxDB 中的组织名称。替换 “your organization” 为你在 InfluxDB 中的实际组织名称。
tee /etc/telegraf/telegraf.conf <<-'EOF'
[global_tags]
  nodetype = "controller" # will tag all metrics with dc=us-east-1
[agent]
  interval = "5s"
  round_interval = true
  metric_batch_size = 1000
  metric_buffer_limit = 10000
  collection_jitter = "0s"
  flush_interval = "10s"
  flush_jitter = "0s"
  precision = ""
  debug = false
  quiet = false
  logfile = ""
  hostname = "Host-CUSTOMER_IP_ADDR_IN_TEMPLATE"
  omit_hostname = false
[[outputs.influxdb_v2]]
urls = ["http://ip:port"]

## Token for authentication.
token = "your token"
organization = "your organization "
bucket = "telegraf"

## CPU cell
[[inputs.cpu]]
  percpu = false
  totalcpu = true
  collect_cpu_time = false
## Disk usage cell
[[inputs.disk]]
  ignore_fs = ["tmpfs", "devtmpfs", "devfs"]
[[inputs.mem]]

## disk IO cell
[[inputs.diskio]]

## Network
[[inputs.net]]

## porcess cell
[[inputs.processes]]

##
[[inputs.netstat]]
[[inputs.swap]]
## 系统负载信息
[[inputs.system]]
EOF
  1. 替换hostname变量
HOST_IP_ADDR=`ifconfig eth0 |grep -E 'inet '|awk '{print $2}'`
sed -i "s/CUSTOMER_IP_ADDR_IN_TEMPLATE/$HOST_IP_ADDR/g" /etc/telegraf/telegraf.conf
  1. 启动服务
systemctl restart telegraf
systemctl status telegraf

二、Docker安装telegraf

  1. 检查是否存在docker
docker --version

不存在说明未安装

  1. 安装docker
# 001 安装docker
 # 安装yum-config-manager配置工具 
yum -y install yum-utils 
# 建议使用阿里云yum源:(推荐) 
# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo 
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo 
# 安装docker-ce版本 
yum install -y docker-ce 
# 启动并开机启动 
systemctl enable --now docker 
docker --version
  1. 安装docker-compose
# 使用代理
curl -SL https://ghp.ci/https://github.com/docker/compose/releases/download/v2.30.1/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose

chmod +x /usr/local/bin/docker-compose 
docker-compose --version
  1. 使用容器化安装telegraf
WORK_HOME='/opt/test'
mkdir -p $WORK_HOME
cd $WORK_HOME
mkdir -p $WORK_HOME/telegraf/
## 上传容器镜像
## rz-be 
tar zxvf telegraf-1.30.3.tar.tgz
docker load -i telegraf-1.30.3.tar
docker tag  0ba979829ba8 telegraf:1.30.3
tee ./docker-compose.yaml <<-'EOF'
version: "3"
services:
  telegraf:
    container_name: telegraf
    image:  telegraf:1.30.3
    volumes:
      - /opt/test/telegraf/telegraf.conf:/etc/telegraf/telegraf.conf
EOF

ip和port同样需要替换。token和organization也需要替换。

tee ./telegraf/telegraf.conf <<-'EOF'
[global_tags]
  nodetype = "controller" # will tag all metrics with dc=us-east-1
[agent]
  interval = "5s"
  round_interval = true
  metric_batch_size = 1000
  metric_buffer_limit = 10000
  collection_jitter = "0s"
  flush_interval = "10s"
  flush_jitter = "0s"
  precision = ""
  debug = false
  quiet = false
  logfile = ""
  hostname = "Host-CUSTOMER_IP_ADDR_IN_TEMPLATE"
  omit_hostname = false
[[outputs.influxdb_v2]]
urls = ["http://ip:port"]

## Token for authentication.
token = "token"
organization = "organization"
bucket = "telegraf"

## CPU cell
[[inputs.cpu]]
  percpu = false
  totalcpu = true
  collect_cpu_time = false
## Disk usage cell
[[inputs.disk]]
  ignore_fs = ["tmpfs", "devtmpfs", "devfs"]
[[inputs.mem]]

## disk IO cell
[[inputs.diskio]]

## Network
[[inputs.net]]

## porcess cell
[[inputs.processes]]

##
[[inputs.netstat]]
[[inputs.swap]]
## 系统负载信息
[[inputs.system]]
EOF
HOST_IP_ADDR=`ifconfig eth0 |grep -E 'inet '|awk '{print $2}'`
sed -i "s/CUSTOMER_IP_ADDR_IN_TEMPLATE/$HOST_IP_ADDR/g" ./telegraf/telegraf.conf
# 赋予权限
chmod 777 -R ./telegraf/*
# 使用 Docker Compose 来启动容器
docker-compose -f   docker-compose.yaml  up -d
# 查看运行的容器
docker ps
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程小白呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值