欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

K8S如何利用Prometheus監(jiān)控pod的實(shí)時(shí)數(shù)據(jù)指標(biāo)

 更新時(shí)間:2024年01月05日 09:41:16   作者:聽(tīng)說(shuō)唐僧不吃肉  
這篇文章主要給大家介紹了關(guān)于K8S如何利用Prometheus監(jiān)控pod的實(shí)時(shí)數(shù)據(jù)指標(biāo)的相關(guān)資料,Prometheus是一個(gè)開(kāi)源的服務(wù)監(jiān)控系統(tǒng)和時(shí)序數(shù)據(jù)庫(kù),其提供了通用的數(shù)據(jù)模型和快捷數(shù)據(jù)采集、存儲(chǔ)和查詢接口,需要的朋友可以參考下

一、監(jiān)控部署

1、將k8s集群中kube-state-metrics指標(biāo)進(jìn)行收集,服務(wù)進(jìn)行部署

1.1 pod性能指標(biāo)(k8s集群組件自動(dòng)集成)

k8s組件本身提供組件自身運(yùn)行的監(jiān)控指標(biāo)以及容器相關(guān)的監(jiān)控指標(biāo)。通過(guò)cAdvisor 是一個(gè)開(kāi)源的分析容器資源使用率和性能特性的代理工具,集成到 Kubelet中,當(dāng)Kubelet啟動(dòng)時(shí)會(huì)同時(shí)啟動(dòng)cAdvisor,且一個(gè)cAdvisor只監(jiān)控一個(gè)Node節(jié)點(diǎn)的信息。cAdvisor 自動(dòng)查找所有在其所在節(jié)點(diǎn)上的容器,自動(dòng)采集 CPU、內(nèi)存、文件系統(tǒng)和網(wǎng)絡(luò)使用的統(tǒng)計(jì)信息。cAdvisor 通過(guò)它所在節(jié)點(diǎn)機(jī)的 Root 容器,采集并分析該節(jié)點(diǎn)機(jī)的全面使用情況。

當(dāng)然kubelet也會(huì)輸出一些監(jiān)控指標(biāo)數(shù)據(jù),因此pod的監(jiān)控?cái)?shù)據(jù)有kubelet和cadvisor,監(jiān)控url分別為

https://NodeIP:10250/metrics
https://NodeIP:10250/metrics/cadvisor

1.2 K8S資源監(jiān)控(k8s集群內(nèi)部署)

kube-state-metrics是一個(gè)簡(jiǎn)單的服務(wù),它監(jiān)聽(tīng)Kubernetes API服務(wù)器并生成關(guān)聯(lián)對(duì)象的指標(biāo)。它不關(guān)注單個(gè)Kubernetes組件的運(yùn)行狀況,而是關(guān)注內(nèi)部各種對(duì)象(如deployment、node、pod等)的運(yùn)行狀況。

注:先手動(dòng)檢查下集群,是否已經(jīng)安裝kube-state-metrics

如果集群沒(méi)有安裝,可參考如下步驟進(jìn)行部署:

docker pull gcr.io/google_containers/kube-state-metrics:v1.6.0
// 鏡像打標(biāo)簽,設(shè)置為當(dāng)前k8s配置的鏡像倉(cāng)庫(kù)地址
docker tag quay.io/coreos/kube-state-metrics:v1.9.0 dockerhub.kubekey.local/library/kube-state-metrics:v1.9.0
// 推進(jìn)倉(cāng)庫(kù)
docker push dockerhub.kubekey.local/library/kube-state-metrics:v1.9.0

1.3 編輯kube-state-metrics.yml文件

vim kube-state-metrics.yml
---
apiVersion: v1
kind: ServiceAccount
metadata:
  labels:
    app: kube-state-metrics
  name: kube-state-metrics
  namespace: prometheus
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: kube-state-metrics
rules:
- apiGroups: [""]
  resources:
  - configmaps
  - secrets
  - nodes
  - pods
  - services
  - resourcequotas
  - replicationcontrollers
  - limitranges
  - persistentvolumeclaims
  - persistentvolumes
  - namespaces
  - endpoints
  verbs: ["list", "watch"]
