简体   繁体   English

使用 Traefik 和 Kubernetes 将流量路由到不同命名空间中的服务

[英]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:您可以使用这样的解决方法:

  1. Create a Service with type 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
  1. Create an ingress that point to this service:创建一个指向此服务的入口:
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.

相关问题 Zuul无法将流量路由到kubernetes上的服务 - Zuul unable to route traffic to service on kubernetes Kubernetes / AKS:将外部流量路由到服务 - Kubernetes / AKS: Route external traffic to Service Kubernetes:一种手动将流量路由到不同副本的方法 - Kubernetes: A Way to Manually Route Traffic to Different Replicas 如何使用 Traefik 和 Kubernetes 创建到正在侦听 Https 的服务的 HTTPS 路由 - How to create a HTTPS route to a Service that is listening on Https with Traefik, and Kubernetes 将外部流量从独立的 nginx 服务路由到 kubernetes 节点端口服务 - Route external traffic from a standalone nginx service to kubernetes nodeport service 为Kubernetes Traefik Ingress配置每个服务的不同路径重写 - Configure Kubernetes Traefik Ingress with different path rewrites for each service 使用来自不同命名空间的 Kubernetes 服务帐户 - use Kubernetes service account from different namespace Traefik Ku​​bernetes:公开非Kubernetes服务 - Traefik Kubernetes: Expose non Kubernetes service Kubernetes nginx 将流量路由到 /api/* - Kubernetes nginx route traffic to /api/* 每个 Kubernetes Traefik 入口路由的 mTLS (clientAuth) - mTLS (clientAuth) per Kubernetes Traefik Ingress Route
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM