Git Product home page Git Product logo

consul-haproxy-example's Introduction

Consul HAProxy Example

This project will show how to dynamically instantiate Node.js application processes and have Consul reconfigure and restart HAProxy.

Overview Diagram

Pre Requisites

Each of these programs consist of a single binary. You can choose to install them completely or simply download and execute the binary. If doing the latter be sure to adjust relative paths.

Also, checkout this repository and run npm install to get the required packages.

HAProxy Stats

At any point you can visit http://localhost:8000/admin?stats to view the inner workings of HAProxy.

Simple Static Usage

If we simply want to use a known, finite number of HTTP servers we can use a simple HAProxy configuration and launch the number of processes.

haproxy ./basic.cfg             # Terminal 1
./service-basic-web.js 20001    # Terminal 2
./service-basic-web.js 20002    # Terminal 3
curl http://localhost:8000/     # Terminal 4

Unfortunately any time we scale the number of processes we need mo manually modify basic.cfg. Luckily we can do better.

Dynamic Usage

Start a couple data providers, launch an HTTP server, launch HAProxy. Make a few requests and note that they are routed to a single HTTP server. Launch a new server and notice how requests are split between both. Kill an HTTP server and again see how requests are back down to a single server.

consul agent -dev               # Terminal 1
./service-data.js 30001         # Terminal 2
./service-data.js 30002         # Terminal 3
./service-www.js 20001          # Terminal 4
# Terminal 4
consul-template -template "./advanced.cfg.template:./advanced.cfg:./haproxy-restart.sh"
curl http://localhost:8000      # Terminal 6
curl http://localhost:8000      # Terminal 6
curl http://localhost:8000      # Terminal 6
./service-www.js 20002          # Terminal 7
curl http://localhost:8000      # Terminal 6
curl http://localhost:8000      # Terminal 6
curl http://localhost:8000      # Terminal 6
# Ctrl + C                      # Terminal 4
curl http://localhost:8000      # Terminal 6
curl http://localhost:8000      # Terminal 6
curl http://localhost:8000      # Terminal 6

Returned Data

As the requests run you will see different data returned when you make cURL requests, depending on which servers are running.

If there are two www servers running you'll see the PID values alternate back and forth as HAProxy does round-robin proxying between the services.

If there are two data servers running you'll see the PID values randomly jump as our application randomly routes traffic between the instances.

Of course if you scale up or down the number of either data or www services, the PIDs will shrink or grow accordingly. I'm specifically using PID values to show that different processes are handling the workloads.

{
  "data": {
    "data": 12345678,
    "data_pid": 20001
  },
  "web_pid": 20101
}

consul-haproxy-example's People

Contributors

tlhunter avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

consul-haproxy-example's Issues

Node.js micro-services best practices

Hi Thomas,

Not an issue, I was looking for some up-to-date info, I hope that's ok to contact you from here.
Interesting video there: https://www.youtube.com/watch?v=I1lLzPb3MLg, which was published a year and half ago.

Is this example still up-to-date in a kubernetes scenario?

I know this is a big subject, but I'm looking for solutions for node.js services discovery (like consul), error handling (not sure with what), messaging (rabbitMQ?).

Is there any tool(s) today (end of 2018) that you would recommend? Or do I need to build custom solutions because there no widely-spread tool that I should use?

Thanks in advance if you have a few minutes to direct me.

Cheers.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.