Replicate Kubernetes Ingress using plain Nginx and Docker Compose in less than 5 minutes
We are using Kubernetes Ingress to deploy our web application.
(To know what is ingress, check out this really nice blog by Sandeep Dinesh)
For a sample application my-bank. The setup is done as shown below:
We needed to replicate above setup in local machine for debugging purposes.
After exploring various alternatives including nginx-proxy (Thank you Kim Wuestkamp for this idea). For us, easiest workaround was using plain nginx and proxy_pass directive beautifully sewn together in a docker-compose file.
This is how the html page of our main app can be coded :
Notice that we only have to give relative path /employee/home
in href and not full url even in local setup.
Employee as well as Customer service both have their Dockerfile ‘s like below:
nginx/conf.d/default.conf
server {
listen 80;
server_name localhost my-bank.com;
location / {
root /usr/share/nginx/html;
try_files $uri $uri/ /index.html;
}
location /employee {
proxy_pass http://employee:8081;
proxy_set_header Host my-bank.com;
}
location /customer {
proxy_pass http://customer:8080;
proxy_set_header Host my-bank.com;
}
error_page 404 500 502 503 504 /error.html;
location /error.html {
root /usr/share/nginx/html;
}
}
Above configuration instructs nginx to start main application at port 80 and proxy the request to 8080 and 8081 if url has /customer or /employee in it’s path respectively.
Finally to put it all together into a docker-compose :
Finally start the app using command docker-compose up
and use docker-compose down --rmi 'all'
to stop the application without any left-over images and containers.
As always, Entire code can be found at Github
Happy Coding :)