【问题标题】:Resolving a url as a different url inside kubernetes pod将 url 解析为 kubernetes pod 中的不同 url
【发布时间】:2019-04-16 20:27:07
【问题描述】:

我的 pod (pod1) 在内部可以使用其服务连接到另一个 pod,如下所示:

pod2-service.namespace.svc.cluster.local

但是,我希望 pod1 使用未在 DNS 中注册的 abc.com 之类的 URL 连接到 pod2。基本上,我希望 pod1 将 abc.com 解析为 pod2-service.namespace.svc.cluster.local

我在这里查看hostAliases

https://kubernetes.io/docs/concepts/services-networking/add-entries-to-pod-etc-hosts-with-host-aliases/.

但是,它需要一个 IP。我如何在 Kubernetes 中做到这一点?

【问题讨论】:

    标签: kubernetes alias google-kubernetes-engine


    【解决方案1】:

    我认为你可以使用一个固定的 ip 作为你的 pod2 的服务 ip,然后在你的 hostalias 定义中使用这个 ip。

    【讨论】:

      【解决方案2】:

      有几件事:

      但是,Deployments 中的 Kubernetes 似乎并不原生支持 Pod 到 Pod,我猜这里的基本原理是 Pod 可以不断更改 IP 地址和名称。您可以使用 Pod default DNS entries,但 DNS 条目将根据分配给 pod 的 IP 地址而有所不同。

      我可以为 Deployments 想到的另一个解决方案是使用 Consulstub domains 之类的东西,然后在每个 pod 上添加一个 initContainer 或 consul agent sidecar 以向 consul 注册其 IP服务,每次 Pod 重启时都需要覆盖 Consul 中的 DNS 注册。

      如果您不想使用存根域,还可以选择使用Pod DNS Configs

      【讨论】:

        【解决方案3】:

        您可以在应用程序代码运行之前获取服务 ip 并附加到 pod1 中的/etc/hosts

        echo "$(getent hosts pod2-service.namespace.svc.cluster.local | awk '{ print $1 }') abc.com" >> /etc/hosts

        Notice: It is pretty hacky because you should guarantee service ip of pod2 is fixed. When service ip changed, pod1 will fail to reslove the host.

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-08-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-02-06
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多