Azure API Management is made up of an API gateway, a management plane, and a developer portal. In this post, let's see how we can add another API gateway that is going to run locally on Docker to an APIM. So if we can have the API gateway running locally, we can basically run it anywhere that supports Containers, for example in Kubernetes, Azure Container Apps, etc.
Here what we are going to be doing is,
- Have a demo API running locally (let's say customers API)
- Since I am a .NET person, I will have an ASP.NET Core Minimal API with a single endpoint. And this is going to be running using the local IP Address and will NOT use localhost. If you are wondering why, I will explain it later.
- Have a Developer Tier APIM created, there I am going to expose our demo customers' API which is running locally. So obviously, from Azure Portal, I won't be able to test the API.
- Add an API gateway to our APIM and specify which APIs it can expose. In this case, the API gateway will be running locally, and it will be communicating with Azure to report its status, get configuration updates, etc.
- Once the API gateway is up, I am going to try calling the customers API through the local API gateway endpoint, and hopefully, it should work,
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
WebApplication app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.MapGet("/info", () =>
{
return "Customers";
})
.WithName("GetApiInfo")
.WithOpenApi();
app.Run();
And I am running it on http://192.168.1.5:5192.
dotnet run --urls=http://192.168.1.5:5192
Demo Customers API running locally on http://192.168.1.5:5192 |
Adding an API into APIM |
So at this stage, our API is not really usable. If you try to call it through the Azure APIM gateway URL, it will try to call http://192.168.1.5:5192/info, and it's not reachable.
Now let's add the Self-hosted gateway.Add Self-hosted Gateway |
Add Self-hosted Gateway |
Self-hosted Gateway Deployment Options |
For Docker, we need to create an env.conf file, copy-paste the content, and then execute the docker run command. I have modified the docker command a bit, so I am not running the gateway on port 80 which could already being used most of the time.
docker run -d -p 8085:8080 --name apimgw-ravana-tpp15 --env-file env.conf mcr.microsoft.com/azure-api-management/gateway:v2
Self-hosted Gateway Running |
Self-hosted Gateway Linked to Azure |
Calling Self-hosted Gateway |
And that's it. Here the gateway is calling our locally running API on http://192.168.1.5:5192. I used the IP address instead of localhost because otherwise, the gateway would be looking inside the container to find the API.
You can check the gateway container logs to see all the information, like requests, how configurations are getting updated, etc.
Hope this helps.
More read:
Self-hosted gateway overview
Happy Coding.
No comments:
Post a Comment