在 Kubernetes(Minikube)中配置 Apache Exporter 与 Prometheus

发布: (2026年2月24日 GMT+8 12:53)
5 分钟阅读
原文: Dev.to

Source: Dev.to

请提供您想要翻译的具体文本内容,我会按照要求将其翻译成简体中文并保留原有的格式。

实验目标

通过完成本实验,您将能够:

  • 在 Kubernetes 中部署 Apache
  • 部署 Apache Exporter
  • 部署 Prometheus
  • 配置 Prometheus 抓取 Apache 指标
  • 产生负载并观察实时指标
  • 清理环境

实验前置条件

确保已安装以下工具:

  • Docker
  • kubectl
  • Minikube
  • Git
  • curl

验证安装:

kubectl version --client
minikube version
docker --version

实验 1 – 启动 Kubernetes 环境

1. 启动 Minikube

minikube start --driver=docker

验证集群:

kubectl get nodes

2. 创建 Namespace

kubectl create namespace monitoring

设置默认 namespace:

kubectl config set-context --current --namespace=monitoring

验证:

kubectl get ns

实验 2 – 部署 Apache Web 服务器

1. 创建 Apache Deployment

创建文件 apache-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: apache
spec:
  replicas: 1
  selector:
    matchLabels:
      app: apache
  template:
    metadata:
      labels:
        app: apache
    spec:
      containers:
      - name: apache
        image: httpd:2.4
        ports:
        - containerPort: 80

应用清单:

kubectl apply -f apache-deployment.yaml

2. 暴露 Apache Service

创建文件 apache-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: apache-service
spec:
  selector:
    app: apache
  ports:
    - port: 80
      targetPort: 80

应用清单:

kubectl apply -f apache-service.yaml

验证:

kubectl get pods
kubectl get svc

实验 3 – 部署 Apache Exporter

我们将使用官方的 Apache Exporter 镜像:quay.io/prometheuscommunity/apache-exporter

1. 创建 Exporter Deployment

创建文件 apache-exporter.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: apache-exporter
spec:
  replicas: 1
  selector:
    matchLabels:
      app: apache-exporter
  template:
    metadata:
      labels:
        app: apache-exporter
    spec:
      containers:
      - name: apache-exporter
        image: quay.io/prometheuscommunity/apache-exporter
        args:
          - --scrape_uri=http://apache-service/server-status?auto
        ports:
        - containerPort: 9117

应用该清单:

kubectl apply -f apache-exporter.yaml

2. 创建 Exporter Service

创建文件 apache-exporter-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: apache-exporter-service
spec:
  selector:
    app: apache-exporter
  ports:
    - port: 9117
      targetPort: 9117

应用该清单:

kubectl apply -f apache-exporter-service.yaml

验证:

kubectl get pods
kubectl get svc

Source:

实验 4 – 部署 Prometheus

1. 创建 Prometheus ConfigMap

创建文件 prometheus-config.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: prometheus-config
data:
  prometheus.yml: |
    global:
      scrape_interval: 5s

    scrape_configs:
      - job_name: 'apache-exporter'
        static_configs:
          - targets: ['apache-exporter-service:9117']

应用清单:

kubectl apply -f prometheus-config.yaml

2. 创建 Prometheus Deployment

创建文件 prometheus-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: prometheus
spec:
  replicas: 1
  selector:
    matchLabels:
      app: prometheus
  template:
    metadata:
      labels:
        app: prometheus
    spec:
      containers:
      - name: prometheus
        image: prom/prometheus
        ports:
        - containerPort: 9090
        volumeMounts:
        - name: config-volume
          mountPath: /etc/prometheus/
      volumes:
      - name: config-volume
        configMap:
          name: prometheus-config

应用清单:

kubectl apply -f prometheus-deployment.yaml

3. 暴露 Prometheus

kubectl expose deployment prometheus --type=NodePort --port=9090

检查服务:

kubectl get svc

Lab 5 – Verify Exporter Metrics

1. Port‑Forward Exporter

kubectl port-forward svc/apache-exporter-service 9117:9117

2. Test Metrics

在另一个终端中:

curl http://localhost:9117/metrics

你应该会看到类似以下的指标:

apache_up
apache_workers
apache_scoreboard

Lab 6 – Access Prometheus UI

1. 获取 Prometheus URL

minikube service prometheus

在浏览器中打开显示的 URL,查看从 Apache Exporter 抓取的指标。

2. 端口转发 Prometheus

kubectl port-forward svc/prometheus 9090:9090

打开浏览器访问:

http://localhost:9090

3. 验证 Target

在 Prometheus UI 中进入 Status → Targets,确保:

apache-exporter = UP

4. 查询指标

尝试以下查询:

apache_up

apache_workers

实验 7 – 生成负载(Apache Benchmark)

使用 Apache Benchmark (ab) 来模拟流量。

如果本地已安装 ab

ab -n 1000 -c 50 http://$(minikube service apache-service --url)/

或者对 Apache 进行端口转发

kubectl port-forward svc/apache-service 8080:80

然后运行:

ab -n 1000 -c 50 http://localhost:8080/

在 Prometheus 中观察指标

在负载测试运行期间监控以下指标:

apache_workers
apache_scoreboard
apache_cpu_load

你应该能看到实时的指标变化。

实验 8 – 故障排除

检查 Pod 日志

kubectl logs deployment/apache-exporter
kubectl logs deployment/prometheus

验证端点

kubectl get endpoints

检查 Prometheus 配置

kubectl describe configmap prometheus-config

Lab 9 – Cleanup Environment

删除 monitoring 命名空间

kubectl delete namespace monitoring

停止 Minikube

minikube stop

(可选)完全清理

minikube delete

实验概述

您已成功完成:

  • 在 Kubernetes 中部署了 Apache
  • 部署了 Apache Exporter
  • 配置了 Prometheus
  • 验证了指标抓取
  • 产生负载并观察实时指标
  • 清理了环境
0 浏览
Back to Blog

相关文章

阅读更多 »

没人想负责的 Systemd Bug

TL;DR:存在一个命名空间 bug,影响 Ubuntu 20.04、22.04 和 24.04 服务器,导致随机服务故障。自 2021 年起已在系统中报告……