简体   繁体   English

kubernetes - 将入口流量路由到某些路径的特定 pod

[英]kubernetes - route ingress traffic to specific pod for some paths

I have multiple pods, that scale up and down automatically.我有多个 pod,它们会自动放大和缩小。

I am using an ingress as entry point.我正在使用入口作为入口点。 I need to route external traffic to a specific pod base on some conditions (lets say path).我需要根据某些条件(比如说路径)将外部流量路由到特定的 pod。 At the point the request is made I am sure the specific pod is up.在提出请求时,我确信特定的 pod 已启动。

For example lets say I have domain someTest.com, that normally routes traffic to pod 1, 2 and 3 (lets say I identify them by internal ips - 192.168.1.10, 192.168.1.11 and 192.168.1.13).例如,假设我有域 someTest.com,通常将流量路由到 pod 1、2 和 3(假设我通过内部 ips 识别它们 - 192.168.1.10、192.168.1.11 和 192.168.1.13)。

When I call someTest.com/specialRequest/12, I need to route the traffic to 192.168.1.12, when I call someTest.com/specialRequest/13, I want to route traffic to 192.168.1.13.当我调用 someTest.com/specialRequest/12 时,我需要将流量路由到 192.168.1.12,当我调用 someTest.com/specialRequest/13 时,我想将流量路由到 192.168.1.13。 For normal cases (someTest.com/normalRequest) I just want to do the lb do his epic job normally.对于正常情况(someTest.com/normalRequest),我只想让 lb 正常完成他的史诗般的工作。

If pods scale up and 192.168.1.14 appears, I need to be able to call someTest.com/specialRequest/14 and be routed to the mentioned pod.如果 pod 扩大并出现 192.168.1.14,我需要能够调用 someTest.com/specialRequest/14 并被路由到提到的 pod。

Is there anyway I can achieve this?无论如何我可以做到这一点吗?

Yes, you can easily achieve this using Kubernetes Ingress.是的,您可以使用 Kubernetes Ingress 轻松实现此目的。 Here is a sample code that might help:这是一个可能有帮助的示例代码:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: app-ingress
  spec:
    rules:
    - host: YourHostName.com
      http:
        paths:
        - path: /
          backend:
            serviceName: Service1
            servicePort: 8000
        - path: /api
          backend:
            serviceName: Service2
            servicePort: 8080
        - path: /admin
          backend:
            serviceName: Service3
            servicePort: 80

Please not that the ingress rules have serviceNames and not pod names, so you will have to create services for your pods.请注意,入口规则有 serviceNames 而不是 pod 名称,因此您必须为您的 pod 创建服务。 Here is an example for a service which exposes nginx as a service in Kubernetes:这是一个将 nginx 公开为 Kubernetes 中的服务的服务示例:

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
  labels:
    io.kompose.service: nginx
spec:
  ports:
  - name: "80"
    port: 80
    targetPort: 80
  selector:
    io.kompose.service: nginx

I am not aware of built-in functionality to implement this (if this is what your really want).我不知道实现此功能的内置功能(如果这是您真正想要的)。 You can achieve this by building your ownoperator for Kubernetes.您可以通过为 Kubernetes 构建自己的运算符来实现此目的。 Your operator may provision a Pod+Ingress combo which will do exactly what you want - forward your traffic to a single pod, or you can provision 2 pods and 1 ingress to achive HA setup.您的操作员可能会配置一个 Pod+Ingress 组合,这将完全按照您的要求进行 - 将您的流量转发到一个 pod,或者您可以配置 2 个 pod 和 1 个入口以实现 HA 设置。

Depending on the Ingress you are using, it also may be possible to group multiple ingress resources under the same load balancer.根据您使用的 Ingress,也可以将多个 Ingress 资源分组在同一个负载均衡器下。

Here is a brief diagram of how this could look like.这是一个简短的图表,说明它的外观。 示例运算符

would it be feasible to create another application that can get the path and target the pod directly via a pattern in the naming convention?创建另一个可以通过命名约定中的模式直接获取路径并以 pod 为目标的应用程序是否可行? for example例如

${podnamePrefix+param}.${service name}.${namespace}.svc.cluster.local ${podnamePrefix+param}.${服务名称}.${命名空间}.svc.cluster.local

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM