Coder Social home page Coder Social logo

dataplaneapi-consul-example's Introduction

HAProxy with Consul service discvoery

Example project that demonstrates using Consul service discovery with HAProxy.

Setup

  1. Call vagrant up from this directory to create the virtual machines:
  • Consul server
  • HAProxy
  • Web server running Consul agent
  1. Log into the HAProxy VM:

    $ vagrant ssh haproxy
    
  2. Use the HAProxy Data Plane API to register HAProxy as a Consul agent. Set :code:address to the IP address or DNS name of your Consul server.

    $ curl -u dataplaneapi:mypassword \
           -H 'Content-Type: application/json' \
           -d '{
                 "address": "192.168.50.21",
                 "port": 8500,
                 "enabled": true,
                 "retry_timeout": 10
               }' http://192.168.50.20:5555/v2/service_discovery/consul
    

    which returns:

    {"address":"192.168.50.21","enabled":true,"id":"1de47b93-0165-44c6-b575-818b786db51d","name":"consul","port":8500,"retry_timeout":10,"server_slots_base":10,"server_slots_growth_increment":10,"server_slots_growth_type":"linear","service-blacklist":null,"service-whitelist":null}
    

How to register service

Call the consul services register command on your webserver:

$ consul services register /vagrant/web.json

The web.json file look like this:

{
  "service": {

    "name": "web",
    "port": 80
  }
}

You can register more services that have the name web and they will be put into the same backend in HAProxy.

The HAProxy configuration is updated with the server:

backend consul-backend-192.168.50.21-8500-web
  server SRV_rfBd5 192.168.50.22:80 check weight 128
  server SRV_6ti2S 192.168.50.23:80 check weight 128
  server SRV_MtYvS 127.0.0.1:80 disabled weight 128
  server SRV_gD5xA 127.0.0.1:80 disabled weight 128
  server SRV_V0YU9 127.0.0.1:80 disabled weight 128
  server SRV_9zamp 127.0.0.1:80 disabled weight 128
  server SRV_ta7Z7 127.0.0.1:80 disabled weight 128
  server SRV_S575K 127.0.0.1:80 disabled weight 128
  server SRV_LkIZ9 127.0.0.1:80 disabled weight 128
  server SRV_PYkL1 127.0.0.1:80 disabled weight 128

You will need to add a frontend that routes to this backend.

dataplaneapi-consul-example's People

Contributors

nickmramirez avatar

Stargazers

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

Watchers

 avatar  avatar  avatar

dataplaneapi-consul-example's Issues

Is there a way to automate the creation of the frontend

Hello,

I am trying to expose the services automatically via HAProxy, I am using the Consul K8S sync mechanism.

Currently I am having a couple of problems:

  • All services get added to HAProxy config, I have not seen a way to filter them.
  • I have also not seen a way to automate the creation of the frontend config.

Any hints how to solve these issues?

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.