Kubernetes

DigitalOcean 上的 Kubernetes 入口 502 錯誤網關

  • September 30, 2021

我正在嘗試在 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

現在我真的很困惑。我的配置有問題,但我不知道是什麼。

提前致謝。

{“message”:“應用程序正在執行:http: //127.0.0.1:3001”}

有你的問題;就像任何伺服器一樣,如果您只綁定到本地主機,那麼除了一台“機器”可以訪問該埠之外什麼都沒有。

你會想要

data:
 SERVER_IP: 0.0.0.0
 SERVER_PORT: '3001'

引用自:https://serverfault.com/questions/1079079