This series is dedicated to DNS-based service discovery, reverse-proxying and load balancing of dockerized microservices using etcd, SkyDNS, nginx and a simple service built with Node.js and node-docker-monitor module.

Part 1 – Setting up SkyDNS for service discovery of dockerized microservices
Part 2 – Automating SkyDNS service discovery with simple Node.js service
Part 3 – Setting up nginx as a reverse-proxy for dockerized microservices

Setting up Nginx as reverse proxy for dockerized microservices

In our pervious post we completed setting up environment for DNS-based service discovery of dockerized microservices. We implemented docker-skydns-adaptor using Node.js and TypeScript.

As we demonstrated, DNS provides a simple and effective way for services to communicate with each other. However, the main goal of any service is to provide some functionality to external clients. To achieve that, API Gateway or Reverse Proxy pattern is often used. Essentially, it means installing some web server running in reverse proxy mode. It has to be aware of microservices running in our environment and should have url rewrite rules to route HTTP requests to those microservies. You can find more information about this pattern (as well as an alternative implementation) in one of our previous articles.

In this article we will demonstrate how it can be implemented using simple nginx server setup. Most of this kind of implementations use config re-generation approach to make it microservice-aware. Firstly, a config template is prepared. This template has a dynamic section that repeats for every microservice (docker container). Then some sort of docker monitor re-generates proxy config when a docker container starts up or goes down. And finally, this monitor restarts the proxy. Here’s one of the tools that can be used for this sort of automation.

In our case, however, we can use static configuration file for the proxy. Our dynamic component providing service discovery is SkyDNS that we prepared in previous two posts.

Nginx as reverse proxy for microservices

Nginx as reverse proxy for microservices

The only thing left to do is to prepare the configuration file.

We will create it on host machine and then map it to the container running nginx.

Then copy / paste file content

This is a minimal nginx config for reverse proxy with DNS resolver configured to use our SkyDNS server plus rewrite rule that will route all requests for path /api/<service host name>  to <service host name>:3000  so it can be handled by corresponding microservice running in docker container.

The only thing left to do is to start nginx docker container (we will use official nginx docker image from Docker Hub)

We assume that environment created during our previous articles is in the same state that we left it: test-etcdtest-skydns, test-skydns-adaptor, test-service11 and test-service12 are still running. In this case we can see our nginx-based reverse proxy for dockerized microservices in action.

Accessing microserivces through Reverse Proxy

Note, that we can access services on root path localhost/api/service, to achieve load balancing, or on exact path like  localhost/api/12.service  to address a specific service instance. If it doesn’t exist or malfunctioning, we will get HTTP 502 – Bad Gateway response, as demonstrated above.

As result of 3 steps described in this series: setting up basic environment, automating service registration in SkyDNS and configuring API Gateway / Reverse Proxy, we achieved both internal (using short host names) and external service discovery and HTTP request routing. Our current setup is limited to one host only, however with some additional steps, it can be extended to work with multiple hosts.

If you want to know more about it, please leave a comment below.

Share This

Share This

Share this post with your friends!