我正在尝试在 DigitalOcean 上部署一个NestJS
应用程序,并且我已按照本教程进行操作,但我总是从.Kubernetes
nginx-ingress-controller
那是我的deployment.yaml
---
apiVersion: v1
kind: Service
metadata:
name: nestjs-api
spec:
ports:
- port: 80
targetPort: 3001
selector:
app: nestjs-api
---
# Create nestjs-api
apiVersion: apps/v1
kind: Deployment
metadata:
name: nestjs-api
labels:
app: nestjs-api
spec:
replicas: 1
selector:
matchLabels:
app: nestjs-api
template:
metadata:
labels:
app: nestjs-api
spec:
containers:
- name: nestjs-api
image: registry.digitalocean.com/nestjs-registry/nestjs-api
ports:
- containerPort: 3001
envFrom:
- secretRef:
name: api-env
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nestjs-ingress
spec:
rules:
- host: api.mydomain.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nestjs-api
port:
number: 80
那是 Docker 镜像registry.digitalocean.com/nestjs-registry/nestjs-api
FROM node:14-alpine3.14 AS BUILD_IMAGE
RUN apk update && apk add yarn curl bash make && rm -rf /var/cache/apk/*
RUN curl -sfL https://install.goreleaser.com/github.com/tj/node-prune.sh | bash -s -- -b /usr/local/bin
WORKDIR /usr/src/app
# install dependencies
RUN yarn --frozen-lockfile
COPY . .
RUN yarn install
RUN yarn build
RUN npm prune --production
RUN /usr/local/bin/node-prune
FROM node:14-alpine3.14
USER 1000
RUN mkdir -p /home/node/app/
RUN mkdir -p /home/node/app/node_modules
RUN mkdir -p /home/node/app/dist
RUN chown -R 1000:1000 /home/node/app
RUN chown -R 1000:1000 /home/node/app/node_modules
RUN chown -R 1000:1000 /home/node/app/dist
WORKDIR /home/node/app
COPY --from=BUILD_IMAGE /usr/src/app/dist /home/node/app/dist
COPY --from=BUILD_IMAGE /usr/src/app/node_modules /home/node/app/node_modules
EXPOSE 3001
ENTRYPOINT ["node"]
CMD ["/home/node/app/dist/main.js"]
这是我的日志nginx-ingress-controller
2021/09/29 18:37:12 [error] 590#590: *147263 connect() failed (111: Connection refused) while connecting to upstream, client: MY_HOME_IP, server: api.mydomain.com, request: "GET / HTTP/1.1", upstream: "http://10.244.0.229:3001/", host: "api.mydomain.com"
2021/09/29 18:37:12 [error] 590#590: *147263 connect() failed (111: Connection refused) while connecting to upstream, client: MY_HOME_IP, server: api.mydomain.com, request: "GET / HTTP/1.1", upstream: "http://10.244.0.229:3001/", host: "api.mydomain.com"
2021/09/29 18:37:12 [error] 590#590: *147263 connect() failed (111: Connection refused) while connecting to upstream, client: MY_HOME_IP, server: api.mydomain.com, request: "GET / HTTP/1.1", upstream: "http://10.244.0.229:3001/", host: "api.mydomain.com"
MY_HOME_IP - - [29/Sep/2021:18:37:12 +0000] "GET / HTTP/1.1" 502 150 "-" "PostmanRuntime/7.28.4" 204 0.000 [default-nestjs-api-80] [] 10.244.0.229:3001, 10.244.0.229:3001, 10.244.0.229:3001 0, 0, 0 0.000, 0.000, 0.000 502, 502, 502 a54bfdae6e0b77bf894e53d8ac8fa29e
以下是一些输出kubectl
$ kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nestjs-api-6bcccbdbd5-zmdqg 1/1 Running 0 61m 10.244.0.238 api-wn5e3n2u8-u3j8q <none> <none>
$ kubectl get service -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
nestjs-api ClusterIP 10.245.37.142 <none> 80/TCP 3h app=nestjs-api
kubernetes ClusterIP 10.245.0.1 <none> 443/TCP 5d6h <none>
$ kubectl get pods -n ingress-nginx -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
ingress-nginx-admission-create-psdn2 0/1 Completed 0 6h34m 10.244.0.251 api-wn5e3n2u8-u3j8q <none> <none>
ingress-nginx-admission-patch-x8vvs 0/1 Completed 1 6h34m 10.244.0.252 api-wn5e3n2u8-u3j8q <none> <none>
ingress-nginx-controller-68649d49b8-bj7vp 1/1 Running 0 6h34m 10.244.0.175 api-wn5e3n2u8-u3j8q <none> <none>
更新
我的日志main.js
显示该应用正在侦听端口3001
{"message":"Application is running on: http://127.0.0.1:3001"}
来自以下代码
// Get Server IP and PORT from configuration
const ip = process.env.SERVER_IP;
const port = parseInt(process.env.SERVER_PORT, 10);
// Start server
await app.listen(port, ip);
logger.log(`Application is running on: ${await app.getUrl()}`);
logger.log(`Environment: ${environment}`);
这些是api-env
在 k8s 上作为 Secret 保存的变量。
SERVER_IP: 127.0.0.1
SERVER_PORT: 3001
无论如何,正如@mdaniel 所建议的那样,我已将其添加到我和 Pod 崩溃livenessProbe
的规范中。Deployment
spec:
containers:
- name: nestjs-api
image: registry.digitalocean.com/nestjs-registry/nestjs-api
ports:
- containerPort: 3001
livenessProbe:
httpGet:
port: 3001
path: '/'
envFrom:
- secretRef:
name: api-env
现在我真的很困惑。我的配置有问题,但我不知道是什么。
提前致谢。
有你的问题;就像任何服务器一样,如果您只绑定到本地主机,那么除了一台“机器”可以访问该端口之外什么都没有。
你会想要