在上篇文章,只简单介绍了一些ingress开源默认支持的Annotation。
下面我来说下怎么实现一个header不同值。
通过修改nginx.ingress.kubernetes.io/configuration-snippet配置,并且配置正则实现:
使用$http_ 获取http请求的header,根据配置中是否为完整或者正则匹配,匹配foo的值
使用$cookie_获取http请求的cookie,根据配置中是否为完整或者正则匹配,匹配foo的值
使用$arg_获取http的请求参数,根据配置中是否为完整或者正则匹配,匹配foo的值
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: tomcat-test
namespace: test
annotations:
kubernetes.io/ingress.class: "nginx-backend"
nginx.ingress.kubernetes.io/enable-access-log: "true"
nginx.ingress.kubernetes.io/configuration-snippet: |
access_log /var/log/nginx/test.sy.com.log;
if ($http_name ~ "^.*sy$|^.*aa$") {
proxy_pass http://tomcat-test-scm.test:6080;
break;
}
spec:
rules:
- host: test.sy.com
http:
paths:
- path: /
backend:
serviceName: tomcat-test-opdev
servicePort: 6080
- path: /
backend:
serviceName: tomcat-test-scm
servicePort: 6080
[root@ingress]# curl http://test.sy.com/abc/check_health.jsp
opdev
[root@ingress]# curl -H "name: sy" http://test.sy.com/abc/check_health.jsp
scm
[root@ingress]# curl -H "name: aa" http://test.sy.com/abc/check_health.jsp
scm
[root@ingress]# curl -H "name: bb" http://test.sy.com/abc/check_health.jsp
opdev
路由规则nginx.ingress.kubernetes.io/service-match
该Annotation用来配置新版本服务的路由匹配规则,格式如下:
nginx.ingress.kubernetes.io/service-match: |
<service-name>: <match-rule>
参数解释
service-name:服务名称,满足match-rule的请求会被路由到该服务中。
match-rule:路由匹配规则。
配置示例
# 请求头中满足foo正则匹配^bar$的请求被转发到新版本服务new-nginx中
new-nginx: header("foo", /^bar$/)
# 请求头中满足foo完整匹配bar的请求被转发到新版本服务new-nginx中
new-nginx: header("foo", "bar")
# cookie中满足foo正则匹配^sticky-.+$的请求被转发到新版本服务new-nginx中
new-nginx: cookie("foo", /^sticky-.+$/)
# query param中满足foo完整匹配bar的请求被转发到新版本服务new-nginx中
new-nginx: query("foo", "bar")
服务权重nginx.ingress.kubernetes.io/service-weight
该Annotation用来配置新老版本服务的流量权重,格式如下:
参数解释
new-svc-name:新版本服务名称。
new-svc-weight:新版本服务权重。
old-svc-name:老版本服务名称。
old-svc-weight:老版本服务权重。
配置示例
nginx.ingress.kubernetes.io/service-weight: |
new-nginx: 20, old-nginx: 60
说明
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: tomcat-test
namespace: test
annotations:
kubernetes.io/ingress.class: "nginx-backend"
nginx.ingress.kubernetes.io/service-match: |
tomcat-test-scm: header("name", /^sy$|^aa$/)
#nginx.ingress.kubernetes.io/service-weight: |
# tomcat-test-opdev: 80, tomcat-test-scm: 20
nginx.ingress.kubernetes.io/enable-access-log: "true"
nginx.ingress.kubernetes.io/configuration-snippet: |
access_log /var/log/nginx/test.sy.com.log;
spec:
rules:
- host: test.sy.com
http:
paths:
- path: /
backend:
serviceName: tomcat-test-opdev
servicePort: 6080
- path: /
backend:
serviceName: tomcat-test-scm
servicePort: 6080
[rsync@ingress]$ curl test.sy.com/abc/check_health.jsp
opdev
[rsync@ingress]$ curl -H "name: sy" test.sy.com/abc/check_health.jsp
scm
[rsync@ingress]$ curl -H "name: aa" test.sy.com/abc/check_health.jsp
scm