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

Viewed 682

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?

3 Answers

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.

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
Related