自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

K8s Nginx Lngress 常用的 9個 配置(Annotation),你知道幾個?

云計算
如果一個k8s 集群里面部署多個ingress controller的時候,如果配置ingress 希望指定到某個ingress controller的時候,ingress claas就發(fā)揮巨大作用了。

上一篇文章介紹了 ingress vhost這個annotation的使用,趁熱打鐵我們一口氣介紹 9 個常用的annotation。

1、ingress class

如果一個k8s 集群里面部署多個ingress controller的時候,如果配置ingress 希望指定到某個ingress controller的時候,ingress claas就發(fā)揮巨大作用了。

一方面在controller啟動的時候需要通過參數(shù)指定ingress class

  1. --ingress-class=ngx-ds 

另一方面,在創(chuàng)建ingress的時候,通過annotation指定ingress class,如下所示

  1. apiVersion: extensions/v1beta1 
  2. kind: Ingress 
  3. metadata: 
  4.   name: other-ngx-k8s 
  5.   namespace: other-ngx 
  6.   annotations: 
  7.     kubernetes.io/ingress.class: "ngx-ds" 
  8. spec: 
  9.   rules: 
  10.   - host: other-ngx-k8s.demo.com.cn 
  11.     http: 
  12.       paths: 
  13.       - path: / 
  14.         backend: 
  15.           serviceName: other-ngx-k8s-ngx-svc 
  16.           servicePort: 9001 

2、 強制https

  1. apiVersion: networking.k8s.io/v1beta1 
  2. kind: Ingress 
  3. metadata: 
  4.   name: test-ingress 
  5.   annotations: 
  6.     nginx.ingress.kubernetes.io/rewrite-target: / 
  7.     nginx.ingress.kubernetes.io/force-ssl-redirect: "true" 
  8.     nginx.ingress.kubernetes.io/ssl-redirect: "true" 
  9.     nginx.ingress.kubernetes.io/preserve-trailing-slash: "true" 
  10. spec: 
  11.   rules: 
  12.   - http: 
  13.       paths: 
  14.       - path: /testpath 
  15.         backend: 
  16.           serviceName: test 
  17.           servicePort: 80 

3、請求超時

  1. apiVersion: networking.k8s.io/v1beta1 
  2. kind: Ingress 
  3. metadata: 
  4.   name: cafe-ingress-with-annotations 
  5.   annotations: 
  6.     nginx.org/proxy-connect-timeout: "30s" 
  7.     nginx.org/proxy-read-timeout: "20s" 
  8. spec: 
  9.   rules: 
  10.   - host: cafe.example.com 
  11.     http: 
  12.       paths: 
  13.       - path: /tea 
  14.         backend: 
  15.           serviceName: tea-svc 
  16.           servicePort: 80 
  17.       - path: /coffee 
  18.         backend: 
  19.           serviceName: coffee-svc 
  20.           servicePort: 80 

4、跨域訪問

我們經(jīng)常將nginx作為api的網(wǎng)關(guān),支持跨域必不可少。通過

  1. apiVersion: networking.k8s.io/v1beta1 
  2. kind: Ingress 
  3. metadata: 
  4.   name: test-ingress 
  5.   annotations: 
  6.     nginx.ingress.kubernetes.io/enable-cors: "true" 
  7.     nginx.ingress.kubernetes.io/cors-allow-methods: "PUT, GET, POST, OPTIONS" 
  8.     nginx.ingress.kubernetes.io/cors-allow-headers: "X-Forwarded-For, X-app123-XPTO" 
  9.     nginx.ingress.kubernetes.io/cors-expose-headers: "*, X-CustomResponseHeader" 
  10.     nginx.ingress.kubernetes.io/cors-max-age: 600 
  11.     nginx.ingress.kubernetes.io/cors-allow-credentials: "false" 
  12. spec: 
  13.   rules: 
  14.   - http: 
  15.       paths: 
  16.       - path: /testpath 
  17.         backend: 
  18.           serviceName: test 
  19.           servicePort: 80 

5、限流

限流也經(jīng)常使用,通過 rps 限制每秒請求數(shù),rpm 限制每分鐘請求數(shù),connections限制連接數(shù)。

  1. apiVersion: networking.k8s.io/v1beta1 
  2. kind: Ingress 
  3. metadata: 
  4.   name: test-ingress 
  5.   annotations: 
  6.     nginx.ingress.kubernetes.io/limit-rps: "5" 
  7.     nginx.ingress.kubernetes.io/limit-rpm: "300" 
  8.     nginx.ingress.kubernetes.io/limit-connections: "10" 
  9. spec: 
  10.   rules: 
  11.   - http: 
  12.       paths: 
  13.       - path: /testpath 
  14.         backend: 
  15.           serviceName: test 
  16.           servicePort: 80 

6、最大body

這個主要是針對外部請求,防止將流量打滿,proxy-body-size 設(shè)置最大請求 body,如果超過則會返回 413 請求錯誤。

  1. apiVersion: networking.k8s.io/v1beta1 
  2. kind: Ingress 
  3. metadata: 
  4.   name: test-ingress 
  5.   annotations: 
  6.     nginx.ingress.kubernetes.io/proxy-body-size: 8m 
  7. spec: 
  8.   rules: 
  9.   - http: 
  10.       paths: 
  11.       - path: /testpath 
  12.         backend: 
  13.           serviceName: test 

7、客戶端白名單

這個主要是用于安全限制,只允許特定的客戶端請求,但由于現(xiàn)在網(wǎng)絡(luò)中NAT的廣泛應(yīng)用,這個參數(shù)使用的場景比較有限。

  1. apiVersion: networking.k8s.io/v1beta1 
  2. kind: Ingress 
  3. metadata: 
  4.   name: test-ingress 
  5.   annotations: 
  6.      ingress.kubernetes.io/whitelist-source-range: "10.0.0.0/24,172.10.0.1" 
  7. spec: 
  8.   rules: 
  9.   - http: 
  10.       paths: 
  11.       - path: /testpath 
  12.         backend: 
  13.           serviceName: test 

8、默認(rèn)服務(wù)

這個經(jīng)常使用,當(dāng)客戶端請求一個不存在的path的時候,我們不希望返回 404 ,跳轉(zhuǎn)到一個默認(rèn)的服務(wù)上。

  1. apiVersion: networking.k8s.io/v1beta1 
  2. kind: Ingress 
  3. metadata: 
  4.   name: test-ingress 
  5.   annotations: 
  6.      nginx.ingress.kubernetes.io/default-backend: <svc name
  7. spec: 
  8.   rules: 
  9.   - http: 
  10.       paths: 
  11.       - path: /testpath 
  12.         backend: 
  13.           serviceName: test 

9、access log開關(guān)

nginx ingress 默認(rèn)是開啟access log的,如果你想關(guān)閉,可以通過將

  1. apiVersion: networking.k8s.io/v1beta1 
  2. kind: Ingress 
  3. metadata: 
  4.   name: test-ingress 
  5.   annotations: 
  6.      nginx.ingress.kubernetes.io/enable-access-log: "false" 
  7. spec: 
  8.   rules: 
  9.   - http: 
  10.       paths: 
  11.       - path: /testpath 
  12.         backend: 
  13.           serviceName: test 

 

責(zé)任編輯:武曉燕 來源: 今日頭條
點贊
收藏

51CTO技術(shù)棧公眾號