k8s部署prometheus

news2024/11/18 14:36:42

k8s部署prometheus

1.下载prometheus文件

cd /soft/src
git clone -b release-0.5 --single-branch https://github.com/coreos/kube-prometheus.git

2.部署

这里部署之前最好改一下alertmanager-alertmanager.yaml这个文件,将replicas:改成2或者3,当为1的时候,后面可能会有点问题

cd kube-prometheus/manifests/setup
kubectl create -f .
cd ..
kubectl create -f .

3.查看

kubectl get all -n monitoring

4.通过ingress-nginx去暴露三个服务

部署ingress-nginx

cd /root/k8s/yaml
mkdir ingress-nginx
cd ingress-nginx

ingress-nginx.yaml

apiVersion: v1
kind: Namespace
metadata:
  labels:
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
  name: ingress-nginx
---
apiVersion: v1
automountServiceAccountToken: true
kind: ServiceAccount
metadata:
  labels:
    app.kubernetes.io/component: controller
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
    app.kubernetes.io/version: 1.3.1
  name: ingress-nginx
  namespace: ingress-nginx
---
apiVersion: v1
kind: ServiceAccount
metadata:
  labels:
    app.kubernetes.io/component: admission-webhook
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
    app.kubernetes.io/version: 1.3.1
  name: ingress-nginx-admission
  namespace: ingress-nginx
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  labels:
    app.kubernetes.io/component: controller
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
    app.kubernetes.io/version: 1.3.1
  name: ingress-nginx
  namespace: ingress-nginx
rules:
- apiGroups:
  - ""
  resources:
  - namespaces
  verbs:
  - get
- apiGroups:
  - ""
  resources:
  - configmaps
  - pods
  - secrets
  - endpoints
  verbs:
  - get
  - list
  - watch
- apiGroups:
  - ""
  resources:
  - services
  verbs:
  - get
  - list
  - watch
- apiGroups:
  - networking.k8s.io
  resources:
  - ingresses
  verbs:
  - get
  - list
  - watch
- apiGroups:
  - networking.k8s.io
  resources:
  - ingresses/status
  verbs:
  - update
- apiGroups:
  - networking.k8s.io
  resources:
  - ingressclasses
  verbs:
  - get
  - list
  - watch
- apiGroups:
  - ""
  resourceNames:
  - ingress-controller-leader
  resources:
  - configmaps
  verbs:
  - get
  - update
- apiGroups:
  - ""
  resources:
  - configmaps
  verbs:
  - create
- apiGroups:
  - coordination.k8s.io
  resourceNames:
  - ingress-controller-leader
  resources:
  - leases
  verbs:
  - get
  - update
- apiGroups:
  - coordination.k8s.io
  resources:
  - leases
  verbs:
  - create
- apiGroups:
  - ""
  resources:
  - events
  verbs:
  - create
  - patch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  labels:
    app.kubernetes.io/component: admission-webhook
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
    app.kubernetes.io/version: 1.3.1
  name: ingress-nginx-admission
  namespace: ingress-nginx
rules:
- apiGroups:
  - ""
  resources:
  - secrets
  verbs:
  - get
  - create
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  labels:
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
    app.kubernetes.io/version: 1.3.1
  name: ingress-nginx
rules:
- apiGroups:
  - ""
  resources:
  - configmaps
  - endpoints
  - nodes
  - pods
  - secrets
  - namespaces
  verbs:
  - list
  - watch
- apiGroups:
  - coordination.k8s.io
  resources:
  - leases
  verbs:
  - list
  - watch
- apiGroups:
  - ""
  resources:
  - nodes
  verbs:
  - get
- apiGroups:
  - ""
  resources:
  - services
  verbs:
  - get
  - list
  - watch
- apiGroups:
  - networking.k8s.io
  resources:
  - ingresses
  verbs:
  - get
  - list
  - watch
- apiGroups:
  - ""
  resources:
  - events
  verbs:
  - create
  - patch
- apiGroups:
  - networking.k8s.io
  resources:
  - ingresses/status
  verbs:
  - update
