LoginSignup
14
15

More than 5 years have passed since last update.

Vagrant で複数サーバー立ち上げて、LoadBalancer と Webサーバー を設定する

Last updated at Posted at 2016-02-22

目的

Web サーバーの構築に関して、以下を確認する

『Load Balancer にぶら下がるWebサーバーのいずれかが落ちた時、アクセスは残りのサーバーだけに振り分けられるようになるのか』

今回は vagrant で仮想環境を3つ立ち上げ、1つを LoadBalancer, 2つを Web サーバーとして設定した。
vagrant を使うのが久しぶりだったため、使い方を復習しながら進めた。

やったこと

1. vagrant で1台サーバーを立ち上げてみる

下記内容を基に実施
Vagrant超入門: 基本コマンドとVagrantfileによる設定

しかし、vagrant の環境に http(80番ポート)でアクセスできない事態が発生

→ firewall で弾かれていたため、iptables で80番ポートを解放する ( /etc/sysconfig/iptables )

/etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

→ 今回は CentOS6だったが、7 だと別の問題が発生する模様
VagrantでCentOS7を立てたが、httpアクセスが繋がらない時にやったこと

2. 複数サーバーを立ち上げる

下記内容を基に実施
Vagrant超入門: 複数サーバを一発で立ち上げる!

使用した Vagrantfile の設定

Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2" 

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  # All Vagrant configuration is done here. The most common configuration
  # options are documented and commented below. For a complete reference,
  # please see the online documentation at vagrantup.com.

  # Every Vagrant virtual environment requires a box to build off of.
  config.vm.box = "centos64box"

  config.vm.define "lb01.dev" do |lb01|
    lb01.vm.network :private_network, ip: "192.168.33.10"
  end 

  config.vm.define "web01.dev" do |web01|
    web01.vm.network :private_network, ip: "192.168.33.11"
  end 

  config.vm.define "web02.dev" do |web02|
    web02.vm.network :private_network, ip: "192.168.33.12"
  end 

  config.vm.define "db01.dev" do |db01|
    db01.vm.network :private_network, ip: "192.168.33.13"
  end 
end

3. lb, web × 2 を設定

各サーバーに Apache を install

lb01 に対しては load balancing の設定をした
RailsのDeploy環境+運用を考慮したサーバー構築

/etc/httpd/conf/httpd.conf
<VirtualHost *:80>
    ProxyPass /balancer-manager !
    <Location /balancer-manager>
        SetHandler balancer-manager
    </Location>

    ProxyPass / balancer://192.168.33.10/
    ProxyPassReverse / balancer://192.168.33.10/

    <Proxy balancer://192.168.33.10/>
        BalancerMember http://192.168.33.11/ loadfactor=5
        BalancerMember http://192.168.33.12/ loadfactor=5
    </Proxy>
</VirtualHost>

4. アクセス検証

  • web01, web02 も正常に動いている時

    • → アクセスは両サーバーに振り分けられる
  • web02 の apache のプロセスを突然killしてみる

    • → アクセスはweb01にしか振られなくなった
  • web01 も web02 も落としてみる

    • → Service Temporarily Unavailable ってなりました
14
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
15