[英]Route traffic to a service in a different namespace with Traefik and Kubernetes
Using Traefik as an ingress controller (on a kube cluster in GCP).使用 Traefik 作为入口控制器(在 GCP 中的 kube 集群上)。 Is it possible to create an ingress rule that uses a backend service from a different namespace?是否可以创建使用来自不同命名空间的后端服务的入口规则?
We have a namespace for each of our "major" versions of code.我们为每个“主要”版本的代码都有一个命名空间。
1-service.com -> 1-service.com ingress in the 1-service ns -> 1-service svc in the same ns 1-service.com -> 1-service.com ingress in the 1-service ns -> 1-service svc in the same ns
2-service.com -> 2-service.com ingress in the 2-service ns... and so on 2-service.com -> 2-service.com ingress in the 2-service ns...等等
I also would like another ingress rule in the "unversioned" namespace that will route traffic to one of the major releases.我还想要“未版本化”命名空间中的另一个入口规则,它将流量路由到一个主要版本。
service.com -> service.com ingress in the "service" ns -> X-service in the X-service namespace service.com -> service.com ingress 在“service” ns -> X-service 命名空间中的 X-service
I would like to keep major versions separate in k8s using versioned host names (1-service.com etc), but still have a "latest" that points to the latest of the releases.我想使用版本化主机名(1-service.com 等)在 k8s 中保持主要版本分开,但仍然有一个指向最新版本的“最新”版本。
I believe voyager can do cross namespace ingress -> svc.我相信 voyager 可以做跨命名空间入口 -> svc。 can Traefik do the same?? Traefik 也可以吗??
You can use a workaround like this:您可以使用这样的解决方法:
ExternalName
in your namespace when you want to create an ingress:当你想创建一个入口时,在你的命名空间中创建一个类型为ExternalName
的服务:apiVersion: v1
kind: Service
metadata:
name: service-1
namespace: unversioned
spec:
type: ExternalName
externalName: service-1.service-1-ns.svc.cluster.local
ports:
- name: http
port: 8080
protocol: TCP
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: traefik
name: ingress-to-other-ns
namespace: service-1-ns
spec:
rules:
- host: latest.example.com
http:
paths:
- backend:
serviceName: service-1
servicePort: 8080
path: /
Just tested with the following example on EKS.刚刚在 EKS 上使用以下示例进行了测试。 Traefik is deployed in default
namespace. Traefik 部署在default
命名空间中。 This is the config used for the k8s service:这是用于 k8s 服务的配置:
---
apiVersion: v1
kind: Service
metadata:
name: 1-service
namespace: 1-service
labels:
app: 1-service
spec:
selector:
app: 1-service
ports:
- name: http
port: 80
targetPort: 80
And this is the config used for Traefik service that will send the request to different namespace:这是用于 Traefik 服务的配置,它将请求发送到不同的命名空间:
services:
1-service:
loadBalancer:
servers:
- url: http://1-service.1-service.svc.cluster.local:80
# - url: http://1-service.1-service:80 # This should work perfectly as well, didn't test it explicitly
As you probably already get that, you can reference to services from different namespace by using SERVICE.NAMESPACE
notation, instead of the SERVICE
, which will automatically assume that you are referencing a service from the current namespace.正如您可能已经知道的那样,您可以使用SERVICE.NAMESPACE
表示法而不是SERVICE
来引用来自不同命名空间的SERVICE
,它会自动假定您正在引用当前命名空间中的服务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.