我有一个非常简单的应用程序,返回“Hello World”字符串,在本地运行良好。如下面的应用程序代码所示,它在端口4000
上运行。当我创建Docker镜像并运行容器时,无法从我的计算机上的localhost:4000
访问它,但我可以看到Docker正确执行了node index.js
命令,并且应用程序没有任何错误。
我还尝试将其部署到Kubernetes集群,当我访问负载均衡器IP时,我收到ERR_EMPTY_RESPONSE
。通过kubectl
检查此应用程序后,我可以看到一切正常,镜像已下载并且Pod正在运行。
我很难理解我错过了什么以及为什么它只在本地工作。
NodeJS应用程序
import fastify from 'fastify';
const server = fastify();
server.get('/', (_request, reply) => {
reply.status(200).send("Hello World");
});
server.listen(4000, error => {
if (error) {
process.exit(1);
}
});
Dockerfile
FROM node:14.2-alpine
WORKDIR /app
COPY package.json yarn.lock /app/
RUN yarn
COPY . .
EXPOSE 4000
CMD ["node", "index.js"]
Kubernetes清单
---
# Load balancer
apiVersion: v1
kind: Service
metadata:
name: development-actions-lb
annotations:
service.beta.kubernetes.io/do-loadbalancer-name: "development-actions-lb"
service.beta.kubernetes.io/do-loadbalancer-algorithm: "round_robin"
spec:
type: LoadBalancer
selector:
app: development-actions
ports:
- name: http
protocol: TCP
port: 80
targetPort: 4000
---
# Actions deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: development-actions
spec:
replicas: 1
selector:
matchLabels:
app: development-actions
template:
metadata:
labels:
app: development-actions
spec:
containers:
- image: registry.digitalocean.com/myapp/my-image:latest
name: development-actions
ports:
- containerPort: 4000
protocol: TCP
imagePullSecrets:
- name: registry-myapp