— Tech, Infra — 5 min read
This project started as a requirement to solve the problem of maintaining multiple sources of truth for multi-regional Kong deployments which essentially needed to have parity in major components across services and routes across regions in the same environment.
Along with the above, there was a requirement for assigning services to specific Kong clusters depending on compliance types.
Hence, this document serves a project home for this piece of software/framework which can be used to control multiple Kong deployments with a single GitOps repo by following custom metadata to Kong declarative configuration.
API Governance: Achieve standardisation for APIs to keep them consistent throughout the org by having a single source of truth and effectively manage the API lifecycle.
API Traffic Observability: Monitor the traffic state (rate/error/duration) of Public APIs for all upstream services from a single dashboard.
Increased Availability: Achieve higher uptime for all upstreams by using health-check based load balancing and auto failover strategies across all the Infra.
Developer Self-Service: Enable the product teams to onboard APIs and manage the lifecycle for their products without admin intervention.
Service Equivalence across DCs: Maintain the same set of Public API configurations along with upstream services across multiple DCs.
The external API gateway in form of the Kong Gateway deployment will be designed keeping the first principles in mind and is detailed as follows:
Kong will be deployed in hybrid mode, i.e, Data Plane(DP) and Control Plane(CP) Nodes are deployed as separate entities along with a PostgreSQL database used to manage and store configurations for that Kong instance and communicates with the CP nodes.
The hybrid mode is chosen because of the following reasons:
Problem Statement
Kong configurations entail all the information regarding upstream services, endpoints, API routes, plugins and so much more. But in the context of service equivalence, only the service, route and other global entities are required to be replicated across multiple regions. Entities like upstreams, certificates, consumers will vary from region to region.
Kong’s declarative configuration tool supports managing the configuration of only one Kong cluster at once. Hence, this calls for a custom solution to manage and maintain the Kong configuration for multiple Kong Clusters in different regions from a single set of conventions.
The following sections will break down the problem and explore the solution in parts:
GitOps Config Management
Folder Structure
The Kong API configuration will be divided on the basis of environments, and each environment will have multiple cluster(s) across which the API service/route configuration has to be kept equivalent.
The services will be further divided into separate products, such product teams can manage their own configurations while maintain a boundary from other services.
There’ll be global entities like plugin and default kong configuration which’ll be merged with the generated Kong configuration.
Simplified Explanation
1One Folder → One Environment2One Environment → Multiple Clusters3Multiple Clusters → Same Kong route config4Different Environments → Different Kong route configs
The folder structure of the Git Repo looks this:
For e.g.,
There are two Kong clusters mentioned inside the production environment across which routes have to be made equivalent.
There is a service defined named service1 inside product1. The service definition looks like this:
The compliance zone where the service belongs(PCI/Non-PCI) will be mentioned in the tags of the service itself.
This service has defined its upstream as servuce.upstream. Now, the goal is to replicate this service route across both clusters of the environment.
So, the upstreams target configuration for this particular product will look like this:
The configurations will be generated separately for each of the clusters using the same service definition but substituting the cluster(DC) specific upstream target configurations, maintaining service(route) equivalence across clusters of the same environment.
Config Generation
The requirement is to generate service configurations across multiple compliance zones and clusters in the same environment and this will be achieved by a convention which’ll be maintained throughout the Git repo and a custom logic to segregate services based on compliance zones and substituting upstream targets accordingly.
For the purpose of service equivalence and keeping a single source of truth, the Kong Manager UI will be disabled and all manual changes to services/routes/upstreams will be prohibited.
decK will be used as the configuration management and drift detection tool to validate, diff and sync the configs on a single cluster. But route equivalence will be achieved across multiple clusters as follows:
Conventions
As this is a very opinionated configuration template, these conventions will be validated across the repo structure and is mandatory to be followed for proceeding through the next stages of the pipeline.
Generation Flow
The service configurations will be segregated on the basis of PCI/Non-PCI compliance type and will be merge to create two separate state maps for each compliance zone type for all the products. The product level plugins(key-auth/jwt) will be merged with the product state maps.
The upstream target inventory will be used to create separate Kong state files for each of the products for all the clusters in that environment. The Kong State files for each compliance zone, and each region DC will be created after collating all the product state maps.
Pipeline workflow
Developers will onboard their APIs from a PR based workflow, where service onboarding/offboarding will require a signoff from their team owners.
There’ll be one stage for each environment and pipelines for diff/validate stages will run on their branch, and after all approvers are taken for the service onboarding/offboarding, the merge event will trigger a sync job with the respective Kong clusters.
There is a scope for race conditions during this workflow, but MRs will not allowed to merge unless the latest commit on master is pulled on the given branch.
For the APIs which require certificate based TLS/SSL authentication, TLS/SSL termination will happen at the Kong Gateway level itself.
The certificates and SNIs will be onboarded to Kong via APIs instead of doing it declaratively via Git repo for obvious security reasons.
The routes will be attached to SNIs during the service configuration declaratively in the Git repo.
Checkout more detailed docs and implementation of the configuration generation and environment validation tool at Github.