# Prometheus部署手册

官方文档:https://prometheus.io/docs/prometheus/latest/installation/

# 二进制部署

下载地址:https://prometheus.io/download/

# 下载二进制包,X86架构,这里以v3.3.0版本为例
wget https://github.com/prometheus/prometheus/releases/download/v3.3.0/prometheus-3.3.0.linux-amd64.tar.gz

# 移动到/usr/local/下
mv prometheus-3.3.0.linux-amd64 /usr/local/prometheus

# 创建systemd服务配置文件
cat > /usr/lib/systemd/system/prometheus.service << EOF
[Unit]
Description=Prometheus Time Series Collection and Processing Server
After=network.target

[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/prometheus/prometheus \
  --config.file=/usr/local/prometheus/prometheus.yml \
  --storage.tsdb.path=/usr/local/prometheus/data \
  --storage.tsdb.retention.time=180d
Restart=always

[Install]
WantedBy=multi-user.target

EOF

# 创建prometheus用户和组
useradd prometheus

# 修改目录属主
chown -R prometheus:prometheus /usr/local/prometheus

systemctl daemon-reload
systemctl start prometheus.service
systemctl enable prometheus.service

# Docker部署

docker run -d \
    --restart unless-stopped \
    --name prometheus-server \
    -p 9090:9090 \
    -v /usr/local/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \
    -v /usr/local/prometheus/data:/prometheus \
    prom/prometheus:latest \
    --storage.tsdb.retention.time=180d

# k8s下部署

创建yaml资源清单文件

vim prometheus.yaml

内容如下

apiVersion: apps/v1
kind: Deployment
metadata:
  name: prometheus
  namespace: monitor
  labels:
    app: prometheus
spec:
  replicas: 1
  selector:
    matchLabels:
      app: prometheus
  template:
    metadata:
      labels:
        app: prometheus
    spec:
      serviceAccountName: prometheus
      containers:
      - name: prometheus
        image: harbor.wyx/prometheus/prometheus-server:latest
        volumeMounts:
        - name: conf
          mountPath: /etc/prometheus/prometheus.yml
          subPath: prometheus.yml
        - name: data
          mountPath: /prometheus
      volumes:
      - name: conf
        configMap:
          name: prometheus-conf
      - name: data
        persistentVolumeClaim:
          claimName: prometheus-data
---
apiVersion: v1
kind: Service
metadata:
  name: prometheus
  namespace: monitor
spec:
  type: NodePort
  selector:
    app: prometheus
  ports:
  - name: app-port
    protocol: TCP
    port: 9090
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: prometheus-conf
  namespace: monitor
data:
  prometheus.yml: >-
    global:
      scrape_interval: 15s
      evaluation_interval: 15s

    alerting:
      alertmanagers:
        - static_configs:
            - targets:

    rule_files:

    scrape_configs:
      - job_name: "prometheus"
        static_configs:
          - targets: ["localhost:9090"]

---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: prometheus-data
  namespace: monitor
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 200G
  storageClassName: nfs-csi  # 修改为实际环境的storageClass名称
编撰人:wangyxyf