Nathan Long
Nathan Long

Reputation: 126092

Can an `ExternalName` service point to the host machine?

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.

Is that correct? Is there a better way?

Upvotes: 4

Views: 2049

Answers (3)

SYN
SYN

Reputation: 5041

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

Upvotes: 0

e2bias
e2bias

Reputation: 66

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).

Upvotes: -1

Nathan Long
Nathan Long

Reputation: 126092

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.

Upvotes: 4

Related Questions