1. 程式人生 > >K8s 多自定義計量(custom metrics)

K8s 多自定義計量(custom metrics)


         這裡介紹下如何在K8s設定多個自定義計量。K8s 預設的可用於PHA(Pod Horizon Autoscaling)只有CPU,自定義的計量feature一直處於alpha 測試階段,而且不穩定,在某些版本未實現custom metric,比如1.6.2。

         這裡以1.5.7 版本為例,首先部署K8s,heapster,grafana,influxdb。部署方法可參考https://github.com/kubernetes/heapster/tree/master/deploy/kube-config/influxdb

         在kubelet 啟動引數裡要加上--enable-custom-metrics=true,kubelet會根據這個引數在容器啟動時,對容器加label"io.cadvisor.metric.prometheus":"/etc/custom-metrics/definition.json", 在1.6.2 版本里,就是因為不再使用/pkg/kubelet/dockertools 管理容器 ,而是使用新的/pkg/kubelet/dockershim,而在dockershim中,還未實現加這中label,導致未實現custom metric。有了這個label後,

         要支援custom metric,對容器也有要求,如果容器內/etc/custom-metrics/definition.json要有API endpoint 供 cAdvisor獲取。

         cat/etc/custom-metrics/definition.json

{"endpoint":"http://192.168.10.2:8080/metrics"}

這個檔案裡面的IP是動態寫入的,在容器初始化時寫入PodIP。具體可參考

https://medium.com/@marko.luksa/kubernetes-autoscaling-based-on-custom-metrics-without-using-a-host-port-b783ed6241ac。具體yaml檔案如下

apiVersion:extensions/v1beta1

kind: Deployment

metadata:

  name: kubia

spec:

  replicas: 1

  template:

    metadata:

      name: kubia

      labels:

        app: kubia

      annotations:

        pod.beta.kubernetes.io/init-containers:'[

            {

                "name":"setup",

                "image":"busybox",

                "imagePullPolicy":"IfNotPresent",

                "command":["sh", "-c", "echo \"{\\\"endpoint\\\":\\\"http://$POD_IP:8080/metrics\\\"}\" >/etc/custom-metrics/definition.json"],

                "env": [{

                  "name":"POD_IP",

                  "valueFrom": {

                    "fieldRef": {

                      "apiVersion":"v1",

                      "fieldPath":"status.podIP"

                    }

                  }

                }],

                "volumeMounts": [

                    {

                        "name":"config",

                        "mountPath":"/etc/custom-metrics"

                    }

                ]

            }

        ]'

    spec:

      containers:

      - image: luksa/kubia:qps

        name: nodejs

        ports:

          - containerPort: 8080

        volumeMounts:

        - name: config

          mountPath: /etc/custom-metrics

 

這樣容器啟動時,容器內就有/etc/custom-metrics/definition.json檔案裡。

在容器裡要提供一個restAPI 服務,服務地址就是definition.json所標記地址。可以嘗試curl 這個地址,看是否正常獲取資料。

curl http://192.168.10.2:8080/metrics

# TYPE qpsgauge                                                                    //不能忽略該行,否則未知型別的資料會被heapster拋棄

qps 0

# TYPE testmetricgauge                                                      //不能忽略該行,否則未知型別的資料會被heapster拋棄

testmetric 0.41291926987469196

         然後兩種方法確認cAdvisor是否正常獲取該資料。

第一種curl  cAdvisor地址:

curl10.140.163.102:4194/api/v1.0/containers/docker/44d52b9acfc334233f685c81f130cd44538602980a8bff16495d1067bf3dfe5d

確認是否存在

"custom_metrics":[{

                           "name": "qps",

                           "type": "gauge",

                           "format": "float",

                           "units": ""

                  },

                  {

                           "name": "testmetric",

                           "type": "gauge",

                           "format": "float",

                           "units": ""

                  }],

                  還有是否存在

                  "custom_metrics": {

                           "qps": [{

                                    "label": "__name__=qps",

                                    "timestamp": "2017-06-02T15:43:54.146+08:00"

                           }],

                           "testmetric": [{

                                    "label": "__name__=testmetric",

                                    "timestamp": "2017-06-02T15:43:54.146+08:00",

                                    "float_value": 4.7396062151528895

                           }]

                  }

第二種方法訪問cAdvisor GUI:

 

然後確認heapster是否正常採集到資料