Search code examples
openshift

Multiple Routes in OpenShift?


I need to have multiple hostnames for an API being hosted in OpenShift. (e.g. host1.mycompany.com, host2.mycompany.com, host3.mycompany.com) Unfortunately I can't find a way to configure multiple routes for an OpenShift project.

The only resources I've found say to list all the routes in route.yaml, with different metadata.name but this doesn't work. Only the last listed route works.

For example if we enter the following route.yaml in Openshift:

apiVersion: route.openshift.io/v1
kind: Route
metadata:
  name: releasename-route
spec:
  host: host1.mycompany.com
  port:
    targetPort: http
  to:
    kind: Service
    name: releasename-service
    weight: 100
  wildcardPolicy: none
apiVersion: route.openshift.io/v1
kind: Route
metadata:
  name: releasename-altroute-1
spec:
  host: host2.mycompany.com
  port:
    targetPort: http
  to:
    kind: Service
    name: releasename-service
    weight: 100
  wildcardPolicy: none

the only route that ends up in Openshift is host2.mycompany.com

We can't use "wildcardPolicy: subdomain" because our company has that restricted (and we only need the three hostnames, not all the subdomains...)

How can we set this up in OpenShift?


Solution

  • If you create multiple routes it absolutely works.

    You're using erroneous syntax in your YAML manifest. If you want to include multiple YAML documents in a single file, they need to be separated by the YAML document separator, ---:

    apiVersion: route.openshift.io/v1
    kind: Route
    metadata:
      name: releasename-route
    spec:
      host: host1.mycompany.com
      port:
        targetPort: http
      to:
        kind: Service
        name: releasename-service
        weight: 100
      wildcardPolicy: none
    ---
    apiVersion: route.openshift.io/v1
    kind: Route
    metadata:
      name: releasename-altroute-1
    spec:
      host: host2.mycompany.com
      port:
        targetPort: http
      to:
        kind: Service
        name: releasename-service
        weight: 100
      wildcardPolicy: none
    

    Alternately, just keep them in different files and apply them separately, or put them in a directory and apply the directory using kubectl apply:

    $ tree routes
    routes
    ├── route1.yaml
    └── route2.yaml
    $ kubectl apply -f routes
    route.route.openshift.io/route1 created
    route.route.openshift.io/route2 created