- Shell script
<code>
#!/bin/bash
#Set Kong Admin API URL
KONG_ADMIN_URL=”http://localhost:8001″
#Define an array of services and routes
declare -A services
services=(
[“service11″]=”http://example11.com:8080”
[“service12″]=”http://example12.com:8080”
[“service13″]=”http://example13.com:8080”
)
Define routes corresponding to the services
declare -A routes
routes=(
[“service11″]=”/example11”
[“service12″]=”/example12”
[“service13″]=”/example13”
)
Loop through the services and create them in Kong
for service in “${!services[@]}”; do
# Create each service
echo “Creating service: $service with URL: ${services[$service]}”
curl -i -X POST $KONG_ADMIN_URL/services \
–data name=$service \
–data url=${services[$service]}
# Create a route for each service
echo “Creating route for service: $service with path: ${routes[$service]}”
curl -i -X POST $KONG_ADMIN_URL/routes \
–data paths[]=${routes[$service]} \
–data service.name=$service
# Optionally, add a plugin (e.g., key-auth) to each route
echo “Adding key-auth plugin to route for service: $service”
curl -i -X POST $KONG_ADMIN_URL/routes/${service}/plugins \
–data name=key-auth
done
echo “All services and routes have been configured.
</code>
</code>
- name: Automate Kong API Mapping for Multiple Services with Different Ports
hosts: localhost
tasks:
-
name: Define a list of services and routes with different ports set_fact: services: – { name: service6, url: http://service6.com:8086, path: /service6 } – { name: service7, url: http://service7.com:8087, path: /service7 } – { name: service8, url: http://service8.com:8088, path: /service8 } – { name: service9, url: http://service9.com:8089, path: /service9 } – { name: service10, url: http://service10.com:8090, path: /service10 }
-
name: Create a Service in Kong for each service with different ports uri: url: http://localhost:8001/services method: POST body_format: json body: name: “{{ item.name }}” url: “{{ item.url }}” status_code: 201 with_items: “{{ services }}” register: service_creation
-
name: Create a Route for each Service uri: url: http://localhost:8001/routes method: POST body_format: json body: service: name: “{{ item.name }}” paths: – “{{ item.path }}” status_code: 201 with_items: “{{ services }}”
-
</code>