简体   繁体   English

在 kubernetes 上将主机作为 localhost 公开节点 js 应用程序

[英]Expose node js app with host as localhost on kubernetes

I have spent whole day looking for an answer why my node js app is not reachable from the internet when I run it on Kubernetes with loadbalancer as a service type.我花了一整天的时间寻找答案,为什么当我在 Kubernetes 上使用负载均衡器作为服务类型运行我的 node js 应用程序时无法从 Internet 访问它。 The solution was to change host from localhost to 0.0.0.0 in node js server app definition, but I still don't know why?解决方案是在node js server app定义中将host从localhost改为0.0.0.0,但是我还是不知道为什么?

I hope this saves one's time in the future.我希望这可以节省未来的时间。

When you say that you set the host I guess you mean what the app is told to listen on in the listen function like express app server .当你说你设置了主机时,我猜你的意思是应用程序被告知在诸如express app server 之类的监听功能中监听什么 listen all intefaces instead of localhost only You can bind to a particular address or leave it open to all by using the 0.0.0.0 mask (the zeros function as a mask for matching rather than a true IP address - https://stackoverflow.com/a/20778887/9705485 ). 侦听所有接口而不是仅 localhost您可以绑定到特定地址或使用 0.0.0.0 掩码(零函数作为匹配掩码而不是真正的 IP 地址 - https://stackoverflow.com /a/20778887/9705485 )。

I imagine you had your app running fine locally and were able to access it from your host machine with your localhost configuration.我想您的应用程序在本地运行良好,并且能够使用 localhost 配置从主机访问它。 This would be because your local machine would be accessing it in a way that conforms to the mask.这是因为您的本地计算机将以符合掩码的方式访问它。 Your config was effectively saying only localhost can access this app.您的配置实际上是说只有 localhost 可以访问此应用程序。 So when you ported to Kubernetes then your machine has to go over a network and the incoming connection was then your machine's external/public IP address, which then did not match the localhost mask.因此,当您移植到 Kubernetes 时,您的机器必须通过网络,传入的连接是您机器的外部/公共 IP 地址,该地址与本地主机掩码不匹配。 So to get it to work you changed the mask to make it open to all IPs.因此,为了使其正常工作,您更改了掩码以使其对所有 IP 开放。

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

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