【发布时间】: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