- apiGroups:
  - networking.k8s.io
  resources:
  - ingressclasses
  verbs:
  - get
  - list
  - watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  labels:
    app.kubernetes.io/component: admission-webhook
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
    app.kubernetes.io/version: 1.3.1
  name: ingress-nginx-admission
rules:
- apiGroups:
  - admissionregistration.k8s.io
  resources:
  - validatingwebhookconfigurations
  verbs:
  - get
  - update
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  labels:
    app.kubernetes.io/component: controller
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
    app.kubernetes.io/version: 1.3.1
  name: ingress-nginx
  namespace: ingress-nginx
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: ingress-nginx
subjects:
- kind: ServiceAccount
  name: ingress-nginx
  namespace: ingress-nginx
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  labels:
    app.kubernetes.io/component: admission-webhook
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
    app.kubernetes.io/version: 1.3.1
  name: ingress-nginx-admission
  namespace: ingress-nginx
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: ingress-nginx-admission
subjects:
- kind: ServiceAccount
  name: ingress-nginx-admission
  namespace: ingress-nginx
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  labels:
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
    app.kubernetes.io/version: 1.3.1
  name: ingress-nginx
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: ingress-nginx
subjects:
- kind: ServiceAccount
  name: ingress-nginx
  namespace: ingress-nginx
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  labels:
    app.kubernetes.io/component: admission-webhook
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
    app.kubernetes.io/version: 1.3.1
  name: ingress-nginx-admission
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: ingress-nginx-admission
subjects:
- kind: ServiceAccount
  name: ingress-nginx-admission
  namespace: ingress-nginx
---
apiVersion: v1
data:
  allow-snippet-annotations: "true"
kind: ConfigMap
metadata:
  labels:
    app.kubernetes.io/component: controller
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
    app.kubernetes.io/version: 1.3.1
  name: ingress-nginx-controller
  namespace: ingress-nginx
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app.kubernetes.io/component: controller
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
    app.kubernetes.io/version: 1.3.1
  name: ingress-nginx-controller
  namespace: ingress-nginx
spec:
  ipFamilies:
  - IPv4
  ipFamilyPolicy: SingleStack
  ports:
  - appProtocol: http
    name: http
    port: 80
    protocol: TCP
    targetPort: http
  - appProtocol: https
    name: https
    port: 443
    protocol: TCP
    targetPort: https
  selector:
    app.kubernetes.io/component: controller
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
  type: NodePort
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app.kubernetes.io/component: controller
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
    app.kubernetes.io/version: 1.3.1
  name: ingress-nginx-controller-admission
  namespace: ingress-nginx
spec:
  ports:
  - appProtocol: https
    name: https-webhook
    port: 443
    targetPort: webhook
  selector:
    app.kubernetes.io/component: controller
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
  type: ClusterIP
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app.kubernetes.io/component: controller
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
    app.kubernetes.io/version: 1.3.1
  name: ingress-nginx-controller
  namespace: ingress-nginx
spec:
  minReadySeconds: 0
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app.kubernetes.io/component: controller
      app.kubernetes.io/instance: ingress-nginx
      app.kubernetes.io/name: ingress-nginx
  template:
    metadata:
      labels:
        app.kubernetes.io/component: controller
        app.kubernetes.io/instance: ingress-nginx
        app.kubernetes.io/name: ingress-nginx
    spec:
      hostNetwork: true
      containers:
      - args:
        - /nginx-ingress-controller
        - --election-id=ingress-controller-leader
        - --controller-class=k8s.io/ingress-nginx
        - --ingress-class=nginx
        - --configmap=$(POD_NAMESPACE)/ingress-nginx-controller
        - --validating-webhook=:8443
        - --validating-webhook-certificate=/usr/local/certificates/cert
        - --validating-webhook-key=/usr/local/certificates/key
        env:
        - name: POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: LD_PRELOAD
          value: /usr/local/lib/libmimalloc.so
        image: dyrnq/ingress-nginx-controller:v1.3.1
        imagePullPolicy: IfNotPresent
        lifecycle:
          preStop:
            exec:
              command:
              - /wait-shutdown
        livenessProbe:
          failureThreshold: 5
          httpGet:
            path: /healthz
            port: 10254
            scheme: HTTP
          initialDelaySeconds: 10
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 1
        name: controller
        ports:
        - containerPort: 80
          name: http
          protocol: TCP
        - containerPort: 443
          name: https
          protocol: TCP
        - containerPort: 8443
          name: webhook
          protocol: TCP
        readinessProbe:
          failureThreshold: 3
          httpGet:
            path: /healthz
            port: 10254
            scheme: HTTP
          initialDelaySeconds: 10
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 1
        resources:
          requests:
            cpu: 100m
            memory: 90Mi
        securityContext:
          allowPrivilegeEscalation: true
          capabilities:
            add:
            - NET_BIND_SERVICE
            drop:
            - ALL
          runAsUser: 101
        volumeMounts:
        - mountPath: /usr/local/certificates/
          name: webhook-cert
          readOnly: true
      dnsPolicy: ClusterFirst
      nodeSelector:
        kubernetes.io/os: linux
      serviceAccountName: ingress-nginx
      terminationGracePeriodSeconds: 300
      volumes:
      - name: webhook-cert
        secret:
          secretName: ingress-nginx-admission
