【问题标题】:Kuberntes/Prometheus - Unable to Annotate in service fileKubernetes/Prometheus - 无法在服务文件中注释
【发布时间】:2017-05-24 13:28:30
【问题描述】:

我的 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 服务和相应的 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"
        }

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

【问题讨论】:

    标签: annotations kubernetes prometheus


    【解决方案1】:

    如果您对服务进行注释,它不会对可能匹配的 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'
    

    【讨论】:

    • 谢谢!!几天来,我一直在寻找如何将注释从部署传递到 pod!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-02
    相关资源
    最近更新 更多