简体   繁体   English

如何从集群内访问 Kubernetes Ingress

[英]How to access Kubernetes Ingress from within a cluster

I have an AWS EKS Cluster with only public subnets (we don't need private subnets for security reasons and I really want to avoid the NAT data transfer charges).我有一个只有公有子网的 AWS EKS 集群(出于安全原因,我们不需要私有子网,我真的想避免 NAT 数据传输费用)。 In the cluster, we have serviceA and serviceB.在集群中,我们有 serviceA 和 serviceB。 Both are exposed via public-facing LB backed Ingress (Traefik).两者都通过面向公众的 LB 支持的 Ingress (Traefik) 公开。

So here is my problem: We have a configuration option 'serviceB_url' This config is both used by serviceA to access serviceB from INSIDE the cluster and to generate Links that should work from OUTSIDE the cluster.所以这是我的问题:我们有一个配置选项'serviceB_url' 这个配置既被serviceA用来从集群内部访问serviceB,也被用来生成应该从集群外部工作的链接。 So basically, I want the same URL to work for both inside and outside the cluster.所以基本上,我想要相同的 URL 在集群内部和外部工作。 The DNS points to the public-facing Loadbalancer IP and of course I can resolve the name from inside the Cluster. DNS 指向面向公众的负载均衡器 IP,当然我可以从集群内部解析名称。

But here is my problem: I cannot access it.但这是我的问题:我无法访问它。 As the Loadbalancer IP is public, the traffic leaves the VPC using the internet gateway and hits the Loadbalancer from the outside, using the nodes public IP, which is NOT whitelisted.由于负载均衡器 IP 是公共的,因此流量使用互联网网关离开 VPC,并使用未列入白名单的节点公共 IP 从外部到达负载均衡器。

My thoughts on this so far:到目前为止,我对此的看法:

  1. We had public and private subnets before.我们之前有公共子网和私有子网。 With a NAT gateway, we could simply whitelist the public IP of the NAT.使用 NAT 网关,我们可以简单地将 NAT 的公共 IP 列入白名单。 Although this worked, I don't think this is a clean solution because the traffik takes a quite unnecessary path.尽管这很有效,但我认为这不是一个干净的解决方案,因为 traffik 采取了一条非常不必要的路径。 Plus, as mentioned before, we want to get rid of the NAT gateway because of the quite high charges.另外,如前所述,由于费用很高,我们希望摆脱 NAT 网关。

  2. I am aware that Ingress is by design to expose to the OUTSIDE, and a Service should be used to expose to the INSIDE.我知道 Ingress 的设计目的是向 OUTSIDE 公开,应该使用 Service 向 INSIDE 公开。 But using the service, I lose my reverse proxy in the middle.但是使用该服务,我在中间丢失了我的反向代理。 Plus I wonder how this would work for a service that is picky about either the URL used in the request or if TLS is a requirement.另外,我想知道对于对请求中使用的 URL 或是否需要 TLS 挑剔的服务,这将如何工作。 Taking this thought further, I could imagine some sort of internally deployed reverse proxy service that does some URL rewrite magic.进一步考虑这个想法,我可以想象某种内部部署的反向代理服务会执行一些 URL 重写魔术。 But yeah, I already dislike this for the hackyness.但是,是的,我已经不喜欢这个了。

  3. In a classic (means more static) infrastructure, I would probably solve this by using /etc/hosts entries pointing to the private of the node running the service our use some custom DNS that is used internally.在经典的(意味着更静态的)基础架构中,我可能会通过使用 /etc/hosts 条目来解决这个问题,这些条目指向运行服务的节点的私有,我们使用一些内部使用的自定义 DNS。

  4. I read that I can use CoreDNS to rewrite URLs, eg.我读到我可以使用 CoreDNS 来重写 URL,例如。 foo.example.com to foo-internal.example.com. foo.example.com 到 foo-internal.example.com。 So I could just rewrite the external URL to serviceB.my-namespace.svc.cluster-domain.example.所以我可以将外部 URL 重写为 serviceB.my-namespace.svc.cluster-domain.example。 Once again, I wonder how this would work for apps picky with their URL and/or TLS.再一次,我想知道这对于使用 URL 和/或 TLS 的应用程序来说是如何工作的。 Would probably need to create a reverse proxy for that.可能需要为此创建一个反向代理。 And, again, sounds hacky to me.而且,再一次,对我来说听起来很老套。

  5. Of course I could go and change my app and split the config option into 'serviceB_url_internal' and 'serviceB_url_external'.当然,我可以 go 并更改我的应用程序并将配置选项拆分为“serviceB_url_internal”和“serviceB_url_external”。 I would just like to solve this withing Kubernetes somehow.我只想用 Kubernetes 以某种方式解决这个问题。

  6. In the end what I think I really want is a cluster wide DNS config that simply returns reverse proxies clusterIP instead of the ip of the internet-facing loadbalancer for the URL I want to access.最后,我认为我真正想要的是一个集群范围的 DNS 配置,它只返回反向代理 clusterIP 而不是面向互联网的负载均衡器的 ip,用于 ZE6B391A8D2C4D45902A23A8B6585703 想要访问。 This would solve the problem and I would not need any URL rewrites or other funky things.这将解决问题,我不需要任何 URL 重写或其他时髦的东西。 Can I configure this somehow using Ingress itself?我可以使用 Ingress 本身以某种方式配置它吗? Would be great to have at least an opt-in for something like this.至少可以选择加入这样的事情会很棒。 Or is there a service that does this?或者有没有这样的服务? (Would this actually work?) (这真的有用吗?)

If you've read this far, thank you:)如果你已经读到这里,谢谢你:)

I just wonder if I am missing something obvious here, and it's actually totally easy to configure.我只是想知道我是否在这里遗漏了一些明显的东西,而且它实际上很容易配置。 How could this be solved in a clean way?如何以干净的方式解决这个问题? Or is the idea of using the same URL to access a service from inside and outside the cluster just plain wrong?或者使用相同的 URL 从集群内部和外部访问服务的想法是完全错误的? I can imagine this is not that big of a deal for on-premise installation, because the 'public ip' of the nodes is well known and whitelisting is not a big issue there.我可以想象这对于本地安装来说没什么大不了的,因为节点的“公共 ip”是众所周知的,白名单在那里并不是一个大问题。

Adding the following rewrite to the CoreDNS configmap worked for me:将以下重写添加到 CoreDNS 配置映射对我有用:

rewrite name external.url.to.servicaA traefik-service.traefik-namespace.svc.cluster.local

Of course this comes with the downside of maintaining this entry in a seperate configmap.当然,在单独的 configmap 中维护这个条目当然也有不利的一面。 Would have preferred a solution with an annotation or CRD.会更喜欢带有注释或 CRD 的解决方案。

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

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