Prometheus监控神器-Kubernetes篇(四)

icegoblin
发布于 2022-7-4 17:03
浏览
0收藏

 

部署 Alertmanager 集群
创建目录、赋权

k8s-m2
mkdir /data/alertmanager
chown -R 65534:65534 /data/alertmanager
k8s-m3
mkdir /data/alertmanager
chown -R 65534:65534 /data/alertmanager 
cd /data/manual-deploy/alertmanager/
cat alertmanager-data-storageclass.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: alertmanager-lpv
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer

创建Alertmanager的pv配置文件

cat alertmanager-data-pv.yaml 
apiVersion: v1
kind: PersistentVolume
metadata:
  name: alertmanager-pv-0
spec:
  capacity:
    storage: 10Gi
  volumeMode: Filesystem
  accessModes:
  - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: alertmanager-lpv
  local:
    path: /data/alertmanager
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - sealos-k8s-m2
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: alertmanager-pv-1
spec:
  capacity:
    storage: 10Gi
  volumeMode: Filesystem
  accessModes:
  - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: alertmanager-lpv
  local:
    path: /data/alertmanager
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - sealos-k8s-m3

创建Alertmanager的configmap配置文件

cat 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:
      resolve_timeout: 5m
      smtp_smarthost: 'smtp.qq.com:465'
      smtp_from: 'yo@qq.com'
      smtp_auth_username: '3452@qq.com'
      smtp_auth_password: 'bhgb'
      smtp_hello: '警报邮件'
      smtp_require_tls: false
    route:
      group_by: ['alertname', 'cluster']
      group_wait: 30s
      group_interval: 30s
      repeat_interval: 12h
      receiver: default

      routes:
      - receiver: email
        group_wait: 10s
        match:
          team: ops
    receivers:
    - name: 'default'
      email_configs:
      - to: '9935226@qq.com'
        send_resolved: true
    - name: 'email'
      email_configs:
      - to: '9935226@qq.com'
        send_resolved: true

创建Alertmanager的statefulset文件,我这里部署的是集群模式,如果需要单体库模式,将replicas改为1,去掉集群参数即可。

cat alertmanager-statefulset-cluster.yaml 
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: alertmanager
  namespace: kube-system
  labels:
    k8s-app: alertmanager
    kubernetes.io/cluster-service: "true"
    addonmanager.kubernetes.io/mode: Reconcile
    version: v0.21.0
spec:
  serviceName: "alertmanager-operated"
  replicas: 2
  selector:
    matchLabels:
      k8s-app: alertmanager
      version: v0.21.0
  template:
    metadata:
      labels:
        k8s-app: alertmanager
        version: v0.21.0
      annotations:
        scheduler.alpha.kubernetes.io/critical-pod: ''
    spec:
      tolerations:
        - key: "CriticalAddonsOnly"
          operator: "Exists"
        - effect: NoSchedule
          key: node-role.kubernetes.io/master
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: k8s-app
                operator: In
                values:
                - alertmanager
            topologyKey: "kubernetes.io/hostname"
      containers:
        - name: prometheus-alertmanager
          image: "prom/alertmanager:v0.21.0"
          imagePullPolicy: "IfNotPresent"
          args:
            - "--config.file=/etc/config/alertmanager.yml"
            - "--storage.path=/data"
            - "--cluster.listen-address=${POD_IP}:9094"
            - "--web.listen-address=:9093"
            - "--cluster.peer=alertmanager-0.alertmanager-operated:9094"
            - "--cluster.peer=alertmanager-1.alertmanager-operated:9094"
          env:
            - name: NODE_NAME
              valueFrom:
                fieldRef:
                  fieldPath: spec.nodeName
            - name: POD_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
          ports:
            - containerPort: 9093
              name: web
              protocol: TCP
            - containerPort: 9094
              name: mesh-tcp
              protocol: TCP
            - containerPort: 9094
              name: mesh-udp
              protocol: UDP
          readinessProbe:
            httpGet:
              path: /#/status
              port: 9093
            initialDelaySeconds: 30
            timeoutSeconds: 60
          volumeMounts:
            - name: config-volume
              mountPath: /etc/config
            - name: storage-volume
              mountPath: "/data"
              subPath: ""
          resources:
            limits:
              cpu: 1000m
              memory: 500Mi
            requests:
              cpu: 10m
              memory: 50Mi
        - name: prometheus-alertmanager-configmap-reload
          image: "jimmidyson/configmap-reload:v0.4.0"
          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
          securityContext:
              runAsUser: 0
              privileged: true
      volumes:
        - name: config-volume
          configMap:
            name: alertmanager-config
  volumeClaimTemplates:
    - metadata:
        name: storage-volume
      spec:
        accessModes: [ "ReadWriteOnce" ]
        storageClassName: "alertmanager-lpv"
        resources:
          requests:
            storage: 5Gi

创建Alertmanager的operated-service配置文件

cat alertmanager-operated-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: alertmanager-operated
  namespace: kube-system
  labels:
    app.kubernetes.io/name: alertmanager-operated
    app.kubernetes.io/component: alertmanager
spec:
  type: ClusterIP
  clusterIP: None
  sessionAffinity: None
  selector:
    k8s-app: alertmanager
  ports:
    - name: web
      port: 9093
      protocol: TCP
      targetPort: web
    - name: tcp-mesh
      port: 9094
      protocol: TCP
      targetPort: tcp-mesh
    - name: udp-mesh
      port: 9094
      protocol: UDP
      targetPort: udp-mesh

部署

cd /data/manual-deploy/alertmanager/
ls
alertmanager-configmap.yaml
alertmanager-data-pv.yaml
alertmanager-data-storageclass.yaml
alertmanager-operated-service.yaml
alertmanager-service-statefulset.yaml
alertmanager-statefulset-cluster.yaml
kubectl apply -f .

OK ,到此我们已经手动在k8s中的kube-system中以statefulset方式部署了Prometheus与Alertmanager,下一篇我们部署grafana与ingress-nginx的相关部署。

 

欢迎大家关注我的公众号ID:k8stech


文章转自公众号:Kubernetes技术栈

已于2022-7-4 17:03:13修改
收藏
回复
举报
回复
    相关推荐