【问题标题】:Kubernetes NodePort is not accessbile outside. Connection refusedKubernetes NodePort 无法在外部访问。拒绝连接
【发布时间】:2020-03-19 10:05:02
【问题描述】:

嗨,我正在努力学习kubernetes

我正在尝试使用minikube 执行此操作,以下是我所做的:

1.) 使用Node编写一个简单的服务器

2.) 为特定的Node 服务器写一个Dockerfile

3.) 创建一个kubernetesdeployment

4.) 创建服务(ClusterIP 类型)

5.) 创建一个服务(NodePort 类型)来公开容器,以便我可以从外部访问(浏览器、curl)

但是当我尝试使用<NodePort>:<port>和`:的格式连接到NodePort时,它给出了一个错误Failed to connect to 192.168.39.192 port 80: Connection refused

这些是我按照上述步骤 (1-5) 创建的文件。

1.) server.js - 这里只有我提到了server.js,相关的package.json 存在并且当我在本地运行服务器时它们按预期工作(没有在docker中部署它),我告诉这个以防你可能会问我的服务器是否正常工作,是的:)

'use strict';

const express = require('express');

// Constants
const PORT = 8080;
const HOST = '0.0.0.0';

// App
const app = express();
app.get('/', (req, res) => {
  res.send('Hello world\n');
});

app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);

2.) Dockerfile

FROM node:10

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm ci --only=production

# Bundle app source
COPY . .

EXPOSE 8080
CMD [ "npm", "start" ]

3.) 部署.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: node-web-app
spec:
  replicas: 2
  selector:
    matchLabels:
      name: node-web-app
  template:
    metadata:
      labels:
        # you can specify any labels you want here
        name: node-web-app
    spec:
      containers:
      - name: node-web-app
        # image must be the same as you built before (name:tag)
        image: banuka/node-web-app
        ports:
        - name: http
          containerPort: 8080
          protocol: TCP
        imagePullPolicy: Never
      terminationGracePeriodSeconds: 60

4.) clusterip.yaml

kind: Service
apiVersion: v1
metadata:
  labels:
    # these labels can be anything
    name: node-web-app-clusterip
  name: node-web-app-clusterip
spec:
  selector:
    app: node-web-app
  ports:
    - protocol: TCP
      port: 80
      # target is the port exposed by your containers (in our example 8080)
      targetPort: 8080

5.) NodePort.yaml

kind: Service
apiVersion: v1
metadata:
  labels:
    name: node-server-nodeport
  name: node-server-nodeport
spec:
  # this will make the service a NodePort service
  type: NodePort
  selector:
    app: node-app-web
  ports:
    - protocol: TCP
      # new -> this will be the port used to reach it from outside
      # if not specified, a random port will be used from a specific range (default: 30000-32767)
      nodePort: 32555
      port: 80
      targetPort: 8080

当我尝试从外面卷曲或使用我的网络浏览器时,它会出现以下错误:

curl: (7) 连接192.168.39.192端口32555失败:连接被拒绝

ps:pod 和容器也按预期工作。

【问题讨论】:

  • 第一个问题是你能运行你的 docker 镜像吗,在进入 kubernetes 之前,你应该首先尝试这样做。构建 docker 镜像并运行以确保您拥有正确的 docker 镜像,然后尝试将其放入 kubernetes 部署中。
  • 是的,现在它正在工作:) 谢谢
  • @BhagyaKolithaJayalath ,如果您可以将您的解决方案描述为答案,那就太好了,这样下一个堆栈用户可以轻松查看/找到它

标签: node.js docker kubernetes minikube


【解决方案1】:

这有几个可能的原因。 第一:您使用的是本地 IP 还是运行 minikube 虚拟机的 IP?验证使用minikube ip。 第二:NodePort 服务想要使用标签为 app: node-app-web 的 pod,但您的 pod 只有标签 name: node-web-app 只是为了确保使用您假设的端口,请使用minikube service list 检查请求的端口是否已分配。还要检查您的防火墙设置。

【讨论】:

  • 谢谢,使用 minikube IP 后就可以了。
【解决方案2】:

当我将错误的选择器写入 NodePort 服务规范时,我总是遇到同样的问题

【讨论】:

    【解决方案3】:

    Service 的选择器必须与 Pod 的标签匹配。

    在您的 NodePort.yaml 选择器中是 app: node-app-web ,而在 deployment.yaml 中标签是 node-web-app

    【讨论】:

      猜你喜欢
      • 2020-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-18
      • 1970-01-01
      • 2021-06-27
      • 1970-01-01
      • 2017-10-05
      相关资源
      最近更新 更多