docker 安裝部署 Prometheus 與grafana的詳細(xì)過程
0. centos 7 安裝后的操作
# 禁用selinux 以防兼容性問題 setenforce 0 sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config # 關(guān)閉防火墻服務(wù) systemctl disable firewalld &>/dev/null systemctl stop firewalld # 更新yum源、epel源 mkdir /etc/yum.repos.d/old/ &>/dev/null mv /etc/yum.repos.d/* /etc/yum.repos.d/old/ &> /dev/null curl -o /etc/yum.repos.d/CentOS-Base.repo https://repo.huaweicloud.com/repository/conf/CentOS-7-reg.repo &>/dev/null curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo &> /dev/null # 安裝docker服務(wù) yum clean all &>/dev/null yum makecache &>/dev/null yum install -y yum-utils device-mapper-persistent-data lvm2 yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo yum install docker-ce docker-ce-cli containerd.io
1. 準(zhǔn)備環(huán)境
確保你已經(jīng)安裝了 Docker 和 Docker Compose。如果沒有安裝,可以參考以下命令:
# 安裝 Docker sudo yum install -y docker sudo systemctl start docker sudo systemctl enable docker # 安裝 Docker Compose sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose sudo chmod +x /usr/local/bin/docker-compose docker-compose --version # 驗(yàn)證安裝
2. 創(chuàng)建工作目錄
創(chuàng)建一個(gè)目錄來存放相關(guān)配置文件:
mkdir prometheus-grafana cd prometheus-grafana
3. 配置 Prometheus
3.1 創(chuàng)建 Prometheus 配置文件
新建 prometheus.yml:
nano prometheus.yml
內(nèi)容如下:
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'node_exporter'
static_configs:
- targets: ['node_exporter:9100']3.2 創(chuàng)建存儲(chǔ)配置
創(chuàng)建一個(gè)目錄用來保存 Prometheus 數(shù)據(jù):
mkdir prometheus_data
4. 創(chuàng)建 Docker Compose 配置
新建 docker-compose.yml:
nano docker-compose.yml
內(nèi)容如下:
version: '3.7'
services:
prometheus:
image: prom/prometheus:latest
container_name: prometheus
ports:
- "9090:9090"
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
- ./prometheus_data:/prometheus
restart: unless-stopped
grafana:
image: grafana/grafana:latest
container_name: grafana
ports:
- "3000:3000"
volumes:
- grafana_data:/var/lib/grafana
environment:
- GF_SECURITY_ADMIN_USER=admin
- GF_SECURITY_ADMIN_PASSWORD=admin
restart: unless-stopped
node_exporter:
image: prom/node-exporter:latest
container_name: node_exporter
ports:
- "9100:9100"
restart: unless-stopped
volumes:
grafana_data:5. 啟動(dòng)服務(wù)
運(yùn)行以下命令啟動(dòng)容器:
docker-compose up -d
檢查服務(wù)狀態(tài):
docker ps
6. 訪問 Prometheus 和 Grafana
- Prometheus:訪問
http://<your_server_ip>:9090。 - Grafana:訪問
http://<your_server_ip>:3000。- 默認(rèn)用戶名:
admin - 默認(rèn)密碼:
admin
- 默認(rèn)用戶名:
7. 配置 Grafana 數(shù)據(jù)源
- 登錄 Grafana 后,進(jìn)入 Configuration > Data Sources。
- 點(diǎn)擊 Add data source,選擇 Prometheus。
- 在 URL 欄輸入
http://prometheus:9090,點(diǎn)擊 Save & Test。
8. 添加監(jiān)控面板
在 Grafana 中:
- 點(diǎn)擊 + > Import。
- 輸入面板的 ID,例如:
- Node Exporter Full:1860
- 點(diǎn)擊 Load,選擇數(shù)據(jù)源后保存。
9. 驗(yàn)證監(jiān)控效果
- 在 Prometheus 中,可以通過
http://<your_server_ip>:9090查看監(jiān)控目標(biāo)狀態(tài)。 - 在 Grafana 中,可以通過導(dǎo)入的面板查看實(shí)時(shí)圖表。
注意事項(xiàng)
- 數(shù)據(jù)持久化:Prometheus 和 Grafana 數(shù)據(jù)通過
docker-compose.yml中的掛載路徑實(shí)現(xiàn)持久化。 - 安全性:生產(chǎn)環(huán)境建議修改 Grafana 默認(rèn)用戶名密碼。
- 擴(kuò)展性:可以通過修改
prometheus.yml添加其他監(jiān)控目標(biāo),例如數(shù)據(jù)庫、應(yīng)用程序等。
這套方案簡單易用,非常適合快速部署監(jiān)控環(huán)境。
到此這篇關(guān)于docker 安裝部署 Prometheus 與grafana的文章就介紹到這了,更多相關(guān)docker 部署 Prometheus 與grafana內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Docker安裝MySQL8.0的實(shí)現(xiàn)方法
這篇文章主要介紹了Docker安裝MySQL8.0的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
詳解使用Docker搭建Java Web運(yùn)行環(huán)境
本篇文章主要介紹了使用Docker搭建Java Web運(yùn)行環(huán)境,現(xiàn)在分享給大家,也給大家做個(gè)參考。感興趣的小伙伴們可以參考一下。2016-11-11
docker中使用mysql數(shù)據(jù)庫詳解(在局域網(wǎng)訪問)
這篇文章主要給大家介紹了在docker中使用mysql數(shù)據(jù)庫,在局域網(wǎng)訪問的相關(guān)資料,文中通過圖文以及示例代碼介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-06-06
Docker部署Logstash同步Mysql數(shù)據(jù)到ES方式
本文介紹了如何搭建ELK(Elasticsearch, Logstash, Kibana)日志系統(tǒng),包括配置文件準(zhǔn)備、Logstash與Elasticsearch部署、驅(qū)動(dòng)文件準(zhǔn)備、配置文件編輯、容器操作以及日志查看等步驟,文章內(nèi)容是作者的個(gè)人經(jīng)驗(yàn)分享2024-10-10
docker<容器數(shù)據(jù)卷-v>對容器內(nèi)數(shù)據(jù)持久化詳解(備份)
容器的數(shù)據(jù)持久化主要是指宿主機(jī)與容器,以及容器與容器之間進(jìn)行數(shù)據(jù)交互,下面這篇文章主要給大家介紹了關(guān)于docker<容器數(shù)據(jù)卷-v>對容器內(nèi)數(shù)據(jù)持久化的相關(guān)資料,需要的朋友可以參考下2023-03-03
windows宿主機(jī)和docker容器設(shè)置掛載共享文件夾的步驟
這篇文章主要介紹了windows宿主機(jī)和docker容器設(shè)置掛載共享文件夾的步驟,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11

