3

I'm working locally (within Docker for Mac) on a Kubernetes cluster that will eventually be deployed to the cloud. We plan to use a database service in that environment. To simulate that, I'd like to have the services in the cluster connect to a database running outside the cluster on my laptop.

Can I do that? Here's what I thought I'd try.

  • Define a Service with type: ExternalName and externalName: somedb.local
  • Add 127.0.0.1 somedb.local to /etc/hosts on the laptop

Is that correct? Is there a better way?

Nathan Long
  • 122,748
  • 97
  • 336
  • 451
  • localhost on the host and in the pod are not the same localhost – Vasili Angapov Jan 28 '21 at 17:30
  • @VasiliAngapov Yes, I know, but will a DNS lookup within the cluster "bubble up" to the host machine? If not, is there another way I could point to the host machine from within the cluster? – Nathan Long Jan 28 '21 at 17:33

3 Answers3

3

After talking with some colleagues, I found a solution.

In Docker for Mac, host.docker.internal points to the host machine, and that lets me connect to the db running there, even from containers running in the K8s cluster.

Nathan Long
  • 122,748
  • 97
  • 336
  • 451
0

You may have a Service pointing to an address out of your SDN, by creating an Endpoint object with matching name.

----
apiVersion: v1
kind: Service
metadata:
  name: external-db
  namespace: my-namespace
spec:
  ports:
  - name: exporter-3306
    port: 3306
  selector:
    name: external-db
---
apiVersion: v1
kind: Endpoints
metadata:
  name: external-db
  namespace: my-namespace
subsets:
- addresses:
  - ip: 10.42.253.110
  ports:
  - name: exporter-3306
    port: 3306

You may add hosts overrides in your Deployment definition:

apiVersion: apps/v1
kind: Deployment
spec:
  template:
    spec:
      containers:
      ...
      hostAliases:
      - ip: 10.42.253.110
        hostnames:
        - external-db
SYN
  • 4,476
  • 1
  • 20
  • 22
  • Does this work if the host machine doesn't have an ip other than 127.0.0.1? – Don Rhummy May 26 '21 at 19:01
  • A Kubernetes container querying 127.0.0.1 won't leave it's netnamespace. As a general rule with virtual/physical servers or containers : 127.0.0.1 won't ever get you to a remote host. If your external database only has a loopback address, then you have another issue. – SYN May 27 '21 at 22:08
-1

It seems the Kubernetes docs provide an instruction on how to achieve this. https://kubernetes.io/docs/concepts/services-networking/service/#services-without-selectors

A note says endpoint IPs must not be: loopback (127.0.0.0/8 for IPv4, ::1/128 for IPv6), or link-local (169.254.0.0/16 and 224.0.0.0/24 for IPv4, fe80::/64 for IPv6).

e2bias
  • 66
  • 4