是否可以将主机重定向到Kubernetes中的服务路径? [英] Can a host be redirected to a service path in Kubernetes?

查看:82
本文介绍了是否可以将主机重定向到Kubernetes中的服务路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将主机地址与服务路径进行匹配,例如,假设我有一个Nginx Pod服务于以下站点:site1和site2。让我们考虑一下名为 my-nginx-service 的服务,该服务将我的两个站点作为路径提供服务:

I'm trying to try to match a host address to a service path, as an example, let's think i have an nginx pod servying to sites: site1 and site2. Let's think about a service called my-nginx-service which services my two sites as paths:

-my- nginx-service(178.123.55.37:80)
-/ site1
-/ site2

有一种方法可以将其映射到以下内容:

There exists a way to map it to something like:

-主机:site-one.mydomain.com
http:
路径:
-后端:
serviceName:nginx-service
servicePort:80
servicePath:/ site2
-主机:site-two.mydomain.com
http:
路径:
-后端:
serviceName:nginx-service
servicePort:80
servicePath:/ site2

?我没有在文档中找到任何有关它的信息。也许我对它们不太了解。

? I have not found anything about it in docs. Or maybe I've not understood well them.

非常感谢您的宝贵时间!

Thank you very much for your time!

推荐答案

您尝试做的事情(如果我理解正确的话)是不可能的,因为服务不知道主机名。您可以通过修改Pod中的Nginx配置来达到所需的目标,但这不是K8s的方法。

What you are trying to do (if I understood right) is impossible, because services are unaware of host names. You might be able to reach what you want by modifying nginx configuration within the pod, but that would not be a K8s approach.

您需要做的是添加一个入口资源。入口知道主机名。它们在服务之间进行负载平衡。因此,/ site1一个将转到一项服务,/ site2将转到另一项服务。

What you need to do is to add an ingress resource. Ingresses are aware of host names. They do load balancing between services. So, /site1 one would go to one service, and /site2 to the other one.

入口应该是这样的:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress
spec:
  rules:
  - host: site-one.mydomain.com
    http:
      paths:
      - backend:
          serviceName: nginx-service1
          servicePort: 80
  - host: site-two.mydomain.com
    http:
      paths:
      - backend:
          serviceName: nginx-service2
          servicePort: 80

如您所见,site1和site2将在不同的部署中运行,针对它们的服务不同。 Ingress(即L7负载平衡器)将能够检查主机名并将请求转发到正确的服务,该服务最终会到达正确的Pod。

As you can see site1 and site2 would be running in different deployments, with different services targeting them. Ingress, which is L7 Load Balancer would be able to check the host name and forward the request to the right service, which in the end would hit the right pod.

您还可以向每个主机添加路径,因此site-one.mydomain.com/path1、site-one.mydomain.com/path2和site-two.mydomain.com/path1(例如)将转发到不同的服务。

You can also add paths to each host, so site-one.mydomain.com/path1, site-one.mydomain.com/path2 and site-two.mydomain.com/path1 (for example), would be forwarded to different services.

Ingress yaml文件如下所示:

The Ingress yaml file would look like this:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress
spec:
  rules:
  - host: site-one.mydomain.com
    http:
      paths:
      - path: /path1
        backend:
          serviceName: nginx-service1
          servicePort: 80
      - path: /path2
        backend:
          serviceName: nginx-service2
          servicePort: 8080
      - path: /
        backend:
          serviceName: nginx-service3
          servicePort: 80
  - host: site-two.mydomain.com
    http:
      paths:
      - path: /path1
        backend:
          serviceName: nginx-service1
          servicePort: 80

我知道这不是您要问的,但这是正确的方法实现您想要的。

I know this is not what you exactly are asking, but this is the right way to achieve what you want.

这篇关于是否可以将主机重定向到Kubernetes中的服务路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