K8s Nginx Lngress 常用的 9個 配置(Annotation),你知道幾個?
上一篇文章介紹了 ingress vhost這個annotation的使用,趁熱打鐵我們一口氣介紹 9 個常用的annotation。
1、ingress class
如果一個k8s 集群里面部署多個ingress controller的時候,如果配置ingress 希望指定到某個ingress controller的時候,ingress claas就發(fā)揮巨大作用了。
一方面在controller啟動的時候需要通過參數(shù)指定ingress class
- --ingress-class=ngx-ds
 
另一方面,在創(chuàng)建ingress的時候,通過annotation指定ingress class,如下所示
- apiVersion: extensions/v1beta1
 - kind: Ingress
 - metadata:
 - name: other-ngx-k8s
 - namespace: other-ngx
 - annotations:
 - kubernetes.io/ingress.class: "ngx-ds"
 - spec:
 - rules:
 - - host: other-ngx-k8s.demo.com.cn
 - http:
 - paths:
 - - path: /
 - backend:
 - serviceName: other-ngx-k8s-ngx-svc
 - servicePort: 9001
 
2、 強(qiáng)制https
- apiVersion: networking.k8s.io/v1beta1
 - kind: Ingress
 - metadata:
 - name: test-ingress
 - annotations:
 - nginx.ingress.kubernetes.io/rewrite-target: /
 - nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
 - nginx.ingress.kubernetes.io/ssl-redirect: "true"
 - nginx.ingress.kubernetes.io/preserve-trailing-slash: "true"
 - spec:
 - rules:
 - - http:
 - paths:
 - - path: /testpath
 - backend:
 - serviceName: test
 - servicePort: 80
 
3、請求超時
- apiVersion: networking.k8s.io/v1beta1
 - kind: Ingress
 - metadata:
 - name: cafe-ingress-with-annotations
 - annotations:
 - nginx.org/proxy-connect-timeout: "30s"
 - nginx.org/proxy-read-timeout: "20s"
 - spec:
 - rules:
 - - host: cafe.example.com
 - http:
 - paths:
 - - path: /tea
 - backend:
 - serviceName: tea-svc
 - servicePort: 80
 - - path: /coffee
 - backend:
 - serviceName: coffee-svc
 - servicePort: 80
 
4、跨域訪問
我們經(jīng)常將nginx作為api的網(wǎng)關(guān),支持跨域必不可少。通過
- apiVersion: networking.k8s.io/v1beta1
 - kind: Ingress
 - metadata:
 - name: test-ingress
 - annotations:
 - nginx.ingress.kubernetes.io/enable-cors: "true"
 - nginx.ingress.kubernetes.io/cors-allow-methods: "PUT, GET, POST, OPTIONS"
 - nginx.ingress.kubernetes.io/cors-allow-headers: "X-Forwarded-For, X-app123-XPTO"
 - nginx.ingress.kubernetes.io/cors-expose-headers: "*, X-CustomResponseHeader"
 - nginx.ingress.kubernetes.io/cors-max-age: 600
 - nginx.ingress.kubernetes.io/cors-allow-credentials: "false"
 - spec:
 - rules:
 - - http:
 - paths:
 - - path: /testpath
 - backend:
 - serviceName: test
 - servicePort: 80
 
5、限流
限流也經(jīng)常使用,通過 rps 限制每秒請求數(shù),rpm 限制每分鐘請求數(shù),connections限制連接數(shù)。
- apiVersion: networking.k8s.io/v1beta1
 - kind: Ingress
 - metadata:
 - name: test-ingress
 - annotations:
 - nginx.ingress.kubernetes.io/limit-rps: "5"
 - nginx.ingress.kubernetes.io/limit-rpm: "300"
 - nginx.ingress.kubernetes.io/limit-connections: "10"
 - spec:
 - rules:
 - - http:
 - paths:
 - - path: /testpath
 - backend:
 - serviceName: test
 - servicePort: 80
 
6、最大body
這個主要是針對外部請求,防止將流量打滿,proxy-body-size 設(shè)置最大請求 body,如果超過則會返回 413 請求錯誤。
- apiVersion: networking.k8s.io/v1beta1
 - kind: Ingress
 - metadata:
 - name: test-ingress
 - annotations:
 - nginx.ingress.kubernetes.io/proxy-body-size: 8m
 - spec:
 - rules:
 - - http:
 - paths:
 - - path: /testpath
 - backend:
 - serviceName: test
 
7、客戶端白名單
這個主要是用于安全限制,只允許特定的客戶端請求,但由于現(xiàn)在網(wǎng)絡(luò)中NAT的廣泛應(yīng)用,這個參數(shù)使用的場景比較有限。
- apiVersion: networking.k8s.io/v1beta1
 - kind: Ingress
 - metadata:
 - name: test-ingress
 - annotations:
 - ingress.kubernetes.io/whitelist-source-range: "10.0.0.0/24,172.10.0.1"
 - spec:
 - rules:
 - - http:
 - paths:
 - - path: /testpath
 - backend:
 - serviceName: test
 
8、默認(rèn)服務(wù)
這個經(jīng)常使用,當(dāng)客戶端請求一個不存在的path的時候,我們不希望返回 404 ,跳轉(zhuǎn)到一個默認(rèn)的服務(wù)上。
- apiVersion: networking.k8s.io/v1beta1
 - kind: Ingress
 - metadata:
 - name: test-ingress
 - annotations:
 - nginx.ingress.kubernetes.io/default-backend: <svc name>
 - spec:
 - rules:
 - - http:
 - paths:
 - - path: /testpath
 - backend:
 - serviceName: test
 
9、access log開關(guān)
nginx ingress 默認(rèn)是開啟access log的,如果你想關(guān)閉,可以通過將
- apiVersion: networking.k8s.io/v1beta1
 - kind: Ingress
 - metadata:
 - name: test-ingress
 - annotations:
 - nginx.ingress.kubernetes.io/enable-access-log: "false"
 - spec:
 - rules:
 - - http:
 - paths:
 - - path: /testpath
 - backend:
 - serviceName: test
 
















 
 
 















 
 
 
 