我想使用k8s入口将域转发给带有URI的某个服务。入口地址如下所示:
- host: foo.example
http:
paths:
- backend:
path: /
pathType: Prefix
service:
name: serviceA
port:
number: 8999
path: /foo/bar # don't have this attribute, but I want something like this
我发现k8s文档说paths
中的path
属性可以这样做:
- foo。例子/foo→serviceA: 8999
- foo。例子/酒吧→serviceB: 9888
但是我想这样做:foo。例子→serviceA: 8999/foo/bar. 在nginx配置中也是这样:
server {
server_name foo.example;
location / {
proxy_pass http://service:8999/foo/bar;
}
}
在k8s入口我该怎么办?谢谢。
Nginx入口控制器支持重写和代码片段。我认为你想要的可以通过注释来完成。从这里的示例https://kubernetes.github.io/ingress-nginx/examples/rewrite/中,您的代码应该看起来像:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /foo/bar/$1
name: <ingress-name>
namespace: <namespace>
spec:
ingressClassName: nginx
rules:
- host: fool.example
http:
paths:
- path: /(.*)
pathType: Prefix
backend:
service:
name: serviceA
port:
number: 8999
你可以用服务器片段添加更复杂的行为(比如你的nginx配置):
https://github.com/kubernetes/ingress-nginx/blob/main/docs/user-guide/nginx-configuration/annotations.md server-snippet