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

Kuberntes/Prometheus - Unable to Annotate in service file

我的 Kubernetes 版本是:

# kubectl --version
Kubernetes v1.4.0

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

我目前的指标 URL 就像:

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 服务和相应的 pods 但是这些配置似乎没有生效。

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"
        }

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

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

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'