---
apiVersion: batch/v1
kind: Job
metadata:
  labels:
    app.kubernetes.io/component: admission-webhook
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
    app.kubernetes.io/version: 1.3.1
  name: ingress-nginx-admission-create
  namespace: ingress-nginx
spec:
  template:
    metadata:
      labels:
        app.kubernetes.io/component: admission-webhook
        app.kubernetes.io/instance: ingress-nginx
        app.kubernetes.io/name: ingress-nginx
        app.kubernetes.io/part-of: ingress-nginx
        app.kubernetes.io/version: 1.3.1
      name: ingress-nginx-admission-create
    spec:
      containers:
      - args:
        - create
        - --host=ingress-nginx-controller-admission,ingress-nginx-controller-admission.$(POD_NAMESPACE).svc
        - --namespace=$(POD_NAMESPACE)
        - --secret-name=ingress-nginx-admission
        env:
        - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        image: dyrnq/kube-webhook-certgen:v1.3.0
        imagePullPolicy: IfNotPresent
        name: create
        securityContext:
          allowPrivilegeEscalation: false
      nodeSelector:
        kubernetes.io/os: linux
      restartPolicy: OnFailure
      securityContext:
        fsGroup: 2000
        runAsNonRoot: true
        runAsUser: 2000
      serviceAccountName: ingress-nginx-admission
---
apiVersion: batch/v1
kind: Job
metadata:
  labels:
    app.kubernetes.io/component: admission-webhook
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
    app.kubernetes.io/version: 1.3.1
  name: ingress-nginx-admission-patch
  namespace: ingress-nginx
spec:
  template:
    metadata:
      labels:
        app.kubernetes.io/component: admission-webhook
        app.kubernetes.io/instance: ingress-nginx
        app.kubernetes.io/name: ingress-nginx
        app.kubernetes.io/part-of: ingress-nginx
        app.kubernetes.io/version: 1.3.1
      name: ingress-nginx-admission-patch
    spec:
      containers:
      - args:
        - patch
        - --webhook-name=ingress-nginx-admission
        - --namespace=$(POD_NAMESPACE)
        - --patch-mutating=false
        - --secret-name=ingress-nginx-admission
        - --patch-failure-policy=Fail
        env:
        - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        image: dyrnq/kube-webhook-certgen:v1.3.0 
        imagePullPolicy: IfNotPresent
        name: patch
        securityContext:
          allowPrivilegeEscalation: false
      nodeSelector:
        kubernetes.io/os: linux
      restartPolicy: OnFailure
      securityContext:
        fsGroup: 2000
        runAsNonRoot: true
        runAsUser: 2000
      serviceAccountName: ingress-nginx-admission
---
apiVersion: networking.k8s.io/v1
kind: IngressClass
metadata:
  labels:
    app.kubernetes.io/component: controller
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
    app.kubernetes.io/version: 1.3.1
  name: nginx
spec:
  controller: k8s.io/ingress-nginx
---
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingWebhookConfiguration
metadata:
  labels:
    app.kubernetes.io/component: admission-webhook
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
    app.kubernetes.io/version: 1.3.1
  name: ingress-nginx-admission
