Kubernetes Nginx Ingress

Kubernetes Nginx Ingress [0] #

版本1-K8s官方维护 [1][2] #

  • There are three ways to customize NGINX:
    • ConfigMap: using a Configmap to set global configurations in NGINX. [滚动更新后生效, 针对全局的配置]
    • Annotations: use this if you want a specific configuration for a particular Ingress rule. [立即生效,针对某个域名location进行配置]
    • Custom template: when more specific settings are required, like open_file_cache, adjust listen options as rcvbuf or when is not possible to change the configuration through the ConfigMap.

版本2-Nginx官方维护 [3][4] #

版本1 vs. 版本2 [5] #

实践 [0] #

域名重定向 *** #

redirect , http 重定向到https, 新旧域名替换

前后端分离 *** #

rewrite

+ www.a.com /    --> 前端服务
+ www.a.com /api --> 后端服务 /api  
  www.a.com/api rewrite到  www.a.com 

SSL配置 [p] #

验证 自签名证书

匹配请求头 #

Mobile 和 PC 的请求头不同,路由到不同的后端服务

实现灰度金丝雀发布[6][7] #

不同灰度方式的优先级由高到低为: canary-by-header>canary-by-cookie>canary-weight

Annotation 说明
nginx.ingress.kubernetes.io/canary 必须设置该Annotation值为true,否则其它规则将不会生效。
nginx.ingress.kubernetes.io/canary-by-header 表示基于请求头的名称进行灰度发布。
nginx.ingress.kubernetes.io/canary-by-header-value 表示基于请求头的值进行灰度发布。
nginx.ingress.kubernetes.io/canary-by-header-pattern 表示基于请求头的值进行灰度发布,并对请求头的值进行正则匹配。
nginx.ingress.kubernetes.io/canary-by-cookie 表示基于Cookie进行灰度发布。
nginx.ingress.kubernetes.io/canary-weight 表示基于权重进行灰度发布。
nginx.ingress.kubernetes.io/canary-weight-total 表示设定的权重总值。

%accordion%灰度发布示例%accordion%

  • 基于Header灰度(自定义header值):当请求Header为ack: alibaba时将访问灰度服务;其它Header将根据灰度权重将流量分配给灰度服务。
  • 基于Cookie灰度:当Header不匹配时,请求的Cookie为hangzhou_region=always时将访问灰度服务。
  apiVersion: networking.k8s.io/v1
  kind: Ingress
  metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/canary: "true"
    nginx.ingress.kubernetes.io/canary-weight: "20"
    nginx.ingress.kubernetes.io/canary-by-header: "ack"
    nginx.ingress.kubernetes.io/canary-by-header-value: "alibaba"
    nginx.ingress.kubernetes.io/canary-by-cookie: "hangzhou_region"

%/accordion%

速率限制 #

黑白名单 #

allow deny

自定义错误页面 #

参考 #

  1. kubernetes架构师课程 162 V ***
  2. Ingress-Nginx Doc
  3. Ingress-Nginx Github
  4. Nginx-ingress Doc
  5. Nginx-ingress GitHub
  6. nginx-ingress-controllers.md 表格
  7. Nginx Ingress高级用法
  8. 【IT老齐294】大厂如何基于K8S实现金丝雀发布