我们的生态系统中有许多微服务,其中两个处理用户数据:
>用户服务 – >
> POST /用户
> GET / users / [[:alnum:]]
>文件服务 – >
> POST / users / [[:alnum:]] / documents
> GET / users / [[:alnum:]] / documents / [[:alnum:]]
因此我想在Ingress中定义它:
apiVersion: extensions/v1beta1
kind: Ingress
Metadata:
name: {{.Release.Name}}
annotations:
kubernetes.io/ingress.class: "Nginx"
kubernetes.io/tls-acme: "true"
ingress.kubernetes.io/enable-cors: "false"
ingress.kubernetes.io/ssl-redirect: "true"
labels:
chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
app: "{{ template "fullname" . }}"
heritage: "{{ .Release.Service }}"
release: "{{ .Release.Name }}"
tier: frontend
spec:
tls:
- secretName: {{ template "tls.fullname" . }}
hosts:
- "staging.ourhost.com"
rules:
- host: "staging.ourhost.com"
http:
paths:
- path: "/users/[[:alnum:]]+"
backend:
serviceName: {{ .Values.api.services.user_service | quote }}
servicePort: 8080
- path: "/users/[[:alnum:]]+/documents"
backend:
serviceName: {{ .Values.api.services.document_service | quote }}
servicePort: 8888
这导致在ngix.conf中设置以下内容:
location /users/[[:alnum:]]+ {
...
}
location /users/[[:alnum:]]+/documents{
...
}
即正则表达式未得到解决.
我尝试将其拆分为单独的入口并使用注释
ingress.kubernetes.io/rewrite-target: "/users/$1/documents"
location ~* ^/users/[[:alnum:]]+/documents/(?<baseuri>.*) {
set $proxy_upstream_name "default-dev-document_service-8888";
...
rewrite /users/[[:alnum:]]+/documents/(.*) /users/$1/documents/$1 break;
proxy_pass http://default-dev-document_service-8888;
}
问题(最后):
>为什么总是追加$1?这对我来说很奇怪
>有人知道另一种让路径与正则表达式一起工作的方法吗?
任何建议都会大大贬低
解决方法:
经过大量的搜索,似乎我试图做的事情在入口中是不可能的.我和他们一起开了一个PR,我正等着看它是否会被合并.
我尚未公开部署具有此更改的版本,但可以使用k8s中的dev docs构建分支