webhooks:
- admissionReviewVersions:
  - v1
  clientConfig:
    service:
      name: ingress-nginx-controller-admission
      namespace: ingress-nginx
      path: /networking/v1/ingresses
  failurePolicy: Fail
  matchPolicy: Equivalent
  name: validate.nginx.ingress.kubernetes.io
  rules:
  - apiGroups:
    - networking.k8s.io
    apiVersions:
    - v1
    operations:
    - CREATE
    - UPDATE
    resources:
    - ingresses
  sideEffects: None

创建ingress-nginx

kubectl create -f ingress-nginx.yaml

查看

kubectl get all -n ingress-nginx 

5.部署ingress代理三个service到公网

domain.yaml

apiVersion: networking.k8s.io/v1 
kind: Ingress
metadata:
  name: grafana
  namespace: monitoring
spec:
  ingressClassName: nginx
  rules:
  - host: grafana.zhubaoyi.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: grafana
            port: 
              number: 3000
  - host: prometheus.zhubaoyi.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: prometheus-k8s
            port:
              number: 9090
  - host: alertmanager.zhubaoyi.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: alertmanager-main
            port:
              number: 9093

创建

kubectl create -f domain.yaml

查看

[root@worker1 prometheus]# kubectl get ingress -n monitoring 
NAME      CLASS   HOSTS                                                                       ADDRESS          PORTS   AGE
grafana   nginx   grafana.zhubaoyi.com,prometheus.zhubaoyi.com,alertmanager.zhubaoyi.com   11.20.28.59   80      3m47s


然后通过浏览器就可以正常访问三个服务了,接下来就是慢慢使用配置了

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述



prometheus监控基础服务和业务

1.k8s监控服务的大概流程

在这里插入图片描述

提供对应服务的metrics接口->创建对应的endpoint(k8s集群内可直接与之通信)->创建对应的service->创建对应的ServiceMonitor->prometheus就可以自动创建对应的target监控

监控具体分为两种,一种是本身提供metrics接口的服务,这种服务直接创建后续的几个服务就可以监控其提供的对应的指标,另一种是没有提供metrics接口的服务,这种服务需要先部署对应的exporter,进而exporter会暴露响应的metrics,通常类似于mysql、redis这些服务都有一些现成的exporter,或是官方的或是社区的,拿来就可以直接用,但对于公司具体的业务,则需要开发人员去提供metrics指标,以此做监控

2.没有metrics接口的监控(kafka)

a.安装kafka

