0
点赞
收藏
分享

微信扫一扫

Prometheus快速入手

本篇文章偏向于快速部署和安装

下载链接

Download | Prometheus

通过这个链接到官网下载,我们这篇文章采用二进制部署

Prometheus快速入手_快速安装


部署脚本

这是自己写好的一键部署脚本

我自己的机器是ubuntu2204 最小化安装

脚本很简单,只不过是把手动执行的一些命令,都集成为脚本了

#!/bin/bash

#支持在线和离线安装,建议离线安装,在线下载很慢

PROMETHEUS_VERSION=2.50.1

PROMETHEUS_FILE="prometheus-${PROMETHEUS_VERSION}.linux-amd64.tar.gz"
#PROMETHEUS_URL="https://mirrors.tuna.tsinghua.edu.cn/github-release/prometheus/prometheus/LatestRelease/${PROMETHEUS_FILE}"
PROMETHEUS_URL="https://github.com/prometheus/prometheus/releases/download/v${PROMETHEUS_VERSION}/${PROMETHEUS_FILE}"

INSTALL_DIR=/usr/local

HOST=`hostname -I|awk '{print $1}'`


. /etc/os-release

msg_error() {
  echo -e "\033[1;31m$1\033[0m"
}

msg_info() {
  echo -e "\033[1;32m$1\033[0m"
}

msg_warn() {
  echo -e "\033[1;33m$1\033[0m"
}


color () {
    RES_COL=60
    MOVE_TO_COL="echo -en \\033[${RES_COL}G"
    SETCOLOR_SUCCESS="echo -en \\033[1;32m"
    SETCOLOR_FAILURE="echo -en \\033[1;31m"
    SETCOLOR_WARNING="echo -en \\033[1;33m"
    SETCOLOR_NORMAL="echo -en \E[0m"
    echo -n "$1" && $MOVE_TO_COL
    echo -n "["
    if [ $2 = "success" -o $2 = "0" ] ;then
        ${SETCOLOR_SUCCESS}
        echo -n $"  OK  "    
    elif [ $2 = "failure" -o $2 = "1"  ] ;then 
        ${SETCOLOR_FAILURE}
        echo -n $"FAILED"
    else
        ${SETCOLOR_WARNING}
        echo -n $"WARNING"
    fi
    ${SETCOLOR_NORMAL}
    echo -n "]"
    echo 
}


install_prometheus () {
    if [ ! -f  ${PROMETHEUS_FILE} ] ;then
        wget ${PROMETHEUS_URL} ||  { color "下载失败!" 1 ; exit ; }
    fi
    [ -d $INSTALL_DIR ] || mkdir -p $INSTALL_DIR
    tar xf ${PROMETHEUS_FILE} -C $INSTALL_DIR
    cd $INSTALL_DIR &&  ln -s prometheus-${PROMETHEUS_VERSION}.linux-amd64 prometheus
    mkdir -p $INSTALL_DIR/prometheus/{bin,conf,data}
    cd $INSTALL_DIR/prometheus && { mv prometheus promtool bin/ ; mv prometheus.yml conf/; }
    id prometheus &>/dev/null || useradd -r -s /sbin/nologin prometheus
    chown -R prometheus.prometheus ${INSTALL_DIR}/prometheus/
    
    cat >>  /etc/profile <<EOF
export PROMETHEUS_HOME=${INSTALL_DIR}/prometheus
export PATH=\${PROMETHEUS_HOME}/bin:\$PATH
EOF

}


prometheus_service () {
    cat > /lib/systemd/system/prometheus.service <<EOF
[Unit]
Description=Prometheus Server
Documentation=https://prometheus.io/docs/introduction/overview/
After=network.target

[Service]
Restart=on-failure
User=prometheus
Group=prometheus
WorkingDirectory=${INSTALL_DIR}/prometheus
ExecStart=${INSTALL_DIR}/prometheus/bin/prometheus --config.file=${INSTALL_DIR}/prometheus/conf/prometheus.yml --web.enable-lifecycle
ExecReload=/bin/kill -HUP \$MAINPID
LimitNOFILE=65535

[Install]
WantedBy=multi-user.target
EOF
    systemctl daemon-reload
    systemctl enable --now prometheus.service
}


start_prometheus() { 
    systemctl is-active prometheus
    if [ $?  -eq 0 ];then  
        echo 
        color "Prometheus 安装完成!" 0
        echo "-------------------------------------------------------------------"
        echo -e "访问链接: \c"
        msg_info "http://$HOST:9090/" 
    else
        color "Prometheus 安装失败!" 1
        exit
    fi 
}

install_prometheus

prometheus_service

start_prometheus


我拷贝到了自己的机器上

Prometheus快速入手_快速安装_02


执行然后访问,由于是自己的虚拟主机,我在这里没有做域名解析

Prometheus快速入手_快速安装_03


我们访问一下这个链接,这样界面就OK了

Prometheus快速入手_运维监控_04


Node Exporter 安装

在需要在被监控的主机都安装

如果想要这个Node Exporter安装脚本可以私信我,我发你

就是这个包

Prometheus快速入手_快速安装_05

这个安装可以参考官方文档,这里不演示了

别的需要监控的机器也安装,安装完成是这样

Prometheus快速入手_快速安装_06


监控其他主机

vim /usr/local/prometheus/conf/prometheus.yml

加入需要监控的两台主机

Prometheus快速入手_运维监控_07


我们到web界面查看一下

Prometheus快速入手_快速安装_08


Grafana 展示 Prometheus 数据

用下面的命令进行安装

[root@prometheus ~]#apt update
[root@prometheus ~]#apt -y install ./grafana_10.4.0_amd64.deb 

[root@prometheus ~]#systemctl daemon-reload
[root@prometheus ~]#systemctl enable --now grafana-server.service

#3000端口访问
[root@prometheus ~]#ss -ntulp|grep grafana
tcp   LISTEN 0      4096               *:3000            *:*    users:(("grafana",pid=3471,fd=9))        
[root@prometheus ~]#


访问进去

Prometheus快速入手_Prometheus_09

配置 Prometheus 数据源

Prometheus快速入手_运维监控_10

选择这个模板

Prometheus快速入手_快速安装_11


记录编号 1860

Prometheus快速入手_Prometheus_12


输入在这里加载

Prometheus快速入手_运维监控_13


选好源进行导入

Prometheus快速入手_Prometheus_14

最后这样显示

Prometheus快速入手_运维监控_15


这样就做好了



举报

相关推荐

0 条评论