Kuberntes / Prometheus - 无法在服务文件中注释

时间:2017-05-24 13:28:30

标签: annotations kubernetes prometheus

我的Kubernetes版本是:

# kubectl --version
Kubernetes v1.4.0

我打算用Prometheus来监控我的Kube集群。为此,我需要注释指标网址。

我当前的指标网址如下:

http://172.16.33.7:8080/metrics

但我希望它像:

http://172.16.33.7:8080/websocket/metrics

首先我尝试手动执行此操作::

kubectl annotate pods websocket-backend-controller-db83999c5b534b277b82badf6c152cb9m1 prometheus.io/path=/websocket/metrics
kubectl annotate pods websocket-backend-controller-db83999c5b534b277b82badf6c152cb9m1 prometheus.io/scrape='true'
kubectl annotate pods websocket-backend-controller-db83999c5b534b277b82badf6c152cb9m1 prometheus.io/port='8080' 

所有这些命令都完美无缺,我能够看到注释。

{
  "metadata": {
    "name": "websocket-backend-controller-v1krf",
    "generateName": "websocket-backend-controller-",
    "namespace": "default",
    "selfLink": "/api/v1/namespaces/default/pods/websocket-backend-controller-v1krf",
    "uid": "e323994b-4081-11e7-8bd0-0050569b6f44",
    "resourceVersion": "27534379",
    "creationTimestamp": "2017-05-24T13:07:06Z",
    "labels": {
      "name": "websocket-backend"
    },
    "annotations": {
      "kubernetes.io/created-by": "{\"kind\":\"SerializedReference\",\"apiVersion\":\"v1\",\"reference\":{\"kind\":\"ReplicationController\",\"namespace\":\"default\",\"name\":\"websocket-backend-controller\",\"uid\":\"e321f1a8-4081-11e7-8bd0-0050569b6f44\",\"apiVersion\":\"v1\",\"resourceVersion\":\"27531840\"}}\n",
      "prometheus.io/path": "/websocket/metrics",
      "prometheus.io/port": "8080",
      "prometheus.io/scrape": "true"
    }

但由于我希望此配置保持永久性,因此我在服务文件中设置了以下注释。

# cat websocket-service.yaml 
apiVersion: v1
kind: Service
metadata:
  name: websocket-service
  labels:
    baseApi: websocket
  annotations:
    prometheus.io/scrape: 'true'
    prometheus.io/path: /websocket/metrics
    prometheus.io/port: '8080'
spec:
  selector:
    name: websocket-backend
  ports:
    - port: 8080
      targetPort: 8080
      nodePort: 30800
      protocol: TCP
  type: NodePort
  clusterIP: 10.100.10.45

我重启了我的websocket服务和相应的pod,但这些配置似乎没有生效。

kubectl create -f websocket-service.yaml
kubectl create -f ../controllers/websocket-replication-controller.yaml

结果未显示已配置的注释。

{
      "metadata": {
        "name": "websocket-backend-controller-v1krf",
        "generateName": "websocket-backend-controller-",
        "namespace": "default",
        "selfLink": "/api/v1/namespaces/default/pods/websocket-backend-controller-v1krf",
        "uid": "e323994b-4081-11e7-8bd0-0050569b6f44",
        "resourceVersion": "27531879",
        "creationTimestamp": "2017-05-24T13:07:06Z",
        "labels": {
          "name": "websocket-backend"
        },
        "annotations": {
          "kubernetes.io/created-by": "{\"kind\":\"SerializedReference\",\"apiVersion\":\"v1\",\"reference\":{\"kind\":\"ReplicationController\",\"namespace\":\"default\",\"name\":\"websocket-backend-controller\",\"uid\":\"e321f1a8-4081-11e7-8bd0-0050569b6f44\",\"apiVersion\":\"v1\",\"resourceVersion\":\"27531840\"}}\n"
        }

我正在做的不是使用命令行,而是使用服务配置来设置配置,但它似乎不起作用。

1 个答案:

答案 0 :(得分:6)

如果您注释该服务,则不会对可能匹配的pod产生任何影响。您的pod由ReplicationController或ReplicaSet / Deployment管理。在这种情况下,注释这些资源以使注释到达pod。在部署示例中,您必须使用模板部分,例如:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  # Unique key of the Deployment instance
  name: deployment-example
spec:
  # 3 Pods should exist at all times.
  replicas: 3
  # Keep record of 2 revisions for rollback
  revisionHistoryLimit: 2
  template:
    metadata:
      annotations:
        prometheus.io/scrape: 'true'
        prometheus.io/path: /websocket/metrics
        prometheus.io/port: '8080'