Kubernetes NodePort 外部无法访问。连接被拒绝

问题描述 投票:0回答:4

我正在尝试学习 Kubernetes。

我正在尝试使用 Minikube 来做到这一点,这就是我所做的:

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

2.) 为该特定节点服务器编写

Dockerfile

3.) 创建 Kubernetes

deployment

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

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

但是当我尝试以

NodePort
和 `: 的格式连接到
<NodePort>:<port>
时,会出现错误:

无法连接到192.168.39.192端口80:连接被拒绝

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

1.) server.js - 这里我只提到了

server.js
,relevent
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.)deployment.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 和容器也按预期工作。

node.js docker kubernetes minikube
4个回答
3
投票

造成这种情况的可能原因有多种。 第一:您使用的是本地IP还是minikube VM运行的IP?要验证,请使用

minikube ip
。 第二:
NodePort
服务想要使用带有标签
app: node-app-web
的pod,但您的pod只有标签
name: node-web-app
为了确保您假设的端口已被使用,请使用
minikube service list
检查请求的端口是否已分配。还要检查您的防火墙设置。


3
投票

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


1
投票

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

在您的 NodePort.yaml 选择器中是

app: node-app-web
,而在 deployment.yaml 中标签是
node-web-app


0
投票

添加查看标签和添加标签的方式。 给豆荚贴标签是我解决这个问题的方法。

kubectl get pods --show-labels -n mynamespace
 
 
 
kubectl label pod nginx-66bccddb68-8tjpk -n mynamespace app=webserver

© www.soinside.com 2019 - 2024. All rights reserved.