背景

说明

某些情况下,因项目环境较多,且因网络安全要求,各环境间互不相通,导致无法一个server同时监控所有node,这时候就需要部署多个server,为了简单方便,将已有的server直接打包到新环境,稍作修改就能使用当然是最理想的,因此才有了此篇记录。

涉及服务

prometheus:prometheus-2.23.0.linux-amd64
node_exporter:node_exporter-1.0.1.linux-amd64
grafana:grafana-7.3.6.linux-amd64

完整版目录树

先附一个完整版的目录树,以便下边的操作更直观

[root@localhost prome]$ tree -L 2
.
├── control
├── grafana
│   ├── bin
│   ├── conf
│   ├── data
│   ├── grafana.pid
│   ├── grafana-server.pid
│   ├── LICENSE
│   ├── node_exporter.pid
│   ├── NOTICE.md
│   ├── plugins-bundled
│   ├── prometheus.pid
│   ├── public
│   ├── README.md
│   ├── scripts
│   ├── VERSION
│   └── watchdog
├── nOde_exporter
│   ├── LICENSE
│   ├── node_exporter
│   ├── node_exporter.pid
│   ├── NOTICE
│   └── watchdog
└── pRometheus├── console_libraries├── consoles├── data├── LICENSE├── nohup.out├── NOTICE├── prometheus├── prometheus.pid├── prometheus-server.pid├── prometheus.yml├── promtool└── watchdog12 directories, 24 files

详情

废话不多说,以下为完整搭建过程,附相关脚本

一、替换yum源

因软件源多为国外,执行下载安装时速度较慢,因此建议替换yum源,当然如果不在乎下载速度慢如蜗牛或无休止的下载失败,可以不替换。
以root用户登录服务器

a. 备份原来的源

mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bk

b. 下载阿里源

cd /etc/yum.repos.d
wget -nc http://mirrors.aliyun.com/repo/Centos-7.repo

c. 更改为yum默认源

mv Centos-7.repo CentOS-Base.repo

d. 更新本地缓存

#全部清除
yum clean all
#缓存yum包信息到本机,提高搜索速度
yum makecache

二、安装golang环境

执行yum安装命令:

yum install golang -y

如报错如下:

原因是缺少 EPEL源 ,所以软件仓库中找不到安装的软件包,所以要先安装一下epel源

rpm --import https://mirror.go-repo.io/centos/RPM-GPG-KEY-GO-REPO
curl -s https://mirror.go-repo.io/centos/go-repo.repo | tee /etc/yum.repos.d/go-repo.repo

再次执行yum install golang -y
安装完成后,执行go version查看版本信息

$ go version
go version go1.15.5 linux/amd64

三、安装Prometheus

1.下载对应版本

官网地址:https://prometheus.io/download/
我使用的是centos7,所以选择linux-amd64

按照公司规范(或个人习惯),将包现在到指定目录下,我们的习惯是软件服务一般放置在/opt目录下,方便统一管理

wget https://github.com/prometheus/prometheus/releases/download/v2.23.0/prometheus-2.23.0.linux-amd64.tar.gz

海外的包,下载速度感人,如果有资源的话,还是利用相关资源下载比较好(比如东南亚的海外服务器),实验证明,源地址直接下载的速度220KB/s,新加坡服务器下载速度在300400KB/s,本地从新加坡服务器下载速度400~500KB/s

2.解压缩包

tar -zxvf prometheus-2.23.0.linux-amd64.tar.gz

创建一个集合目录用以存放Prometheus、grafana等组件

mkdir prome

将包移动到目录下并改名(个人喜好,感觉更方便)

# pRometheus目录改成这样,是为了防止脚本判断错误
mv prometheus-2.23.0.linux-amd64 prome/pRometheus

3.关闭防火墙

在配置过程中建议关闭防火墙,配置完成后在开启防火墙并开放相应端口

systemctl stop firewalld
systemctl disable firewalld

4.启动服务

cd /opt/prome/pRometheus
./prometheus

5. 打开页面

浏览器打开IP:9090
能打开这个界面表示服务启动成功

6.服务自启动与监控

