简体   繁体   English

Nginx 入口:TCP 端口上基于主机的路由

[英]Nginx ingress : Host based routing on TCP port

Usage the same TCP port for Rabbitmq 5672 and transfer requests to different namespaces/rabbitmq_service based on the host-based routing.为 Rabbitmq 5672 使用相同的 TCP 端口,并根据基于主机的路由将请求传输到不同的命名空间/rabbitmq_service。

What works:什么工作:

chart: nginx-git/ingress-nginx
version: 3.32.0
values:
  - tcp:
      5672: "cust1namespace/rabbitmq:5672"

Block reflected in nginx.conf :反映在 nginx.conf 中的块:

server {
    preread_by_lua_block {
        ngx.var.proxy_upstream_name="tcp-cust1namespace-services-rabbitmq-5672";
    }
    listen                  :5672;
    proxy_pass              upstream_balancer;
}

Note: this will transfer all the requests coming to port 5672 to cust1namespace/rabbitmq:5672, irrespective of the client domain name and we want host-based routing based on domain name注意:这会将所有到达端口 5672 的请求传输到 cust1namespace/rabbitmq:5672,而不管客户端域名如何,我们希望基于域名的基于主机的路由

What is expected :什么是预期:

chart: nginx-git/ingress-nginx
version: 3.32.0
values:
  - tcp:
      cust1domainname:5672: "cust1namespace/rabbitmq:5672"
      cust2domainname:5672: "cust2namespace/rabbitmq:5672"

Error: Failed to render chart: exit status 1: Error: unable to build kubernetes objects from release manifest: error validating "": error validating data: [ValidationError(Service.spec.ports[3].port): invalid type for io.k8s.api.core.v1.ServicePort.port: got "string", expected "integer", ValidationError(Service.spec.ports[4].port): invalid type for io.k8s.api.core.v1.ServicePort.port: got "string", expected "integer"]错误:无法呈现图表:退出状态 1:错误:无法从发布清单构建 kubernetes 对象:错误验证“”:错误验证数据:[ValidationError(Service.spec.ports[3].port):io 类型无效.k8s.api.core.v1.ServicePort.port:得到“字符串”,预期为“整数”,ValidationError(Service.spec.ports[4].port):io.k8s.api.core.v1 的无效类型。 ServicePort.port:得到“字符串”,应为“整数”]

The final nginx.conf should look like :最终的 nginx.conf 应如下所示:

server {
    preread_by_lua_block {
        ngx.var.proxy_upstream_name="tcp-cust1namespace-services-rabbitmq-5672";
    }
    listen                  cust1domainname:5672;
    proxy_pass              upstream_balancer;
}
    
server {
    preread_by_lua_block {
        ngx.var.proxy_upstream_name="tcp-cust2namespace-services-rabbitmq-5672";
    }
    listen                  cust2domainname:5672;
    proxy_pass              upstream_balancer;
}

A bit of theory一点理论

Approach you're trying to implement is not possible due to network protocols implementation and difference between them.由于网络协议的实现和它们之间的差异,您尝试实现的方法是不可能的。

TCP protocol works on transport layer, it has source and destination IPs and ports, it does not have any hosts information within. TCP协议的工作原理传输层上,它的源和目的IP地址和端口,它没有任何内部主机的信息。 In turn HTTP protocol works on application layer which seats on top of the TCP and it does have information about host where this request is intended to be sent.反过来, HTTP协议在位于TCP顶部的应用层上工作,并且它确实具有有关要发送此请求的主机的信息。

Please get familiar with OSI model and protocols which works on these levels .请熟悉适用于这些级别的 OSI 模型和协议 This will help to avoid any confusion why this works this way and no other.这将有助于避免任何混淆为什么以这种方式工作而不是其他方式。

Also there's a good answer on quora about difference between HTTP and TCP protocols . 在 quora 上也有一个关于 HTTP 和 TCP 协议之间差异很好的答案

Answer回答

At this point you have two options:此时你有两个选择:

  1. Use ingress to work on application layer and let it direct traffic to services based on hosts which are presented in request body .使用 ingress 在应用层上工作,让它根据request body中呈现的主机将流量引导到服务。 All traffic should go through ingress endpoint (usually it's loadbalancer which is exposed outside of the cluster).所有流量都应该通过入口端点(通常是暴露在集群外部的负载均衡器)。

Please find examples with请查找示例

  1. Use ingress to work on transport layer and expose separate TCP ports for each service/customer.使用 ingress 在传输层上工作并为每个服务/客户公开单独的 TCP 端口。 In this case traffic will be passed through ingress directly to services.在这种情况下,流量将通过入口直接传递到服务。

Based on your example it will look like:根据您的示例,它将如下所示:

chart: nginx-git/ingress-nginx
    version: 3.32.0
    values:
    - tcp:
        5672: "cust1namespace/rabbitmq:5672" # port 5672 for customer 1
        5673: "cust2namespace/rabbitmq:5672" # port 5673 for customer 2
        ...

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

相关问题 基于Header的NGINX入口路由 - NGINX Ingress Routing based on Header Kubernetes Nginx-Ingress 基于主机的路由工作,而基于路径的路由不起作用 - Kubernetes Nginx-Ingress Host-Based Routing works, while Path Based Routing doesn't work Nginx 入口控制器 - 基于路径的路由 - Nginx Ingress controller- Path based routing 使用 nginx-Ingress 在 Kubernetes 中暴露集群外的 TCP 端口 - Exposing a TCP port out of cluster in Kubernetes using nginx-Ingress 在 Nginx 上配置 TCP 端口 Azure Z30136395F018797812198317C 上的入口 - Configure TCP Port on Nginx Ingress on Azure Kubernetes Cluster (AKS) 基于源IP的Nginx TCP流路由 - Nginx TCP stream routing based on source IP Kubernetes nginx HTTPS 在 AWS 中的基于入口路径的路由 - Kubernetes nginx ingress path-based routing of HTTPS in AWS nginx虚拟主机,http服务器和tcp端口转发 - nginx Virtual host, http server and tcp port forward 我如何配置入口和Nginx入口控制器以使用相同的主机和路径将HTTP流量发送到端口80并将https流量发送到443端口 - How can i configure ingress and nginx ingress controller to send http traffic to port 80 and https traffic to 443 port, with the same host and path 如何使用 Docker 桌面为 Windows Kube.netes 和 ingress-nginx 公开 postgres tcp 端口 - How to expose postgres tcp port using Docker Desktop for Windows Kubernetes and ingress-nginx
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM