Centos7上安装部署Prometheus + Grafana (prometheus笔记01)

一、目标

在Centos7.6上安装部署Prometheus 2.17.1 + Grafana 6.7.2-1。(无坑版)

prometheus默认端口9090,node_exporter默认端口9100

二、平台

[[email protected] ~]# uname -a
Linux client 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
[[email protected] ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 
grafana-6.7.2-1,Prometheus 2.17.1

三、前言

1.Grafana官网https://grafana.com/
2.Grafana软件包下载页面和安装方法https://grafana.com/grafana/download
3.Grafana默认的端口号:3000
4.Prometheus官网https://prometheus.io
5.Prometheus和node_exporter的下载地址https://prometheus.io/download/

四、拓扑

主机名       ip                          主要安装的软件                角色(作用)
mycat34    192.168.73.34      prometheus、grafana      prometheus和grafana服务端(收集数据并展示)
mycat33    192.168.73.33      node_exporter                  prometheus客户端(被收集数据的客户端)

五、在centos7上安装Prometheus 2.17.1

啰嗦:本大段都在服务端mycat34上安装。

1.基本操作,关闭centos防火墙和selinux

yum install -y wget ntp curl vim net-tools
tar -zcvf /etc/yum.repos.d/yumRepo.bak /etc/yum.repos.d/*.repo
rm -rf /etc/yum.repos.d/*.repo
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum install epel-release -y
yum clean all && yum makecache
timedatectl set-timezone Asia/Shanghai
timedatectl set-local-rtc 0
sed -i 's/server 0.centos.pool.ntp.org iburst/server ntp1.aliyun.com iburst/g' /etc/ntp.conf
sed -i 's/server 1.centos.pool.ntp.org iburst/server ntp2.aliyun.com iburst/g' /etc/ntp.conf
sed -i 's/server 2.centos.pool.ntp.org iburst/server 1.centos.pool.ntp.org iburst/g' /etc/ntp.conf
sed -i 's/server 3.centos.pool.ntp.org iburst/server 2.centos.pool.ntp.org iburst/g' /etc/ntp.conf
systemctl restart ntpd
systemctl enable ntpd
systemctl stop firwalld
systemctl disable firewalld
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

2.下载最新版的Prometheus 2.17.1

wget -P /usr/local/src https://github.com/prometheus/prometheus/releases/download/v2.17.1/prometheus-2.17.1.linux-amd64.tar.gz

3.解压Prometheus
现在的Prometheus都是编译过的,无需安装go语言环境,直接解压即可使用。

tar -zxvf /usr/local/src/prometheus-2.17.1.linux-amd64.tar.gz -C /usr/local

4.将Prometheus做成软连接的形式(可选,如果你对linux不熟悉,建议跟着做下去)

ln -s /usr/local/src/prometheus-2.17.1.linux-amd64 /usr/local/prometheus

5.创建Prometheus数据存储目录

mkdir -p /var/lib/prometheus
chown -R prometheus /var/lib/prometheus

6.创建用于运行Prometheus的组和用户

groupadd prometheus
useradd -g prometheus -s /sbin/nologin prometheus

7.给Prometheus主目录赋用户Prometheus权限

chown -R prometheus:prometheus /usr/local/prometheus/

8.将Prometheus加入到系统管理程序中

cat >/etc/systemd/system/prometheus.service <<EOF

[Unit]
Description=Prometheus
Documentation=https://prometheus.io/
After=network.target

[Service]
Type=simple
User=prometheus
# --storage.tsdb.path是可选项,默认数据目录在运行目录的./dada目录中
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/var/lib/prometheus
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

至此,最基本的Prometheus服务端就部署完毕了。下面开始把客户端mycat33给添加到Prometheus的配置文件中

9.(可选)将客户端加入到Prometheus监控中
注意:将配置文件中的ip地址改成你的被监控客户端的ip,(node_exporter的)端口号默认是9100

cp /usr/local/prometheus/prometheus.yml{,.bak}
cat >> /usr/local/prometheus/prometheus.yml <<EOF


  - job_name: 'xnode1-mycat33'
    scrape_interval: 10s
    static_configs:
    - targets: ['192.168.73.33:9100']
      labels:
        instance: xnode1-instance
EOF

10.启动Prometheus并设置其开机自启
注意:我们写的自启程序较为简单,启动成功与否不能只看systemctl start没报错是不行的。要看systemctl status prometheus或ss -ntl |grep 9090才行

systemctl start prometheus.service
systemctl enable prometheus.service

11.验证prometheus的Web页面,prometheus默认的端口号是9090,浏览器输入http://192.168.73.34:9090/

12.在prometheus的web页面上查看主机监控状态
在prometheus的web页面上依次点击 “Status”---“Targets”,你会发现只监控到prometheus服务器自己。这就对了

13.使用prometheus的web方式查看主机的监控值
就如上图中的那个地址所写的一样,格式:http://prometheus服务器ip:9090/metrics 。看到的一堆数据,存在形式是【key {数量} value】

14.拿一个监控的key去用图形方式查看(文字描述的很别扭)

在第13部中找个已经产生数值的key,等会便于查看数据的变化情况。如【go_goroutines】
在最上面的文本框中输入key,如【go_goroutines】,然后点【Execute】,然后点【Graph】就能看到key[go_goroutines]被绘制出的图形了。

六、在客户端安装部署node_exporter

感觉这个node_exporter有点想elk中的各种监控插件

本步需要在被监控端mycat33上执行。

1.下载最新版的node_exporter(这玩意比较小,8.2M很快就下载完了)

wget -P /usr/local/src https://github.com/prometheus/node_exporter/releases/download/v1.0.0-rc.0/node_exporter-1.0.0-rc.0.linux-amd64.tar.gz

2.解压node_exporter

tar -zxvf /usr/local/src/node_exporter-1.0.0-rc.0.linux-amd64.tar.gz -C /usr/local

3.(可选)创建软连接(如果你对linux不熟悉,建议跟着继续做)

ln -s /usr/local/node_exporter-1.0.0-rc.0.linux-amd64/ /usr/local/node_exporter

4.创建用于运行node_exporter的用户

groupadd prometheus
useradd -g prometheus -s /sbin/nologin prometheus

5.给node_exporter主目录赋权限

chown -R prometheus:prometheus /usr/local/node_exporter/

6.将node_exporter加入到系统服务当中

cat >/usr/lib/systemd/system/node_exporter.service <<EOF
[Unit]
Description=node_exporter
Documentation=https://prometheus.io/
After=network.target

[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/node_exporter/node_exporter
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

7.启动node_exporter并将其设置开机自启

systemctl start node_exporter
systemctl enable node_exporter

8.检查node_exporter是否已启动
node_exporter默认的端口是9100

systemctl status node_exporter
ss -ntl |grep 9100

9.(啰嗦)将9100在防火墙中放行,或者直接关闭防火墙

systemctl stop firewalld
systemctl disable firewalld

10.在prometheus的web上检查是否监控到了本机
在【五9】中已经修改过配置文件,将本客户端加入到了prometheus服务器的配置文件中了。

登录prometheus的web,依次点击【Status---Targets】,正常的话就会看得到被监控端mycat33主机了。

七、在Grafana上展示prometheus

关于Grafana的安装,请参考《Centos7上安装Grafana》,Grafana默认的端口是3000。

1.浏览器打开Grafana主页 http://192.168.73.34:3000,默认用户名和密码都是admin。

2.添加数据源
依次点开左侧的齿轮状图标【Configuration】---【Data Source】,再新页面中点【Add data source】

3.点一下数据源类型Prometheus

4.为Grafana数据源prometheus添加数据源参数
Name:随便输入
Default:设置为开启状态
URL:http://192.168.73.34:9090/ ,写你的prometheus主页地址。
Access:Server(default)
Scrape interval:15s ,因为我们这是测试环境,尽量把刷新数据的时间写小点。

点【Save & Test】 后,能弹出绿色的【Data source is working】就说明我们的prometheus数据源添加成功了。

5.在Grafana上查看默认的prometheus仪表盘。

然后点一下【Prometheus Stats】就能看到默认的仪表盘了。

------------------END--------------2020年4月16日22:58:55------------------------------------------------------

------------------最后照例送上鸡汤一碗:好男儿,人穷志不缺,天道也酬勤。---------------------------------

猜你喜欢

转载自blog.csdn.net/xoofly/article/details/105558947