OpenShift部署的Java应用调用外部服务提示‘Name or service not known’
问题解决:OpenShift中Java应用调用外部服务报“Name or service not known”
核心问题分析
当前配置存在两个关键错误:
- ExternalName Service与Endpoints误用:ExternalName类型的Service不需要绑定Endpoints,它的作用是让集群内部通过Service名称解析到指定外部域名;你额外创建的Endpoints名字与实际存在的资源不匹配(YAML声明为
external-application,但oc get endpoints显示名字是aaa.bbb.my-other-application.com),且两者无关联关系。 - 应用调用路径错误:Java应用直接调用
aaa.bbb.my-other-application.com时,请求走集群外部DNS解析,完全没用到你配置的OpenShift资源,导致域名无法解析报错。
可行解决方案
方案一:通过ExternalName Service访问(推荐)
利用ExternalName Service将集群内部Service名称映射到外部域名,让应用通过Service名称访问:
- 保留现有ExternalName Service配置(已正确):
apiVersion: "v1" kind: "Service" metadata: name: "external-application" namespace: exe-aa01 spec: type: ExternalName externalName: aaa.bbb.my-other-application.com
- 修改Java应用的调用地址为集群内部Service的FQDN:
external-application.exe-aa01.svc.cluster.local
集群DNS会自动将这个地址解析到aaa.bbb.my-other-application.com。
方案二:用Endpoints+ClusterIP Service绑定固定IP
如果需要直接将外部域名映射到指定IP(12.13.14.15),采用ClusterIP Service关联Endpoints的方式:
- 删除现有ExternalName Service:
oc delete svc external-application -n exe-aa01
- 修正Endpoints资源(确保名字为
external-application):
apiVersion: v1 kind: Endpoints metadata: name: external-application namespace: exe-aa01 subsets: - addresses: - ip: 12.13.14.15 ports: - port: 6789 name: external-application protocol: TCP
- 创建ClusterIP类型的Service(与Endpoints同名,无需selector):
apiVersion: v1 kind: Service metadata: name: external-application namespace: exe-aa01 spec: type: ClusterIP ports: - port: 6789 name: external-application protocol: TCP
- 修改Java应用的调用地址为:
external-application.exe-aa01.svc.cluster.local:6789
此时请求会直接转发到12.13.14.15:6789。
内容的提问来源于stack exchange,提问作者Broshet
相关产品推荐
相关产品推荐

