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,
Let's start with getting a demo API (customers' API) up and running. I have
the following Minimal API.
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
Now I have a Developer Tier APIM created and have added a single API.
|
Adding an API into APIM
|
Note: for the URL scheme, I have used HTTP(s), because I am going to expose
the Self-hosted gateway via HTTP for demo purposes. And I am not asking for a subscription key for this API.
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
|
I am giving it a Name, Location & optional Description, selecting our API
and adding it.
|
Add Self-hosted Gateway
|
Once it's added, now comes the interesting part.
|
Self-hosted Gateway Deployment Options
|
Look at the
Deployment scripts section. We are given instructions to run
the gateway on Docker, K8s and Helm.
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
And once the command is executed, I can check the logs and make sure it's up
and running.
|
Self-hosted Gateway Running
|
And now if I check the Azure Portal, I can see the gateway is up and running
and it's communicating with Azure.
|
Self-hosted Gateway Linked to Azure
|
And now the moment of truth. Let's call our API through the Self-hosted
gateway. The URL of the Self-hosted gateway is
http://localhost:8085.
|
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.
Regards,
Jaliya