首页 文章

在GKE上使用Ingress进行服务路由

提问于
浏览
0

我们在GKE(Google Kubernetes Engine)上的Kubernetes集群中运行多项服务,并且无法使用Ingress配置路由 .

假设我们有 auth-serviceuser-service ,并希望通过以下网址访问它们: http://www.example.com/authhttp://www.example.com/user . 对这些URL的所有请求都应重定向到正确的服务并在内部路由( http://www.example.com/user/people - > http://user-service/people ) .

这些是我们的auth服务配置:

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: api-auth
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: api-auth
        tier: backend
        track: stable
    spec:
      containers:
        - name: api-auth
          image: "<our-image>"
          ports:
            - name: http
              containerPort: 9000
          livenessProbe:
            httpGet:
              path: /health
              port: 9000
            initialDelaySeconds: 180
            timeoutSeconds: 5
          readinessProbe:
            httpGet:
              path: /health
              port: 9000
            initialDelaySeconds: 180
            timeoutSeconds: 5
---
kind: Service
apiVersion: v1
metadata:
  name: auth-service
  labels:
    app: api-auth
spec:
  type: NodePort
  selector:
    app: api-auth
    tier: backend
  ports:
  - port: 80
    targetPort: 9000

在内部,该服务在端口9000上运行在Tomcat上,这部分工作正常 .

问题在于我们的Ingress配置:

kind: Ingress
apiVersion: extensions/v1beta1
metadata:
  name: auth-ingress
  annotations:
    kubernetes.io/ingress.global-static-ip-name: <our-static-api>
    kubernetes.io/ingress.class: "gce"
  labels:
    app: api-auth
spec:
  rules:
  - http:
      paths:
      - path: /auth
        backend:
          serviceName: auth-service
          servicePort: 80
      - path: /auth/*
        backend:
          serviceName: auth-service
          servicePort: 80
      - path: /user
        backend:
          serviceName: user-service
          servicePort: 80
      - path: /user/*
        backend:
          serviceName: user-service
          servicePort: 80

每当我以下列方式访问我们的静态api(我们现在称之为 example.com )时: http://www.example.com/auth ,我得到502 - Bad gateway . 运行 kubectl describe ingress 说,我们服务的 Health 状况是 unknown .

我正在运行我的想法可能导致这种奇怪的行为 . 有人能指出我正确的方向吗?

2 回答

  • 2

    您的 Health 检查将reflect your readiness probes . 运行状况检查需要使用nodePort端口,因为请求来自Load Balancer . 如果您的运行状况检查是针对端口9000,则请求将无法通过,因为该节点上的端口未处于活动状态 .

    确保您的LB运行状况检查针对正确的端口(在30000范围内),并且目标路径将以200响应,否则您的运行状况检查将继续失败并且您将继续获得502错误

  • 0

    你在Slack上提到过服务是Spring Boot应用程序 . 它可能与此无关,但您需要确保入口路径与Spring Boot应用程序的上下文匹配,即 . 即如果您的入口路径是 /user ,则必须使用 server.context-path=/user 配置您的应用程序上下文 . 然后可以在 http://user-service/user 下访问该服务 .

相关问题