Kubernetes java.net.UnknownHostException:postgres

Kubernetes java.net.UnknownHostException:postgres,kubernetes,Kubernetes,我正在尝试在kubernetes上部署spring引导应用程序,并连接到本地主机上的postgres Spring引导部署很好。 对于博士后,我也这样做了 kind: Service apiVersion: v1 metadata: name: postgres namespace: default spec: type: ExternalName # https://docs.docker.com/docker-for-mac/networking/#use-cases-and

我正在尝试在kubernetes上部署spring引导应用程序,并连接到本地主机上的postgres

Spring引导部署很好。 对于博士后,我也这样做了

kind: Service
apiVersion: v1
metadata:
  name: postgres
  namespace: default
spec:
  type: ExternalName
  # https://docs.docker.com/docker-for-mac/networking/#use-cases-and-workarounds
  externalName: host.docker.internal
  ports:
    - name: port
      port: 5432
kubectl获得svc

postgres           ExternalName   <none>           host.docker.internal   5432/TCP         9m13s
所以SpringBoot部署的日志会给出异常

 java.net.UnknownHostException: postgres

这个功能只适用于Mac和Win Docker,所以如果你在Linux上运行它,它就不起作用了

Linux对此主机名的支持已经实现,可能在未来几天/几周内实现:

还有一些变通方法,虽然它不是针对Linux发布的

I. 您可以使用以下内容在项目中创建名为.ddev/docker-compose.xdebug.yaml的文件:

# For Linux users; Docker has not yet supported the "host.docker.internal"
# convention that is used in ddev v0.18.0. But if you add this file as
# .ddev/docker-compose.xdebug.yaml it should sort that out.
# Note that the IP address in your environment might not be 172.17.0.1,
# Find out what address to use with "ifconfig docker0" or "ip addr show docker0 | grep inet"
version: "3"
services:
  web:
    extra_hosts:
      # Find out what address to use with "ifconfig docker0" or "ip addr show docker0 | grep inet"
      - "host.docker.internal:172.17.0.1"
II.
使用此选项将TCP和UDP通信转发到docker主机

您是否使用docker for Mac?哪个版本?实际上没有。我想那个链接被注释了。这很重要吗?那么我应该停止我的服务帖子并创建这个文件并部署吗?或者两者都需要吗?@Dhanraj用第二个选项更新了我的答案,对你来说可能更容易
# For Linux users; Docker has not yet supported the "host.docker.internal"
# convention that is used in ddev v0.18.0. But if you add this file as
# .ddev/docker-compose.xdebug.yaml it should sort that out.
# Note that the IP address in your environment might not be 172.17.0.1,
# Find out what address to use with "ifconfig docker0" or "ip addr show docker0 | grep inet"
version: "3"
services:
  web:
    extra_hosts:
      # Find out what address to use with "ifconfig docker0" or "ip addr show docker0 | grep inet"
      - "host.docker.internal:172.17.0.1"