kubernetes各个条件下使用nginx-ingress进行路由映射
1、nginx-ingress路由配置并将路由名称去除(不传递到后台service)
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: example-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
rules:
- http:
paths:
- path: /foo/(.*)
backend:
serviceName: example-service
servicePort: 80
- path: /bar/(.*)
backend:
serviceName: example-service
servicePort: 80
上述配置得到结果是请求的接口/foo和/bar不会传递到后台service
对应kuboard配置方式如下
请求测试
2、如果将路由路径传到后台service
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: example-ingress
annotations: {}
spec:
rules:
- http:
paths:
- path: /foo
backend:
serviceName: example-service
servicePort: 80
- path: /bar
backend:
serviceName: example-service
servicePort: 80
这个前提是要保证后台service中有对应的foo和bar路径
3、部分路由传递,其余的不匹配的不传递过去
nginx.ingress.kubernetes.io/server-snippet: |
if ($uri = /spot-all/) {
rewrite ^ /;
proxy_pass http://a-service;
}
if ($uri = /spot-order/) {
rewrite ^ /;
proxy_pass http://b-service;
}
4、部分路由传递,不匹配的不传递
nginx.ingress.kubernetes.io/server-snippet: |
location /spot-all/ {
proxy_pass http://a-service;
}
location /spot-order/ {
proxy_pass http://b-service;
}
5、同一配置不同路由路径配置
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$1
nginx.ingress.kubernetes.io/rewrite-target: /foo/$1
spec:
rules:
- http:
paths:
- path: /app1/(.*)
pathType: Prefix
backend:
service:
name: app1
port:
name: http
- path: /app2/(.*)
pathType: Prefix
backend:
service:
name: app2
port:
name: http
在这个例子中,/app1 和 /app2 的请求路径都会被重写,但每个请求路径的替换目标不同。/app1 的请求路径将重写为 /,而 /app2 的请求路径将重写为 /foo/