Prometheus在k8s上的安裝與使用

Prometheus可是個好東西,云原生時代監(jiān)控領(lǐng)域的現(xiàn)象級產(chǎn)品底扳,常與Grafana搭配使用寂嘉,是當(dāng)前互聯(lián)網(wǎng)企業(yè)的首選監(jiān)控解決方案。

一班挖、安裝Prometheus

安裝主要有YAML鲁捏、Operater兩種,先從YAML開始可以更好的理解細節(jié)(Operater最終也是生成的yml文件)。需要考慮幾個點:

  • 訪問權(quán)限
  • 配置文件
  • 存儲卷
    訪問權(quán)限相關(guān)的配置:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: prometheus
rules:
  - apiGroups: [""] # "" indicates the core API group
    resources:
      - nodes
      - nodes/proxy
      - services
      - endpoints
      - pods
    verbs:
      - get
      - watch
      - list
  - apiGroups:
      - extensions
    resources:
      - ingresses
    verbs:
      - get
      - watch
      - list
  - nonResourceURLs: ["/metrics"]
    verbs:
      - get
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: prometheus
  namespace: smac
  labels:
    app: prometheus
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: prometheus
subjects:
  - kind: ServiceAccount
    name: prometheus
    namespace: smac
roleRef:
  kind: ClusterRole
  name: prometheus
  apiGroup: rbac.authorization.k8s.io

配置文件configmap

apiVersion: v1
kind: ConfigMap
metadata:
  name: prometheus-rules
  namespace: smac
  labels:
    app: prometheus
data:
  cpu-usage.rule: |
    #因篇幅過長给梅,此處內(nèi)容忽略
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: prometheus-conf
  namespace: smac
  labels:
    app: prometheus
data:
  prometheus.yml: |-
  #因篇幅過長假丧,此處內(nèi)容忽略

存儲卷相關(guān)的配置,建議使用StorageClass动羽,官方不建議使用NFS包帚,極端情況會導(dǎo)致數(shù)據(jù)丟失,配置如下:

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: prometheus-pvc
  namespace: smac
  labels:
    app: prometheus
  annotations:
    volume.beta.kubernetes.io/storage-class: "local"
  finalizers:
    - kubernetes.io/pvc-protection
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi

后面运吓,就是常規(guī)的deployment和service的配置:

kind: Deployment
apiVersion: apps/v1
metadata:
  labels:
    app: prometheus
  name: prometheus
  namespace: smac
spec:
  replicas: 1
  selector:
    matchLabels:
      app: prometheus
  template:
    metadata:
      labels:
        app: prometheus
    spec:
      serviceAccountName: prometheus
      securityContext:
        runAsUser: 0
      containers:
        - name: prometheus
          image: prom/prometheus:v2.29.1
          imagePullPolicy: IfNotPresent
          volumeMounts:
            - mountPath: /prometheus
              name: prometheus-data-volume
            - name: prometheus-conf-volume  #注意渴邦,此處不能用subPath,會導(dǎo)致configmap的熱更新失效
              mountPath: /etc/prometheus
            - name: prometheus-rules-volume
              mountPath: /etc/prometheus/rules
          ports:
            - containerPort: 9090
              protocol: TCP
      volumes:
        - name: prometheus-data-volume
          persistentVolumeClaim:
            claimName: prometheus-data-pvc
        - name: prometheus-conf-volume
          configMap:
            name: prometheus-conf
        - name: prometheus-rules-volume
          configMap:
            name: prometheus-rules
---
#service
kind: Service
apiVersion: v1
metadata:
  annotations:
    prometheus.io/scrape: 'true'
  labels:
    app: prometheus
  name: prometheus-service
  namespace: smac
spec:
  ports:
    - port: 9090
      targetPort: 9090
  selector:
    app: prometheus
  type: NodePort

二拘哨、配置熱更新

接下來谋梭,我們要在prometheus中添加一個job。修改configmap中的prometheus.yml倦青,增加如下內(nèi)容:

