apollo gateway(federation)在kubernetes环境下连接服务失败
apollo gateway (federation) fails to connect to services in kubernetes environment
我有使用 graphql 的经验,但这是我第一次尝试 apollo federation 和 kubernetes。
我首先尝试使用 apollo federation 将我的单体 graphql 服务器拆分为微服务。
它工作正常(下面是工作代码)。现在我正在尝试 运行 kubernetes 集群中的这些微服务,但在 apollo 网关和其他后端服务之间一直存在网络问题。
从 apollo 网关服务器(当我在 kubernetes 上尝试 运行 时),我得到这个错误:
Encountered error when loading auth at auth-cluster-ip-service: Only absolute URLs are supported
这是我服务添加到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 的部署配置文件。我怀疑问题出在环境变量中。如您所见,我在相应的环境变量值上使用了 service name
,而不是 url
。但据我了解,kubernetes master 将获取此 "url"(clusterIP 名称)并替换为相应 pods 的 IP 地址。所以应该没问题。
我练习kubernetes的时候效果很好。在我的实践中,我通过 clusterIP 名称连接到 redis 和 postgres pods。
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
示例服务集群 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
当错误消息显示 "only absolute URLs are supported" 时,这意味着配置的 URL 需要包括 http://
方案部分以及主机名。您可以通过更改环境设置来解决此问题,以包含
- name: SERVICE1_URL
value: 'http://service1-cluster-ip-service'
如果服务正在侦听默认 HTTP 端口 80 以外的其他端口,您还需要在 URL.
中包含该端口号
我有使用 graphql 的经验,但这是我第一次尝试 apollo federation 和 kubernetes。
我首先尝试使用 apollo federation 将我的单体 graphql 服务器拆分为微服务。 它工作正常(下面是工作代码)。现在我正在尝试 运行 kubernetes 集群中的这些微服务,但在 apollo 网关和其他后端服务之间一直存在网络问题。
从 apollo 网关服务器(当我在 kubernetes 上尝试 运行 时),我得到这个错误:
Encountered error when loading auth at auth-cluster-ip-service: Only absolute URLs are supported
这是我服务添加到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 的部署配置文件。我怀疑问题出在环境变量中。如您所见,我在相应的环境变量值上使用了 service name
,而不是 url
。但据我了解,kubernetes master 将获取此 "url"(clusterIP 名称)并替换为相应 pods 的 IP 地址。所以应该没问题。
我练习kubernetes的时候效果很好。在我的实践中,我通过 clusterIP 名称连接到 redis 和 postgres pods。
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
示例服务集群 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
当错误消息显示 "only absolute URLs are supported" 时,这意味着配置的 URL 需要包括 http://
方案部分以及主机名。您可以通过更改环境设置来解决此问题,以包含
- name: SERVICE1_URL
value: 'http://service1-cluster-ip-service'
如果服务正在侦听默认 HTTP 端口 80 以外的其他端口,您还需要在 URL.
中包含该端口号