資源指標(metrics)和自定義指標(prometheus)


k8s的資源指標分類:

  • 資源指標  metrics-server內建API
  • 自定義指標  prometheus來采集,需要組件k8s-prometheus-adapter

新一代架構:

  核心指標流水線: 由kubelet、metrics-server以及由API server提供的api組成;CPU累計使用率、內存的實時使用率、pod的資源占用率及容器的磁盤占用率

  監控流水線:需要在集群上部署監控工具,用於從系統收集各種指標數據並提供終端用戶、存儲 系統以及HPA,他們包含核心指標及許多非核心指標;非核心指標本身不能被k8s所解析,需要第三方工具

♦  https://github.com/kubernetes/kubernetes/tree/master/cluster/addons    k8s插件

一、核心指標獲取

metrics-server: API server

♦  kubectl api-versions 中默認不包含metrics.k8s.io/v1beta1;使用時需要添加kube-aggregator前綴

♦  metrics部署文件: https://github.com/kubernetes-incubator/metrics-server/tree/master/deploy/1.8%2B

下載到本地並應用之后就可以使用 kubectl api-versions查詢,看到metrics.k8s.io/v1beta1已經存在了

應用之前需要修改metrics-server-deployment.yaml文件,添加如下:

  • Warming: metrics-server這個容器不能通過CoreDNS 10.96.0.10:53 解析各Node的主機名,metrics-server連節點時默認是連接節點的主機名,需要加個參數,讓它連接節點的IP:“–kubelet-preferred-address-types=InternalIP”

        因為10250是https端口,連接它時需要提供證書,所以加上–kubelet-insecure-tls,表示不驗證客戶端證書,此前的版本中使用–source=這個參數來指定不驗證客戶端證書

執行kukectl apply -f ./  

 然后就可以使用kubetctl top nodes來獲取信息了

♦   也可以使用kubectl proxy --port=xxxx代理后使用curl http://localhost:xxxx/apis/metrics.k8s.io/v1beta1/  接口來獲取數據

 

二、自定義指標  --- Prometheus

♦   node_exporter用來暴露node信息,還有其他的exporter

♦   PromQL查詢語句,不能直接被k8s直接解析,需要通過kube-state-metrics組件轉k8s-promethues-adpater轉為Custom Metrics API

 

 

安裝node-exporter:
# kubectl apply -f node-exporter-ds.yml # kubectl apply -f node-exporter-service.yaml ♦ Prometheus需要先創建pv ♦ pv始終處於“Terminating”狀態,而且delete不掉。直接刪除k8s中的記錄:kubectl patch pv xxx -p '{"metadata":{"finalizers":null}}' ♦ Prometheus service 需要修改type:NodePort以便外部訪問
安裝prometheus: # kubectl apply
-f prometheus-rbac.yaml # kubectl apply -f prometheus-configmap.yaml # kubectl apply -f prometheus-service.yaml # kubectl apply -f prometheus-statefulset.yaml

到目前為止們就可以使用http://nodeip:serviceport登陸prometheus了
  

安裝k8s-prometheus-adapter:
https://github.com/DirectXMan12/k8s-prometheus-adapter
把adapterdeploy文件下載到本地服務器
首先需要自建證書:
Create a secret called cm-adapter-serving-certs with two values: serving.crt and serving.key. These are the serving certificates used by the adapter for serving HTTPS traffic.
# cd /etc/kubernetes/pki
# (umask 077;openssl genrsa -out serving.key 2048)

# openssl req -new -key serving.key -out serving.csr -subj "/CN=serving"
# openssl x509 -days 1000 -req -in serving.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out serving.crt
# openssl x509 -in serving.crt -text -noout
創建secret:
# kubectl create secret generic cm-adapter-serving-certs --from-file=serving.crt=./serving.crt --from-file=serving.key=./serving.key
修改custom-metrics-config-map.yaml custom-metrics-apiserver-deployment.yaml中namespace為自己規划的namespace,然后運行
# kubectl apply -f custom-metrics-config-map.yaml
# kubectl apply -f custom-metrics-apiserver-deployment.yaml
創建grafana,grafana.yaml influxdb.yaml中namespace為自己規划的namespace,並且gfannao中的添加“type: NodePort”,然后運行
# kubectl apply -f influxdb.yaml
# kubectl apply -f grafana.yaml
grafana接入prometheus數據
首先找到grafana的service暴露出來的端口通過http://ip:port進入grafana主頁,左側設置按鍵Configration->Data Sources->Add data source

  然后點擊save&Test,點擊上方Dashboards默認存在幾個dashborad,點擊右側import即可導入。

  

  然后在dashboard home中點擊剛剛導入的dashboard即可看到視圖, 也可以自行到grafana模板中心下載自己想要的模板導入,或者自定義模板都可以

  

 

 

 三、HPA --- 基於資源值指標的伸縮,默認是使用v1控制器,也可以指定使用v2版本

 指定Deployment、ReplicaSet或ReplicationController,並創建已經定義好資源的自動伸縮器。使用自動伸縮器可以根據需要自動增加或減少系統中部署的pod數量.

部署一個deploy服務
# kubectl run ngx-autoscale --image=nginx --replicas=1 --requests='cpu=20m,memory=256Mi' --limits='cpu=30m,memory=256Mi' --labels='app=myapp' --expose --port=80 -n develop
# kubectl patch svc ngx-autoscale -p '{"spec":{"type":"NodePort"}}' -n develop

 # kubectl autoscale deploy deploy-autoscale-test --min=1 --max=4 --cpu-percent=60 -n develop

 

 使用ab壓力測試工具測試

  # ab -c 100 -n 50000 http://34.240.13.92:32541/index.html

 

 測試完成之后,pod會自動伸縮,有一定的延遲。

♦ 也可以根據自定義的merics參數來定義參考值

 

 


免責聲明!

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



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