Skip to content

How to deploy OSRM in Kubernetes

In order to implement OSRM in a Kubernetes cluster, it is necessary to have the routing data within the cluster. There are various methods to achieve this, but the most dependable approach involves utilizing the persistent volume model in a dynamic Kubernetes cluster. For a standalone k8s cluster, the simplest and most straightforward method is to use the hostPath.

Once the data generation process is complete, for deployment on a standalone cluster, all of the generated files should be copied to the directory /data/ on the server. It is important to ensure that this data is accessible on all of the Kubernetes nodes.

Create /data/ folder and cd in it.

Download OpenStreetMap extracts for example from Geofabrik

wget http://download.geofabrik.de/europe/germany/berlin-latest.osm.pbf

Pre-process the extract with the car profile and start a routing engine HTTP server on port 5000

docker run -t -v "${PWD}:/data" ghcr.io/project-osrm/osrm-backend osrm-extract -p /opt/car.lua /data/berlin-latest.osm.pbf || "osrm-extract failed"

The flag -v "${PWD}:/data" creates the directory /data inside the docker container and makes the current working directory "${PWD}" available there. The file /data/berlin-latest.osm.pbf inside the container is referring to "${PWD}/berlin-latest.osm.pbf" on the host.

docker run -t -v "${PWD}:/data" ghcr.io/project-osrm/osrm-backend osrm-partition /data/berlin-latest.osrm || "osrm-partition failed"

docker run -t -v "${PWD}:/data" ghcr.io/project-osrm/osrm-backend osrm-customize /data/berlin-latest.

To deploy the osrm-backend application in k8s, use the YAML file provided below.

osrm-deployment.yaml

apiVersion: v1
kind: Service
metadata:
  name: osrm
  labels:
    app: osrm
spec:
  ports:
  - port: 5000
    targetPort: 5000
    nodePort: 32513
    name: http
  selector:
    app: osrm
  type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: osrm
  labels:
    app: osrm
spec:
  replicas: 1
  selector:
    matchLabels:
      app: osrm
  template:
    metadata:
      labels:
        app: osrm
    spec:
      containers:
      - name: osrm
        image: ghcr.io/project-osrm/osrm-backend:latest
        imagePullPolicy: IfNotPresent
        resources:
          requests:
            memory: "512Mi"
            cpu: "0.5"
          limits:
            memory: "512Mi"
            cpu: "0.5"
        command: ["/bin/sh", "-c"]
        args: ["osrm-routed --algorithm mld /data/berlin-latest.osm.pbf"]
        ports:
        - containerPort: 5000
        volumeMounts:
        - name: osrm
          readOnly:  true
          mountPath: "/data/"
      volumes:
      - name: osrm
        hostPath:
          path: /data

Command to run the osrm-backend image in Kubernetes

kubectl apply -f osrm-deployment.yaml

Published indockerKubernetesLinux