我使用k8s来部署服务。我希望当我访问入口主机foo.example.com时并将其转发到服务器:8000/proxy/foo. 子域foo是动态的,可以更改为任何单词。预期结果如下:
- foo.example.com→服务器:8000/代理/foo
- bar.example.com→服务器:8000/代理/酒吧
- …
我知道ingress主机可以使用通配符,使用ingress-nginx rewrite可以重写url路径。我使用入口-nginx控制器。入口yaml文件如下所示:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: /proxy/${wildcard}/$1 # Could I use the host wildcard here?
name: nginx-forward
spec:
rules:
- host: *.example.com # I want to get the subdomain wildcard
http:
paths:
- backend:
service:
name: service
port:
number: 8080
path: /(.*)
pathType: Prefix
tls: # update this attribute
- hosts:
- *.example.com
secretName: my-secret
我如何使用k8s入口或其他东西来获得我想要的?谢谢。
您可以使用服务器代码片段获取子域:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/server-snippet: |
server_name ~^(?<subdomain>w+).example.com$;
nginx.ingress.kubernetes.io/rewrite-target: /proxy/$subdomain/$1
name: nginx-forward
spec:
rules:
- http:
paths:
- backend:
service:
name: service
port:
number: 8080
path: /(.*)
pathType: Prefix