我们刚才看到了,启动Prometheus服务就是简单的./Prometheus
在日常使用中这显然是不合适的,首先我们需要保证服务是后台运行的,其次要保证服务是被监控的,即一旦服务宕掉,要能及时自动拉起,将它写入service显然是个不错的注意,但是我们后面还要用到相关的其他服务,一个个管理启动的话比较不方便,所以此处我使用的是以crontab+shell脚本的方式来管理监控Prometheus及其相关服务,watchdog脚本代码:

[root@localhost pRometheus]$ cat watchdog
#!/bin/bash
#Author:wangjb
#Date:2020-12-30APP=prometheus
CURR_DIR=$(cd `dirname $0`; pwd)
PROGRAM=$CURR_DIR/watchdog
CRONTAB_CMD="* * * * * $PROGRAM start"
flag=$1
cd $CURR_DIR#创建定时任务
count=`crontab -l | grep $PROGRAM | grep -v "grep" | wc -l`
if [ $count -lt 1 ]; then(crontab -l 2>/dev/null | grep -Fv $PROGRAM; echo "$CRONTAB_CMD") | crontab -count=`crontab -l | grep $PROGRAM | grep -v "grep" | wc -l`if [ $count -lt 1 ]; thenecho "create cron faild."exit 1fi
fifunction pid()
{ser=`ps -ef |grep -w $APP |grep -v grep|grep -v $flag |awk '{print $2}'`echo $ser >"$CURR_DIR/$APP.pid"
}function start()
{pidif [ "$ser" != "" ];thenecho "$APP running. pid=$ser"elseecho "Starting $APP ..."    echo `date +"%Y-%m-%d %H:%M:%S"`,"start $APP" >> $DOG_LOG/${DATE}.lognohup $CURR_DIR/$APP >/dev/null 2>&1 &fisleep 1chkn=`ps -ef |grep -w $APP |grep -v grep|grep -v $flag |wc -l`if [ $chkn -eq 1 ];thenecho "$APP is started"elseecho "$APP start failed"exit 1fi
}function stop()
{echo "Stopping $APP ..."appid=`pgrep -f $APP`if [ "$appid" != "" ];thenkill -TERM $appidfisleep 1chkn=`ps -ef |grep -w $APP |grep -v grep|grep -v $flag |wc -l`if [ $chkn -eq 0 ];thenecho "$APP is stoped"elseecho "$APP stop failed"exit 1fi
}function restart() {stopstart
}function k()
{kill -9 `pgrep -f $APP`
}function status() {pidif [ "$ser" != "" ];thenecho $APP started pid=$serelseecho $APP stopedfi
}function usage()
{echo "$0 start      启动服务"echo "$0 stop       停止服务"echo "$0 restart    重启服务"echo "$0 status     查询服务状态"echo "$0 k          强制杀死服务进程"
}case "$1" instart)start;;stop)stop;;restart)restart;;status)status;;k)k;;*)usage
esac

四、安装node_exporter

既然是linux监控,我们就需要安装node_exporter,同样的,我们从官网下载。

1.下载对应版本

官网地址:https://prometheus.io/download/

wget https://github.com/prometheus/node_exporter/releases/download/v1.0.1/node_exporter-1.0.1.linux-amd64.tar.gz

2.解压缩并将包放到prome目录下

tar -zxvf node_exporter-1.0.1.linux-amd64.tar.gz
# nOde_exporter目录改成这样,是为了防止脚本判断错误
mv node_exporter-1.0.1.linux-amd64 /opt/prome/nOde_exporter

3.启动服务

cd /opt/prometheus/nOde_exporter
./node_exporter

4.打开页面

IP:9100

5.Prometheus拉取数据

修改Prometheus 的 static_configs 来拉取 node_exporter 的数据

vim /opt/prome/pRometheus/prometheus.yml

修改完成后,重启Prometheus服务

6.验证页面

IP:9090/targets

7.服务自启动与监控

与Prometheus同理,我们使用脚本来管理node_exporter

