# RKE2 Ingress nginx annotations 設定 ## 設定 rke2-ingress-nginx-controller helm chart config * rke2 預設會用 helm 幫我們裝 ingress-nginx,因此修改時需要透過 ingress-nginx 的 helm chart config 做修改 * 在 rke2 master 新增以下設定檔 ``` $ nano /var/lib/rancher/rke2/server/manifests/rke2-ingress-nginx-config.yaml apiVersion: helm.cattle.io/v1 kind: HelmChartConfig metadata: name: rke2-ingress-nginx namespace: kube-system spec: valuesContent: |- controller: enableAnnotationValidations: "true" ``` * 更新完後在 ingress DaemonSets 會看到 `--enable-annotation-validation=true` ![image](https://hackmd.io/_uploads/B1NvGc-vC.png) ## annotations * 這些 annotations 定義了連接和傳輸速率的限制。這些可用於減輕 DDoS 攻擊 * `nginx.ingress.kubernetes.io/limit-connections`:單一 IP 位址允許的同時連線數。超過此限制會傳回 503 錯誤。 * `nginx.ingress.kubernetes.io/limit-rps`:每秒從指定 IP 接受的請求數。超過此限制會傳回 503 錯誤。 * `nginx.ingress.kubernetes.io/limit-rpm`:每分鐘從給定 IP 接受的請求數。突發限制設定為此限制乘以突發乘數,預設值為 5。當客戶端超出此限制時,超過此限制會傳回 503 錯誤。 * `nginx.ingress.kubernetes.io/limit-burst-multiplier`:用於限制從特定 IP 位址到 Ingress 資源的突發連線數。預設值為 5,此 annotations 會覆寫預設的 multiplier。超過此限制會傳回 503 錯誤。 * `nginx.ingress.kubernetes.io/limit-rate-after`:用於限制從特定 IP 位址到 Ingress 資源的連線速率。此功能必須在啟用 proxy-buffering 的情況下使用。 * `nginx.ingress.kubernetes.io/limit-rate`:限制從特定 IP 位址到Ingress 資源的請求速率。此功能必須在啟用 proxy-buffering 的情況下使用。 * `nginx.ingress.kubernetes.io/limit-whitelist`:用於將特定 IP 位址排除在 Ingress 資源的速率限制之外,如果一個 IP 位址位於白名單中,則該 IP 位址將不受 Ingress 資源的速率限制的影響。該值是逗號分隔的 CIDR 清單。 ## 測試單一 IP 位址允許的同時連線數 * 這裡限制 1 代表最高同時只能有一個連線 ``` apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: test-nginx-ingress annotations: nginx.ingress.kubernetes.io/limit-connections: "1" spec: ingressClassName: nginx rules: - host: test.cooloo9871.com http: paths: - path: / pathType: Prefix backend: service: name: svc-web port: number: 80 ``` * 在同一台 client 上同時執行兩次以下 script ``` $ vim ingtest.sh #!/bin/bash while true do curl -s http://test.cooloo9871.com | grep 503 >> check.txt done ``` * 執行 script ``` $ bash ingtest.sh ``` * 同時在同個 client 上多次執行 script 可以發現有 503 的錯誤訊息 ``` $ cat check.txt <center><h1>503 Service Temporarily Unavailable</h1></center> <head><title>503 Service Temporarily Unavailable</title></head> <center><h1>503 Service Temporarily Unavailable</h1></center> <head><title>503 Service Temporarily Unavailable</title></head> <center><h1>503 Service Temporarily Unavailable</h1></center> ......... ``` ### 參考連結 https://github.com/kubernetes/ingress-nginx/blob/main/docs/user-guide/nginx-configuration/annotations.md https://docs.rke2.io/networking/networking_services?_highlight=ingress&_highlight=nginx#nginx-ingress-controller https://github.com/kubernetes/ingress-nginx/tree/main/charts/ingress-nginx#configuration https://github.com/kubernetes/ingress-nginx/blob/main/docs/user-guide/nginx-configuration/annotations.md