我正试图使用基于路径的路由,在kubernetes上使用nginx入口控制器来组合我们的几个微服务。
这是一个场景:
http://myapi.develop.mydomain.com/data-service/request->转到服务名称http://myapi.develop.mydomain.com/model-service/request->转到服务名称
我的问题是,当url/路径到达后端服务时,它会从请求中获得url。
因此/data-service/request
以/data-service/request
的形式点击应用程序,并需要它以/request
的形式点击该应用程序(但仍以/data-service/request
的形式显示给用户(
尝试从这里查看使用入口重写规则:https://kubernetes.github.io/ingress-nginx/examples/rewrite/
虽然不太确定这是要更改用户的url,还是只是从入口到服务。我们需要url对最终用户保持不变,并且更改仅在集群内。
这是我们正在使用的入口yaml:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: myapi-ingress
namespace: mynamespace
uid:
resourceVersion: ''
generation: 1
creationTimestamp: '2021-11-16T15:12:42Z'
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$2
managedFields:
- manager: kubectl-create
operation: Update
apiVersion: networking.k8s.io/v1
time: '2021-11-16T15:12:42Z'
fieldsType: FieldsV1
fieldsV1:
'f:metadata':
'f:annotations':
.: {}
'f:nginx.ingress.kubernetes.io/rewrite-target': {}
'f:spec':
'f:ingressClassName': {}
'f:rules': {}
- manager: nginx-ingress
operation: Update
apiVersion: networking.k8s.io/v1
time: '2021-11-16T15:12:42Z'
fieldsType: FieldsV1
fieldsV1:
'f:status':
'f:loadBalancer':
'f:ingress': {}
selfLink: /apis/networking.k8s.io/v1/namespaces/mynamespace/ingresses/myapi-ingress
status:
loadBalancer:
ingress:
- ip: myip
spec:
ingressClassName: nginx
rules:
- host: myapi.mynamespace.mydomain.com
http:
paths:
- path: /data-service(/|$)(.*)
pathType: Prefix
backend:
service:
name: data-service
port:
number: 3000
- path: /model-service(/|$)(.*)
pathType: Prefix
backend:
service:
name: model-service
port:
number: 3000
有人告诉我哪里可能出错了吗?
由于您使用的是API版本的,因此可能会出现问题
您的K8是否支持ingress API版本networking.k8s.io/v1beta1
?
networking.k8s.io/v1beta1
代替networking.k8s.io/v1
和其他微小变化
试用:
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: /$2
name: example
# namespace: foo
spec:
rules:
- host: [your ip address].sslip.io
http:
paths:
- backend:
serviceName: registry-app-server
servicePort: 8761
path: /eureka/(.*)
- backend:
serviceName: api-gateway-server
servicePort: 7000
path: /api(/|$)(.*)