• OpenShift 4 - 用 KEDA 实现基于定制指标的弹性部署扩展


    OpenShift 4.x HOL教程汇总
    本文在 OpenShift 4.13 环境中进行验证。

    什么是定制 Autoscaler 指标

    在 OpenShift 中自带 VPA(垂直自动扩展) 和 HPA (水平自动扩展)功能,可通过动态更改 Pod 的资源分配数量或更改 Pod 的数量来满足应用的弹性负载变化。不过 HPA 和 VPA 主要是以 Pod 消耗的 CPU 和内存量判断是否需要对 Pod 进行扩展,因此使用上有一定局限。而 OpenShift 的 Custom Metrics Autoscaler 提供了可完全定制的扩展指标和扩展架构,能够实现更加灵活的容器弹性运行。

    OpenShift 的 Custom Metrics Autoscaler 是基于 CNCF 沙箱项目 KEDA 实现的。它本质上是基于 HPA 进行扩展的,但可以结合定制的运行指标判断是否需要扩展。

    Custom Metrics Autoscaler 是通过 Operator 安装运行的。在运行前,我们需要把扩展的对象(主要是 Deployment 对象)、定制的指标、以及何时触发扩展等保存到 ScaledObject 中。在运行时,监控体系要能定期获得这些定制指标的运行情况,在 OpenShift 中我们可以使用 Prometheus 实现应用监控。然后 Custom Metrics Autoscaler 会根据获得定制指标的运行情况来判断是否需要对 Deployment 进行扩展。
    在这里插入图片描述

    实现基于定制指标的应用弹性扩展

    安装配置 Custom Metrics Autoscaler

    1. 在 OpenShift 的 OperatorHub 中找到 Custom Metrics Autoscaler,然后使用缺省配置安装它。默认会安装将其安装到 openshift-keda 项目中。
    2. 进入安装好的 Custom Metrics Autoscaler,然后在 openshift-keda 项目中使用缺省配置创建一个 KedaController 实例。
    apiVersion: keda.sh/v1alpha1
    kind: KedaController
    metadata:
      name: keda
      namespace: openshift-keda
    spec:
      operator:
        logLevel: info
        logEncoder: console
      metricsServer:
        logLevel: '0'
      serviceAccount: {}
      watchNamespace: ''
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    1. 完成后可查看运行的资源。
    $ oc get deployment -n openshift-keda
    NAME                                 READY   UP-TO-DATE   AVAILABLE   AGE
    custom-metrics-autoscaler-operator   1/1     1            1           5m
    keda-metrics-apiserver               1/1     1            1           3m
    keda-operator                        1/1     1            1           3m
    
    • 1
    • 2
    • 3
    • 4
    • 5

    配置可基于定制指标弹性扩展的应用

    部署应用

    1. 为了能使用到基于 Prometheus 的 OpenShift Monitoring 环境监视应用的定制指标,我们需要在 OpenShift 中运行以下 YAML,设置允许 OpenShift Monitoring 监视用户命名空间的资源。
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: cluster-monitoring-config
      namespace: openshift-monitoring
    data:
      config.yaml: |
            enableUserWorkload: true
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    1. 确认在 prometheus-user-workload 中创建了 prometheus-user-workload 等 Pod。
    $ oc get pod -n openshift-user-workload-monitoring
    NAME                                   READY   STATUS    RESTARTS   AGE
    prometheus-operator-79dc5458f7-vz98q   2/2     Running   0          102s
    prometheus-user-workload-0             6/6     Running   0          99s
    thanos-ruler-user-workload-0           3/3     Running   0          95s
    
    • 1
    • 2
    • 3
    • 4
    • 5
    1. 在 OpenShift 中创建一个测试项目 test。
    2. 在测试项目中执行以下 YAML,创建测试应用相关资源。测试应用通过接口会向 Prometheus 提供其运行指标。
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      labels:
        app: test-app
      name: test-app
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: test-app
      template:
        metadata:
          labels:
            app: test-app
            type: keda-testing
        spec:
          containers:
          - name: prom-test-app
            image: quay.io/zroubalik/prometheus-app:latest
            imagePullPolicy: IfNotPresent
    ---
    apiVersion: v1
    kind: Service
    metadata:
      labels:
        app: test-app
      annotations:
        prometheus.io/scrape: "true"
      name: test-app
    spec:
      ports:
      - name: http
        port: 80
        protocol: TCP
        targetPort: 8080
      selector:
        type: keda-testing
    ---
    apiVersion: monitoring.coreos.com/v1
    kind: ServiceMonitor
    metadata:
      labels:
      name: keda-testing-sm
    spec:
      endpoints:
      - scheme: http
        port: http
      namespaceSelector: {}
      selector:
        matchLabels:
           app: test-app
    
    • 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
    1. 查看测试应用部署情况。
    $ oc get deploy test-app
    NAME       READY   UP-TO-DATE   AVAILABLE   AGE
    test-app   1/1     1            1           94s
    
    • 1
    • 2
    • 3

    创建从 Thanos 获取应用定制指标的 Service Account 和 Role

    1. 在部署测试应用的项目下执行以下命令,创建一个 ServiceAccount。
    $ oc create serviceaccount thanos 
    
    • 1
    1. 执行命令查看 ServiceAccount 相关 token。下面将使用其中的 thanos-token-gjprx 作为访问凭证。
    $ oc describe serviceaccount thanos
    Name:                thanos
    Namespace:           test
    Labels:              <none>
    Annotations:         <none>
    Image pull secrets:  thanos-dockercfg-zbh7g
    Mountable secrets:   thanos-token-nmqpv
                         thanos-dockercfg-zbh7g
    Tokens:              thanos-token-gjprx
                         thanos-token-nmqpv
    Events:              <none>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    1. 执行以下 YAML 创建 TriggerAuthentication,其中的 bearerToken 和 ca 都使用了在 ServiceAccount 中的 thanos-token-gjprx 作为参数。
    apiVersion: keda.sh/v1alpha1
    kind: TriggerAuthentication
    metadata:
      name: keda-trigger-auth-prometheus
    spec:
      secretTargetRef:
      - parameter: bearerToken
        name: thanos-token-gjprx       # update this
        key: token
      - parameter: ca
        name: thanos-token-gjprx       # update this
        key: ca.crt
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    1. 执行以下 YAML 创建 Role,用它来访问 Thanos 以查询到应用指标。
    apiVersion: rbac.authorization.k8s.io/v1
    kind: Role
    metadata:
      name: thanos-metrics-reader
    rules:
    - apiGroups:
      - ""
      resources:
      - pods
      verbs:
      - get
    - apiGroups:
      - metrics.k8s.io
      resources:
      - pods
      - nodes
      verbs:
      - get
      - list
      - watch
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    1. 执行命令,将 Role 赋给 ServiceAccount。
    $ oc adm policy add-role-to-user thanos-metrics-reader -z thanos --role-namespace=test
    
    • 1

    为扩展部署创建 ScaledObject

    1. 执行以下 YAML 创建ScaledObject。它关联了 Deployment 对象,并说明了扩展上下限、查询的定制指标的 Thanos 访问地址和查询轮训时间等内容。
    apiVersion: keda.sh/v1alpha1
    kind: ScaledObject
    metadata:
      name: prometheus-scaledobject
    spec:
      scaleTargetRef:
        name: test-app
      minReplicaCount: 1
      maxReplicaCount: 10
      pollingInterval: 5
      cooldownPeriod:  10
      triggers:
      - type: prometheus
        metadata:
          serverAddress: https://thanos-querier.openshift-monitoring.svc.cluster.local:9092
          namespace: test                         # replace 
          metricName: http_requests_total
          threshold: '5'
          query: sum(rate(http_requests_total{job="test-app"}[1m]))
          authModes: "bearer"
        authenticationRef:
          name: keda-trigger-auth-prometheus
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    1. 查看 ScaledObject 对象的状态。
    $ oc get scaledobject prometheus-scaledobject -n test
    NAME                      SCALETARGETKIND      SCALETARGETNAME   MIN   MAX   TRIGGERS     AUTHENTICATION                 READY   ACTIVE   FALLBACK   AGE
    prometheus-scaledobject   apps/v1.Deployment   test-app          1     10    prometheus   keda-trigger-auth-prometheus   True    False    False      9m
    
    • 1
    • 2
    • 3

    测试验证

    1. 执行以下 YAML,运行一个 Job 向运行应用进行并发压力访问。
    apiVersion: batch/v1
    kind: Job
    metadata:
      generateName: generate-requests-
    spec:
      template:
        spec:
          containers:
          - image: quay.io/zroubalik/hey
            name: test
            command: ["/bin/sh"]
            args: ["-c", "for i in $(seq 1 30);do echo $i;/hey -c 5 -n 100 http://test-app.test.svc;sleep 1;done"]   # replace 
          restartPolicy: Never
      activeDeadlineSeconds: 120
      backoffLimit: 2
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    1. 在 OpenShift 控制台监控 test-app 部署对应的 Pod 数量,或执行以下命令查看 Pod 数量。确认数量会从 1 增加,最后会降到 1。
     $ watch oc get deployment test-app
    
    • 1
    1. 在 test 项目中进入“观察”菜单,然后进入“指标”栏。在选择“自定义查询”后输入 “sum(rate(http_requests_total{job=“test-app”}[1m]))” ,可以查询出 ScaledObject 定义的扩展指标值。
      在这里插入图片描述

    参考

    https://cloud.redhat.com/blog/custom-metrics-autoscaler-on-openshift
    https://rhthsa.github.io/openshift-demo/KEDA.html
    https://www.opensourcerers.org/2022/02/14/enabling-monitoring-scaling-alerting/

  • 相关阅读:
    【第48篇】MaxViT:多轴视觉转换器
    接口压力测试 jmeter--进阶篇(三)
    2023最新SSM计算机毕业设计选题大全(附源码+LW)之java宠物领养平台2x520
    网络空间安全web向基础知识
    基于Java的长整数加减法算法设计
    shiro入门基础
    Java 进阶集合和数据结构
    c语言进阶部分详解(指针初阶)
    C语言--每日五道练习题--Day16
    RIP路由信息协议
  • 原文地址:https://blog.csdn.net/weixin_43902588/article/details/126701740