自从 上次 介绍了 Prometheus 以后,就想到要在 k8s 中使用了,不过,在这以前,先介绍下 k8s 的监控。node
k8s 默认以及推荐的监控体系是它本身的一套东西:Heapster + cAdvisor + Influxdb + Grafana,具体能够看 这里 。git
包括 k8s 自身的 HPA (Horizontal Pod Autoscaler),默认从 Heapster 中获取数据进行自动伸缩。(顺便提一句,当你部署完 k8s 集群以后,若是从 Dashboard 中看不到监控数据,每每就是由于你没有部署 Heapster,或者网络层有问题, Dashboard 没法访问 Heapster。)github
那,这跟咱们介绍的 Prometheus 有什么关系?后端
首先,它们都是一套监控解决方案,而 k8s 没有把 Prometheus 做为默认监控,所以,若是你想直接使用 HPA,你仍是须要部署 Heapster。api
其次,kubelet 中的 cAdvisor 实际上是支持 Prometheus 做为存储的后端的,只是相对于 Prometheus 本身的 SD 解决方案来讲,太弱了点。网络
最后,k8s 1.6 以后,在 annotations 中配置 custom metrics 的方式已经被移除了,而根据
Prometheus 的监控数据来进行自动伸缩仍是颇有可操做性的。app
其实部署很简单,关键是配置,所以这里着重介绍下,如何配置。spa
首先,先来了解下,什么是 relabel_config。debug
就如字面意思而言,它的做用是 Prometheus 抓取 metrics 以前,就将对象相关的 labels 重写。下面是它几个重要的 label:对象
其次,上次提到,咱们能够用到 Service Discovery 这个功能,其中就包含 Kubernetes SD。
它包含四种角色:
因为篇幅所限,这里只是简单介绍下其中的 node 还有 pod 角色:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
- job_name: 'kubernetes-nodes' scheme: https tls_config: ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token kubernetes_sd_configs: - role: node relabel_configs: # 即从 __meta_kubernetes_node_label_<labelname> 这个配置中取出 labelname 以及 value - action: labelmap regex: __meta_kubernetes_node_label_(.+) # 配置 address 为 k8s api 的地址,相关的 ca 证书以及 token 在上面配置 - target_label: __address__ replacement: kubernetes.default.svc:443 # 取出全部的 node,而后设置 /api/v1/nodes/<node_name>/proxy/metrics 为 metrics path - source_labels: - __meta_kubernetes_node_name regex: (.+) target_label: __metrics_path__ replacement: /api/v1/nodes/${1}/proxy/metrics |
接下来的这个 pod 角色挺重要:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
- job_name: 'kubernetes-pods' kubernetes_sd_configs: - role: pod relabel_configs: - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape] action: keep regex: true - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path] action: replace target_label: __metrics_path__ regex: (.+) - source_labels: [__address__, __meta_kubernetes_pod_annotation_prometheus_io_port] action: replace regex: ([^:]+)(?::\d+)?;(\d+) replacement: $1:$2 target_label: __address__ - action: labelmap regex: __meta_kubernetes_pod_label_(.+) - source_labels: [__meta_kubernetes_namespace] action: replace target_label: kubernetes_namespace - source_labels: [__meta_kubernetes_pod_name] action: replace target_label: kubernetes_pod_name |
在定义了这个角色以后,你只要在你部署的应用 Pod 描述中,加入如下 annotations 就能让 Prometheus 自动发现此 Pod 并采集监控数据了:
1 2 3 |
annotations: prometheus.io/scrape: "true" prometheus.io/port: "<your app port>" |
其它详细配置请看 这里。
最后,部署 Prometheus,须要注意的是,咱们已经在 k8s 以外单独部署了一套,为了统一处理,在这里是打算做为中转的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
apiVersion: v1 kind: ConfigMap metadata: name: prometheus namespace: kube-system labels: app: prometheus data: prometheus.yml: |- # 省略,在这里定义你须要的配置 --- apiVersion: extensions/v1beta1 kind: Deployment metadata: name: prometheus namespace: kube-system spec: replicas: 1 template: metadata: labels: app: prometheus spec: containers: - name: prometheus image: prom/prometheus:latest args: - '-config.file=/prometheus-data/prometheus.yml' # 显然,这里没有用 `Stateful Sets`,存储时间不用太长 - '-storage.local.retention=48h0m0s' ports: - name: prometheus containerPort: 9090 volumeMounts: - name: data-volume mountPath: /prometheus-data volumes: - name: data-volume configMap: name: prometheus --- # 简单处理,直接使用 NodePort 暴露服务,你也可使用 Ingress apiVersion: v1 kind: Service metadata: name: prometheus namespace: kube-system spec: selector: app: prometheus ports: - name: prometheus protocol: TCP port: 9090 nodePort: 30090 type: NodePort |
而在咱们外部单独的 Prometheus 中,须要配置 Federate,将 k8s 中 Prometheus 采集的 metrics 所有同步出来。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
- job_name: 'federate' scrape_interval: 15s honor_labels: true metrics_path: '/federate' params: 'match[]': - '{job=~".+"}' # 取 k8s 里面部署的 Prometheus 中全部的 job 数据 static_configs: - targets: - '<k8s-node1>:30090' - '<k8s-node2>:30090' - '<k8s-node3>:30090' |