scrape_configs:
  ...
  - job_name: "demo-service"
    metrics_path: "/actuator/prometheus"
    static_configs:
      - targets: ["10.233.97.135:8080"]

嗯瓮床?發(fā)現(xiàn)并沒有生效誒?難道需要重啟产镐?有沒有熱更新的方式隘庄?
于是一通搜索,得到以下結(jié)論:

Prometheus支持熱更新癣亚,在啟動時通過參數(shù)--web.enable-lifecycle開啟丑掺,之后通過 curl -X POST http://localhost:9090/-/reload 即可更新。

于是調(diào)整配置如下:

      containers:
        - name: prometheus
          args:
            - '--config.file=/etc/prometheus/prometheus.yml'
            - '--web.enable-lifecycle'

調(diào)整之后進行了重新部署逃糟,然后修改configmap的內(nèi)容吼鱼,按照上面的命令執(zhí)行就可以了。但是绰咽,手動更新依然很麻煩菇肃,能不能自動更細呢?于是取募,又一通搜索琐谤,發(fā)現(xiàn)了一款神器:configmap-reload,于是趕緊配置上:

      containers:
        - name: prometheus
          image: prom/prometheus:v2.29.1
          args:
            - '--config.file=/etc/prometheus/prometheus.yml'
            - '--web.enable-lifecycle'
          ...
        - name: prometheus-configmap-reloader
          image: 'jimmidyson/configmap-reload:v0.3.0'
          args:
            - '--webhook-url=http://localhost:9090/-/reload'
            - '--volume-dir=/etc/prometheus'   #此處的volume-dir應(yīng)該與volumes的定義完全一致
          volumeMounts:
            #注意玩敏,此處不能用subPath斗忌,會導(dǎo)致configmap的熱更新失效
            - name: prometheus-conf-volume
              mountPath: /etc/prometheus

調(diào)整之后,發(fā)現(xiàn)容器組里多了一個prometheus-configmap-reloader的pod旺聚。此時再嘗試织阳,修改configmap后過一小會兒(大概10s,不要問我為什么砰粹,你懂的)唧躲,新增的配置項生效了,我們從Targets中發(fā)現(xiàn)了‘demo-service’。

注意:configmap如果使用subPath進行掛載弄痹,將無法自動更新饭入。

三、Job的服務(wù)發(fā)現(xiàn)

上面我們添加一個job肛真,targets中是指定的ip:port谐丢,在k8s中顯然是不實用的,我們必須要實現(xiàn)動態(tài)獲取target蚓让。好在prometheus已經(jīng)支持了該功能乾忱,原理是通過apiserver輪詢pod信息。通過kubernetes_sd_configs凭疮,可以實現(xiàn)各種資源的服務(wù)發(fā)現(xiàn)饭耳,下面是pod的配置示例:

      - job_name: 'kubernetes-pods'
        kubernetes_sd_configs:
        - role: pod
        relabel_configs:
        - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
          action: keep
          regex: true
        - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path]
          action: replace
          target_label: __metrics_path__
          regex: (.+)
        - source_labels: [__address__, __meta_kubernetes_pod_annotation_prometheus_io_port]
          action: replace
          regex: ([^:]+)(?::\d+)?;(\d+)
          replacement: $1:$2
          target_label: __address__
        - action: labelmap
          regex: __meta_kubernetes_pod_label_(.+)
        - source_labels: [__meta_kubernetes_namespace]
          action: replace
          target_label: kubernetes_namespace
        - source_labels: [__meta_kubernetes_pod_name]
          action: replace
          target_label: kubernetes_pod_name

這個是官方例子串述,具體label的清單以及含義执解,請參照官網(wǎng):Prometheus#Configuration#kubernetes_sd_config

我的實戰(zhàn)配置如下:

