无法使用 DNS 为 kubernetes 中的服务从另一个服务调用一个服务
Failed to invoke one service from another service using DNS for services in kubernetes
我已经为 Kubernetes 设置了一个 AWS kops 集群,我有多个微服务,每个应用程序都需要相互交互。
场景:我的 ta2carbon 应用程序尝试通过服务 (dns) 名称调用 ta1carbon 应用程序中的函数。
结果:尝试访问端口 80(但已配置端口 -3000)失败并出现超时错误
我的 nodejs 应用程序控制台日志,
apiUrl: http://ta1carbon/api/app1/app1Func2
{ Error: connect ETIMEDOUT 100.66.7.165:80
at Object._errnoException (util.js:992:11)
at _exceptionWithHostPort (util.js:1014:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1186:14)
code: 'ETIMEDOUT',
errno: 'ETIMEDOUT',
syscall: 'connect',
address: '100.66.7.165',
port: 80 }
当我尝试在 ta2carbon pod 中 curl 我的 ta1carbon 应用程序时,curl 的相同错误日志。
root@ta2carbon-5fdcfb97cc-8j4nl:/home/appHome# curl -i http://ta1carbon/api/app1/app1Func2
curl: (7) Failed to connect to ta1carbon port 80: Connection timed out
但我在 service.yaml 中定义的端口是 3000 而不是 80!
下面是两个微服务的 yml 配置。
ta1carbon 服务 yaml
apiVersion: v1
kind: Service
metadata:
name: ta1carbon
labels:
app: ta1carbon
spec:
ports:
- port: 3000
targetPort: 3000
type: ClusterIP
selector:
app: ta1carbon
ta2carbon 服务 yaml
apiVersion: v1
kind: Service
metadata:
name: ta2carbon
labels:
app: ta2carbon
spec:
ports:
- port: 3001
targetPort: 3001
type: ClusterIP
selector:
app: ta2carbon
以下是 ta1carbon 和 ta2 carbon 的服务详情。
kubectl describe service ta1carbon
Name: ta1carbon
Namespace: default
Labels: app=ta1carbon
Annotations: <none>
Selector: app=ta1carbon
Type: ClusterIP
IP: 100.66.7.165
Port: <unset> 3000/TCP
TargetPort: 3000/TCP
Endpoints: 100.96.1.13:3000
Session Affinity: None
Events: <none>
kubectl describe service ta2carbon
Name: ta2carbon
Namespace: default
Labels: app=ta2carbon
Annotations: <none>
Selector: app=ta2carbon
Type: ClusterIP
IP: 100.67.129.126
Port: <unset> 3001/TCP
TargetPort: 3001/TCP
Endpoints: 100.96.1.12:3001
Session Affinity: None
Events: <none>
所以根据我的观察,url http://ta1carbon/api/app1/app1Func2
服务 dns ta1carbon 正在解析为 100.67.24.69:80 导致超时。
但是,如果我从 ta2carbon pod 中 curl 进入 100.67.24.69:3000,我会得到一个 成功响应
此外,如果我更改服务 yaml - 端口:80 并再次部署和测试,我会得到 成功响应
我发现 kubernetes 中的这种行为很奇怪,不确定是天气还是环境问题。
我的查询是-
为什么将服务 ta1carbon 解析为 100.67.24.69:80 并超时,而端口本应为 3000!
如有任何意见,我们将不胜感激。请让我知道这里缺少什么。
DNS 将域名解析为 IP 地址,而不是 IP 地址 + 端口。
有两种可能的解决方案:
修改您的应用程序源以向 http://ta1carbon:3000
发出 API 请求
将 ta1carbon
服务上的 port
设置为 80
。
我建议选择选项 2。在这种情况下,您将利用 Kubernetes 服务的强大功能。 Kubernetes 将在端口 80 上公开服务,但会向支持端口 3000 上的服务的 pods 发送请求(因为 targetPort: 3000
)。
我已经为 Kubernetes 设置了一个 AWS kops 集群,我有多个微服务,每个应用程序都需要相互交互。
场景:我的 ta2carbon 应用程序尝试通过服务 (dns) 名称调用 ta1carbon 应用程序中的函数。
结果:尝试访问端口 80(但已配置端口 -3000)失败并出现超时错误
我的 nodejs 应用程序控制台日志, apiUrl: http://ta1carbon/api/app1/app1Func2
{ Error: connect ETIMEDOUT 100.66.7.165:80
at Object._errnoException (util.js:992:11)
at _exceptionWithHostPort (util.js:1014:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1186:14)
code: 'ETIMEDOUT',
errno: 'ETIMEDOUT',
syscall: 'connect',
address: '100.66.7.165',
port: 80 }
当我尝试在 ta2carbon pod 中 curl 我的 ta1carbon 应用程序时,curl 的相同错误日志。
root@ta2carbon-5fdcfb97cc-8j4nl:/home/appHome# curl -i http://ta1carbon/api/app1/app1Func2
curl: (7) Failed to connect to ta1carbon port 80: Connection timed out
但我在 service.yaml 中定义的端口是 3000 而不是 80! 下面是两个微服务的 yml 配置。
ta1carbon 服务 yaml
apiVersion: v1
kind: Service
metadata:
name: ta1carbon
labels:
app: ta1carbon
spec:
ports:
- port: 3000
targetPort: 3000
type: ClusterIP
selector:
app: ta1carbon
ta2carbon 服务 yaml
apiVersion: v1
kind: Service
metadata:
name: ta2carbon
labels:
app: ta2carbon
spec:
ports:
- port: 3001
targetPort: 3001
type: ClusterIP
selector:
app: ta2carbon
以下是 ta1carbon 和 ta2 carbon 的服务详情。
kubectl describe service ta1carbon
Name: ta1carbon
Namespace: default
Labels: app=ta1carbon
Annotations: <none>
Selector: app=ta1carbon
Type: ClusterIP
IP: 100.66.7.165
Port: <unset> 3000/TCP
TargetPort: 3000/TCP
Endpoints: 100.96.1.13:3000
Session Affinity: None
Events: <none>
kubectl describe service ta2carbon
Name: ta2carbon
Namespace: default
Labels: app=ta2carbon
Annotations: <none>
Selector: app=ta2carbon
Type: ClusterIP
IP: 100.67.129.126
Port: <unset> 3001/TCP
TargetPort: 3001/TCP
Endpoints: 100.96.1.12:3001
Session Affinity: None
Events: <none>
所以根据我的观察,url http://ta1carbon/api/app1/app1Func2 服务 dns ta1carbon 正在解析为 100.67.24.69:80 导致超时。
但是,如果我从 ta2carbon pod 中 curl 进入 100.67.24.69:3000,我会得到一个 成功响应
此外,如果我更改服务 yaml - 端口:80 并再次部署和测试,我会得到 成功响应
我发现 kubernetes 中的这种行为很奇怪,不确定是天气还是环境问题。
我的查询是-
为什么将服务 ta1carbon 解析为 100.67.24.69:80 并超时,而端口本应为 3000!
如有任何意见,我们将不胜感激。请让我知道这里缺少什么。
DNS 将域名解析为 IP 地址,而不是 IP 地址 + 端口。
有两种可能的解决方案:
修改您的应用程序源以向
http://ta1carbon:3000
发出 API 请求
将
ta1carbon
服务上的port
设置为80
。
我建议选择选项 2。在这种情况下,您将利用 Kubernetes 服务的强大功能。 Kubernetes 将在端口 80 上公开服务,但会向支持端口 3000 上的服务的 pods 发送请求(因为 targetPort: 3000
)。