這個是非常方便可以HPA自動橫向縮、放的功能

因為我弄了自動,所以需要加載k3s 權限

先加上

1 prometheus-rbac.yaml

apiVersion: v1
kind: ServiceAccount
metadata:
  name: prometheus-sa
  namespace: default
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: prometheus-cluster-role
rules:
- apiGroups: [""]
  resources: ["nodes", "nodes/metrics", "nodes/proxy"]
  verbs: ["get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: prometheus-cluster-role-binding
subjects:
- kind: ServiceAccount
  name: prometheus-sa
  namespace: default
roleRef:
  kind: ClusterRole
  name: prometheus-cluster-role
  apiGroup: rbac.authorization.k8s.io

2 prometheus-deploy.yaml

apiVersion: v1
kind: ServiceAccount
metadata:
  name: prometheus-sa
  namespace: default
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: prometheus-cluster-role
rules:
- apiGroups: [""]
  resources: ["nodes", "nodes/metrics", "nodes/proxy"]
  verbs: ["get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: prometheus-cluster-role-binding
subjects:
- kind: ServiceAccount
  name: prometheus-sa
  namespace: default
roleRef:
  kind: ClusterRole
  name: prometheus-cluster-role
  apiGroup: rbac.authorization.k8s.io

3 keda-app-scaler.yaml

apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
  name: app-keda-scaler
  namespace: default
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: app                     # 精準對齊你名為 app 的 Deployment
  minReplicaCount: 4               # 離峰保底 4 個 Pod (對稱分配 2+2)
  maxReplicaCount: 12              # 尖峰最多 12 個 Pod (對稱分配 6+6)
  pollingInterval: 15              # 每 15 秒去問一次 Prometheus 流量
  cooldownPeriod: 120              # 流量退去後,等 2 分鐘再縮減,防 Pod 頻繁起降
  
  triggers:
  # ─── 監控 Worker 1 的 WAF 流量 ───
  - type: prometheus
    metadata:
      serverAddress: http://cluster.local
      metricName: caddy_http_requests_total_w1
      query: sum(rate(caddy_http_requests_total{waf_node="worker-1"}[1m])) # 請確保這名稱與你 K3s 的 Worker 1 節點名稱一致
      threshold: '60'              # 每個 Pod 超過 60 RPS 就啟動擴展
  
  # ─── 監控 Worker 2 的 WAF 流量 ───
  - type: prometheus
    metadata:
      serverAddress: http://cluster.local
      metricName: caddy_http_requests_total_w2
      query: sum(rate(caddy_http_requests_total{waf_node="worker-2"}[1m])) # 請確保這名稱與你 K3s 的 Worker 2 節點名稱一致
      threshold: '60'

之所以這麼麻煩是因為我的環境是沒有互通的,也是是k3s是沒有互相調度的,只鎖定入口後,流量就在該台worker運作,不會跨worker。

因為資源稀缺,所以刻意拿掉了KUBE-PROXY這一塊。

正常來說怖了keda,它是會自動調度資源的。所以這個是特例。

上面的抓取指標,我因為是用CADDY,BUILD的waf,所以我就是寫抓CADDY裡面的指標。它的功能非常多,並且和雲端環境高度適合,再自行挑選。

忘了最重要的

 

Joomla templates by a4joomla