[root@localhost nOde_exporter]$ cat watchdog
#!/bin/bash
#Author:wangjb
#Date:2020-12-30APP=node_exporter
CURR_DIR=$(cd `dirname $0`; pwd)
PROGRAM=$CURR_DIR/watchdog
CRONTAB_CMD="* * * * * $PROGRAM start"
flag=$1
cd $CURR_DIR#创建定时任务
count=`crontab -l | grep $PROGRAM | grep -v "grep" | wc -l`
if [ $count -lt 1 ]; then(crontab -l 2>/dev/null | grep -Fv $PROGRAM; echo "$CRONTAB_CMD") | crontab -count=`crontab -l | grep $PROGRAM | grep -v "grep" | wc -l`if [ $count -lt 1 ]; thenecho "create cron faild."exit 1fi
fifunction pid()
{ser=`ps -ef |grep -w $APP |grep -v grep|grep -v $flag |awk '{print $2}'`echo $ser >"$CURR_DIR/$APP.pid"
}function start()
{pidif [ "$ser" != "" ];thenecho "$APP running. pid=$ser"elseecho "Starting $APP ..."    echo `date +"%Y-%m-%d %H:%M:%S"`,"start $APP" >> $DOG_LOG/${DATE}.lognohup $CURR_DIR/$APP >/dev/null 2>&1 &fisleep 1chkn=`ps -ef |grep -w $APP |grep -v grep|grep -v $flag |wc -l`if [ $chkn -eq 1 ];thenecho "$APP is started"elseecho "$APP start failed"exit 1fi
}function stop()
{echo "Stopping $APP ..."kill -TERM `pgrep -f $APP`sleep 1chkn=`ps -ef |grep -w $APP |grep -v grep|grep -v $flag |wc -l`if [ $chkn -eq 0 ];thenecho "$APP is stoped"elseecho "$APP stop failed"exit 1fi
}function restart() {stopstart
}function k()
{kill -9 `pgrep -f $APP`
}function status() {pidif [ "$ser" != "" ];thenecho $APP started pid=$serelseecho $APP stopedfi
}function usage()
{echo "$0 start      启动服务"echo "$0 stop       停止服务"echo "$0 restart    重启服务"echo "$0 status     查询服务状态"echo "$0 k          强制杀死服务进程"
}case "$1" instart)start;;stop)stop;;restart)restart;;status)status;;k)k;;*)usage
esac

五、安装grafana

1.官网下载

对应版本https://grafana.com/grafana/download

选择适合自己的版本和方式,此处为了和Prometheus统一管理(打包成一个可移植包),所以我选择了Standalone Linux Binaries

wget https://dl.grafana.com/oss/release/grafana-7.3.6.linux-amd64.tar.gz
tar -zxvf grafana-7.3.6.linux-amd64.tar.gz

将包移动到Prometheus目录下

mv grafana-7.3.6.linux-amd64 /opt/prome/grafana

2.启动服务

cd /opt/prometheus/grafana/bin
./grafana-service

3.打开页面

IP:3000

4.将Prometheus添加为数据源

a.首页点击Add your first data source

b.选择Prometheus

c.填写地址信息,保存

d.创建dashboard

以本机的CPU使用率为例



配置完成后,点击右上角save

我们在界面上就能看到刚才添加的dashboard

回到桌面,我们能在dashboards里边看到我们刚才创建的dashboard

这里只是演示了如何手工添加一个dashboard,对于小白或者懒惰一点的,可以直接去下载模板,导入就好,具体此处不再赘述,请自行百度

5.服务自启动与监控

与Prometheus同理,我们使用脚本来管理grafana

