在 Kubernetes 中使用 Openstreetmap

Using Openstreetmap in Kubernetes

我在 Kubernetes 中有一个 运行 应用程序需要使用 Leaflet 显示地图,地图数据来自 Openstreetmap。

我用来设置地图的代码如下所示:

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

现在困扰我的是 url {s}.tile.openstreetmap.org。由于 openstreetmap 数据驻留在 k8s 集群之外,我需要在 Kubernetes 中创建一个服务。

我试图定义这些服务:

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

然而,这导致在部署服务时出现以下错误消息:

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])?')

我知道我不能在 .metadata.name 中使用点,但是否有其他方法可以实现这一点?据我从传单来源的描述中可以看出,TileLayer 中的 URL 需要类似于 http://{s}.somedomain.com/blabla/{z}/{x}/{y}{r}.png

通过名称调用外部服务应该不是问题。如果服务需要作为 Kubernetes 资源提供,例如将入口路由代理到外部服务,则可以使用 ExternalName

如果您仍想使用 ExternalName 服务而不是真正的 FQDN,只需使用 a-tile-openstreetmap-org 之类的有效名称并用该名称替换客户端脚本中的域名。不过,您可能必须设置正确的 HTTP 主机 header 以避免目标服务器出现问题。

我仍然建议使用真实姓名,因为它简单明了,使用别名也没有任何好处。