Kubernetes如何在同一地址运行多个服务



我在http://service1/apihttp://service2/api有几个站点可用我希望它们工作在相同的外部地址和路由发生在kube

http:/exturi/service1/api
http:/exturi/service2/api

导入配置

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: haproxy
name: ingress-api
spec:
rules:
- host: api.sample.com
http:
paths:
- backend:
serviceName: edpcore-db-api
servicePort: 13001
path: /db-api/
pathType: Prefix
tls:
- hosts:
- api.sample.com
secretName: tls-api-secret

如何配置ingress

如果你使用的是Nginx入口,你可以使用

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$2
name: rewrite
namespace: default
spec:
rules:
- host: rewrite.bar.com
http:
paths:
- backend:
serviceName: service-1
servicePort: 80
path: /service-1(/|$)(.*)
- backend:
serviceName: service-2
servicePort: 80
path: /service-2(/|$)(.*)

例如,上面的入口定义将导致以下重写:

rewrite.bar.com/service1 rewrites to rewrite.bar.com/
rewrite.bar.com/service1/ rewrites to rewrite.bar.com/
rewrite.bar.com/service1/new rewrites to rewrite.bar.com/new

参考:https://kubernetes.github.io/ingress-nginx/examples/rewrite/

额外

如果您不想删除服务名称或重写simple,请使用此配置

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: haproxy
name: simple
namespace: default
spec:
rules:
- host: simple.bar.com
http:
paths:
- backend:
serviceName: service-1
servicePort: 80
path: /service-1
- backend:
serviceName: service-2
servicePort: 80
path: /service-2

你应该这样做(nginx ingress):

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-ingress
namespace: my-ingress-namespace
spec:
rules:
- host: exturi
http:
paths:
- backend:
service:
name: service1
port:
number: 80
path: /service1/(.*)
pathType: ImplementationSpecific      
- backend:
service:
name: service2
port:
number: 80
path: /service2/(.*)
pathType: ImplementationSpecific
status:
loadBalancer:
ingress:
- ip: {}

相关内容

  • 没有找到相关文章

最新更新