How to define kubernetes configmap for multiple routes in Spring Cloud Gateway[Kubernetes]

Kubernetes version:v4.5.4
Host OS: Fedora

How should the kubernetes ConfigMap yaml definition be for more than one route in Spring Cloud Gateway?

application.properties

spring.application.name=spring-gateway
server.port=8080

spring.cloud.gateway.routes[0].uri=http://example1:8082/
spring.cloud.gateway.routes[0].predicates[0].name=Path
spring.cloud.gateway.routes[0].predicates[0].args[0]=/user/**
spring.cloud.gateway.routes[0].filters[0]=TestFilter1
spring.cloud.gateway.routes[0].filters[1]=TestFilter2

spring.cloud.gateway.routes[1].uri=http://example2:8083/
spring.cloud.gateway.routes[1].predicates[0].name=Path
spring.cloud.gateway.routes[1].predicates[0].args[0]=/account/**
spring.cloud.gateway.routes[1].filters[0]=TestFilter1
spring.cloud.gateway.routes[1].filters[1]=TestFilter2

You can put the whole application.properties content into a single key inside the ConfigMap, or switch to application.yml, which is usually much cleaner for multiple routes.

For .properties, the ConfigMap can look like this:

apiVersion: v1
kind: ConfigMap
metadata:
  name: spring-gateway-config
data:
  application.properties: |
    spring.application.name=spring-gateway
    server.port=8080

    spring.cloud.gateway.routes[0].uri=http://example1:8082/
    spring.cloud.gateway.routes[0].predicates[0].name=Path
    spring.cloud.gateway.routes[0].predicates[0].args[0]=/user/**
    spring.cloud.gateway.routes[0].filters[0]=TestFilter1
    spring.cloud.gateway.routes[0].filters[1]=TestFilter2

    spring.cloud.gateway.routes[1].uri=http://example2:8083/
    spring.cloud.gateway.routes[1].predicates[0].name=Path
    spring.cloud.gateway.routes[1].predicates[0].args[0]=/account/**
    spring.cloud.gateway.routes[1].filters[0]=TestFilter1
    spring.cloud.gateway.routes[1].filters[1]=TestFilter2

Then mount it into the container as /config/application.properties or another location Spring Boot reads automatically.

Example Deployment snippet:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: spring-gateway
spec:
  replicas: 1
  selector:
    matchLabels:
      app: spring-gateway
  template:
    metadata:
      labels:
        app: spring-gateway
    spec:
      containers:
        - name: spring-gateway
          image: your-image:latest
          volumeMounts:
            - name: config-volume
              mountPath: /config
      volumes:
        - name: config-volume
          configMap:
            name: spring-gateway-config

A cleaner option is to use YAML instead of indexed .properties. That would look like this:

apiVersion: v1
kind: ConfigMap
metadata:
  name: spring-gateway-config
data:
  application.yml: |
    spring:
      application:
        name: spring-gateway
      cloud:
        gateway:
          routes:
            - uri: http://example1:8082/
              predicates:
                - Path=/user/**
              filters:
                - TestFilter1
                - TestFilter2
            - uri: http://example2:8083/
              predicates:
                - Path=/account/**
              filters:
                - TestFilter1
                - TestFilter2
    server:
      port: 8080

For multiple routes, application.yml is usually easier to read and maintain than the indexed .properties form.

One small note: if you want more explicit routing, it is also common to give each route an id, like:

- id: user-route
  uri: http://example1:8082/
  predicates:
    - Path=/user/**

That is not strictly required, but it is a good practice.