如何使用Istio的Prometheus配置kubernetes hpa?

2024-01-07

我们有一个 Istio 集群,我们正在尝试为 Kubernetes 配置水平 pod 自动缩放。我们希望使用请求计数作为 hpa 的自定义指标。我们如何利用 Istio 的 Prometheus 来达到同样的目的?


这个问题比我想象的要复杂得多,但我终于找到了答案。

  1. 首先,您需要配置应用程序以提供自定义指标。它位于开发应用程序方面。下面是一个例子,如何用Go语言制作:使用 Prometheus 查看指标 https://mycodesmells.com/post/watching-metrics-with-prometheus

  2. 其次,您需要定义应用程序(或 Pod,或任何您想要的)的 Deployment 并将其部署到 Kubernetes,例如:

    apiVersion: extensions/v1beta1
    kind: Deployment
    metadata:
      name: podinfo
    spec:
      replicas: 2
      template:
        metadata:
          labels:
            app: podinfo
          annotations:
            prometheus.io/scrape: 'true'
        spec:
          containers:
          - name: podinfod
            image: stefanprodan/podinfo:0.0.1
            imagePullPolicy: Always
            command:
              - ./podinfo
              - -port=9898
              - -logtostderr=true
              - -v=2
            volumeMounts:
              - name: metadata
                mountPath: /etc/podinfod/metadata
                readOnly: true
            ports:
            - containerPort: 9898
              protocol: TCP
            readinessProbe:
              httpGet:
                path: /readyz
                port: 9898
              initialDelaySeconds: 1
              periodSeconds: 2
              failureThreshold: 1
            livenessProbe:
              httpGet:
                path: /healthz
                port: 9898
              initialDelaySeconds: 1
              periodSeconds: 3
              failureThreshold: 2
            resources:
              requests:
                memory: "32Mi"
                cpu: "1m"
              limits:
                memory: "256Mi"
                cpu: "100m"
          volumes:
            - name: metadata
              downwardAPI:
                items:
                  - path: "labels"
                    fieldRef:
                      fieldPath: metadata.labels
                  - path: "annotations"
                    fieldRef:
                      fieldPath: metadata.annotations
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: podinfo
      labels:
        app: podinfo
    spec:
      type: NodePort
      ports:
        - port: 9898
          targetPort: 9898
          nodePort: 31198
          protocol: TCP
      selector:
        app: podinfo
    

    关注领域annotations: prometheus.io/scrape: 'true'。需要请求Prometheus从资源中读取metrics。另请注意,还有两个注释,它们具有默认值;但如果您在应用程序中更改它们,则需要使用正确的值添加它们:

    • prometheus.io/path:如果metrics路径不是/metrics,则用该注解定义。
    • prometheus.io/port:在指定端口而不是 pod 声明的端口上抓取 pod(如果没有声明,则默认为无端口目标)。
  3. 接下来,Istio 中的 Prometheus 使用自己针对 Istio 目的进行修改的配置,并且默认情况下它会跳过 Pod 中的自定义指标。因此,您需要对其进行一些修改。 就我而言,我从以下位置获取了 Pod 指标的配置这个例子 https://github.com/stefanprodan/k8s-prom-hpa/blob/master/prometheus/prometheus-cfg.yaml并仅针对 Pod 修改了 Istio 的 Prometheus 配置:

    kubectl edit configmap -n istio-system prometheus
    

    我根据前面提到的示例更改了标签的顺序:

    # pod's declared ports (default is a port-free target if none are declared).
    - job_name: 'kubernetes-pods'
      # if you want to use metrics on jobs, set the below field to
      # true to prevent Prometheus from setting the `job` label
      # automatically.
      honor_labels: false
      kubernetes_sd_configs:
      - role: pod
      # skip verification so you can do HTTPS to pods
      tls_config:
        insecure_skip_verify: true
      # make sure your labels are in order
      relabel_configs:
      # these labels tell Prometheus to automatically attach source
      # pod and namespace information to each collected sample, so
      # that they'll be exposed in the custom metrics API automatically.
      - source_labels: [__meta_kubernetes_namespace]
        action: replace
        target_label: namespace
      - source_labels: [__meta_kubernetes_pod_name]
        action: replace
        target_label: pod
      # these labels tell Prometheus to look for
      # prometheus.io/{scrape,path,port} annotations to configure
      # how to scrape
      - 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__
      - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scheme]
        action: replace
        target_label: __scheme__
    

    之后,自定义指标出现在 Prometheus 中。但,更改 Prometheus 配置时要小心,因为 Istio 所需的一些指标可能会消失,所以请仔细检查所有内容。

  4. 现在是时候安装了Prometheus 自定义公制适配器 https://github.com/directxman12/k8s-prometheus-adapter.

    • 下载this https://github.com/directxman12/k8s-prometheus-adapter存储库
    • 更改文件中 Prometheus 服务器的地址<repository-directory>/deploy/manifests/custom-metrics-apiserver-deployment.yaml。例子,- --prometheus-url=http://prometheus.istio-system:9090/
    • 运行命令kubectl apply -f <repository-directory>/deploy/manifests一段时间后,custom.metrics.k8s.io/v1beta1应出现在命令“kubectl api-versions”的输出中。

    另外,使用命令检查自定义 API 的输出kubectl get --raw "/apis/custom.metrics.k8s.io/v1beta1" | jq . and kubectl get --raw "/apis/custom.metrics.k8s.io/v1beta1/namespaces/default/pods/*/http_requests" | jq .最后一个的输出应如下例所示:

    {
      "kind": "MetricValueList",
      "apiVersion": "custom.metrics.k8s.io/v1beta1",
      "metadata": {
        "selfLink": "/apis/custom.metrics.k8s.io/v1beta1/namespaces/default/pods/%2A/http_requests"
      },
      "items": [
        {
          "describedObject": {
            "kind": "Pod",
            "namespace": "default",
            "name": "podinfo-6b86c8ccc9-kv5g9",
            "apiVersion": "/__internal"
              },
              "metricName": "http_requests",
              "timestamp": "2018-01-10T16:49:07Z",
              "value": "901m"    },
            {
          "describedObject": {
            "kind": "Pod",
            "namespace": "default",
            "name": "podinfo-6b86c8ccc9-nm7bl",
            "apiVersion": "/__internal"
          },
          "metricName": "http_requests",
          "timestamp": "2018-01-10T16:49:07Z",
          "value": "898m"
        }
      ]
    }
    

    如果是这样,您可以继续下一步。如果没有,请在 CustomMetrics 中查看哪些 API 可用于 Podkubectl get --raw "/apis/custom.metrics.k8s.io/v1beta1" | jq . | grep "pods/"对于 http_requestskubectl get --raw "/apis/custom.metrics.k8s.io/v1beta1" | jq . | grep "http"。 MetricNames 是根据 Prometheus 从 Pod 收集的指标生成的,如果它们为空,则需要朝该方向查看。

  5. 最后一步是配置 HPA 并对其进行测试。因此,就我而言,我为 podinfo 应用程序创建了 HPA,定义如下:

    apiVersion: autoscaling/v2beta1
    kind: HorizontalPodAutoscaler
    metadata:
      name: podinfo
    spec:
      scaleTargetRef:
        apiVersion: extensions/v1beta1
        kind: Deployment
        name: podinfo
      minReplicas: 2
      maxReplicas: 10
      metrics:
      - type: Pods
        pods:
          metricName: http_requests
          targetAverageValue: 10
    

    并使用简单的 Go 应用程序来测试负载:

    #install hey
    go get -u github.com/rakyll/hey
    #do 10K requests rate limited at 25 QPS
    hey -n 10000 -q 5 -c 5 http://<K8S-IP>:31198/healthz
    

    一段时间后,我看到使用命令的缩放发生了变化kubectl describe hpa and kubectl get hpa

