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

时间:2020-07-14 08:16:56

标签: docker kubernetes

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

  1. 从Yaml运行2个吊舱:
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. 该命令还运行着另一个容器: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. 我“ ssh”进入nginx独立版
  2. 我安装了curl(在nginx-standalone内部)
  3. 我尝试了以下操作(在nginx-standalone中):
    1. curl nginx-nodeport-效果很好,我得到了正确的答复
    2. curl nginx-nodeport:31000-不起作用,我得到curl: (7) Failed to connect to nginx-nodeport port 31000: Connection refused

我不明白为什么第二个命令没有返回成功的HTTP响应。我知道31000端口有效,因为我可以从主机PC上执行curl nginx-nodeport:31000。为什么从Nginx-standalone pod上不起作用?

1 个答案:

答案 0 :(得分:3)

这是预期的行为,因为nodePort 31000正在侦听节点网络接口,并且在pod的网络接口中不存在。如果要通过kubernetes服务从另一个容器访问一个容器,请使用clusterIP类型的服务而不是NodePort类型的服务。应该使用NodePort类型的服务来向kubernetes集群外部的使用者公开kubernetes容器。