Prometheus 容器化部署,配合Grafan

Prometheus、Grafan 容器化部署

环境说明

主机名 IP
master 192.168.10.202
client 192.168.10.203

master上部署prometheus

运行prometheus容器,并进行端口和目录文件映射

//提供配置文件
[root@master ~]# mkdir /promethues
[root@master ~]# vim /promethues/prometheus.yml
# my global config
global:
  scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
    - static_configs:
        - targets:
          # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: "prometheus"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["localhost:9090"]
      
//运行容器
[root@master ~]# docker run -d --name prometheus --restart always -p 9090:9090 -v /prometheus/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
35ed6a5bf7f1644f8ffe2570bcf888be
在 Docker 或 Kubernetes 环境中部署 PrometheusGrafana 容器化应用是构建现代监控系统的关键步骤。以下是详细的部署指南: ### 在 Docker 中部署 PrometheusGrafana #### 1. 使用 Docker Compose 部署 Docker Compose 是一种便捷的方式,可以通过一个 `docker-compose.yml` 文件定义和运行多容器应用。 ```yaml version: '3' services: prometheus: image: prom/prometheus container_name: prometheus ports: - "9090:9090" volumes: - ./prometheus.yml:/etc/prometheus/prometheus.yml command: - '--config.file=/etc/prometheus/prometheus.yml' grafana: image: grafana/grafana container_name: grafana ports: - "3000:3000" environment: - GF_SECURITY_ADMIN_PASSWORD=admin volumes: - grafana_data:/var/lib/grafana volumes: grafana_data: ``` 启动服务: ```bash docker-compose up -d ``` 访问 Prometheus UI:`http://localhost:9090` 访问 Grafana UI:`http://localhost:3000`(默认用户名/密码为 `admin/admin`)[^2] #### 2. 配置 Prometheus Prometheus 的配置文件 `prometheus.yml` 示例内容如下: ```yaml global: scrape_interval: 15s scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['localhost:9090'] ``` ### 在 Kubernetes 中部署 PrometheusGrafana #### 1. 创建命名空间 为了更好地组织资源,建议为监控组件创建一个专用的命名空间: ```bash kubectl create namespace monitor-sa ``` #### 2. 部署 Prometheus 使用 Kubernetes Deployment 和 Service 来部署 Prometheus。以下是一个简单的 `prometheus-deploy.yaml` 示例: ```yaml apiVersion: apps/v1 kind: Deployment metadata: name: prometheus-server namespace: monitor-sa spec: replicas: 1 selector: matchLabels: app: prometheus template: metadata: labels: app: prometheus spec: containers: - name: prometheus image: prom/prometheus args: - "--config.file=/etc/prometheus/prometheus.yml" ports: - containerPort: 9090 volumeMounts: - name: config-volume mountPath: /etc/prometheus volumes: - name: config-volume configMap: name: prometheus-config --- apiVersion: v1 kind: Service metadata: name: prometheus-service namespace: monitor-sa spec: selector: app: prometheus ports: - protocol: TCP port: 80 targetPort: 9090 ``` 同时需要创建一个 ConfigMap 来存储 Prometheus 的配置文件: ```bash kubectl create configmap prometheus-config --from-file=prometheus.yml -n monitor-sa ``` 部署完成后,可以通过以下命令查看 Pod 状态: ```bash kubectl get pods -n monitor-sa -l app=prometheus ``` #### 3. 部署 Grafana 同样地,可以使用 Deployment 和 Service 来部署 Grafana: ```yaml apiVersion: apps/v1 kind: Deployment metadata: name: grafana namespace: monitor-sa spec: replicas: 1 selector: matchLabels: app: grafana template: metadata: labels: app: grafana spec: containers: - name: grafana image: grafana/grafana ports: - containerPort: 3000 env: - name: GF_SECURITY_ADMIN_PASSWORD value: "admin" --- apiVersion: v1 kind: Service metadata: name: grafana-service namespace: monitor-sa spec: selector: app: grafana ports: - protocol: TCP port: 80 targetPort: 3000 ``` 部署完成后,可以通过以下命令查看 Pod 状态: ```bash kubectl get pods -n monitor-sa -l app=grafana ``` #### 4. 访问服务 - Prometheus UI 可以通过 `http://<NodeIP>:<NodePort>` 访问。 - Grafana UI 同样可以通过 `http://<NodeIP>:<NodePort>` 访问,默认用户名/密码为 `admin/admin`。 #### 5. 配置热加载(可选) 为了确保每次修改配置文件后可以热加载 Prometheus,可以在 Kubernetes 中执行以下命令: ```bash kubectl get pods -n monitor-sa -o wide -l app=prometheus ``` 然后使用 `kubectl exec` 进入容器并发送 SIGHUP 信号来触发配置重载: ```bash kubectl exec -it <pod-name> -n monitor-sa -- kill -HUP 1 ``` 这将使新的配置生效而无需重启 Prometheus 服务[^4]。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值