首頁>技術>

使用Prometheus對系統進行監控,使用Grafana進行資料展示。我的環境是K8S,我的部署方式是通過 yaml檔案進行部署。已經部署檔案上傳到了Github。

部署node-expoter對k8s叢集監控

使用DaemonSet方式使用最新映象:node-exporter:v0.18.1

部署描述檔案如下:

在這裡我建立了一個DaemonSet型別的部署,會在每個node節點上執行。執行的Namespace是在kube-system中。建立service 使用nodePort方式對映埠。

---apiVersion: apps/v1kind: DaemonSetmetadata:  name: node-exporter  namespace: kube-system  labels:    k8s-app: node-exporterspec:  selector:    matchLabels:      name: node-exporter  template:    metadata:      labels:        name: node-exporter    spec:      containers:      - name: node-exporter        image: prom/node-exporter:v0.18.1        ports:        - containerPort: 9100          protocol: TCP          name: http---apiVersion: v1kind: Servicemetadata:  labels:    k8s-app: node-exporter  name: node-exporter  namespace: kube-systemspec:  ports:  - name: http    port: 9100    nodePort: 31672    protocol: TCP  type: NodePort  selector:    k8s-app: node-exporter
部署Prometheus服務

部署prometheus服務

deploy.yaml

這裡我建立了一個應用部署檔案deployment物件,掛載configmap配置檔案和volume儲存持久化資料。

---apiVersion: apps/v1kind: Deploymentmetadata:  labels:    name: prometheus-deployment  name: prometheus  namespace: kube-systemspec:  replicas: 1  selector:    matchLabels:      app: prometheus  template:    metadata:      labels:        app: prometheus    spec:      containers:      - image: prom/prometheus:v2.15.2        name: prometheus        command:        - "/bin/prometheus"        args:        - "--config.file=/etc/prometheus/prometheus.yml"        - "--storage.tsdb.path=/prometheus"        - "--storage.tsdb.retention=24h"        ports:        - containerPort: 9090          protocol: TCP        volumeMounts:        - mountPath: "/prometheus"          name: data        - mountPath: "/etc/prometheus"          name: config-volume        resources:          requests:            cpu: 100m            memory: 100Mi          limits:            cpu: 500m            memory: 2500Mi      serviceAccountName: prometheus      volumes:      - name: data        hostPath:          path: /data/devops/prometheus          type: Directory      - name: config-volume        configMap:          name: prometheus-config

service.yaml

這裡我建立了一個service物件,nodePort方式暴露30003埠進行訪問。

---kind: ServiceapiVersion: v1metadata:  labels:    app: prometheus  name: prometheus  namespace: kube-systemspec:  type: NodePort  ports:  - port: 9090    targetPort: 9090    nodePort: 30003  selector:    app: prometheus

RBAC Setting(配置應用程式許可權)

apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRolemetadata:  name: prometheusrules:- apiGroups: [""]  resources:  - nodes  - nodes/proxy  - services  - endpoints  - pods  verbs: ["get", "list", "watch"]- apiGroups:  - extensions  resources:  - ingresses  verbs: ["get", "list", "watch"]- nonResourceURLs: ["/metrics"]  verbs: ["get"]---apiVersion: v1kind: ServiceAccountmetadata:  name: prometheus  namespace: kube-system---apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRoleBindingmetadata:  name: prometheusroleRef:  apiGroup: rbac.authorization.k8s.io  kind: ClusterRole  name: prometheussubjects:- kind: ServiceAccount  name: prometheus  namespace: kube-system
部署Grafana服務
apiVersion: apps/v1kind: Deploymentmetadata:  name: grafana-core  namespace: kube-system  labels:    app: grafana    component: corespec:  selector:    matchLabels:      app: grafana  replicas: 1  template:    metadata:      labels:        app: grafana        component: core    spec:      containers:      - image: grafana/grafana:6.5.3        name: grafana-core        imagePullPolicy: IfNotPresent        env:          # The following env variables set up basic auth twith the default admin user and admin password.          - name: GF_AUTH_BASIC_ENABLED            value: "true"          - name: GF_AUTH_ANONYMOUS_ENABLED            value: "false"          # - name: GF_AUTH_ANONYMOUS_ORG_ROLE          #   value: Admin          # does not really work, because of template variables in exported dashboards:          # - name: GF_DASHBOARDS_JSON_ENABLED          #   value: "true"        readinessProbe:          httpGet:            path: /login            port: 3000          # initialDelaySeconds: 30          # timeoutSeconds: 1        volumeMounts:        - name: grafana-persistent-storage          mountPath: /var      volumes:      - name: grafana-persistent-storage        hostPath:          path: /data/devops/grafana          type: Directory

service

apiVersion: v1kind: Servicemetadata:  name: grafana  namespace: kube-system  labels:    app: grafana    component: corespec:  type: NodePort  ports:    - port: 3000      nodePort: 30011  selector:    app: grafana    component: core
訪問驗證

我們先看下dashboard中pod執行狀態

然後看下pometheus中是否有資料

到此我們就成功的將Prometheus+Grafana部署到了Kubernetes系統中。接下來我們來配置grafana展示叢集中的監控資料。

配置Grafana 面板

新增資料來源

匯入面板

dashboardsid: 315

最新評論
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • Flutter混合開發探索與實踐