AlertManager 的在k8s集群上面的安装部署使用

https://www.jianshu.com/p/23a1cc116b1c

前面部署 Prometheus 的时候就了解到 Prometheus 包含一个报警模块,就是 AlertManager,Alertmanager 主要用于接收 Prometheus 发送的告警信息,它支持丰富的告警通知渠道,而且很容易做到告警信息进行去重,降噪,分组等,是一款前卫的告警通知系统。

 
架构

安装alertmanager

1、alertmanager-configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: alertmanager-config
  namespace: kube-system
  labels:
    kubernetes.io/cluster-service: "true"
    addonmanager.kubernetes.io/mode: EnsureExists
data:
  alertmanager.yml: |
    global: null
    receivers:
    - name: default-receiver
    route:
      group_interval: 5m
      group_wait: 10s
      repeat_interval: 10m

创建

$ kubectl create -f alertmanager-configmap.yaml
configmap "alert-config" created

2、alertmanager-pvc.yaml
注意storageClassName: 填写为自己集群的storageclass name

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: alertmanager
  namespace: kube-system
  labels:
    kubernetes.io/cluster-service: "true"
    addonmanager.kubernetes.io/mode: EnsureExists
spec:
  storageClassName: course-nfs-storage
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: "2Gi"

创建:

$ kubectl apply -f  alertmanager-pvc.yaml 
persistentvolumeclaim/alertmanager created

查看pvc和pv是否绑定成功

$ kubectl get pvc -n kube-system
NAME                           STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS         AGE
alertmanager                   Bound    pvc-aff3b0e1-be38-11e9-8abf-005056a5c8b0   2Gi        RWO            course-nfs-storage   22h

$ kubectl get pvc -n kube-system
pvc-aff3b0e1-be38-11e9-8abf-005056a5c8b0   2Gi        RWO            Delete           Bound    kube-system/alertmanager                   course-nfs-storage            22h

3、alertmanager-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: alertmanager
  namespace: kube-system
  labels:
    k8s-app: alertmanager
    kubernetes.io/cluster-service: "true"
    addonmanager.kubernetes.io/mode: Reconcile
    version: v0.14.0
spec:
  replicas: 1
  selector:
    matchLabels:
      k8s-app: alertmanager
      version: v0.14.0
  template:
    metadata:
      labels:
        k8s-app: alertmanager
        version: v0.14.0
      annotations:
        scheduler.alpha.kubernetes.io/critical-pod: ''
    spec:
      priorityClassName: system-cluster-critical
      containers:
        - name: prometheus-alertmanager
          image: "prom/alertmanager:v0.14.0"
          imagePullPolicy: "IfNotPresent"
          args:
            - --config.file=/etc/config/alertmanager.yml
            - --storage.path=/data
            - --web.external-url=/
          ports:
            - containerPort: 9093
          readinessProbe:
            httpGet:
              path: /#/status
              port: 9093
            initialDelaySeconds: 30
            timeoutSeconds: 30
          volumeMounts:
            - name: config-volume
              mountPath: /etc/config
            - name: storage-volume
              mountPath: "/data"
              subPath: ""
          resources:
            limits:
              cpu: 10m
              memory: 50Mi
            requests:
              cpu: 10m
              memory: 50Mi
        - name: prometheus-alertmanager-configmap-reload
          image: "jimmidyson/configmap-reload:v0.1"
          imagePullPolicy: "IfNotPresent"
          args:
            - --volume-dir=/etc/config
            - --webhook-url=http://localhost:9093/-/reload
          volumeMounts:
            - name: config-volume
              mountPath: /etc/config
              readOnly: true
          resources:
            limits:
              cpu: 10m
              memory: 10Mi
            requests:
              cpu: 10m
              memory: 10Mi
      volumes:
        - name: config-volume
          configMap:
            name: alertmanager-config
        - name: storage-volume
          persistentVolumeClaim:
            claimName: alertmanager

创建

$ kubectl create -f alertmanager-deployment.yaml
deployment.extensions "alertmanager" created

查看alertmanager pod是否创建成功

$ kubectl get pod -n kube-system
NAME                                         READY   STATUS    RESTARTS   AGE
alertmanager-6b5bbd5bd4-g9mpd                2/2     Running   0          48m

4、alertmanager-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: alertmanager
  namespace: kube-system
  labels:
    kubernetes.io/cluster-service: "true"
    addonmanager.kubernetes.io/mode: Reconcile
    kubernetes.io/name: "Alertmanager"
spec:
  ports:
    - name: http
      port: 80
      protocol: TCP
      targetPort: 9093
  selector:
    k8s-app: alertmanager
  type: "ClusterIP"

创建

$ kubectl apply -f alertmanager-service.yaml 
service/alertmanager created

查看

$ kubectl get svc -n kube-system 
NAME                      TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                       AGE
alertmanager              ClusterIP   10.254.229.58    <none>        80/TCP                        22h

5、配置Prometheus与Alertmanager通信

$vim prometheus-configmap.yaml   在底部添加alerting配置
alerting:
      alertmanagers:
      - static_configs:
          - targets: ["alertmanager:80"]

更新

$ kubectl apply -f prometheus-configmap.yaml 
configmap/prometheus-config configured


作者:祁恩达
链接:https://www.jianshu.com/p/23a1cc116b1c
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

posted on 2021-12-02 11:35  luzhouxiaoshuai  阅读(746)  评论(0编辑  收藏  举报

导航