这一步就直接省略,具体可以看前一章部署k8s收集日志那篇[https://blog.csdn.net/ss810540895/article/details/128476758?spm=1001.2014.3001.5501]

b.部署kafka-exporter-deploy、kafka-service-exporter、kafka-servicemonitor

kafka-deploy-exporter.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
  creationTimestamp: "2020-05-12T14:00:40Z"
  generation: 1
  labels:
    app: kafka-exporter
  name: kafka-exporter
  namespace: monitoring
  resourceVersion: "11300398"
  selfLink: /apis/apps/v1/namespaces/monitoring/deployments/kafka-exporter
  uid: 7a9471de-cf8f-4622-884b-130d2505d6ec
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: kafka-exporter
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: kafka-exporter
    spec:
      containers:
      - args:
        - --kafka.server=192.168.255.96:9092
        env:
        - name: TZ
          value: Asia/Shanghai
        - name: LANG
          value: C.UTF-8
        image: danielqsj/kafka-exporter:latest
        imagePullPolicy: IfNotPresent
        lifecycle: {}
        name: kafka-exporter
        ports:
        - containerPort: 9308
          name: web
          protocol: TCP
        resources:
          limits:
            cpu: 249m
            memory: 318Mi
          requests:
            cpu: 10m
            memory: 10Mi
        securityContext:
          allowPrivilegeEscalation: false
          privileged: false
          readOnlyRootFilesystem: false
          runAsNonRoot: false
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
        - mountPath: /usr/share/zoneinfo/Asia/Shanghai
          name: tz-config
        - mountPath: /etc/localtime
          name: tz-config
        - mountPath: /etc/timezone
          name: timezone
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
      volumes:
      - hostPath:
          path: /usr/share/zoneinfo/Asia/Shanghai
          type: ""
        name: tz-config
      - hostPath:
          path: /etc/timezone
          type: ""
        name: timezone

kafka-service-exporter.yaml

apiVersion: v1
kind: Service
metadata:
  creationTimestamp: "2020-05-12T14:00:39Z"
  labels:
    app: kafka-exporter
  name: kafka-exporter
  namespace: monitoring
  resourceVersion: "11300354"
  selfLink: /api/v1/namespaces/monitoring/services/kafka-exporter
  uid: e5967e11-4c96-4daf-ac98-429f430229ab
spec:
  clusterIP: 
  ports:
  - name: container-1-web-1
    port: 9308
    protocol: TCP
    targetPort: 9308
  selector:
    app: kafka-exporter
  sessionAffinity: None
  type: ClusterIP

kafka-servicemonitor.yaml

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  labels:
    k8s-app: kafka-exporter
  name: kafka-exporter
  namespace: monitoring
spec:
  endpoints:
  - interval: 30s
    port: container-1-web-1
  namespaceSelector:
    matchNames:
    - monitoring
  selector:
    matchLabels:
      app: kafka-exporter

3.查看prometheus的target

在这里插入图片描述

可以看到已经有了

查看对应的指标

在这里插入图片描述

4.grafana添加dashboard

我这边添加dashboard后无法查看到数据,发现我这个dashboard监控的语法规则和prometheus查到的一些指标不匹配,

在这里插入图片描述

具体的大家可以去根据需要,编写对应的指标查询规则



部署黑盒监控

所需资源

[root@worker1 blackbox_exporter]# ll
total 20
-rw-r--r-- 1 root root  755 Dec 31 13:15 additional-scrape-configs.yaml
-rw-r--r-- 1 root root  917 Dec 31 11:13 blackbox_exporter_cm.yaml
-rw-r--r-- 1 root root 2315 Dec 31 11:15 blackbox_exporter_deploy.yaml
-rw-r--r-- 1 root root  491 Dec 31 11:16 blackbox_exporter_service.yaml
-rw-r--r-- 1 root root  465 Dec 31 13:13 prometheus-additional.yaml

1.创建secret

prometheus-additional.yaml

- job_name: 'blackbox'
  metrics_path: /probe
  params:
    module: [http_2xx]  # Look for a HTTP 200 response.
  static_configs:
    - targets:
      - https://www.baidu.com/
  relabel_configs:
    - source_labels: [__address__]
      target_label: __param_target
    - source_labels: [__param_target]
      target_label: instance
    - source_labels: [instance]
      target_label: target
    - target_label: __address__
      replacement: blackbox-exporter:9115
kubectl create secret generic additional-scrape-configs --from-file=prometheus-additional.yaml --dry-run -oyaml > additional-scrape-configs.yaml

kubectl create -f additional-scrape-configs.yaml -n monitoring

2.修改prometheus的crd

cd /soft/src/kube-prometheus/manifests/
vim prometheus-prometheus.yaml

在最后添加下面几行

  additionalScrapeConfigs:
    name: additional-scrape-configs
    key: prometheus-additional.yaml

然后应用

kubectl replace -f prometheus-prometheus.yaml -n monitoring

3.创建configmap、deploy、service

blackbox_exporter_cm.yaml

apiVersion: v1
data:
  blackbox.yml: |-
    modules:
      http_2xx:
        prober: http
      http_post_2xx:
        prober: http
        http:
          method: POST
      tcp_connect:
        prober: tcp
      pop3s_banner:
        prober: tcp
        tcp:
          query_response:
          - expect: "^+OK"
          tls: true
          tls_config:
            insecure_skip_verify: false
      ssh_banner:
        prober: tcp
        tcp:
          query_response:
          - expect: "^SSH-2.0-"
      irc_banner:
        prober: tcp
        tcp:
          query_response:
          - send: "NICK prober"
          - send: "USER prober prober prober :prober"
          - expect: "PING :([^ ]+)"
            send: "PONG ${1}"
          - expect: "^:[^ ]+ 001"
      icmp:
        prober: icmp
kind: ConfigMap
metadata:
  creationTimestamp: "2020-05-13T13:44:52Z"
  name: blackbox-conf
  namespace: monitoring

blackbox_exporter_deploy.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
  creationTimestamp: "2020-05-13T13:46:29Z"
  generation: 1
  labels:
    app: blackbox-exporter
  name: blackbox-exporter
  namespace: monitoring
  resourceVersion: "11572499"
  selfLink: /apis/apps/v1/namespaces/monitoring/deployments/blackbox-exporter
  uid: 2c192340-3be1-49db-945f-01a3f1c20576
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: blackbox-exporter
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: blackbox-exporter
    spec:
      containers:
      - args:
        - --config.file=/mnt/blackbox.yml
        env:
        - name: TZ
          value: Asia/Shanghai
        - name: LANG
          value: C.UTF-8
        image: prom/blackbox-exporter:master
        imagePullPolicy: IfNotPresent
        lifecycle: {}
        name: blackbox-exporter
        ports:
        - containerPort: 9115
          name: web
          protocol: TCP
        resources:
          limits:
            cpu: 324m
            memory: 443Mi
          requests:
            cpu: 10m
            memory: 10Mi
        securityContext:
          allowPrivilegeEscalation: false
          privileged: false
          readOnlyRootFilesystem: false
          runAsNonRoot: false
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
        - mountPath: /usr/share/zoneinfo/Asia/Shanghai
          name: tz-config
        - mountPath: /etc/localtime
          name: tz-config
        - mountPath: /etc/timezone
          name: timezone
        - mountPath: /mnt
          name: config
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
      volumes:
      - hostPath:
          path: /usr/share/zoneinfo/Asia/Shanghai
          type: ""
        name: tz-config
      - hostPath:
          path: /etc/timezone
          type: ""
        name: timezone
      - configMap:
          defaultMode: 420
          name: blackbox-conf
        name: config

blackbox_exporter_service.yaml

apiVersion: v1
kind: Service
metadata:
  creationTimestamp: "2020-05-13T13:46:29Z"
  labels:
    app: blackbox-exporter
  name: blackbox-exporter
  namespace: monitoring
  resourceVersion: "11572454"
  selfLink: /api/v1/namespaces/monitoring/services/blackbox-exporter
  uid: 3c5f01eb-b331-4455-956a-9c9a331f2906
spec:
  ports:
  - name: container-1-web-1
    port: 9115
    protocol: TCP
    targetPort: 9115
  selector:
    app: blackbox-exporter
  sessionAffinity: None
  type: ClusterIP

然后应用

kubectl create -f blackbox_exporter_cm.yaml
kubectl create -f blackbox_exporter_deploy.yaml
kubectl create -f blackbox_exporter_service.yaml

4.prometheus查看

可以看到对应的指标

在这里插入图片描述

target这边也可以看到了

在这里插入图片描述

5.添加对应的grafana

在这里插入图片描述

最后还差一个自动发现的没写,后续加上去



添加邮件告警

1.修改alertmanager-secret.yaml

alertmanager-secret.yaml

apiVersion: v1
kind: Secret
metadata:
  name: alertmanager-main
  namespace: monitoring
stringData:
  alertmanager.yaml: |-
    "global":
      "resolve_timeout": "5m"
      smtp_from: "8100895@qq.com"
      smtp_smarthost: "smtp.qq.com:465"
      smtp_hello: "163.com"
      smtp_auth_username: "8100895@qq.com"
      smtp_auth_password: "bydkomubmbejf"
      smtp_require_tls: false
      # wechat
      #      wechat_api_url: 'https://qyapi.weixin.qq.com/cgi-bin/'
      #      wechat_api_secret: 'ZZQt0Ue9mtplH9u1g8PhxR_RxEnRu512CQtmBn6R2x0'
      #      wechat_api_corp_id: 'wwef86a30130f04f2b'
    "inhibit_rules":
    - "equal":
      - "namespace"
      - "alertname"
      "source_match":
        "severity": "critical"
      "target_match_re":
        "severity": "warning|info"
    - "equal":
      - "namespace"
      - "alertname"
      "source_match":
        "severity": "warning"
      "target_match_re":
        "severity": "info"
    "receivers":
    - "name": "Default"
      "email_configs":
      - to: "8100895@qq.com"
        send_resolved: true
        #    - "name": "Watchdog"
        #      "email_configs":
        #      - to: "kubernetes_guide@163.com"
        #        send_resolved: true
        #    - "name": "Critical"
        #      "email_configs":
        #      - to: "kubernetes_guide@163.com"
        #        send_resolved: true
        #    - name: 'wechat'
        #      wechat_configs:
        #      - send_resolved: true
        #        to_tag: '1'
        #        agent_id: '1000003'
        #    - "name": "Default"
        #    - "name": "Watchdog"
        #    - "name": "Critical"
    "route":
      "group_by":
      - "namespace"
      "group_interval": "5m"
      "group_wait": "30s"
      "receiver": "Default"
      "repeat_interval": "12h"
      "routes":
      - "match":
          "alertname": "Watchdog"
        "receiver": "Watchdog"
      - "match":
          "severity": "critical"
        "receiver": "Critical"
type: Opaque
kubectl delete -f alertmanager-secret.yaml
kubectl create -f alertmanager-secret.yaml

2.然后就会收到告警了

其他的上面部署prometheus的时候都已经部署好了
在这里插入图片描述

不过这个告警界面比较丑,后面可以自定义一下告警模板

告警模板、自动发现以及业务监控后续再加上

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/135977.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Spark 3.0 - 17 ML PCA 主成分分析理论与实战

目录 一.引言 二.PCA 理论 1.主成分分析定义 2.数学基础 A.数据归一化 B.协方差矩阵计算 C.计算协方差矩阵的特征向量和特征值,以识别主成分 D.构造特征向量矩阵 E.沿着主成分轴重新计算数据 三.PCA 实战 1.数据准备 2.PCA 初始化 3.数据降维 四.总结 …

举一反三-自建zabbix监控php

php-fpm监控需要通过nginx服务。因此需要开启相关配置。 php-fpm如果是yum安装的,那么在/etc/php-fpm.d/www.config中编辑如下选项: pm.status_path /php_status 保存并退出编辑。重启php-fpm服务。 接下来编辑nginx配置项。编辑如下: l…

Linux下cal命令C/C++实现(显示日历信息)

大多数用户自然会将计算机上的日历视为GUI应用程序,或直接从桌面显示的东西。但也可以在Linux中使用cal命令通过命令行终端查看日历。 如果您通常使用Linux上的桌面环境,或者您来自Windows或MacOS操作系统,可能会认为这是一种过于复杂的日历…

RFID技术在固定资产管理中的作用

固定资产因具有数量庞大、分布广、变动频繁等特点造成管理难度较大,一直成为企业管理的一大重点。固定资产管理包括资产的新增、调拨、闲置、报废、维修、盘点等操作,让投入使用到报废退出使用的全过程进行全面的信息化管控。以往固定资产的一直由于管理…

AlexNet详解

入门小菜鸟,希望像做笔记记录自己学的东西,也希望能帮助到同样入门的人,更希望大佬们帮忙纠错啦~侵权立删。 ✨完整代码在我的github上,有需要的朋友可以康康✨ GitHub - tt-s-t/Deep-Learning: Store some of your own in-depth …

Python学习笔记——起步

Python 2 和 Python 3的区别 一、维护及版本更新不同 官方通知python2 2020开始已不再维护,python3的版本还在不断迭代更新 。 因为python2官方不再维护且python3的广泛使用,很多新的算法包、AI包等其他功能模块包都是基于python3开发的,p…

LaTeX使用方法

1.工具 1.生成表格:LaTeX Tables Editor 2.生成公式:Online LaTeX Equation Editor 、texmath demo、在线LaTeX公式编辑器-编辑器 2.技巧 1.插入图片: \begin{figure}[t] %t表示图片置顶,h表示指定位置,b表示置底…

条件分支if和else的使用详解

顺序结构没什么好讲的,就是从上到下的写代码,所以接下来,壹哥就直接带各位来学习分支结构里的条件分支。我们在前面说过,分支结构,或者叫做条件分支,其实有两种情况。一种是if...else...类型的条件分支&…

vue组件通信6种方式总结(常问知识点)

前言 在Vue组件库开发过程中,Vue组件之间的通信一直是一个重要的话题,虽然官方推出的 Vuex 状态管理方案可以很好的解决组件之间的通信问题,但是在组件库内部使用 Vuex 往往会比较重,本文将系统的罗列出几种不使用 Vuex&#xff…

X.509证书详解

概述 X.509是公钥基础设施(PKI)的标准格式。X.509证书就是基于国际电信联盟(ITU)制定的X.509标准的数字证书。X.509证书主要用于识别互联网通信和计算机网络中的身份,保护数据传输安全。X.509证书无处不在&#xff0c…

Spring之底层架构核心概念-过滤器

文章目录1.excludeFilters:排除过滤器2.includeFilters:包含过滤器3.问题:spring为什么能通过是否有Component注解来判断是否需要去注册bean呢?3.1 看源码3.2 原因4.总结1.excludeFilters:排除过滤器 用excludeFilters排除UserService 这个类后&#xf…

nature级别图表:单细胞转录组细胞比例统计可视化函数

单细胞转录组细胞比例: 关于单细胞比例的计算和作图我们之前出过3期,单细胞比例的展示是很多单细胞文章必不可少的内容:跟着Cell学单细胞转录组分析(六):细胞比例计算及可视化 相信跟着学习的小伙伴已经掌握了。最近学习一篇《nature medici…

JavaScript 数组-概念,创建数组,遍历数组,新增元素

JavaScript 数组-概念,创建数组,遍历数组,新增元素 目录JavaScript 数组-概念,创建数组,遍历数组,新增元素1. 数组的概念2. 创建数组2.1 数组的创建方式2.2 利用 new 创建数组2.3 利用数组字面量创建数组2.…

模式分类识别 | BiLSTM双向长短期记忆神经网络数据多特征分类预测(Matlab完整程序)

模式分类识别 | BiLSTM双向长短期记忆神经网络数据多特征分类预测(Matlab完整程序) 目录 模式分类识别 | BiLSTM双向长短期记忆神经网络数据多特征分类预测(Matlab完整程序)分类结果基本介绍程序设计参考资料分类结果

深度学习实验(五)——循环神经网络编程

深度学习实验五:循环神经网络编程 本次实验练习使用torch.nn中的类设计一个循环神经网络进行MNIST图像分类。 在本次实验中,你要设计一个CNN,用于将282828 \times 282828的MNIST图像转换为MMDM\times M\times DMMD的特征图,将该特征图看作是…

我,做了两年程序员,存了巨款5000,你们拿什么跟我比?

🕐没错,标题所见,从21年1月份开始从事程序员工作也过了两年了,今年还是没有存到钱。 今年换了一份工作,四月份的时候,从惠州换到了广州工作,从制造业转行到了政务行业,工资涨了&…

Keithley 2604B数字源表-安泰测试

Keithley 2600B 系列系统 SMU 仪器是业界标准电流-电压源和测量解决方案,适用于高度自动化生产测试应用。 双通道和单通道型号都紧密集成一个精密电源、真正电流源、数字万用表和具有脉冲生成功能的电子负载。 另外,TSP? 技术可运行完整测试程序&#x…

多点双向重发布的应用

目录 1.拓扑图 2.实验思路 3.主要配置 4.测试 5.实验总结 1.拓扑图 2.实验思路 在配置完RIP和OSPF之后,在2,3号设备上进行多点的双向重发布由于在进行了多点的双向重发布之后,会出现大量的负载均衡,导致选路不佳的问题解决办…

前端工程师leetcode算法面试必备-二叉树深度广度遍历

一、前言 Medium 难度主要考察结合二叉树性质的 CRUD 操作,而这一切的基础都离不开遍历二叉树。 二叉树是图的子集,因而同样适用以下两种搜索思想: **DFS(深度优先搜索):**沿着根节点递归下去&#xff0c…

普通程序员怎么赚多份钱?解锁更多赚钱新姿势

在当下这个社会,学会如何make money很重要。 咱们是个俗人,赚钱才是社会生存的头等大事。这不是高山流水的世界,而是能力创造财富,对于程序员来说,更是如此。 作为程序员,我们有更多挣钱的姿势!…