Home > database >  Clean way to connect to services running on the same host as the Kubernetes cluster
Clean way to connect to services running on the same host as the Kubernetes cluster

Time:12-14

I have a single node Kubernetes cluster, installed using k3s on bare metal. I also run some services on the host itself, outside the Kubernetes cluster. Currently I use the external IP address of the machine (192.168.200.4) to connect to these services from inside the Kubernetes network.

Is there a cleaner way of doing this? What I want to avoid is having to reconfigure my Kubernetes pods if I decide to change the IP address of my host.

Possible magic I which existed: a Kubernetes service or IP that automagically points to my external IP (192.168.200.4) or a DNS name that points the node's external IP address.

CodePudding user response:

That's what ExternalName services are for (https://kubernetes.io/docs/concepts/services-networking/service/#externalname):

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  type: ExternalName
  externalName: ${my-hostname}
  ports:
  - port: 80

Then you can access the service from withing kubernetes as my-service.${namespace}.svc.cluster.local.

See: https://livebook.manning.com/concept/kubernetes/external-service

After the service is created, pods can connect to the external service through the external-service.default.svc.cluster.local domain name (or even external-service) instead of using the service’s actual FQDN. This hides the actual service name and its location from pods consuming the service, allowing you to modify the service definition and point it to a different service any time later, by only changing the externalName attribute or by changing the type back to ClusterIP and creating an Endpoints object for the service—either manually or by specifying a label selector on the service and having it created automatically.

ExternalName services are implemented solely at the DNS level—a simple CNAME DNS record is created for the service. Therefore, clients connecting to the service will connect to the external service directly, bypassing the service proxy completely. For this reason, these types of services don’t even get a cluster IP.

This relies on using a resolvable hostname of your machine. On minikube there's a DNS alias host.minikube.internal that is setup to resolve to an IP address that routes to your host machine, I don't know if k3s supports something similar.

CodePudding user response:

Thanks @GeertPt,

With minikube's host.minikube.internal in mind I search around and found that CoreDNS has a DNS entry for each host it's running on.

Checking

kubectl -n kube-system get configmap coredns -o yaml

reveals there is the following entry:

  NodeHosts: |
    192.168.200.4 my-hostname

So if the hostname doesn't change, I can use this instead of the IP.

To bad there's no catch-all entry so that something.my-hostname would also work...

  • Related