Fastify在Docker/Kubernetes上不起作用 [英] Fastify not working on Docker / Kubernetes

查看:17
本文介绍了Fastify在Docker/Kubernetes上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的应用程序,它返回"Hello World"字符串,它在本地运行得很好。正如您将从下面的应用程序代码中看到的,它在端口4000上运行。当我创建Docker映像并运行容器时,我无法从我的计算机上的localhost:4000访问它,但我可以看到Docker正确地访问了node index.js命令,并且应用程序正在运行,没有任何错误。

也尝试部署到Kubernetes集群,访问负载均衡IP得到ERR_EMPTY_RESPONSE。通过kubectl检查此应用程序后,我可以看到一切运行正常,图像已下载,Pod正在运行。

我很难理解我错过了什么,以及为什么它只在本地有效。

NodeJS APP

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);
  }
});

文档文件

FROM node:14.2-alpine

WORKDIR /app

COPY package.json yarn.lock /app/

RUN yarn

COPY . .

EXPOSE 4000

CMD ["node", "index.js"]

库伯内斯清单

---
# 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

推荐答案

首先,当我尝试您的代码时,我使用本地停靠工具进行了尝试,但行为完全相同,因此我预计这是因为fastify默认情况下只侦听localhost

docker build -t development-actions:latest .
docker run -it -p 4000:4000 development-actions:latest
在Docker内部,您应该明确提到'0.0.0.0',因为默认情况下fastify只监听localhost 127.0.0.1接口。要侦听所有可用的IPv4接口,应将该示例修改为侦听0.0.0.0,如下所示:

const server = require('fastify')({ logger: true })

server.get('/', (_request, reply) => {
   reply.status(200).send("Hello World");
});

server.listen(4000, '0.0.0.0', error => {
  if (error) {
    process.exit(1);
  }
});

其余部分应该相同。要在本地试用,您可以使用:

引用:

  1. https://www.fastify.io/docs/latest/Getting-Started/#your-first-server

这篇关于Fastify在Docker/Kubernetes上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