首页 > 解决方案 > 无法使用 DNS 为 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 内卷曲我的 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 吊舱内卷曲到100.67.24.69:3000 ,我会得到成功响应

此外,如果我更改我的服务 yaml - 端口:80并再次部署和测试,我会得到成功响应

我在 kubernetes 中发现这种行为很奇怪,不确定天气我犯了错误还是环境。

我的查询是 -

为什么将服务 ta1carbon 解析到 100.67.24.69:80 并超时,而端口应该是 3000!

对此的任何意见将不胜感激。请让我知道其中缺少什么。

标签: kuberneteskubernetes-service

解决方案


DNS将域名解析为IP地址,而不是IP地址+端口。

有两种可能的解决方案:

  1. 修改您的应用程序源以发出 API 请求http://ta1carbon:3000

  2. port您的ta1carbon服务上的 设置为80

我建议使用选项 2。在这种情况下,您正在利用 Kubernetes 服务的强大功能。Kubernetes 将在端口 80 上公开服务,但将请求发送到支持端口 3000 上的服务的 pod(因为targetPort: 3000.


推荐阅读