CloudStackでLVS(Linux Virtual Server)

概要

CloudStackのVMを使ってLVS(Linux Virtual Server)を構成してみます。 CloudStackの場合、機能として負荷分散機能を持っているので、わざわざLVSで構築する必要ないのですが、LPIC304の勉強がてら構築してみました。

参考

https://www.server-world.info/query?os=CentOS_7&p=lvs http://dokuwiki.fl8.jp/01_linux/01_net/23_lvs_on_cloudstack

構成

本当はLVSサーバーにNIC2つ・VLAN2つとかできれば良いのですが、環境がないのでroutingモードで試してみます。 CloudStackの場合、上位のVirtual RouterでNATされるので、NATされた状態で構成を考えます。 LVSサーバーの eth0 へ来た HTTP パケットを Backend01, Backend02 の Webサーバーへ routing 方式で負荷分散します。 Virtual RouterはCloudStackの初期機能です。今回構築したのは、LVSとBackend01とBackend02だけです。

                         (internet)
                             |
                             | グローバルIP
                   +---------+-----------+
                   |  Virtual Router     |
                   +---------+-----------+
                             | 192.168.0.1/24 GW
                             |
                             |
                             |
                         eth0|192.168.0.223/24
                      eth0:0 |192.168.0.100/24
                       +----------+
                       |    LVS   |
                       +-----+----+
                             |
+------------+               |                 +------------+
|  Backend01 |192.168.0.62/24| 192.168.0.243/24|  Backend02 |
| Web Server +------------+--------------------+ Web Server |
|            |eth0                        eth0 |            |
+------------+                                 +------------+

lvsサーバー:インストール・設定

cloudStackでの操作

(1) 事前にセカンダリIP192.168.0.100を付けておく。

(2) グローバルIPのNATの設定で、セカンダリIP 192.168.0.100へNAT設定しておく(送信元NATのグローバルIPではなく、別で用意する)

VIPつける

ip addr add 192.168.0.100/24 dev eth0

firewalld 止める

systemctl status firewalld.service
systemctl stop firewalld.service
systemctl disable firewalld.service
systemctl status firewalld.service

ipvsadmの設定

yum -y install ipvsadm

ip_forwardを有効にする
echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
[root@mukkun-lvs ~]# sysctl -p
net.ipv4.conf.all.arp_notify = 1
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv4.ip_forward = 1
[root@mukkun-lvs ~]# touch /etc/sysconfig/ipvsadm
[root@mukkun-lvs ~]# systemctl start ipvsadm
[root@mukkun-lvs ~]# systemctl enable ipvsadm
Created symlink from /etc/systemd/system/multi-user.target.wants/ipvsadm.service to /usr/lib/systemd/system/ipvsadm.service.
[root@mukkun-lvs ~]# systemctl status ipvsadm
● ipvsadm.service - Initialise the Linux Virtual Server
   Loaded: loaded (/usr/lib/systemd/system/ipvsadm.service; enabled; vendor preset: disabled)
   Active: active (exited) since 木 2020-07-16 21:52:18 JST; 13s ago
 Main PID: 29307 (code=exited, status=0/SUCCESS)

負荷分散の設定

#テーブルクリア
ipvsadm -C

# 仮想サービスを登録
# [ipvsadm -A -t (サービス用IP:ポート) -s (分散方式)]
ipvsadm -A -t 192.168.0.100:80 -s lc

# バックエンドサーバーを登録
# [ipvsadm -a -t (サービス用IP:ポート) -r (実サーバのIP:ポート) -g] (g で gatewaying (routing))
ipvsadm -a -t 192.168.0.100:80 -r 192.168.0.62:80 -g
ipvsadm -a -t 192.168.0.100:80 -r 192.168.0.243:80 -g

# 設定確認
[root@mukkun-lvs ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.0.100:80 lc
  -> 192.168.0.62:80              Route   1      0          0
  -> 192.168.0.243:80             Route   1      0          0

BackEndServer:設定

まずhttpd入れておく

yum -y install httpd
systemctl start httpd
systemctl enable httpd
systemctl status httpd

# わかりやすいようにindex.htmlを作っておきます。
[root@mukkun-backend-1 ~]# cat /var/www/html/index.html
<h1><font color="red">backend-1 192.168.0.62/24</font></h1>


[root@mukkun-backend-2 ~]# cat /var/www/html/index.html
<h1>backend-2 192.168.0.243/24</h1>

パケットを受け付ける準備をする

LVSサーバ側の設定が終わっていると、「グローバルIP → 192.168.0.100」への通信がそのままBackend01、Backend02へ転送されます。 そのままだと、自分宛てと気づけずにパケットを処理する事ができません。つまり、上位から流れてきたパケットは宛先IPが192.168.0.100宛で流れてきます。そのため、以下のどちらかの方法でパケットを処理できるようにします。

(1) iptables(firewalld)のPREROUTINGチェインにREDIRECTオプションを追加する (2) Backend01、Backend02にlvsサーバーと同じVIPを付けて、擬似的に通信を受けて返す

(1)のパターンの設定

firewalld使う場合
firewall-cmd --permanent --direct --add-rule ipv4 nat PREROUTING 100 -d 192.168.0.100 -j REDIRECT
firewalld --reload
firewalldを使わない場合
firewalldサービス止める
yum -y install iptables-services
iptables -t nat -I PREROUTING -d 192.168.0.100 -j REDIRECT
iptables -I INPUT -p ALL -s 192.168.0.0/24 -j ACCEPT

(2)のパターンの設定

firewalld 止める
systemctl status firewalld.service
systemctl stop firewalld.service
systemctl disable firewalld.service
systemctl status firewalld.service
VIPつける(ループバックアドレスを設定)
ip addr add 192.168.0.100/24 dev eth0

ループバックアドレスを設定すると、arp関係で問題が出るので、 下記の設定を入れてあげる。

cat << EOF >> /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
EOF

sysctl -p

これでグローバルIPでブラウザから接続すると、パケットがBackend01、Backend02へ転送されて、それぞれのWebページが表示されます。

f:id:mukkun0824:20200716232332p:plain f:id:mukkun0824:20200716232338p:plain