1

我想根据自定义指标(在这种情况下为 RPS 或活动连接)扩展我的应用程序。无需设置 prometheus 或使用任何外部服务。我可以从我的网络应用程序中公开这个 API。我有哪些选择?

4

1 回答 1

0

在大多数 Kubernetes 集群上监控不同类型的指标(例如自定义指标)是导致更稳定和可靠的系统/应用程序/工作负载的基础。如评论部分所述,要监控自定义指标,建议使用为此目的设计的工具,而不是发明解决方法。我很高兴在这种情况下最终决定是使用PrometheusKEDA来正确扩展 Web 应用程序。

我想简要地向其他正在为类似考虑而苦苦挣扎的社区成员展示 KEDA 是如何工作的。


要使用 Prometheus 作为 Keda 的缩放器,我们需要安装和配置 Prometheus。安装 Prometheus 的方法有很多种,您应该选择适合您需要的一种。

我已经使用 Helm安装了kube-prometheus 堆栈:
注意:我允许 Prometheus 发现其命名空间内的所有PodMonitors/ ,而无需通过将和值设置为来ServiceMonitors应用标签过滤。prometheus.prometheusSpec.podMonitorSelectorNilUsesHelmValuesprometheus.prometheusSpec.serviceMonitorSelectorNilUsesHelmValuesfalse

$ helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
$ helm repo update
$ helm install prom-1 prometheus-community/kube-prometheus-stack --set prometheus.prometheusSpec.podMonitorSelectorNilUsesHelmValues=false --set prometheus.prometheusSpec.serviceMonitorSelectorNilUsesHelmValues=false

$ kubectl get pods
NAME                                                     READY   STATUS    RESTARTS   AGE
alertmanager-prom-1-kube-prometheus-sta-alertmanager-0   2/2     Running   0          2m29s
prom-1-grafana-865d4c8876-8zdhm                          3/3     Running   0          2m34s
prom-1-kube-prometheus-sta-operator-6b5d5d8df5-scdjb     1/1     Running   0          2m34s
prom-1-kube-state-metrics-74b4bb7857-grbw9               1/1     Running   0          2m34s
prom-1-prometheus-node-exporter-2v2s6                    1/1     Running   0          2m34s
prom-1-prometheus-node-exporter-4vc9k                    1/1     Running   0          2m34s
prom-1-prometheus-node-exporter-7jchl                    1/1     Running   0          2m35s
prometheus-prom-1-kube-prometheus-sta-prometheus-0       2/2     Running   0          2m28s

然后我们可以部署一个将被 Prometheus 监控的应用程序。我创建了一个简单的应用程序,它在路径上公开了一些指标(例如nginx_vts_server_requests_total) :/status/format/prometheus

$ cat app-1.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: app-1
spec:
  selector:
    matchLabels:
      app: app-1
  template:
    metadata:
      labels:
        app: app-1
    spec:
      containers:
      - name: app-1
        image: mattjcontainerregistry/nginx-vts:v1.0
        resources:
          limits:
            cpu: 50m
          requests:
            cpu: 50m
        ports:
        - containerPort: 80
          name: http
---
apiVersion: v1
kind: Service
metadata:
  name: app-1
  labels:
    app: app-1
spec:
  ports:
  - port: 80
    targetPort: 80
    name: http
  selector:
    app: app-1
  type: LoadBalancer

接下来,创建一个ServiceMonitor来描述如何监控我们的app-1应用程序:

$ cat servicemonitor.yaml
kind: ServiceMonitor
apiVersion: monitoring.coreos.com/v1
metadata:
  name: app-1
  labels:
    app: app-1
spec:
  selector:
    matchLabels:
      app: app-1
  endpoints:
  - interval: 15s
    path: "/status/format/prometheus"
    port: http

等待一段时间后,让我们检查app-1日志以确保它被正确报废:

$ kubectl get pods | grep app-1
app-1-5986d56f7f-2plj5                                   1/1     Running   0          35s

$ kubectl logs -f app-1-5986d56f7f-2plj5
10.44.1.6 - - [07/Feb/2022:16:31:11 +0000] "GET /status/format/prometheus HTTP/1.1" 200 2742 "-" "Prometheus/2.33.1" "-"
10.44.1.6 - - [07/Feb/2022:16:31:26 +0000] "GET /status/format/prometheus HTTP/1.1" 200 3762 "-" "Prometheus/2.33.1" "-"
10.44.1.6 - - [07/Feb/2022:16:31:41 +0000] "GET /status/format/prometheus HTTP/1.1" 200 3762 "-" "Prometheus/2.33.1" "-"

现在是部署 KEDA 的时候了。如KEDA 文档中所述,有几种部署 KEDA 运行时的方法。我选择使用 Helm 安装 KEDA,因为它非常简单 :-)

$ helm repo add kedacore https://kedacore.github.io/charts
$ helm repo update
$ kubectl create namespace keda
$ helm install keda kedacore/keda --namespace keda

我们需要创建的最后一件事ScaledObject是用于定义 KEDA 应该如何扩展我们的应用程序以及触发器是什么。在下面的示例中,我使用了nginx_vts_server_requests_total指标。
注意:有关 prometheus 触发器的更多信息,请参阅触发器规范文档。

$ cat scaled-object.yaml
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
  name: scaled-app-1
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: app-1
  pollingInterval: 30                               
  cooldownPeriod:  120                              
  minReplicaCount: 1                                
  maxReplicaCount: 5                               
  advanced:                                         
    restoreToOriginalReplicaCount: false            
    horizontalPodAutoscalerConfig:                  
      behavior:                                     
        scaleDown:
          stabilizationWindowSeconds: 300
          policies:
          - type: Percent
            value: 100
            periodSeconds: 15
  triggers:
  - type: prometheus
    metadata:
      serverAddress: http://prom-1-kube-prometheus-sta-prometheus.default.svc:9090
      metricName: nginx_vts_server_requests_total
      query: sum(rate(nginx_vts_server_requests_total{code="2xx", service="app-1"}[2m])) # Note: query must return a vector/scalar single element response
      threshold: '10'
  
$ kubectl apply -f scaled-object.yaml
scaledobject.keda.sh/scaled-app-1 created

最后,我们可以app-1根据请求的数量检查应用程序是否正确扩展:

$ for a in $(seq 1 10000); do curl <PUBLIC_IP_APP_1> 1>/dev/null 2>&1; done

$ kubectl get hpa -w
NAME                    REFERENCE          TARGETS          MINPODS   MAXPODS   REPLICAS   
keda-hpa-scaled-app-1   Deployment/app-1   0/10 (avg)        1         5         1           
keda-hpa-scaled-app-1   Deployment/app-1   15/10 (avg)       1         5         2         
keda-hpa-scaled-app-1   Deployment/app-1   12334m/10 (avg)   1         5         3       
keda-hpa-scaled-app-1   Deployment/app-1   13250m/10 (avg)   1         5         4      
keda-hpa-scaled-app-1   Deployment/app-1   12600m/10 (avg)   1         5         5          

$ kubectl get pods | grep app-1
app-1-5986d56f7f-2plj5                                   1/1     Running   0          36m
app-1-5986d56f7f-5nrqd                                   1/1     Running   0          77s
app-1-5986d56f7f-78jw8                                   1/1     Running   0          94s
app-1-5986d56f7f-bl859                                   1/1     Running   0          62s
app-1-5986d56f7f-xlfp6                                   1/1     Running   0          45s

正如您在上面看到的,我们的应用程序已正确缩放到 5 个副本。

于 2022-02-07T17:35:44.663 回答