RHEL6.2平台利用KVM虛擬機搭建高可用負載均衡WEB集群(keepalived+lvs+iscsi+gfs)

←手機掃碼閱讀     火星人 @ 2014-03-04 , reply:0

RHEL6.2平台利用KVM虛擬機搭建高可用負載均衡WEB集群(keepalived+lvs+iscsi+gfs)

RHEL6.2平台利用KVM虛擬機搭建高可用負載均衡WEB集群(keepalived+lvs+iscsi+gfs)

最近學習KVM虛擬化及負載均衡集群技術,參考了很多帖子,在學習的過程中順便將練手的過程記錄如下,希望能給大家些許幫助!
keepalived+lvs+iscsi+gfs搭建高可用負載均衡web集群,主要參考了這個帖子:http://www.linuxidc.com/Linux/2011-12/48409.htm
也不知是不是原作者,謹此感謝!

一、環境搭建及虛擬機部署:

1、虛擬機宿主:
主機名                        IP地址
WEB                                172.16.9.1

RHEL6.2基本伺服器和虛擬化



KVM虛擬機採用橋接方式安裝

網卡橋接:
$cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-br0

修改為如下即可:
$more /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE="eth0"
NM_CONTROLLED="yes"
ONBOOT=yes
HWADDR=5C:F3:FC:94:88:EC
TYPE=Ethernet
BRIDGE=br0
BOOTPROTO=none
PREFIX=27
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
NAME="System eth0"
註:eth0不要設置IP

$more /etc/sysconfig/network-scripts/ifcfg-br0
DEVICE="br0"
ONBOOT=yes
TYPE=Bridge
BOOTPROTO=none
IPADDR=172.16.9.2
GATEWAY=172.16.9.1
DNS1=219.149.6.99



2、虛擬機安裝:

安裝、克隆總計8台虛擬機
虛擬機                                        主機名                                IP
LVS-MASTER                                LVS-MASTER                        172.16.9.10
LVS-BACKUP                                LVS-BACKUP                        172.16.9.11
Mysql-A                                        Mysql-A                                172.16.9.21
Mysql-B                                        Mysql-B                                172.16.9.22
Web-A                                        Web-A                                172.16.9.31
Web-B                                        Web-B                                172.16.9.32
Web-C                                        Web-C                                172.16.9.33
Firesystem                                Firesystem                        172.16.9.41

VIP IP                172.16.9.254

root@Web:~$virt-install \
--name Web-A \
--ram 2048 \
--vcpus=64 \                ##虛擬機CPU個數
--disk path=/var/lib/libvirt/images/Web-A.img,size=11 \
--network bridge=br0 \
--os-variant=rhel6 \
--cdrom /var/lib/libvirt/rhel-server-6.2-x86_64-dvd.iso \
--vnclisten=172.16.9.1 \
--vncport=6901 \
--vnc

安裝過程選RHEL6.2基本伺服器即可

3、克隆虛擬機:

暫停虛擬機以便克隆
root@Web:~$virsh suspend Web-A
開始克隆
root@Web:~$virt-clone --connect qemu:///system --original=Web-A --name=Web-B --file=/var/lib/libvirt/images/Web-B.img
root@Web:~$virt-clone --connect qemu:///system --original=Web-A --name=Web-C --file=/var/lib/libvirt/images/Web-C.img
root@Web:~$virt-clone --connect qemu:///system --original=Web-A --name=Mysql-A--file=/var/lib/libvirt/images/Mysql-A.img
root@Web:~$virt-clone --connect qemu:///system --original=Web-A --name=Mysql-B --file=/var/lib/libvirt/images/Mysql-B.img
root@Web:~$virt-clone --connect qemu:///system --original=Web-A --name=Firesystem --file=/var/lib/libvirt/images/Firesystem.img
root@Web:~$virt-clone --connect qemu:///system --original=Web-A --name=LVS-MASTER --file=/var/lib/libvirt/images/LVS-MASTER.img
root@Web:~$virt-clone --connect qemu:///system --original=Web-A --name=LVS-BACKUP --file=/var/lib/libvirt/images/LVS-BACKUP.img

