• Ubuntuに関連したハード、ソフトの備忘録サイトです

LVSによる負荷分散対策を取り入れたSOHOネットワークの構築方法(基礎編)

  • 2013年10月29日

最近のネットワークは相当複雑なものになっています。一説に、Googleのデータセンターでは少なくも30万台のLinuxサーバーが有機的に稼働していると言われています。サイト管理者が以前に所属していた会社ではインターネット・サーバーが1台しか無く、夜中に電話がかかってくると相当緊張したものです。やはり、外部にサーバーを公開する場合は、冗長化対策と負荷分散対策が欠かせません。Linuxには死活監視をするkeepalivedと負荷分散をするためのロードバランサー(LVS=Linux Virtual Server=)がついています。試してみました。

これについては、「Linux Virtual ServerとKeepalivedで作る冗長化ロードバランサ」に詳述されています。基本的にはRedhat/CentOS用ですが、もちろん、Ubuntuでも問題なく構成できるようです。実際に、Ubuntuサーバー兼デスクトップで構成してみました。

lb

 

サイト管理者の宅内では、GMO(とくとくBB)からNTT東日本のBフレッツマンションタイプに固定IPを割り当ててもらっています。やはり、Dynamic DNSはどうも使いづらい。ということで、インターネットと宅内LANを結ぶのはNTT東日本からレンタルしているNEC製のブロードバンドルーターです。それで、宅内LANの構成を表にしてみます。

サーバー名 IPアドレス 用途 その他
ブロードバンドルーター 192.168.1.1 宅内LANのインターネットへの出入口 NAT機能付き固定アドレスとLAN内アドレスを1対1変換
公開サーバー 192.168.1.5 スタンドアロン・サーバー All in Oneサーバー
宅内メインサーバー 192.168.1.2/192.168.10.1 宅内LANの制御サーバー DHCP, LVS, Keepalived、KVM,Samba4,NFSサーバー。NICを二枚差している。
Web1サーバー 192.168.10.10 実験用のWebサーバー WordPress 3.7.1をインストール
Web2サーバー 192.168.10.11 実兼用のWebサーバー WordPress 3.7.1をインストール
DBサーバー 192.168.10.12 実験用のデータベースサーバー WordPressで使うMySQLをインストール

それと、IPアドレスの名前解決に/etc/hostsを使っていましたが、煩雑になるので、Samba4.1とBind9を併せて、ドメイン名it-ishin.comの宅内LANシステムを作ってみました。実際の構築手順とは若干異なるのですが、次の方法で実験システムができています。なお、それぞれのサーバーはOSはUbuntu13.10で統一していますが、ハードはCPUもマザーボードもばらばらです。全部、手作りです。

【1】宅内メインサーバーでのSamba4.1サーバーの構築。

これについては、「Samba4 でのActive Directory Domain Controllerの作成について」をご覧下さい。Sambaは現時点で4.1.0が最新バージョンですが、それをインストールし直しました。注意するべきところは、/usr/local/samba/bin/samba domain provisionでRealmに宅内LANシステムのドメイン名it-ishin.comを指定することです。また、DNS機能はSamba4の内臓機能を使わずに、BIND_FLAT_FILEを選択します。というのは、宅内LAN用のzoneファイルを作ってくれるからです。なお、/usr/local/samba/etc/smb.confがあると失敗してしまうことがあります。

宅内LAN用のzoneファイルは、/usr/local/samba/private/dns/it-ishin.com.zoneですが、そのままではアクセスできないので、sudo ln -sでシンボリックリンクファイルを作り、こちらに上記表のAレコードを登録します。

【2】宅内メインサーバーでのDHCPサーバーの構築

負荷分散の実験を行うには、NICを二枚差しにして外部のネットワーク(192.168.1.0)と内部のネットワーク(192.168.10.0)の二つのネットワークをつくり、ルーティングを行う必要があります。まず、DHCPサーバーをインストールする必要がありますが、$ sudo apt-get install isc-dhcp-serverでインストールします。設定ファイルは/etc/dhcp/dhcpd.confですので、これを適当に書き換えます。サイト管理者の場合は次のようにしました。