我使用了文章中有关创建自定义指标的说明使用 Kubernetes Horizo​​ntal Pod Autoscaler 和 Prometheus 确保高可用性和正常运行时间 https://dzone.com/articles/ensure-high-availability-and-uptime-with-kubernete

所有有用的链接都集中在一处:

  • 使用 Prometheus 查看指标 https://mycodesmells.com/post/watching-metrics-with-prometheus- 将指标添加到应用程序的示例
  • k8s-prom-hpa https://github.com/stefanprodan/k8s-prom-hpa- 为Prometheus创建自定义指标的示例(与上面的文章相同)
  • Prometheus 的 Kubernetes 自定义指标适配器 https://github.com/directxman12/k8s-prometheus-adapter
  • 设置自定义指标适配器和示例应用程序 https://github.com/luxas/kubeadm-workshop#deploying-a-custom-metrics-api-server-and-a-sample-app
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用Istio的Prometheus配置kubernetes hpa? 的相关文章

随机推荐

  • 如何使用 NEST 客户端将 Elasticsearch 配置为使用 AutoMap 类型作为动态映射对象的默认模板?

    我动态地将对象添加到索引中 因此它们都使用 default 映射进行索引 这是有问题的 因为它会导致诸如Guid被映射为text字段而不是作为keyword The AutoMap https www elastic co guide en
  • JavaScript 中的 360 度旋转视图 [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 我如何获得每个工作的詹金斯许可?

    有些工作推向质量检查 有些工作推向生产 只应允许开发人员推送到 QA 并且只应允许 QA 推送到生产 除了创建两个独立的构建服务器 通过某种方式共享工件 之外 我如何限制权限 你需要基于项目的矩阵授权策略在全局配置和作业配置中 使用启用基于
  • Flask SQLAlchemy 对关系中的对象进行分页

    所以我有两个模型 文章和标签 以及正确设置的 m2m 关系 我有一条 文章 标签 类型的路线 我想仅显示与该标签相关的那些文章 我已经解决了这个问题 但我正在寻找更优雅的解决方案 包括分页但我无法使用paginate在文章列表上 因为它不是
  • 条件构造在 Python 正则表达式中不起作用

    我是 python 的新手 我想在中使用我的正则表达式re sub 我试穿了regex101 https regex101 com r InljCc 1它有效 不知何故 当我尝试在我的 python 版本 3 6 上使用它时 它无法正常工作
  • “无法设置插件占位符”消息?

    Xcode 9 1 模拟器10 0 操作系统 10 12 6 我想将我的贴纸包的新版本上传到 iTunes Connect 但 产品 gt 存档 是灰色的 所以我运行模拟器并得到 无法为 名称 设置插件占位符 在全世界任何地方只有一个答案
  • 这两种关系对于联合运算是否兼容?

    我不确定以下两种关系是否适合联合 R lt schema name B 1 2 2 3 3 And Q lt schema name A B 5 1 6 1 4 2 3 4 我想加入工会 Q U R 可以吗 结果如何 并集运算符要求两个关系
  • svn:ignore 的双重性质

    阅读周围 svn ignore 命令似乎以两种方式之一工作 如果文件或目录尚未处于版本控制之下 则元数据将添加到存储库中 而忽略签出该项目的所有其他团队成员的文件或目录 如果文件或目录已处于版本控制之下 则忽略将仅在本地应用 并且不会将元数
  • 将数据框组合成列表

    我正在尝试将多个数据帧存储在列表中 但是 在某些时候 数据帧最终会转换为列表 因此我最终会得到一个列表列表 我真正想做的就是将所有数据帧以某种结构保存在一起 这是失败的代码 all dframes lt list initialise a
  • WinSCP 命令行被动模式

    我通过命令行调用 WinSCP 但我不知道如何为脚本正确设置被动模式 现在是脚本 option batch on option confirm off open ftp user and pass details 21 cd out opt
  • Spring/Hibernate 连接泄漏与 ScrollableResults

    我目前正在调查以下问题 该问题也影响我们的生产环境https github com querydsl querydsl issues 3089 https github com querydsl querydsl issues 3089 这
  • 递归多对多关系的相关名称不起作用

    多对多 非递归 class A models Model pass class B models Model parents models ManyToManyField A related name children gt gt gt A
  • 如何通过.apk传递dalvik命令行参数?

    我知道可以通过以下方式在Android中启动java程序 adb push hello world zip REMOTE PATH adb shell mkdir REMOTE PATH dalvik cache adb shell dal
  • 搜索方法迭代完成后重新加载/显示 UISearchDisplayController 的 searchResultsTableView

    我修改了Apple的示例iOS项目的代码表格搜索 http developer apple com library ios samplecode TableSearch Introduction Intro html以便通过解析 Web 服
  • 替换图像的颜色

    我试图用白色替换图片的黑色 反之亦然 这实际上是为了让我的 OCR 代码可以在白色背景上更好地读取它 当前正在从剪贴板获取图像 Image img Clipboard GetImage pictureBox1 SizeMode Pictur
  • 为什么这么多 wpf 控件实现 CLR 属性而不是依赖属性?

    是因为控件程序员太懒 太难实现还是知识不够 无论它们是来自第 3 方供应商还是 Microsoft 本身的自定义控件 很多控件通常具有 clr 属性而不是 DP 结果是我无法绑定到它们 wpf 不就是绑定吗 我的下一个问题是 为什么这么多
  • Razor视图引擎,html之间怎么写?

    我的剃刀语法有一些问题 我想知道如何在 html 之间编写 请参阅此示例 ul foreach var x in Model li x Subject Tags nbsp if x Tags null foreach var t in x
  • 如何在 iOS 6.1 上重现网站崩溃

    我收到了一份错误报告这个网站 http festivals ie 在 iOS 6 1 上查看时会崩溃 尽管我猜想 iOS 6 1 附带的浏览器版本比操作系统本身更相关 我没有任何 iOS 设备 因此我不确定如何重现 调查该问题 有人建议我如
  • 将字典转换为结构化格式字符串

    我有一个 Dictionary 对象声明为var as Dictionary of String String 我正在尝试利用通用集合可用的 LINQ 扩展 但我只获得非扩展方法 我需要将 Dictionary 集合转换为具有以下模式的字符
  • 如何使用Istio的Prometheus配置kubernetes hpa?

    我们有一个 Istio 集群 我们正在尝试为 Kubernetes 配置水平 pod 自动缩放 我们希望使用请求计数作为 hpa 的自定义指标 我们如何利用 Istio 的 Prometheus 来达到同样的目的 这个问题比我想象的要复杂得