恢復虛擬機
root@Web:~$virsh resume Web-A


如需強制關閉虛擬機,如下:
root@Web:~$virsh destroy  Web-A


虛擬機克隆完畢后,需修改vnc埠
root@Web:~$vi /etc/libvirt/qemu/Web-B.xml

<graphics type='vnc' port='6902' autoport='no' listen='172.16.9.32'>
      <listen type='address' address='172.16.9.32'/>
    </graphics>

所有克隆虛擬機的VNC埠修改完畢后,從啟libvirtd服務

從啟libvirtd服務
root@Web:~$service libvirtd restart
Stopping libvirtd daemon: [  OK  ]
Starting libvirtd daemon: 15:06:04.222: 51157: info : libvirt version: 0.9.4, package: 23.el6_2.4 (Red Hat, Inc. <http://bugzilla.redhat.com/bugzilla>, 2012-01-06-02:50:25, x86-007.build.bos.redhat.com)
15:06:04.222: 51157: warning : virGetHostname:1957 : getaddrinfo failed for 'Web': Name or service not known
[  OK  ]

出現此警告warning : virGetHostname:1957 : getaddrinfo failed for 'Web': Name or service not known
解決方法:
root@Web:~$vi /etc/hosts
添加
172.16.9.2                Web


root@Web:~$service libvirtd restart
Stopping libvirtd daemon: [  OK  ]
Starting libvirtd daemon: [  OK  ]



啟動全部虛擬機
root@Web:~$virsh start XXX

root@Web:~$virsh list
Id Name                 State
----------------------------------
  1 LVS-MASTER               running
  2 LVS-BACKUP               running
  3 Filesystem                      running
  4 Web-A                                running
  5 Web-A                           running
  6 Web-C                           running
  7 Mysql-A                          running
  8 Mysql-B                         running

虛擬機啟動后網卡無法正常工作,解決方法如下:

kvm虛擬機克隆后網卡無法啟動解決辦法:
1 修改/etc/udev/rules.d/70-persistent-net.rules文件,刪除第一個網卡記錄,並將第二個的NAME="eth1"改為NAME="eth0"
2 如果在/etc/sysconfig/network-scripts/ifcfg-eth0中有配置網卡信息的話,如:#HWADDR="00:0C:29:C8:1A:92",將其註釋。


順便修改主機名和ip地址
修改主機名:
root@Web-A:~$vi /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=LVS-BACKUP
GATEWAY=172.16.9.1

更新系統時間

yum -y install  ntp
ntpdate time.nist.gov
echo "00 01 * * * /usr/sbin/ntpdate time.nist.gov"  > /etc/crontab


重啟系統


全部虛擬機準備好后,開始搭建高可用負載均衡WEB集群!



二、keepalived+lvs搭建高可用負載均衡WEB集群

rhel6.2系統盤裡有ipvsadm-1.25-9.el6.x86_64.rpm
keepalived-1.2.2.tar.gz需要下載http://www.keepalived.org



1、LVS-MASTER及LVS-BACKUP安裝步驟:

登錄LVS-MASTER
root@LVS-MASTER:~$rpm -ivh ipvsadm-1.25-9.el6.x86_64.rpm
root@LVS-MASTER:~$yum -y install kernel-devel
root@LVS-BACKUP:~$yum -y groupinstall "Development tools"
root@LVS-MASTER:~$tar xzvf keepalived-1.2.2.tar.gz
root@LVS-MASTER:~$cd keepalived-1.2.2
root@LVS-MASTER:~$./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.32-220.el6.x86_64/

出錯:
configure: error:
  !!! OpenSSL is not properly installed on your system. !!!
  !!! Can not include OpenSSL headers files.            !!!
解決方法:
root@LVS-MASTER:~$yum -y install openssl-devel

出錯:
checking for poptGetContext in -lpopt... no
configure: error: Popt libraries is required
解決方法:
root@LVS-MASTER:~$yum -y install popt-devel

再次運行
root@LVS-MASTER:~$./configure -- prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.32-220.el6.x86_64/
完畢后出現如下信息:
############################################
Keepalived configuration
------------------------
Keepalived version       : 1.2.2
Compiler                 : gcc
Compiler flags           : -g -O2
Extra Lib                : -lpopt -lssl -lcrypto
Use IPVS Framework       : Yes
IPVS sync daemon support : Yes
IPVS use libnl           : No
Use VRRP Framework       : Yes
Use Debug flags          : No
############################################



root@LVS-MASTER:~$make
root@LVS-MASTER:~$make install
############################################
make -C keepalived install
make: Entering directory `/root/keepalived-1.2.2/keepalived'
install -d /usr/local/keepalived/sbin
install -m 700 ../bin/keepalived /usr/local/keepalived/sbin/
install -d /usr/local/keepalived/etc/rc.d/init.d
install -m 755 etc/init.d/keepalived.init /usr/local/keepalived/etc/rc.d/init.d/keepalived
install -d /usr/local/keepalived/etc/sysconfig
install -m 755 etc/init.d/keepalived.sysconfig /usr/local/keepalived/etc/sysconfig/keepalived
install -d /usr/local/keepalived/etc/keepalived/samples
install -m 644 etc/keepalived/keepalived.conf /usr/local/keepalived/etc/keepalived/
install -m 644 ../doc/samples/* /usr/local/keepalived/etc/keepalived/samples/
install -d /usr/local/keepalived/share/man/man5
install -d /usr/local/keepalived/share/man/man8
install -m 644 ../doc/man/man5/keepalived.conf.5 /usr/local/keepalived/share/man/man5
install -m 644 ../doc/man/man8/keepalived.8 /usr/local/keepalived/share/man/man8
make: Leaving directory `/root/keepalived-1.2.2/keepalived'
make -C genhash install
make: Entering directory `/root/keepalived-1.2.2/genhash'
install -d /usr/local/keepalived/bin
install -m 755 ../bin/genhash /usr/local/keepalived/bin/
install -d /usr/local/keepalived/share/man/man1
install -m 644 ../doc/man/man1/genhash.1 /usr/local/keepalived/share/man/man1
make: Leaving directory `/root/keepalived-1.2.2/genhash'
############################################
安裝文件路徑


2、LVS-MASTER及LVS-BACKUP主機配置keepalived

(1)、LVS-MASTER

root@LVS-MASTER:~$mkdir /etc/keepalived
root@LVS-MASTER:~$ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
root@LVS-MASTER:~$ln -s /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
root@LVS-MASTER:~$ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
root@LVS-MASTER:~$ln -s /usr/local/keepalived/bin/* /bin/
root@LVS-MASTER:~$ln -s /usr/local/keepalived/sbin/* /sbin/
root@LVS-BACKUP:~$modprobe ip_vs
root@LVS-MASTER:~$vi /etc/rc.local
############################################
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local
modprobe ip_vs
############################################


LVS-MASTER主機keepalived.conf文件設置:
root@LVS-MASTER:~$vi /etc/keepalived/keepalived.conf
############################################
! Configuration File for keepalived

global_defs {
   notification_email {
   root@localhost         
   }
   notification_email_from root@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.9.254
    }
}

virtual_server 172.16.9.254 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
#    nat_mask 255.255.255.0
    persistence_timeout 50
    protocol TCP

    real_server 172.16.9.31 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 172.16.9.32 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 172.16.9.33 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}
############################################

啟動keepalived
root@LVS-MASTER:~$service keepalived restart
停止 keepalived:[失敗]
正在啟動 keepalived:[確定]

root@LVS-MASTER:~$chkconfig --level 2345 keepalived on


(2)、LVS-BACKUP

LVS-BACKUP主機keepalived.conf文件設置:
root@LVS-BACKUP:~$vi /etc/keepalived/keepalived.conf
############################################
root@LVS-BACKUP:~$more /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
   root@localhost         
   }
   notification_email_from root@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.9.254
    }
}

virtual_server 172.16.9.254 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
#    nat_mask 255.255.255.0
    persistence_timeout 50
    protocol TCP

    real_server 172.16.9.31 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 172.16.9.32 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 172.16.9.33 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}
############################################

啟動keepalived
root@LVS-BACKUP:~$service keepalived restart
停止 keepalived:[失敗]
正在啟動 keepalived:[確定]

root@LVS-BACKUP:~$chkconfig --level 2345 keepalived on



3、REAL_SERVER(WEB-A、WEB-B、WEB-C)做如下設置:

分別在 REAL_SERVER(WEB-A、WEB-B、WEB-C)上執行以下命令:

# yum groupremove "MySQL Database client" "MySQL Database server"                ##原來安裝了mysql,卸載掉


安裝web服務及php
# yum -y groupinstall "Web Server" "PHP Support"

# vi /etc/sysctl.conf
################################################
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2
################################################

# sysctl -p

# ifconfig eth0:0 172.16.9.254 netmask 255.255.255.255 up
# route add -host 172.16.9.254 dev eth0:0
# echo `hostname` > /var/www/html/index.html
# vi /etc/httpd/conf/httpd.conf
修改
ServerName
# service httpd start




4、測試:

(1)
把所有三個WEB服務全停掉
service httpd stop
service httpd stop
service httpd stop

訪問VIP
root@Web:~$lynx 172.16.9.254

Alert!: Unable to connect to remote host.


Looking up 172.16.9.254 first
Looking up 172.16.9.254
Making HTTP connection to 172.16.9.254
Alert!: Unable to connect to remote host.

lynx: Can't access startfile http://172.16.9.254/

(2)
開啟Test-Web-A服務:
service httpd start
root@Web:~$lynx 172.16.9.254
顯示頁面如下:
Test-Web-A

(3)
停掉Test-Web-A服務,啟動Test-Web-B服務:
service httpd stop
service httpd start


root@Web:~$lynx 172.16.9.254
刷新幾次后,顯示頁面如下:
Test-Web-B

(4)
停掉Test-Web-B服務,啟動Test-Web-C服務:
service httpd stop
service httpd start


root@Web:~$lynx 172.16.9.254
刷新幾次后,顯示頁面如下:
Test-Web-C

(5)
把所有三個WEB服務全啟動
service httpd start
service httpd start
service httpd start

分別用不同ip的主機訪問會發現顯示不同的頁面



今天試驗到此結束,接下來是mysql雙主高可用伺服器的搭建及iscsi+gfs文件系統的搭建。

明天繼續…………
《解決方案》

RHEL6.2的鏡像難找。呵呵。回頭在DEBIAN6。0。4上實驗一下。呵呵
《解決方案》

你的這個只是一個實驗環境中。
《解決方案》

非常不錯的文章,幫頂一下了!
《解決方案》

頂~~~~~~學習一下。。。下周我也實踐一把。。
《解決方案》

非常給力哈。支持!
《解決方案》

mark
《解決方案》

問題是這樣的,我一共用了三台伺服器,其中一台是Direct Server,其餘兩台是Real server。按照網上介紹的方法,進行配置。結果最後訪問http://虛擬IP 總是出現網頁無法訪問的情況,不能轉到實際的伺服器上。而通過ipvsadm命令卻可以看到http訪問已經平均分發到兩台伺服器上了,即inActConn數隨著訪問頁面的增多,也增多。
請各位大俠給支個招。




[火星人 ] RHEL6.2平台利用KVM虛擬機搭建高可用負載均衡WEB集群(keepalived+lvs+iscsi+gfs)已經有670次圍觀

http://coctec.com/docs/service/show-post-4617.html