首页 > 解决方案 > 将 Apollo Server 应用程序部署到 GKE 时出现 502 Bad Gateway

问题描述

我正在尝试将我的 Apollo Server 应用程序部署到我的 GKE 集群。但是,当我访问我的站点的静态 IP 时,我收到 502 Bad Gateway 错误。我能够让我的客户以类似的方式正确部署,所以我不确定我做错了什么。我的部署日志似乎显示服务器正常启动。但是,我的入口表明我的服务不健康,因为它似乎没有通过健康检查。

这是我的deployment.yml

apiVersion: apps/v1

kind: Deployment

metadata:
  name: <DEPLOYMENT_NAME>
  labels:
    app: <DEPLOYMENT_NAME>

spec:
  replicas: 1
  selector:
    matchLabels:
      app: <POD_NAME>
  template:
    metadata:
      name: <POD_NAME>
      labels:
        app: <POD_NAME>
    spec:
      serviceAccountName: <SERVICE_ACCOUNT_NAME>
      containers:
        - name: <CONTAINER_NAME>
          image: <MY_IMAGE>
          imagePullPolicy: Always
          ports:
            - containerPort: <CONTAINER_PORT>
        - name: cloud-sql-proxy
          image: gcr.io/cloudsql-docker/gce-proxy:1.17
          command:
            - '/cloud_sql_proxy'
            - '-instances=<MY_PROJECT>:<MY_DB_INSTANCE>=tcp:<MY_DB_PORT>'
          securityContext:
            runAsNonRoot: true

我的服务.yml

apiVersion: v1

kind: Service

metadata:
  name: <MY_SERVICE_NAME>
  labels:
    app: <MY_SERVICE_NAME>
  annotations:
    cloud.google.com/neg: '{"ingress": true}'

spec:
  type: NodePort
  ports:
    - protocol: TCP
      port: 80
      targetPort: <CONTAINER_PORT>

  selector:
    app: <POD_NAME>

还有我的 ingress.yml

apiVersion: networking.k8s.io/v1beta1

kind: Ingress

metadata:
  name: <INGRESS_NAME>
  annotations:
    kubernetes.io/ingress.global-static-ip-name: <CLUSTER_NAME>
    networking.gke.io/managed-certificates: <CLUSTER_NAME>

spec:
  rules:
    - http:
        paths:
          - path: /*
            backend:
              serviceName: <SERVICE_NAME>
              servicePort: 80

任何想法是什么导致了这个失败?

标签: kubernetesgoogle-kubernetes-engineapollokubernetes-ingressapollo-server

解决方案


使用 Apollo Server,您需要进行健康检查以查看正确的端点。因此,将以下内容添加到容器下的 deployment.yml 中。

livenessProbe:
  initialDelaySeconds: 30
  periodSeconds: 30
  httpGet:
    path: '/.well-known/apollo/server-health'
    port: <CONTAINER_PORT>
readinessProbe:
  initialDelaySeconds: 30
  periodSeconds: 30
  httpGet:
    path: '/.well-known/apollo/server-health'
    port: <CONTAINER_PORT>

推荐阅读