英文:
Which port is used to access another Pod on the same Cluster? Port, NodePort or TargetPort
问题
我已经在同一个集群中创建了2个Pods。一个服务被初始化为
kubectl create deployment my-web --image=nginx --port=80
kubectl expose deployment my-web --target-port=80 --type=NodePort
据我理解,这创建了一个带有一个Pod my-web-<string>
的部署,并暴露了一个端口。使用 kubectl describe services my-web
,我发现以下信息:
Port: <unset> 80/TCP
TargetPort: 80/TCP
NodePort: <unset> 32004/TCP
Endpoints: 10.244.0.10:80
测试Pod:
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
kubectl create deployment my-web --image=nginx --port=80
kubectl expose deployment my-web --target-port=80 --type=NodePort
to my understanding, this creates a deployment with one pod my-web-<string>
and exposes a port. With kubectl describe services my-web
, I find that the following information:
Port: <unset> 80/TCP
TargetPort: 80/TCP
NodePort: <unset> 32004/TCP
Endpoints: 10.244.0.10:80
testing pod:
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:
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
curl http://10.244.0.10:32004
For further info check the Kubernetes official documentation.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论