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

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)文章

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

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

    Kubernetes通過(guò)定向調(diào)度(NodeName/NodeSelector)、親和性調(diào)度(NodeAffinity/PodAffinity/PodAntiAffinity)及污點(diǎn)容忍(Taints/Toleration)實(shí)現(xiàn)Pod節(jié)點(diǎn)控制,分別用于強(qiáng)制指定節(jié)點(diǎn)、優(yōu)化部署位置和靈活管理節(jié)點(diǎn)準(zhǔn)入,滿足不同場(chǎng)景下的調(diào)度需求
    2025-09-09
  • 如何在 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 top如何查看資源占用

    kubectl top如何查看資源占用

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

    K8s Token過(guò)期問(wèn)題及解決(Kubeadm)

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

    k8s強(qiáng)制刪除一個(gè)Pod的詳細(xì)步驟

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

    Kubernetes中使用PersistentVolume掛載云盤方式

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

    k8s中topologyKey的作用及說(shuō)明

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

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

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

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

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

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

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

最新評(píng)論

桂东县| 静安区| 开封县| 元朗区| 邵阳县| 山阴县| 巴东县| 普宁市| 平乡县| 康马县| 扎囊县| 罗定市| 马关县| 鲁山县| 冷水江市| 丹东市| 临朐县| 永昌县| 石门县| 邓州市| 武冈市| 贡山| 正镶白旗| 大港区| 云安县| 凤台县| 鸡东县| 天等县| 泰安市| 鸡西市| 汉寿县| 磐石市| 潍坊市| 西青区| 铜鼓县| 大兴区| 濮阳县| 永川市| 青铜峡市| 新蔡县| 凌云县|