在同一集群中访问另一个 Pod 使用的端口是 Port、NodePort 还是 TargetPort?

huangapple go评论80阅读模式
英文:

Which port is used to access another Pod on the same Cluster? Port, NodePort or TargetPort

问题

我已经在同一个集群中创建了2个Pods。一个服务被初始化为

  1. kubectl create deployment my-web --image=nginx --port=80
  2. kubectl expose deployment my-web --target-port=80 --type=NodePort

据我理解,这创建了一个带有一个Pod my-web-<string> 的部署,并暴露了一个端口。使用 kubectl describe services my-web,我发现以下信息:

  1. Port: <unset> 80/TCP
  2. TargetPort: 80/TCP
  3. NodePort: <unset> 32004/TCP
  4. Endpoints: 10.244.0.10:80

测试Pod:

  1. kubectl run test-pod --image=nginx --restart=Never

这创建了另一个Pod,我尝试使用命令 curl 10.244.0.10:32004 来访问my-web Pod的nginx服务,但该请求超时。但是不知何故,当我使用 curl 10.244.0:80 时它却正常工作。这是为什么呢?我以为服务是在my-web Pod之外的端口32004上公开的?

请还告诉我从我的主机机器上通过curl访问my-web Pod所需的IP和端口。我正在MacOS上使用minikube运行集群。

感谢您的帮助!

英文:

I have created 2 pods within the same cluster. One service is initialized as

  1. kubectl create deployment my-web --image=nginx --port=80
  2. kubectl expose deployment my-web --target-port=80 --type=NodePort

to my understanding, this creates a deployment with one pod my-web-&lt;string&gt; and exposes a port. With kubectl describe services my-web, I find that the following information:

  1. Port: &lt;unset&gt; 80/TCP
  2. TargetPort: 80/TCP
  3. NodePort: &lt;unset&gt; 32004/TCP
  4. Endpoints: 10.244.0.10:80

testing pod:

  1. kubectl run test-pod --image=nginx --restart=Never

this creates another pod and I try to curl the nginx of my-web pod with the command curl 10.244.0.10:32004. That request times out. But somehow it works when I use curl 10.244.0:80. Why is that? I thought the service was exposed on port 32004 outside the my-web pod?

Please also let me know what IP and port to curl from my host machine to access the my-web pod. I am running the cluster from minikube on MacOS.

Thanks for the help!

I try to curl the nginx of my-web pod with the command curl 10.244.0.10:32004. That request times out. But somehow it works when I use curl 10.244.0:80.

答案1

得分: 1

NodePort用于在集群范围内访问服务。
您可以创建一个允许在节点端口上进行TCP流量的防火墙规则。创建一个允许在端口32004上进行TCP流量的防火墙规则。
在Ubuntu上,您可以执行如下操作:

sudo ufw allow 32004/tcp

然后使用以下命令检查端口状态:

sudo ufw status

一旦您确保端口已打开,您可以使用curl命令访问ip:port:

  1. curl http://10.244.0.10:32004

有关更多信息,请查阅Kubernetes官方文档

英文:

NodePort is used to access a service within the cluster scope.
You might create a firewall rule that allows TCP traffic on your node port. Create a firewall rule that allows TCP traffic on port 32004.
On Ubuntu you can do something like:

> sudo ufw allow 32004/tcp

And check port status with:

> sudo ufw status

Once you are sure the port is opened you can curl the ip:port

  1. curl http://10.244.0.10:32004

For further info check the Kubernetes official documentation.

huangapple
  • 本文由 发表于 2023年5月28日 02:28:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76348419.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定