简体   繁体   English

用于路由 TCP 流量的入口控制器

[英]Ingress controller to route TCP traffic

I'm trying to setup an ingress controller(nginx) to forward some TCP traffic to a kubernetes service(GCP).我正在尝试设置一个入口控制器(nginx)来将一些 TCP 流量转发到 kubernetes 服务(GCP)。 There's this tutorial that shows how to route HTTP traffic to services (based on the paths) using nginx. 本教程展示了如何使用 nginx 将 HTTP 流量路由到服务(基于路径)。 I want to have a similar setup to forward TCP traffic.我想要一个类似的设置来转发 TCP 流量。

In my cluster, I have a pod running a TCP echo server written in python using sockets.在我的集群中,我有一个 pod,它运行一个使用套接字用 Python 编写的 TCP 回显服务器。 There is a service attached to the pod.有一个服务附加到 pod。 If I set the service type of this service to LoadBalancer, I can run my client as follows and get the echo from the cluster.如果我将这个服务的服务类型设置为LoadBalancer,我可以按如下方式运行我的客户端并从集群中获取回声。

python client.py --host <EXTERNAL-IP-OF-LOAD-BALANCER> --port <PORT>

Similar to the echo server, I have other TCP services in my cluster that serves other pods.与回显服务器类似,我的集群中有其他 TCP 服务为其他 pod 提供服务。 Currently I have set all of them to LoadBalancers.目前我已将它们全部设置为 LoadBalancers。 So, they have external IPs and listen for traffic on different ports.因此,它们具有外部 IP 并侦听不同端口上的流量。 However, I do not want to create LoadBalancers to all of these services.但是,我不想为所有这些服务创建 LoadBalancer。 How would I use the nginx to route the TCP traffic to different services based on the port numbers.我将如何使用 nginx 根据端口号将 TCP 流量路由到不同的服务。 If nginx cannot do this, are there other options that I can use to achieve this?如果 nginx 无法做到这一点,我是否可以使用其他选项来实现这一点?


UPDATE: Following the HangDu 's answer I created the following files.更新:按照HangDu的回答,我创建了以下文件。

apiVersion: v1
kind: ConfigMap
metadata:
  name: tcp-services
  namespace: default
data:
  9000: "default/echo-service:50000"

and

apiVersion: v1
kind: Service
metadata:
  name: ingress-nginx
  namespace: default
  labels:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
spec:
  type: LoadBalancer
  ports:
    - name: http
      port: 80
      targetPort: 80
      protocol: TCP
    - name: https
      port: 443
      targetPort: 443
      protocol: TCP
    - name: proxied-tcp-9000
      port: 9000
      targetPort: 9000
      protocol: TCP
  selector:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx

Then I used kubectl create -f <FILE_NAME> to create the config map and the service.然后我使用kubectl create -f <FILE_NAME>来创建配置映射和服务。 So I was hoping I could use the external IP of the newly created service and the port 9000 and run python client.py --host <EXTERNAL-IP-OF-LOAD-BALANCER> --port 9000 to run my echo client.所以我希望我可以使用新创建的服务的外部 IP 和端口 9000 并运行python client.py --host <EXTERNAL-IP-OF-LOAD-BALANCER> --port 9000来运行我的回显客户端。 However, I get a connection refused error when I do that.但是,当我这样做时,我收到连接被拒绝的错误。 Am I doing something wrong?难道我做错了什么?

I answered a similar question on another thread.我在另一个线程上回答了类似的问题。 How to use nginx ingress TCP service on different namespace 如何在不同的命名空间上使用 nginx 入口 TCP 服务

Basically, you can specify the port and backend for your service in configmap.基本上,您可以在 configmap 中为您的服务指定端口和后端。

The following is the link to the document.以下是该文档的链接。 https://github.com/kubernetes/ingress-nginx/blob/master/docs/user-guide/exposing-tcp-udp-services.md https://github.com/kubernetes/ingress-nginx/blob/master/docs/user-guide/exposing-tcp-udp-services.md

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

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