docker - apollo网关(federation)无法连接kubernetes环境中的服务

标签 docker kubernetes apollo apollo-federation

我有使用 graphql 的经验,但这是我第一次尝试 apollo federation 和 kubernetes。

我首先尝试使用 apollo 联合将我的整体式 graphql 服务器拆分为微服务。 它工作得很好(下面是工作代码)。现在我尝试在 kubernetes 集群中运行这些微服务,但 apollo 网关和其他后端服务之间不断出现网络问题。

从 apollo 网关服务器(当我尝试在 kubernetes 上运行它时),我收到此错误:

在 auth-cluster-ip-service 加载身份验证时遇到错误:仅支持绝对 URL

这是我将服务添加到apollo网关的部分:

const gateway = new ApolloGateway({
  serviceList: [
    { name: 'service1', url: process.env.SERVICE1_URL },
    { name: 'service2', url: process.env.SERVICE2_URL },
    { name: 'service3', url: process.env.SERVICE3_URL },
  ],
  buildService({ name, url }) {
    return new AuthenticatedDataSource({ name, url });
  }
});

首先我尝试使用以下环境变量

(.env)
SERVICE1_URL =http://localhost:3051
SERVICE2_URL =http://localhost:3052
SERVICE3_URL =http://localhost:3053

我只是在我的笔记本电脑上的本地主机上运行了 4 个节点应用程序,它就可以工作了!

现在是 kubernetes 部分。

下面是 apollo-gateway 的部署配置文件。我怀疑问题出在环境变量上。正如您所看到的,我在相应的环境变量值上有服务名称,而不是url。但据我所知,kubernetes master 会获取这个“url”(clusterIP 名称)并替换为相应 pod 的 IP 地址。所以应该没问题。

我练习kubernetes的时候效果很好。在我的实践中,我通过 clusterIP 名称连接到 redis 和 postgres pod。

apollo-gateway部署配置文件

apiVersion: apps/v1
kind: Deployment
metadata:
  name: gateway-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: gateway
  template:
    metadata:
      labels:
        component: gateway
    spec:
      containers:
        - name: gateway
          image:<docker-id>/gateway
          ports:
            - containerPort: 4000
          env:
            - name: ACCESS_TOKEN_SECRET
              value: fas69dO2Z15nkev6157
            - name: SERVICE1_URL
              value: service1-cluster-ip-service
            - name: SERVICE2_URL
              value: service1-cluster-ip-service
            - name: SERVICE3_URL
              value: service1-cluster-ip-service

示例 SERVICE cluster-ip 配置文件

apiVersion: v1
kind: Service
metadata:
  name: service1-cluster-ip-service
spec:
  type: ClusterIP
  selector:
    component: service1
  ports:
    - port: 3051
      targetPort: 3051

示例服务部署配置文件

apiVersion: apps/v1
kind: Deployment
metadata:
  name: service1-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: service1
  template:
    metadata:
      labels:
        component: service1
    spec:
      containers:
        - name: auth
          image:<docker-id>/service1
          ports:
            - containerPort: 3051

最佳答案

当错误消息显示“仅支持绝对 URL”时,这意味着配置的 URL 需要包含 http:// 方案部分以及主机名。您可以通过更改环境设置来解决此问题

- name: SERVICE1_URL
  value: 'http://service1-cluster-ip-service'

如果服务正在监听默认 HTTP 端口 80 以外的其他端口,您还需要在 URL 中包含该端口号。

关于docker - apollo网关(federation)无法连接kubernetes环境中的服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60883100/

相关文章:

javascript - GraphQL:不可为空的数组/列表

graphql - NestJS GraphQL 联合循环解析器

node.js - 如何配置 nginx docker 容器以识别不同端口中的本地主机应用程序?

kubernetes - 为什么client.Update(...)会忽略非原始值?

spring-boot - java.lang.RuntimeException:使用本地的Spring Boot容器api时连接被拒绝

spring-boot - 上游连接错误或在 header 之前断开/重置。重置原因 : connection failure. Spring Boot 和 java 11

kubernetes - 使用 Kubernetes : one works, 的多个卷挂载一个没有

vue.js - 结果缺少 allposts 属性

docker - 在拉取之前获取 Docker 镜像的大小?

mysql - AWS ECS - Rails 容器无法通过 mysqld.sock 连接链接容器 mysql