scrape_configs:
  - job_name: "demo-service"
    metrics_path: "/actuator/prometheus"
    static_configs:
      - targets: ["demo-service:8080"]

    kubernetes_sd_configs:
    - role: pod

    relabel_configs:
    - source_labels: [__meta_kubernetes_pod_label_app]
      regex: demo-service
      action: keep
    - source_labels: [__meta_kubernetes_pod_label_app]
      action: replace
      target_label: application
    - source_labels: [__address__, __meta_kubernetes_pod_annotation_prometheus_io_port]
      action: replace
      regex: ([^:]+)(?::\d+)?;(\d+)
      replacement: $1:$2
      target_label: __address__

此配置的效果是從所有pod中尋找label:app=demo-serivce的pod,將pod的地址和端口替換上面配置中targets的內(nèi)容纲酗,并添加一個application=demo-service的label衰腌。

至此,基本滿足平時的使用了觅赊,再往后就是高可用HA右蕊、第三方存儲、PromQL的實戰(zhàn)等高級內(nèi)容吮螺,敬請期待~饶囚!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市鸠补,隨后出現(xiàn)的幾起案子萝风,更是在濱河造成了極大的恐慌,老刑警劉巖紫岩,帶你破解...
    沈念sama閱讀 221,695評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件规惰,死亡現(xiàn)場離奇詭異,居然都是意外死亡泉蝌,警方通過查閱死者的電腦和手機歇万,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來勋陪,“玉大人贪磺,你說我怎么就攤上這事∽缬蓿” “怎么了寒锚?”我有些...
    開封第一講書人閱讀 168,130評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我壕曼,道長苏研,這世上最難降的妖魔是什么赚哗? 我笑而不...
    開封第一講書人閱讀 59,648評論 1 297
  • 正文 為了忘掉前任流部,我火速辦了婚禮,結(jié)果婚禮上扰法,老公的妹妹穿的比我還像新娘轧飞。我一直安慰自己衅鹿,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 68,655評論 6 397
  • 文/花漫 我一把揭開白布过咬。 她就那樣靜靜地躺著大渤,像睡著了一般。 火紅的嫁衣襯著肌膚如雪掸绞。 梳的紋絲不亂的頭發(fā)上泵三,一...
    開封第一講書人閱讀 52,268評論 1 309
  • 那天,我揣著相機與錄音衔掸,去河邊找鬼烫幕。 笑死,一個胖子當(dāng)著我的面吹牛敞映,可吹牛的內(nèi)容都是我干的较曼。 我是一名探鬼主播,決...
    沈念sama閱讀 40,835評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼振愿,長吁一口氣:“原來是場噩夢啊……” “哼捷犹!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起冕末,我...
    開封第一講書人閱讀 39,740評論 0 276
  • 序言:老撾萬榮一對情侶失蹤萍歉,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后栓霜,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體翠桦,經(jīng)...
    沈念sama閱讀 46,286評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,375評論 3 340
  • 正文 我和宋清朗相戀三年胳蛮,在試婚紗的時候發(fā)現(xiàn)自己被綠了销凑。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,505評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡仅炊,死狀恐怖斗幼,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情抚垄,我是刑警寧澤蜕窿,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布谋逻,位于F島的核電站,受9級特大地震影響桐经,放射性物質(zhì)發(fā)生泄漏毁兆。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,873評論 3 333
  • 文/蒙蒙 一阴挣、第九天 我趴在偏房一處隱蔽的房頂上張望气堕。 院中可真熱鬧,春花似錦畔咧、人聲如沸茎芭。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽梅桩。三九已至,卻和暖如春拜隧,著一層夾襖步出監(jiān)牢的瞬間宿百,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評論 1 272
  • 我被黑心中介騙來泰國打工虹蓄, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留犀呼,地道東北人。 一個月前我還...
    沈念sama閱讀 48,921評論 3 376
  • 正文 我出身青樓薇组,卻偏偏與公主長得像,于是被迫代替她去往敵國和親坐儿。 傳聞我的和親對象是個殘疾皇子律胀,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,515評論 2 359

推薦閱讀更多精彩內(nèi)容