我在运行 NodeJS 服务器的 GCP 上有一个集群。此服务器在本地运行良好,但是当我向路由发送帖子时,该服务器会停止,没有任何消息。这篇文章应该使用 FCM 向我的一些用户发送一条推送消息。我的数据库是Cloud Firestore。
豆荚日志:
Not sending to xxxxxxxxxxxxxxx
Not sending to xxxxxxxxxxxxxyx
app@1.0.0 prestart /opt/app
tsc
app@1.0.0 start /opt/app
node src/index.js
Dockerfile:
FROM node:11.15-alpine
# install deps
ADD package.json /tmp/package.json
RUN apk update && apk add yarn python g++ make && rm -rf /var/cache/apk/*
RUN cd /tmp && npm install
# Copy deps
RUN mkdir -p /opt/app && cp -a /tmp/node_modules /opt/app
# Setup workdir
WORKDIR /opt/app
COPY . /opt/app
# run
EXPOSE 3000
CMD ["npm", "start"]
Kubernetes.yaml.tpl
apiVersion: apps/v1
kind: Deployment
metadata:
name: app
labels:
app: app
spec:
replicas: 1
selector:
matchLabels:
app: app
template:
metadata:
labels:
app: app
spec:
containers:
- name: app
env:
- name: var1
value: value1
- name: var2
value: value2
- name: var3
value: value3
- name: var4
value: value4
- name: var5
value: value5
- name: var6
value: value6
image: gcr.io/${PROJECT_ID}/app:COMMIT_SHA
ports:
- containerPort: 3000
livenessProbe:
httpGet:
path: /alive
port: 3000
initialDelaySeconds: 30
readinessProbe:
httpGet:
path: /alive
port: 3000
initialDelaySeconds: 30
timeoutSeconds: 1
---
apiVersion: networking.gke.io/v1beta1
kind: ManagedCertificate
metadata:
name: app
spec:
domains:
- myDomain.com.br
---
apiVersion: v1
kind: Service
metadata:
name: app
spec:
type: NodePort
selector:
app: app
ports:
- protocol: TCP
port: 80
targetPort: 3000
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: app
annotations:
kubernetes.io/ingress.global-static-ip-name: "00.000.000.000"
networking.gke.io/managed-certificates: app
spec:
backend:
serviceName: app
servicePort: 80
我正在调用的函数:
var query = tokens;
const getTokens = (
doc: FirebaseFirestore.QueryDocumentSnapshot
) => {
// Get user token and send push
}
const canSend = (user: User): boolean => {
// Apply business logic to check if the user will receive a push
}
let allUsers: FirebaseFirestore.QuerySnapshot = userdata;
let allGroups: FirebaseFirestore.QuerySnapshot = groups;
await this.asyncForEach(
query.docs,
async (doc: FirebaseFirestore.QueryDocumentSnapshot) => {
let userDoc: User;
allUsers.docs.filter(
(userDoc) => userDoc.data()['userId'] === doc.data()['id']
).forEach((user: any) => {
userDoc = new User(user);
});
if (userDoc) {
if (canSend(userDoc)) {
console.log(`Sending to: ${userDoc.id}`);
await getTokens(doc);
} else {
console.log(`Not sending to: ${doc.data()['id']} `);
}
} else {
console.log(`${doc.data()['id']} Has no document`);
}
}
);
console.log('Finished');
编辑1
我刚刚注意到当我的服务器发送大量请求或大量小请求时会发生这种情况
编辑 2
kubectl get events
返回No resources found.
正如 OP 所确认的那样,问题出在livenessProbe
失败的到期超时上,这导致了 pod 终止。
我还建议不要完全删除探测器,而是增加超时值(探测器超时的秒数(。默认为 1 秒(,比方说,在您的部署中最多 3-5 秒 yaml
timeoutSeconds: 5
有关配置探测器的详细信息