#
# Sample configuration file for ISC dhcpd for Debian
#
# Attention: If /etc/ltsp/dhcpd.conf exists, that will be used as
# configuration file instead of this file.
#
## The ddns-updates-style parameter controls whether or not the server will
# attempt to do a DNS update when a lease is confirmed. We default to the
# behavior of the version 2 packages (‘none’, since DHCP v2 didn’t
# have support for DDNS.)
ddns-update-style none;# option definitions common to all supported networks…
option domain-name “it-ishin.com”;
option domain-name-servers 192.168.1.1;default-lease-time 600;
max-lease-time 7200;# If this DHCP server is the official DHCP server for the local

# network, the authoritative directive should be uncommented.
#authoritative;# Use this to send dhcp log messages to a different log file (you also
# have to hack syslog.conf to complete the redirection).
log-facility local7;# No service will be given on this subnet, but declaring it helps the
# DHCP server to understand the network topology.#subnet 10.152.187.0 netmask 255.255.255.0 {
#}# This is a very basic subnet declaration.#subnet 10.254.239.0 netmask 255.255.255.224 {
# range 10.254.239.10 10.254.239.20;
# option routers rtr-239-0-1.example.org, rtr-239-0-2.example.org;
#}# This declaration allows BOOTP clients to get dynamic addresses,
# which we don’t really recommend.#subnet 10.254.239.32 netmask 255.255.255.224 {
# range dynamic-bootp 10.254.239.40 10.254.239.60;
# option broadcast-address 10.254.239.31;
# option routers rtr-239-32-1.example.org;
#}# A slightly different configuration for an internal subnet.
#subnet 10.5.5.0 netmask 255.255.255.224 {
# range 10.5.5.26 10.5.5.30;
# option domain-name-servers ns1.internal.example.org;
# option domain-name “internal.example.org”;
# option routers 10.5.5.1;
# option broadcast-address 10.5.5.31;
# default-lease-time 600;
# max-lease-time 7200;
#}# Hosts which require special configuration options can be listed in
# host statements. If no address is specified, the address will be
# allocated dynamically (if possible), but the host-specific information
# will still come from the host declaration.#host passacaglia {
# hardware ethernet 0:0:c0:5d:bd:95;
# filename “vmunix.passacaglia”;
# server-name “toccata.fugue.com”;
#}# Fixed IP addresses can also be specified for hosts. These addresses
# should not also be listed as being available for dynamic assignment.
# Hosts for which fixed IP addresses have been specified can boot using
# BOOTP or DHCP. Hosts for which no fixed address is specified can only
# be booted with DHCP, unless there is an address range on the subnet
# to which a BOOTP client is connected which has the dynamic-bootp flag
# set.
#host fantasia {
# hardware ethernet 08:00:07:26:c0:a5;
# fixed-address fantasia.fugue.com;
#}# You can declare a class of clients and then do address allocation
# based on that. The example below shows a case where all clients
# in a certain class get addresses on the 10.17.224/24 subnet, and all
# other clients get addresses on the 10.0.29/24 subnet.#class “foo” {
# match if substring (option vendor-class-identifier, 0, 4) = “SUNW”;
#}#shared-network 224-29 {
# subnet 10.17.224.0 netmask 255.255.255.0 {
# option routers rtr-224.example.org;
# }
# subnet 10.0.29.0 netmask 255.255.255.0 {
# option routers rtr-29.example.org;
# }
# pool {
# allow members of “foo”;
# range 10.17.224.10 10.17.224.250;
# }
# pool {
# deny members of “foo”;
# range 10.0.29.10 10.0.29.230;
# }
#}

subnet 192.168.10.0 netmask 255.255.255.0 {
# デフォルトゲートウェイ指定
option routers 192.168.10.1;
# サブネットマスク指定
option subnet-mask 255.255.255.0;
# 貸し出すIPアドレスの範囲指定
range dynamic-bootp 192.168.10.200 192.168.10.254;
}

サブネットマスクの指定のところを適宜、変更する必要があります。

【3】ロードバランサーのインストール

