最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

一、監(jiān)控部署

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

1.1 pod性能指標(k8s集群組件自動集成)

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

當(dāng)然kubelet也會輸出一些監(jiān)控指標數(shù)據(jù),因此pod的監(jiān)控數(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是一個簡單的服務(wù),它監(jiān)聽Kubernetes API服務(wù)器并生成關(guān)聯(lián)對象的指標。它不關(guān)注單個Kubernetes組件的運行狀況,而是關(guān)注內(nèi)部各種對象(如deployment、node、pod等)的運行狀況。

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

如果集群沒有安裝,可參考如下步驟進行部署:

docker pull gcr.io/google_containers/kube-state-metrics:v1.6.0
// 鏡像打標簽,設(shè)置為當(dāng)前k8s配置的鏡像倉庫地址
docker tag quay.io/coreos/kube-state-metrics:v1.9.0 dockerhub.kubekey.local/library/kube-state-metrics:v1.9.0
// 推進倉庫
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配置的倉庫地址
      - 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對外暴露
  type: NodePort

1.4 啟動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集群對外暴露的端口為 62177

1.7 查看集群信息

kubectl get po -n prometheus -owide

然后查看metrics信息

可以手動

curl k8s02:62177/metrics

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

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

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

# 1.創(chuàng)建serviceaccounts
kubectl create sa prometheus -n default
# 2.創(chuàng)建prometheus角色并對其綁定cluster-admin
kubectl create clusterrolebinding prometheus --clusterrole cluster-admin --serviceaccount=default:prometheus
# 3. 創(chuàng)建secret; k8s1.24之后默認不會為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. 測試訪問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. 測試訪問指標
# 訪問pod性能資源指標:(訪問kubelet)
# 注意:master1為當(dāng)前master節(jié)點的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"   // 修改為對應(yīng)的k8s master的節(jié)點
      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信息,請根據(jù)目錄進行配置即可

然后重啟prometheus

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

即可

docker restart prometheus

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

默認情況下 prometheus url: http://IP:9090

4、集成grafana

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

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

load 即可

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

JSON ID:15760

大盤信息即可完全展示~

總結(jié)

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

相關(guān)文章

  • Kubernetes之Pod的調(diào)度實現(xiàn)方式

    Kubernetes之Pod的調(diào)度實現(xiàn)方式

    Kubernetes通過定向調(diào)度(NodeName/NodeSelector)、親和性調(diào)度(NodeAffinity/PodAffinity/PodAntiAffinity)及污點容忍(Taints/Toleration)實現(xiàn)Pod節(jié)點控制,分別用于強制指定節(jié)點、優(yōu)化部署位置和靈活管理節(jié)點準入,滿足不同場景下的調(diào)度需求
    2025-09-09
  • 如何在 K8S 中使用 Values 文件定制不同環(huán)境下的應(yīng)用配置

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

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

    kubectl top如何查看資源占用

    文章介紹了如何安裝和使用metricserver來監(jiān)控Kubernetes集群中的節(jié)點和Pod資源使用情況,包括CPU和內(nèi)存的計量單位以及如何通過這些指標來管理資源請求和限制,同時,文章還討論了在Docker容器中使用jps命令和釋放ptrace權(quán)限的配置問題
    2026-03-03
  • K8s Token過期問題及解決(Kubeadm)

    K8s Token過期問題及解決(Kubeadm)

    本文介紹了Kubernetes中Token的生成、管理和使用方法,包括生成默認24小時Token和永久有效Token,以及查看、刪除Token和獲取CA證書Hash值,此外,還提供了Node節(jié)點加入K8s集群的完整命令
    2026-01-01
  • k8s強制刪除一個Pod的詳細步驟

    k8s強制刪除一個Pod的詳細步驟

    有時候遇到node宕機或者失聯(lián)太久導(dǎo)致pod一直處于Terminating狀態(tài),kubectl?delete又刪不掉,其實這個pod已經(jīng)確定已經(jīng)死了,需要強制把他摘掉,這篇文章主要給大家介紹了關(guān)于k8s強制刪除一個Pod的詳細步驟,需要的朋友可以參考下
    2024-11-11
  • Kubernetes中使用PersistentVolume掛載云盤方式

    Kubernetes中使用PersistentVolume掛載云盤方式

    這篇文章主要介紹了Kubernetes中使用PersistentVolume掛載云盤方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • k8s中topologyKey的作用及說明

    k8s中topologyKey的作用及說明

    topologyKey定義拓撲域,用于Pod親和/反親和性規(guī)則,調(diào)度器根據(jù)其分組節(jié)點,當(dāng)前配置僅強制節(jié)點標簽匹配,未限制副本數(shù)量,建議補充podAntiAffinity實現(xiàn)隔離
    2025-08-08
  • K8S?實用工具之合并多個kubeconfig實現(xiàn)詳解

    K8S?實用工具之合并多個kubeconfig實現(xiàn)詳解

    這篇文章主要為大家介紹了K8S?實用工具之合并多個kubeconfig實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03
  • Centos?8.2?升級內(nèi)核通過elrepo源的方法

    Centos?8.2?升級內(nèi)核通過elrepo源的方法

    這篇文章主要介紹了Centos?8.2?升級內(nèi)核通過elrepo源,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-10-10
  • MinIO使用基礎(chǔ)教程(最新整理)

    MinIO使用基礎(chǔ)教程(最新整理)

    文章介紹了MinIO云存儲服務(wù)的快速安裝和使用,并通過SpringBoot實現(xiàn)文件上傳和查詢的功能,感興趣的朋友跟隨小編一起看看吧
    2025-03-03

最新評論

昂仁县| 东阿县| 长治市| 宁都县| 长治市| 南溪县| 龙游县| 鹤山市| 嘉荫县| 田林县| 宝山区| 江永县| 楚雄市| 闸北区| 高青县| 加查县| 融水| 新泰市| 仁怀市| 阿坝| 巴彦县| 鄂伦春自治旗| 北流市| 平舆县| 平和县| 长岭县| 金山区| 柞水县| 和静县| 胶南市| 阿鲁科尔沁旗| 东乌珠穆沁旗| 闵行区| 梁河县| 新源县| 巴青县| 寿光市| 昌都县| 凉山| 汝阳县| 雅江县|