[root@localhost grafana]$ cat watchdog
#!/bin/bash
#Author:wangjb
#Date:2020-12-30APP=grafana-server
CURR_DIR=$(cd `dirname $0`; pwd)
PROGRAM=$CURR_DIR/watchdog
CRONTAB_CMD="* * * * * $PROGRAM start"
flag=$1
cd $CURR_DIR
#创建定时任务
count=`crontab -l | grep $PROGRAM | grep -v "grep" | wc -l`
if [ $count -lt 1 ]; then(crontab -l 2>/dev/null | grep -Fv $PROGRAM; echo "$CRONTAB_CMD") | crontab -count=`crontab -l | grep $PROGRAM | grep -v "grep" | wc -l`if [ $count -lt 1 ]; thenecho "create cron faild."exit 1fi
fifunction pid()
{ser=`ps -ef |grep -w $APP |grep -v grep|grep -v $flag |awk '{print $2}'`echo $ser >"$CURR_DIR/$APP.pid"
}function start()
{pidif [ "$ser" != "" ];thenecho "$APP running. pid=$ser"elseecho "Starting $APP ..."    echo `date +"%Y-%m-%d %H:%M:%S"`,"start $APP" >> $DOG_LOG/${DATE}.lognohup $CURR_DIR/bin/$APP >/dev/null 2>&1 &fisleep 1chkn=`ps -ef |grep -w $APP |grep -v grep|grep -v $flag |wc -l`if [ $chkn -eq 1 ];thenecho "$APP is started"elseecho "$APP start failed"exit 1fi
}function stop()
{echo "Stopping $APP ..."appid=`pgrep -f $APP`if [ "$appid" != "" ];thenkill -TERM $appidfisleep 1chkn=`ps -ef |grep -w $APP |grep -v grep|grep -v $flag |wc -l`if [ $chkn -eq 0 ];thenecho "$APP is stoped"elseecho "$APP stop failed"exit 1fi
}function restart() {stopstart
}function k()
{kill -9 `pgrep -f $APP`
}function status() {pidif [ "$ser" != "" ];thenecho $APP started pid=$serelseecho $APP stopedfi
}function usage()
{echo "$0 start      启动服务"echo "$0 stop       停止服务"echo "$0 restart    重启服务"echo "$0 status     查询服务状态"echo "$0 k          强制杀死服务进程"
}case "$1" instart)start;;stop)stop;;restart)restart;;status)status;;k)k;;*)usage
esac

六、服务总体管理

我们将服务部署好之后,需要启动所有服务,某些场景下可能需要重启所有服务或者停止所有服务,现在是三个服务,我们还可以一个目录一个目录的切换进去执行watchdog,如果服务再多点,我们总会觉得有点麻烦,所有就写了一个脚本来整体控制:

[root@localhost prome]$ cat control
#!/bin/bash
#Author:wangjb
#Date:2020-12-30CURR_DIR=$(cd `dirname $0`; pwd)if [[ $EUID -gt 0 ]]; thenecho -e "Error:This script Not be run as root!"exit 1
fifunction usage()
{echo "$0 start      启动当前目录下所有服务"echo "$0 stop       停止当前目录下所有服务"echo "$0 restart    重启当前目录下所有服务"echo "$0 status     查询当前目录下所有服务状态"
}cmd=$1
function start()
{sh $CURR_DIR/$dst/watchdog start
}function stop()
{sh $CURR_DIR/$dst/watchdog stop
}function restart()
{sh $CURR_DIR/$dst/watchdog restart
}function status()
{sh $CURR_DIR/$dst/watchdog status
}for dst in `ls $CURR_DIR`
doif [ -d $dst ];thenif [ -f $CURR_DIR/$dst/watchdog ];thencase "$cmd" instart)start;;stop)stop;;status)status;;restart)restart;;*)usageesacfifi
done

CentOS7安装可移植Prometheus+grafana--基础搭建相关推荐

  1. CentOS7安装可移植Prometheus+grafana--pushgateway及自定义监控

    背景 基础搭建篇我们讲了如何配置可移植的Prometheus+grafana,客户端使用的是node_exporter,采用server端pull的方式采集数据,本次我们来试一下client端push ...

  2. CentOS7安装可移植Prometheus+grafana--alertmanager配置邮件告警

    背景 前两篇博文我们介绍了Prometheus及其相关的监控组件,本次我们记录一下告警组件:alertmanager 安装配置 获取安装包 官方下载地址:https://prometheus.io/d ...

  3. centos7 安装git_Centos7.4 Yapi 服务搭建

    Centos Yapi服务搭建 转载请标明原文出处 参考以下网址,排名不分先后 https://github.com/YMFE/yapi https://blog.csdn.net/guangzhou ...

  4. Prometheus+Grafana+Alertmanager搭建全方位的监控告警系统-超详细文档

    微信公众号搜索DevOps和k8s全栈技术,每天分享技术和生活点滴,共同成长,共同进步~ 前两篇文章 k8s中部署prometheus监控告警系统-prometheus系列文章第一篇 k8s中部署Gr ...

  5. CentOS7安装wdCP面板,快速搭建web运行环境(图文详解)

    文章目录 1. wdCP简介 2. 安装过程 2.1 源码安装 2.1.1 ssh登录服务器 2.1.2 源码的下载.解压.安装 2.1.3 软件安装目录 2.2 进入后台管理 3.wdCP面板的卸载 ...

  6. citus介绍和centos7安装部署和集群搭建

    文章目录 citus 简介 citus主要特性 部署 centos单节点版本部署启动 centos集群部署启动 要在所有节点上执行的步骤 要在协调器节点上执行的步骤 常用语句 遇到的问题 参考 cit ...

  7. 【DevOps】 Prometheus + Grafana (一)安装配置与系统级监控

    文章目录 关于Prometheus 介绍 数据格式与实例 四种数据类型 promql 相关组件 Centos7 安装 Prometheus Centos7 安装 Go 环境 Centos7 安装 Pr ...

  8. 0基础搭建Prometheus+Grafana监控服务器CPU、磁盘、内存等信息

    这里写自定义目录标题 0基础搭建Prometheus+Grafana监控服务器CPU.磁盘.内存等信息 1.实验环境准备 2.基础环境配置 3.部署prometheus 4.部署Grafana可视化图 ...

  9. Grafana + prometheus在Centos搭建服务器监控系统(一)---安装、配置

    一.Grafana介绍 Grafana是一个跨平台的开源的度量分析和可视化工具,可以通过将采集的数据查询然后可视化的展示,并及时通知.它主要有以下六大特点: 展示方式:快速灵活的客户端图表,面板插件有 ...

最新文章

  1. Tensorflow中的mnist例子
  2. R语言ggplot2可视化:自定义函数在箱图(boxplot)上添加分组样本个数(count)、分组均值(mean)、箱体填充色自定义、数据标签色彩自定义
  3. boost::mpl模块实现front相关的测试程序
  4. 为什么判断 n 是否为质数只需除到开平方根就行了?(直接证明)
  5. 马来西亚热情拥抱阿里巴巴 马云倡议的eWTP首次落地海外
  6. 网站显示不正常服务器怎么弄,你真的知道网站出现收录不正常的原因是什么吗...
  7. 第 8 章 配置listener监听器
  8. php无法添加数据库,无法添加数据到数据库
  9. 计算两个日期的时间间隔 python
  10. 关于HP C7K的firmware management中的power policy理解
  11. Xcode8 - apploader 上传失败 - ERROR ITMS-90168: The binary you uploaded was invalid.
  12. html3d房子立体图片,如何制作3D立体图片
  13. 思科交换机不同vlan互通
  14. bzoj 3640 JC的小苹果
  15. php 各个版本,PHP各版本之间差异
  16. 哪家python培训最好
  17. python 读写csv文件(创建、追加、覆盖)_python文件操作
  18. 翻译:如何成功How to be successful OpenAI CEO山姆奥特曼
  19. BiMap(HashBiMap,EnumBiMap,ImmutableBiMap)实战
  20. 2022年最新全国城市/县/区天气查询API接口分享

热门文章

  1. Log4J使用说明书
  2. GPS北斗卫星授时设备(京准)
  3. IllegalStateException:AnnotationConfigServletWebServerApplicationContext@73ba6fe6 has been closed
  4. 英语语法-- 第二讲、Be动词的形式和用法
  5. SeaJS之use函数
  6. JavaScript基础知识学习与刷题
  7. 身残志不残-霍金精神
  8. NBT|45种单细胞轨迹推断方法比较,110个实际数据集和229个合成数据集
  9. css3——max-height 和 min-height
  10. 【转】Clique共识算法