これは、$ sudo apt-get install ipvsadmでインストールできます。設定ファイルはサイト管理者の場合、/root/ip.shファイルを次のように作りました。

#!/bin/bash
/sbin/ipvsadm -C
/sbin/ipvsadm -A -t 192.168.1.120:80 -s rr
/sbin/ipvsadm -a -t 192.168.1.120:80 -r 192.168.10.10:80 -m
/sbin/ipvsadm -a -t 192.168.1.120:80 -r 192.168.10.11:80 -m
/sbin/ipvsadm -Ln
/etc/init.d/ipvsadm save

192.168.1.120というのは、eth1のエイリアス(影武者)です。NICには複数のIPアドレスを設定できるのですが、ロードバランサーを使う場合は、このエイリアスを使うようです。ちなみに、サイト管理者の/etc/network/interfaceは次のようになっています。メインサーバーでは、KVMを使っているためbr0がeth1に相当し、br0:0にエイリアスを割り当てています。

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).# The loopback network interface
auto lo
iface lo inet loopback# The primary network interface
auto eth0
iface eth0 inet manualauto br0
iface br0 inet static
address 192.168.1.2
netmask 255.255.255.0
broadcast 192.168.1.255
gateway 192.168.1.1
dns-nameservers 192.168.1.2 192.168.1.1
dns-search it-ishin.com
bridge_ports eth0
bridge_fd 9
bridge_hello 2
bridge_maxage 12
bridge_stp off

auto br0:0
iface br0:0 inet static
address 192.168.1.120
netmask 255.255.255.0
broadcast 192.168.1.255
gateway 192.168.1.1
dns-nameservers 192.168.1.2 192.168.1.1
dns-search it-ishin.com
bridge_ports eth0
bridge_fd 9
bridge_hello 2
bridge_maxage 12
bridge_stp off

auto p10p1
iface p10p1 inet static
address 192.168.10.1
netmask 255.255.255.0
broadcast 192.168.10.255
network 192.168.10.0
gateway 192.168.1.1
dns-nameservers 192.168.1.1
pre-up /sbin/sysctl net.ipv4.conf.p10p1.forwarding=1
pre-up /sbin/iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -j MASQUERADE
# pre-up /etc/init.d/isc-dhcp3-server stop
# post-up /etc/init.d/isc-dhcp3-server start
post-down /sbin/iptables -t nat -D POSTROUTING -s 192.168.10.0/24 -j MASQUERADE
post-down /sbin/sysctl net.ipv4.conf.p10p1.forwarding=0

p10p1はPCIに取り付けたNICの名前で、上図ではeth0に相当します。このメインサーバーのマザーボードでは、NICの名前がeth0とかeth1にならないので、注意が必要です。どちらがどちらかわからなくなるので、注意が必要です。大事なことは、外付けのNICに対してパケット転送機能を有効にすることです。なお、/etc/sysctl.confの設定ファイルで、net.ipv4.ip_forward=1としておきましょう。

【4】スイッチングハブを用意してネットワークケーブルを取り付ける。
スイッチングハブを用意して、メインサーバーの外付けNICのケーブル、Web1, Web2,DBの各サーバーの内蔵NICにつけたケーブルを取りつけます。/etc/network/interfaceの設定は、iface eth0 inet staticと固定IPを設定します。

【5】Web1,Web2, DBサーバーへのWordpressのインストール
通常のインストールと変わりありませんが、①MySQLはデフォルトでは外部ホストからの接続を受け付けない設定(bind_address = 127.0.0.1)が有効になっているのでコメント化して再起動する②外部のクライアントからwww3.it-ishin.comでアクセスするようにするため、各Webサーバーの/etc/hostsファイルに192.168.10.10 www3.it-ishin.com、または192.168.10.11 www3.it-ishin.comと書き込んでおき、Wordpressの管理画面でアクセス名をhttp://www3.it-ishin.com/と指定しておきます。

なお、Web1からインストールすると、Web2ではMySQLのテーブルの作成は省略されるようです。以上で、簡単ですが負荷分散の実験システムは終わりです。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA