prometheus之一:在k8s集群中安裝prometheus


Kubernetes 集群的監控方案目前主要有以下幾種方案:
1、Heapster:Heapster 是一個集群范圍的監控和數據聚合工具,以 Pod 的形式運行在集群中。
2、metrics-server:metrics-server 也是一個集群范圍內的資源數據聚合工具,是 Heapster 的替代品,同樣的,metrics-server 也只是顯示數據,並不提供數據存儲服務。
3、cAdvisor:cAdvisor是Google開源的容器資源監控和性能分析工具,它是專門為容器而生,本身也支持 Docker 容器,在 Kubernetes 中,我們不需要單獨去安裝,cAdvisor 作為 kubelet 內置的一部分程序可以直接使用。
4、Kube-state-metrics:kube-state-metrics通過監聽 API Server 生成有關資源對象的狀態指標,比如 Deployment、Node、Pod,需要注意的是 kube-state-metrics 只是簡單提供一個 metrics 數據,並不會存儲這些指標數據。

例子1:采集CPU的七種等待狀態參數,采集用戶每秒訪問請求量QPS

1、創建獨立的命名空間

apiVersion: v1
kind: Namespace
metadata:
  name: kube-ops

2、以configmap的形式管理配置文件prometheus.yml

apiVersion: v1
kind: ConfigMap
metadata:
  name: prometheus-config
  namespace: kube-ops
data:
  prometheus.yml: |
    global:
      scrape_interval: 15s
      scrape_timeout: 15s
    scrape_configs:
    - job_name: 'prometheus'
      static_configs:
      - targets: ['localhost:9090']

1、其中 global 模塊控制 Prometheus Server 的全局配置:
scrape_interval:表示 prometheus 抓取指標數據的頻率,默認是15s,我們可以覆蓋這個值
evaluation_interval:⽤來控制評估規則的頻率,prometheus 使⽤規則產⽣新的時間序列數據或 者產⽣警報
2、rule_files 模塊制定了規則所在的位置,prometheus 可以根據這個配置加載規則,⽤於⽣成新的時間 序列數據或者報警信息
3、scrape_configs ⽤於控制 prometheus 監控哪些資源。圖中配置是監聽自身

3、配置rbac認證

apiVersion: v1
kind: ServiceAccount
metadata:
  name: prometheus
  namespace: kube-ops
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: prometheus
rules:
- apiGroups:
  - ""
  resources:
  - nodes
  - services
  - endpoints
  - pods
  - nodes/proxy
  verbs:
  - get
  - list
  - watch
- apiGroups:
  - ""
  resources:
  - configmaps
  - nodes/metrics
  verbs:
  - get
- nonResourceURLs:
  - /metrics
  verbs:
  - get
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: prometheus
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: prometheus
subjects:
- kind: ServiceAccount
  name: prometheus
  namespace: kube-ops

4、配置pv和pvc用於數據持久化

apiVersion: v1
kind: PersistentVolume
metadata:
  name: prometheus
spec:
  capacity:
    storage: 10Gi
  accessModes:
  - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  nfs:
    server: 192.168.1.244
    path: /data/k8s

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: prometheus
  namespace: kube-ops
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi

5、創建 prometheus 的 Pod 資源

$ docker pull prom/prometheus:v2.4.3

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: prometheus
  namespace: kube-ops
  labels:
    app: prometheus
spec:
  template:
    metadata:
      labels:
        app: prometheus
    spec:
      serviceAccountName: prometheus
      containers:
      - image: prom/prometheus:v2.4.3
        name: prometheus
        command:
        - "/bin/prometheus"
        args:
        - "--config.file=/etc/prometheus/prometheus.yml"
        - "--storage.tsdb.path=/prometheus"
        - "--storage.tsdb.retention=24h"
        - "--web.enable-admin-api"  # 控制對admin HTTP API的訪問,其中包括刪除時間序列等功能
        - "--web.enable-lifecycle"  # 支持熱更新,直接執行localhost:9090/-/reload立即生效
        ports:
        - containerPort: 9090
          protocol: TCP
          name: http
        volumeMounts:
        - mountPath: "/prometheus"
          subPath: prometheus
          name: data
        - mountPath: "/etc/prometheus"
          name: config-volume
        resources:
          requests:
            cpu: 100m
            memory: 512Mi
          limits:
            cpu: 100m
            memory: 512Mi
      securityContext:
        runAsUser: 0
      volumes:
      - name: data
        persistentVolumeClaim:
          claimName: prometheus
      - configMap:
          name: prometheus-config
        name: config-volume

$ kubectl get pod -n kube-ops
prometheus-77d968648-w5j6z 1/1 Running 53 82d

6、創建prometheus pod的svc

apiVersion: v1
kind: Service
metadata:
  name: prometheus
  namespace: kube-ops
  labels:
    app: prometheus
spec:
  selector:
    app: prometheus
  type: NodePort
  ports:
    - name: web
      port: 9090
      targetPort: http

$ kubectl get svc -n kube-ops
prometheus NodePort 10.102.197.83 <none> 9090:32619/TCP


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM