prometheusalert部署说明

PrometheusAlert可以部署在本地和云平台上,支持windows、linux、公有云、私有云、混合云、容器和kubernetes。你可以根据实际场景或需求,选择相应的方式来部署PrometheusAlert:

这里我们选择使用 Kubernetes 部署 PrometheusAlert。

部署 PrometheusAlert

# Kubernetes中运行可以直接执行以下命令行即可(注意默认的部署模版中未挂载模版数据库文件 db/PrometheusAlertDB.db,为防止模版数据丢失,请自行增加挂载配置 )
kubectl apply -n kube-mon -f https://raw.githubusercontent.com/feiyu563/PrometheusAlert/master/example/kubernetes/PrometheusAlert-Deployment.yaml
# 也可把 yaml 文件下载到本地
wget https://raw.githubusercontent.com/feiyu563/PrometheusAlert/master/example/kubernetes/PrometheusAlert-Deployment.yaml
curl -O https://raw.githubusercontent.com/feiyu563/PrometheusAlert/master/example/kubernetes/PrometheusAlert-Deployment.yaml
# 更改服务暴露方式,也可以更改配置文件中服务暴露方式
kubectl patch svc prometheus-alert-center -n kube-mon -p '{"spec": {"type": "NodePort"}}'
# 获取默认登录账号和密码
kubectl describe configmap prometheus-alert-center-conf -n kube-mon
#登录用户名
login_user=prometheusalert
#登录密码
login_password=prometheusalert

说明:

(1)启动后可使用浏览器打开以下地址查看:http://[YOUR-PrometheusAlert-URL]:8080

(2)默认登录帐号和密码在app.conf中有配置

部署 Mysql

启动mysql容器

# cat PrometheusMysql.yaml
apiVersion: v1
kind: Secret
metadata:
  name: mysql-user-pwd
  namespace: kube-mon
data:
  mysql-root-pwd: VHJhbnNzaW9uQDEwMTE=

---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: mysql
  name: mysql
  namespace: kube-mon
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - image: mysql:5.7
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-user-pwd
              key: mysql-root-pwd
        ports:
        - name: mysql-port
          containerPort: 3306
        volumeMounts:
        - mountPath: /etc/mysql/conf.d
          name: mysql-config
        - mountPath: /var/lib/mysql 
          name: mysql-data
      volumes:
      - name: mysql-config
        hostPath:
          path: /mnt/data-s3-fs/prometheusalert-mysql/conf
      - name: mysql-data
        hostPath:
          path: /mnt/data-s3-fs/prometheusalert-mysql/data

---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: mysql
  name: mysql
  namespace: kube-mon
spec:
  ports:
  - name: mysql-port
    port: 3306
    protocol: TCP
    targetPort: 3306
  selector:
    app: mysql
# apply mysql
kubectl apply -f PrometheusMysql.yaml

使用mysql作为后端数据存储

PrometheusAlert默认使用sqlite3作为后端自定义模板的存储,这种方式适合于单机部署,满足绝大部分生产场景使用。考虑到部分企业对于服务的高可用要求较高,同时也为了让PrometheusAlert更易于横向扩展,用户可以更改PrometheusAlert的默认存储为mysql。(推荐使用mysql 5.7及以上版本)

创建数据库

# 获取 mysql pod
kubectl get pod -n kube-mon|grep mysql
# 进入 mysql pod
kubectl exec -it mysql-6785bdcf-wsxhn -n kube-mon /bin/bash
# 在 mysql pod 容器内执行
# 登录 mysql
mysql -uroot -p
# 创建数据库 prometheusalert
CREATE DATABASE prometheusalert CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

获取 prometheusalert.sql

# 获取pod
kubectl get pod -n kube-mon|grep prometheus
# 进入pod
kubectl exec -it prometheus-alert-center-5d7dfb8c99-h78z4 -n kube-mon /bin/sh
# 拷贝文件
kubectl cp kube-mon/prometheus-alert-center-5d7dfb8c99-l48gw:/app/db/prometheusalert.sql /mnt/data-s3-fs/prometheusalert-mysql/data/prometheusalert.sql

导入sql

利用Navicat或命令行将db目录中的 prometheusalert.sql 导入数据库prometheusalert:

# 进入 mysql pod
kubectl exec -it mysql-6785bdcf-wsxhn -n kube-mon /bin/bash
# 在 mysql pod 容器内执行
# 切换到 mysql 数据目录下
cd /var/lib/mysql
# 登录 mysql
mysql -uroot -p
use prometheusalert
source prometheusalert.sql

更改 PrometheusAlert 配置

开启PrometheusAlert配置文件中关于mysql的配置 conf/app.conf,数据库名称与上面创建的数据一致:

# delete PrometheusAlert-Deployment.yaml
kubectl delete -f PrometheusAlert-Deployment.yaml
# 数据库驱动,支持sqlite3,mysql,如使用mysql,请开启db_host,db_user,db_password,db_name的注释
db_driver=mysql
db_host=mysql
db_port=3306
db_user=root
db_password=Transsion@1011
db_name=prometheusalert

重启PrometheusAlert

kubectl apply -f PrometheusAlert-Deployment.yaml

查看pod状态

kubectl get pod -n kube-mon

这样即完成配置PrometheusAlert使用mysql数据库作为默认后端存储。

获取yaml文件

上面完整 yaml 文件详见本仓库~