简体   繁体   English

apollo gateway (federation) 无法连接到 kubernetes 环境中的服务

[英]apollo gateway (federation) fails to connect to services in kubernetes environment

I have experience with graphql but its my first time trying apollo federation and kubernetes.我有使用 graphql 的经验,但这是我第一次尝试阿波罗联邦和 kubernetes。

I first tried splitting my monolith graphql server to micro services using apollo federation.我首先尝试使用 apollo federation 将我的整体 graphql 服务器拆分为微服务。 It worked fine(below is the working code).它工作正常(下面是工作代码)。 Now I am trying to run these micro services in kubernetes cluster, but keep having network problems between the apollo gateway and the other backend services.现在我试图在 kubernetes 集群中运行这些微服务,但在 apollo 网关和其他后端服务之间一直存在网络问题。

From the apollo gateway server(when i try running it on kubernetes), I get this error:从 apollo 网关服务器(当我尝试在 kubernetes 上运行它时),我收到此错误:

Encountered error when loading auth at auth-cluster-ip-service: Only absolute URLs are supported

This is the part where I services are added to the apollo gateway:这是将 I 服务添加到 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 });
  }
});

First I tried, with the following environment variables首先我尝试,使用以下环境变量

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

I simply ran 4 node apps on localhost on my laptop and it works!我只是在我的笔记本电脑上的本地主机上运行了 4 个节点应用程序,它工作正常!

Now the kubernetes part.现在是 kubernetes 部分。

Below is the deployment config file for apollo-gateway.下面是 apollo-gateway 的部署配置文件。 I am suspecting that the problem lies within the environment variables.我怀疑问题出在环境变量中。 As you can see, instead of an url , I have the service name on the corresponding environment variables' values.正如您所看到的,我在相应的环境变量值上有service name ,而不是url But from what I have learned, kubernetes master will fetch this "url"(clusterIP name) and replace with the IP address of the corresponding pods.但是据我所知,kubernetes master 会获取这个“url”(clusterIP 名称)并替换为相应 pod 的 IP 地址。 So it should be fine.所以应该没问题。

It worked well when I practiced kubernetes.当我练习 kubernetes 时,它运行良好。 In my practice I was connecting to redis and postgres pods, via clusterIP name.在我的实践中,我通过 clusterIP 名称连接到 redis 和 postgres pod。

apollo-gateway deploymant config file apollo-gateway deploymant 配置文件

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

sample SERVICE cluster-ip config file示例 SERVICE cluster-ip 配置文件

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

sample SERVICE deployment config file示例 SERVICE 部署配置文件

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

When the error message says "only absolute URLs are supported", that means the configured URL needs to include the http:// scheme part as well as the host name.当错误消息显示“仅支持绝对 URL”时,这意味着配置的 URL 需要包含http://方案部分以及主机名。 You can fix this by changing your environment settings to include that您可以通过更改环境设置来解决此问题

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

If the Service is listening on something other than the default HTTP port 80 you'll also need to include that port number in the URL.如果服务正在侦听默认 HTTP 端口 80 以外的其他内容,您还需要在 URL 中包含该端口号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM