Kubernetes NodePort不能在外部访问。拒绝连接

嗨,我正在尝试学习kubernetes

我正在尝试使用minikube来做到这一点,以下是我所做的:

1。)使用Node

编写一个简单的服务器

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

3。)创建一个kubernetes deployment

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

5。)创建一个服务(NodePort类型)以暴露容器,以便我可以从外部(浏览器,curl)访问

但是当我尝试使用NodePort和`:格式连接到<NodePort>:<port>时,会出现错误无法连接到192.168.39.192端口80:连接被拒绝

这些是我按照上述步骤(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。)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

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

  

卷曲:(7)无法连接到192.168.39.192端口32555:连接被拒绝

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

A327619111 回答:Kubernetes NodePort不能在外部访问。拒绝连接

有几种可能的原因。 第一:您是使用本地IP还是运行minikube VM的IP?要进行验证,请使用minikube ip。 其次:NodePort服务希望使用标签为app: node-app-web的广告连播,但您的广告连播仅带有标签name: node-web-app 只是要确保使用您假定的端口,请用minikube service list检查是否已分配了请求的端口。还要检查您的防火墙设置。

,

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

本文链接:https://www.f2er.com/3045140.html

大家都在问