Using Openstreetmap in Kubernetes

8/9/2019

I have an application that is running inside of Kubernetes which needs to display a map using Leaflet, the map data comes from Openstreetmap.

The code I use to set up the map looks like this:

map = L.map('mapid', {
        center: [lat, long],
        zoom: 19
    });
    L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
        attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
    }).addTo(map);

What is bothering me right now is the url {s}.tile.openstreetmap.org. As the openstreetmap data resides outside the k8s cluster, I need to create a Service in Kubernetes.

I tried to define these services:

apiVersion: v1
kind: Service
metadata:
  name: a.tile.openstreetmap.org
spec:
  type: ExternalName
  externalName: a.tile.openstreetmap.org
---
apiVersion: v1
kind: Service
metadata:
  name: b.tile.openstreetmap.org
spec:
  type: ExternalName
  externalName: b.tile.openstreetmap.org
---
apiVersion: v1
kind: Service
metadata:
  name: c.tile.openstreetmap.org
spec:
  type: ExternalName
  externalName: c.tile.openstreetmap.org

However that resulted in the following error message when deploying the service:

Error from server (Invalid): error when creating "openstreetmap-service.yaml": Service "a.tile.openstreetmap.org" is invalid: metadata.name: Invalid value: "a.tile.openstreetmap.org": a DNS-1035 label must consist of lower case alphanumeric characters or '-', start with an alphabetic character, and end with an alphanumeric character (e.g. 'my-name',  or 'abc-123', regex used for validation is '[a-z]([-a-z0-9]*[a-z0-9])?')
Error from server (Invalid): error when creating "openstreetmap-service.yaml": Service "b.tile.openstreetmap.org" is invalid: metadata.name: Invalid value: "b.tile.openstreetmap.org": a DNS-1035 label must consist of lower case alphanumeric characters or '-', start with an alphabetic character, and end with an alphanumeric character (e.g. 'my-name',  or 'abc-123', regex used for validation is '[a-z]([-a-z0-9]*[a-z0-9])?')
Error from server (Invalid): error when creating "openstreetmap-service.yaml": Service "c.tile.openstreetmap.org" is invalid: metadata.name: Invalid value: "c.tile.openstreetmap.org": a DNS-1035 label must consist of lower case alphanumeric characters or '-', start with an alphabetic character, and end with an alphanumeric character (e.g. 'my-name',  or 'abc-123', regex used for validation is '[a-z]([-a-z0-9]*[a-z0-9])?')

I understand that I am not allowed to use dots in .metadata.name, but is there a different possibility to achieve this? As far as I can see from the description of the leaflet source, the URL in TileLayer needs to be something like http://{s}.somedomain.com/blabla/{z}/{x}/{y}{r}.png

-- purzel
kubernetes
leaflet
openstreetmap

1 Answer

8/9/2019

Calling an external service by it's name shouldn't be a problem. ExternalName can be used, if the service needs to be available as a Kubernetes resource, for example to proxy an ingress route to an external service.

If you still want to use the ExternalName service instead of the real FQDN, simply use valid names like a-tile-openstreetmap-org and replace the domain name in your client script with that name. You might have to set a correct HTTP host header though to avoid problems with the target server.

I'd still suggest using the real name, as it's simple and straight forward and there's no benefit in aliasing it.

-- Markus Dresch
Source: StackOverflow