Giter Club home page Giter Club logo

lbnr's Introduction

Use nginx and rails config load balance

Source code: https://github.com/zhulinpinyu/lbnr

Requirement

  • docker
  • docker-compose

###I. Build rails app image HomeHelper

require 'socket'

module HomeHelper
  def local_ip
    orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true  # turn off reverse DNS resolution temporarily

    UDPSocket.open do |s|
      s.connect '192.168.99.100', 1
      s.addr.last
    end
  ensure
    Socket.do_not_reverse_lookup = orig
  end
end

Home#index

<h1>Home#index</h1>
<p>Hostname: <%= Socket.gethostname %></p>
<p>IP: <%= local_ip %></p>

Dockerfile

FROM rails:4.2.3
RUN bundle config 'mirror.https://rubygems.org' 'https://ruby.taobao.org'
ADD Gemfile /lbnr/Gemfile
ADD Gemfile.lock /lbnr/Gemfile.lock
RUN cd /lbnr && bundle install
ADD . /lbnr
WORKDIR /lbnr

ENTRYPOINT ["rails","s","-e","production","-b","0.0.0.0"]
CMD ["-p","3000"]

Build lbnr_app image

docker build -t lbnr_app .

###II. Build nginx image Dockerfile

FROM nginx:latest

RUN rm -fr /usr/share/nginx/html
ADD nginx.conf /etc/nginx/conf.d/default.conf

docker-compose.yml

web:
  build: .
  ports:
    - 80:80
  container_name: "web"

Build nginx_web image

docker-compose build

###III. Start 3 App Server docker-compose.yml

app1:
  image: lbnr_app
  command: -p 3001
  ports:
    - "3001:3001"
  container_name: "app1"
app2:
  image: lbnr_app
  command: -p 3002
  ports:
    - "3002:3002"
  container_name: "app2"
app3:
  image: lbnr_app
  command: -p 3003
  ports:
    - "3003:3003"
  container_name: "app3"
docker-compose up

###IV. Start Nginx Web Server docker-compose.yml

web:
  build: .
  ports:
    - 80:80
  container_name: "web"

start web

docker-compose up

###V. Result

1st access

1st-refresh-load-balance

2nd access

2nd-refresh-load-balance.png

3rd access

3th-refresh-load-balance.png

4th access

4th-refresh-load-balance.png

5th access (start next repeat)

5th-refresh-load-balance


Reference:
http://liubin.org/2014/02/18/rails-cluster-with-ruby-load-balancer-using-docker/
https://github.com/Muriel-Salvan/rails-cluster-docker/tree/master/server

lbnr's People

Contributors

zhulinpinyu avatar

Watchers

James Cloos avatar

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.