- apiGroups: ["extensions"]
  resources:
  - daemonsets
  - deployments
  - replicasets
  - ingresses
  verbs: ["list", "watch"]
- apiGroups: ["apps"]
  resources:
  - daemonsets
  - deployments
  - replicasets
  - statefulsets
  verbs: ["list", "watch"]
- apiGroups: ["batch"]
  resources:
  - cronjobs
  - jobs
  verbs: ["list", "watch"]
- apiGroups: ["autoscaling"]
  resources:
  - horizontalpodautoscalers
  verbs: ["list", "watch"]
- apiGroups: ["policy"]
  resources:
  - poddisruptionbudgets
  verbs: ["list", "watch"]
- apiGroups: ["certificates.k8s.io"]
  resources:
  - certificatesigningrequests
  verbs: ["list", "watch"]
- apiGroups: ["storage.k8s.io"]
  resources:
  - storageclasses
  verbs: ["list", "watch"]
- apiGroups: ["autoscaling.k8s.io"]
  resources:
  - verticalpodautoscalers
  verbs: ["list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  labels:
    app: kube-state-metrics
  name: kube-state-metrics
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: kube-state-metrics
subjects:
- kind: ServiceAccount
  name: kube-state-metrics
  namespace: prometheus
---
#apiVersion: extensions/v1beta1
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: kube-state-metrics
  name: kube-state-metrics
  namespace: prometheus
spec:
  replicas: 1
  selector:
    matchLabels:
      app: kube-state-metrics
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: kube-state-metrics
    spec:
      containers:
      # 注意,這里image地址修改為你k8s配置的倉(cāng)庫(kù)地址
      - image: dockerhub.kubekey.local/library/kube-state-metrics:v1.9.0
        imagePullPolicy: IfNotPresent
        livenessProbe:
          failureThreshold: 3
          httpGet:
            path: /
            port: 8080
            scheme: HTTP
          initialDelaySeconds: 30
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 30
        name: kube-state-metrics
        ports:
        - containerPort: 8080
          protocol: TCP
        readinessProbe:
          failureThreshold: 3
          httpGet:
            path: /
            port: 8080
            scheme: HTTP
          initialDelaySeconds: 30
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 5
        resources:
          limits:
            cpu: 500m
            memory: 768Mi
          requests:
            cpu: 250m
            memory: 768Mi
      restartPolicy: Always
      serviceAccount: kube-state-metrics
      serviceAccountName: kube-state-metrics
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: kube-state-metrics
  name: kube-state-metrics
  namespace: prometheus
spec:
  ports:
  - name: kube-state-metrics
    port: 80
    protocol: TCP
    targetPort: 8080
  selector:
    app: kube-state-metrics
    ## 注意這里kube-state-metrics暴露類型修改為NodePort對(duì)外暴露
  type: NodePort

1.4 啟動(dòng)yaml文件

kubectl apply -f kube-state-metrics.yaml

1.5 查看pod信息

kubectl get pod -n prometheus

1.6 查看service信息

kubectl get svc -n prometheus

這里可以看到k8s集群對(duì)外暴露的端口為 62177

1.7 查看集群信息

kubectl get po -n prometheus -owide

然后查看metrics信息

可以手動(dòng)

curl k8s02:62177/metrics

正常,數(shù)據(jù)metrics就會(huì)出現(xiàn)

二、創(chuàng)建token供集群外部訪問(wèn)

集群外部監(jiān)控K8s集群,通過(guò)訪問(wèn)kube-apiserver來(lái)訪問(wèn)集群資源。通過(guò)這種方式集群外部prometheus也能自動(dòng)發(fā)現(xiàn)k8s集群服務(wù)

# 1.創(chuàng)建serviceaccounts
kubectl create sa prometheus -n default
# 2.創(chuàng)建prometheus角色并對(duì)其綁定cluster-admin
kubectl create clusterrolebinding prometheus --clusterrole cluster-admin --serviceaccount=default:prometheus
# 3. 創(chuàng)建secret; k8s1.24之后默認(rèn)不會(huì)為serveiceaccounts創(chuàng)建secret
kubectl apply -f - <<EOF
apiVersion: v1
kind: Secret
type: kubernetes.io/service-account-token
metadata:
  name: prometheus-token
  namespace: default
  annotations:
    kubernetes.io/service-account.name: "prometheus"
EOF
# 4. 測(cè)試訪問(wèn)kube-apiserver
APISERVER=$(kubectl config view --minify -o jsonpath='{.clusters[0].cluster.server}')
TOKEN=$(kubectl get secret  prometheus-token -n default -o jsonpath='{.data.token}' | base64 --decode)
curl $APISERVER/api --header "Authorization: Bearer $TOKEN" --insecure
# 5. 保存token
echo $TOKEN > k8s_token
# 6. 測(cè)試訪問(wèn)指標(biāo)
# 訪問(wèn)pod性能資源指標(biāo):(訪問(wèn)kubelet)
# 注意:master1為當(dāng)前master節(jié)點(diǎn)的hostname,需要修改
curl $APISERVER/api/v1/nodes/master1:10250/proxy/metrics --header "Authorization: Bearer $TOKEN" --insecure

三、集成Prometheus配置

vim prometheus.yml
scrape_configs:
  - job_name: "k8s-cadvisor"
    honor_timestamps: true
    metrics_path: /metrics
    scheme: https
    kubernetes_sd_configs:
    - api_server: https://10.142.155.202:6443
      role: node
      bearer_token_file: /prometheus/data/k8s_token
      tls_config:
        insecure_skip_verify: true
    bearer_token_file: /prometheus/data/k8s_token
    tls_config:
      insecure_skip_verify: true
    relabel_configs:
    - action: labelmap
      regex: __meta_kubernetes_node_label_(.+)
    - separator: ;
      regex: (.*)
      target_label: __address__
      replacement: 10.142.155.202:6443
      action: replace
    - source_labels: [__meta_kubernetes_node_name]
      separator: ;
      regex: (.+)
      target_label: __metrics_path__
      replacement: /api/v1/nodes/${1}:10250/proxy/metrics/cadvisor
      action: replace
  - job_name: "kube-node-kubelet"
    scheme: https
    tls_config:
      insecure_skip_verify: true
    bearer_token_file: /prometheus/data/k8s_token
    kubernetes_sd_configs:
    - role: node
      api_server: "https://10.142.155.202:6443"   // 修改為對(duì)應(yīng)的k8s master的節(jié)點(diǎn)
      tls_config:
        insecure_skip_verify: true
      bearer_token_file: /prometheus/data/k8s_token
    relabel_configs:
    - target_label: __address__
      replacement: 10.142.155.202:6443
    - source_labels: [__meta_kubernetes_node_name]
      regex: (.+)
      target_label: __metrics_path__
      replacement: /api/v1/nodes/${1}:10250/proxy/metrics
    - action: labelmap
      regex: __meta_kubernetes_service_label_(.+)
    - source_labels: [__meta_kubernetes_namespace]
      action: replace
      target_label: kubernetes_namespace
    - source_labels: [__meta_kubernetes_service_name]
      action: replace
      target_label: service_name

注意:bearer_token_file: /prometheus/data/k8s_token

這里的token為上面生成的token信息,請(qǐng)根據(jù)目錄進(jìn)行配置即可

然后重啟prometheus

如果是容器部署的prometheus,需要考慮映射token,可docker cp到/prometheus/data/ 即可

即可

docker restart prometheus

3、進(jìn)入prometheus界面,查看相關(guān)指標(biāo)

默認(rèn)情況下 prometheus url: http://IP:9090

4、集成grafana

導(dǎo)入grafana JSON ID, 747

4.1、導(dǎo)入node信息指標(biāo)

load 即可

4.2、導(dǎo)入pod信息指標(biāo)

JSON ID:15760

大盤信息即可完全展示~

總結(jié)

到此這篇關(guān)于K8S如何利用Prometheus監(jiān)控pod的實(shí)時(shí)數(shù)據(jù)指標(biāo)的文章就介紹到這了,更多相關(guān)K8S Prometheus監(jiān)控pod實(shí)時(shí)數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 如何在 K8S 中使用 Values 文件定制不同環(huán)境下的應(yīng)用配置

    如何在 K8S 中使用 Values 文件定制不同環(huán)境下的應(yīng)用配置

    Kubernetes是一個(gè)開(kāi)源的容器編排平臺(tái),它可以自動(dòng)化容器的部署、擴(kuò)展和管理,在 K8s 中,應(yīng)用程序通常以容器的形式運(yùn)行,這些容器被組織在不同的資源對(duì)象中,這篇文章主要介紹了如何在 K8S 中使用 Values 文件定制不同環(huán)境下的應(yīng)用配置,需要的朋友可以參考下
    2025-03-03
  • 詳解kubectl資源管理命令(陳述式)

    詳解kubectl資源管理命令(陳述式)

    kubectl是官方的CTL命令行工具,用于與apiserver進(jìn)行通信,將用戶在命令行輸入的命令,組織并轉(zhuǎn)化為apiserver能識(shí)別的信息,進(jìn)而實(shí)現(xiàn)管理k8s各種資源的一種有效途徑,這篇文章主要介紹了詳解kubectl資源管理命令(陳述式),需要的朋友可以參考下
    2025-04-04
  • k8s查看pod日志的幾種實(shí)用方法匯總

    k8s查看pod日志的幾種實(shí)用方法匯總

    Pod是Kubernetes中能夠創(chuàng)建和部署的最小單元,是Kubernetes集群中的一個(gè)應(yīng)用實(shí)例,總是部署在同一個(gè)節(jié)點(diǎn)Node上,下面這篇文章主要給大家介紹了k8s查看pod日志的幾種實(shí)用方法,需要的朋友可以參考下
    2022-07-07
  • Kubernetes教程之Windows?HostProcess?運(yùn)行容器化負(fù)載

    Kubernetes教程之Windows?HostProcess?運(yùn)行容器化負(fù)載

    這篇文章主要介紹了Kubernetes?Windows?HostProcess?運(yùn)行容器化負(fù)載,本篇內(nèi)容還是比較多的,總共包含了?Windows?HostProcess的創(chuàng)建、為?Windows?Pod?和容器配置?GMSA?和?Windows?的?Pod?和容器配置?RunAsUserName三大功能模塊,需要的朋友可以參考下
    2022-07-07
  • k8s高可用集群安裝教程

    k8s高可用集群安裝教程

    本文給大家介紹k8s高可用集群安裝教程,本文通過(guò)圖文示例相結(jié)合給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧
    2025-03-03
  • Kubernetes控制器中DaemonSet與Job的使用教程

    Kubernetes控制器中DaemonSet與Job的使用教程

    這篇文章主要介紹了Kubernetes控制器中DaemonSet與Job的使用,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-08-08
  • k8s跨服務(wù)調(diào)用入門到實(shí)戰(zhàn)示例詳解

    k8s跨服務(wù)調(diào)用入門到實(shí)戰(zhàn)示例詳解

    這篇文章主要為大家介紹了k8s跨服務(wù)調(diào)用入門到實(shí)戰(zhàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • 二進(jìn)制方式安裝?Kubernetes1.18.3版本實(shí)現(xiàn)腳本

    二進(jìn)制方式安裝?Kubernetes1.18.3版本實(shí)現(xiàn)腳本

    這篇文章主要為大家介紹了二進(jìn)制方式安裝Kubernetes1.18.3版本實(shí)現(xiàn)腳本,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-03-03
  • 如何在Centos中搭建 K8s 1.23 集群

    如何在Centos中搭建 K8s 1.23 集群

    文章詳細(xì)介紹了在CentOS上搭建Kubernetes 1.23集群的步驟,包括準(zhǔn)備環(huán)境、安裝Kubernetes軟件包、上傳離線鏡像、初始化集群、添加節(jié)點(diǎn)、安裝網(wǎng)絡(luò)插件以及測(cè)試驗(yàn)證,感興趣的朋友一起看看吧
    2025-03-03
  • k8s入門實(shí)戰(zhàn)deployment使用詳解

    k8s入門實(shí)戰(zhàn)deployment使用詳解

    這篇文章主要為大家介紹了k8s入門實(shí)戰(zhàn)deployment使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03

最新評(píng)論