无法使用 NodePort 服务从另一个 Pod 访问 Pod



我有一个非常简单的设置。我在电脑上使用Docker Desktop Kubernetes功能运行Kubernete。

  1. 有两个吊舱从山上运行:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
labels:
app: my-nginx
spec:
replicas: 2
selector:
matchLabels:
app: my-nginx
template:
metadata:
labels:
app: my-nginx
spec:
containers:
- name: my-nginx
image: nginx:alpine
  1. 有另一个pod从命令运行:kubectl run nginx-standalone --image nginx:alpine

  2. 有一项来自yaml的服务:

apiVersion: v1
kind: Service
metadata:
name: nginx-nodeport
spec:
type: NodePort
selector:
app: my-nginx
ports:
- port: 80
targetPort: 80
nodePort: 31000

基本上,该服务是";连接的";由于标签选择器,仅适用于来自yaml部署的pod。

我在做什么:

  1. I";ssh";进入nginx单机版
  2. 我安装了curl(nginx单机版内部(
  3. 我尝试了以下操作(在nginx单机版中(:
    1. curl nginx-nodeport-运行良好,我得到了正确的响应
    2. curl nginx-nodeport:31000-不工作,我得到curl: (7) Failed to connect to nginx-nodeport port 31000: Connection refused

我不明白为什么第二个命令没有返回成功的HTTP响应。我知道31000端口可以工作,因为我可以在主机上执行curl nginx-nodeport:31000。为什么它不能在nginx独立吊舱上工作?

这是预期行为,因为nodePort31000正在节点网络接口上侦听,而在pod的网络接口中不存在。如果您想通过kubernetes服务从另一个pod访问一个pod,请使用clusterIP类型的服务,而不是NodePort类型的服务。NodePort类型的服务应该用于向kubernetes集群之外的消费者公开kubernetespod。

最新更新