Kubernetes在同一子域中部署两个微服务,导致频繁和随机的404错误

问题描述

我们有一个Kubernetes部署,它由一个nodejs前端和一个Nginx后端组成。我们发现,这两种部署在Kubernetes中分别运行良好,但是当它们都部署到前端时,几乎50%的时间返回404请求。

很自然地假设我们的虚拟服务存在问题,但是基于vs / gateway的部署不足以引起问题的事实,似乎并非如此。看来,如果我们在后端部署其他不相关的映像,则前端将继续正常工作而不会出现404错误

该应用最初是通过JHipster生成的,我们手动分离了前端和后端组件。前端是nodejs,后端是Java / Nginx。该应用程序可在本地运行,但无法在k8s部署中失败。

此外,我们的Kubernetes部署在Rancher中。

实验似乎表明它与后端部署中的某些内容有关,因此我在下面包括了我们的后端deployement.yaml:

apiVersion: apps/v1
kind: Deployment
Metadata:
  name: ourapp-be-custom-mount
spec:
  revisionHistoryLimit: 3
  replicas: 1
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 25%
      maxSurge: 25%
  template:
    spec:
      containers:
        - name: ourapp-be-custom-mount
          image: "IMAGE_SET_BY_OVERLAYS_KUSTOMIZATION"
          envFrom:
            - configMapRef:
                name: ourapp-be-config
          ports:
          - name: http
            containerPort: 8080
          resources:
            limits:
              cpu: "0.5"
              memory: "2048Mi"
            requests:
              cpu: "0.1"
              memory: "64Mi"
          imagePullPolicy: IfNotPresent
          volumeMounts:
            - mountPath: /usr/share/h2/data
              name: ourapp-db-vol01-custom-mount

          securityContext:
            runAsNonRoot: true
            runAsUser: 1000
      imagePullSecrets:
        - name: regcred-nexus
      volumes:
      - name: ourapp-db-vol01-custom-mount
        persistentVolumeClaim:
          claimName: ourapp-db-pvc-volume01-custom-mount
      terminationGracePeriodSeconds: 30

解决方法

每个服务都需要指向另一个应用程序。您可以在Rancher中验证每个服务都指向不同的应用程序。检查您的Yaml。如果使用Kustomize,则commonLabels:app可以使您绊倒。确保它指向前端和后端的不同应用。