CentOS 6.4
WEB节点一:192.168.1.10
WEB节点二:192.168.1.11
WEB节点三:192.168.1.12
LVS_VIP:192.168.1.200
LVS_Master:192.168.1.14
LVS_Slave: 192.168.1.15
――安装Apache
注:本次在WEB节点一执行安装配置 [其他节点安装配置一样]
1)下载apr
[root@CentOS Linux]# wget http://apache.fayea.com/apache-mirror//apr/apr-1.4.8.tar.gz
2)解压并安装apr
[root@CentOS Linux]# tar zfvx apr-1.4.8.tar.gz [root@CentOS Linux]# cd apr-1.4.8 [root@CentOS apr-1.4.8]# ./configure [root@CentOS apr-1.4.8]# make && make install
3)下载apr-util
[root@CentOS apr-1.4.8]# cd .. [root@CentOS Linux]# wget http://apache.fayea.com/apache-mirror//apr/apr-util-1.5.2.tar.gz
4)解压并安装apr-util
[root@CentOS Linux]# tar zfvx apr-util-1.5.2.tar.gz [root@CentOS Linux]# cd apr-util-1.5.2 [root@CentOS apr-util-1.5.2]# ./configure --with-apr=/usr/local/apr/ [root@CentOS apr-util-1.5.2]# make && make install
5)下载Apache
[root@CentOS apr-util-1.5.2]# cd .. [root@CentOS Linux]# wget http://mirrors.hust.edu.cn/apache//httpd/httpd-2.4.6.tar.gz
6)解压并安装Apache
[root@CentOS Linux]# tar zfvx httpd-2.4.6.tar.gz [root@CentOS Linux]# cd httpd-2.4.6 [root@CentOS httpd-2.4.6]# ./configure --prefix=/usr/local/apache-2.4 \ --enable-so \ --enable-deflate \ --enable-expires \ --enable-rewrite \ --enable-authn-dbm=shared \ --enable-cache \ --enable-file-cache \ --enable-mem-cache \ --enable-disk-cache \ --enable-ssl \ --with-ssl \ --with-pcre \ --enable-static-support \ --enable-static-htpasswd \ --enable-static-logresolve \ --enable-static-rotatelogs \ --enable-module=so [root@CentOS httpd-2.4.6]# make && make install
7)添加启动 脚本
[root@CentOS httpd-2.4.6]# cp /usr/local/apache-2.4/bin/apachectl /etc/init.d/httpd [root@CentOS httpd-2.4.6]# vi /etc/init.d/httpd # chkconfig: 2345 85 15 # description: Activates/Deactivates Apache Web Server 一定要添加上面两行,不然无法启动 [root@CentOS httpd-2.4.6]# chmod +x /etc/init.d/httpd [root@CentOS httpd-2.4.6]# chkconfig --add httpd [root@CentOS httpd-2.4.6]# chkconfig --level 35 httpd on
8)启动以及启动错误解决办法
[root@CentOS httpd-2.4.6]# service httpd start [root@CentOS httpd-2.4.6]# netstat -antup |grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 26152/httpd 1.启动时显示: AH00557: httpd: apr_sockaddr_info_get() failed for CentOS AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message 解决办法: [root@CentOS httpd-2.4.6]# vi /usr/local/apache-2.4/conf/httpd.conf 将ServerName前的“#”去掉 2.启动失败 [root@CentOS apache-2.4]# tail logs/error_log查看错误日志 AH01177: Failed to lookup provider 'shm' for 'slotmem': is mod_slotmem_shm loaded?? 解决办法: [root@CentOS apache-2.4]# vi conf/httpd.conf 把注释去掉 #LoadModule slotmem_shm_module modules/mod_slotmem_shm.so LoadModule slotmem_shm_module modules/mod_slotmem_shm.so
――搭建LVS环境
1)加载ip_vs模块
[root@master Linux]# modprobe ip_vs
2)安装所需依赖包
[root@master Linux]# yum install libnl-devel libnl kernel-devel -y [root@master Linux]# tar zfvx popt-1.16.4.tar.gz [root@master Linux]# cd popt-1.16 [root@master popt-1.16]# ./configure [root@master popt-1.16]# make && make install
3)安装ipvsadm
[root@master popt-1.16]# cd .. [root@master Linux]# tar zfvx ipvsadm-1.26.tar.gz [root@master Linux]# cd ipvsadm-1.26 [root@master ipvsadm-1.26]# make && make install [root@master ipvsadm-1.26]# chkconfig ipvsadm on
4)验证是否安装成功
[root@CentOS ipvsadm-1.26]# ipvsadm IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn
5)安装keealived
[root@master Linux]# tar zfxv keepalived-1.2.8.tar.gz [root@master Linux]# cd keepalived-1.2.8 [root@master keepalived-1.2.8]# ./configure --prefix= [root@master keepalived-1.2.8]# make && make install [root@master keepalived-1.2.8]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ [root@master keepalived-1.2.8]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ [root@master keepalived-1.2.8]# mkdir /etc/keepalived [root@master keepalived-1.2.8]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ [root@master keepalived-1.2.8]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ [root@master keepalived-1.2.8]# chkconfig keepalived on
6)配置keealived
[root@master keepalived-1.2.8]# vi /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { [email protected] } notification_email_from [email protected] 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#priority越高越优先 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.200 #虚拟IP } } virtual_server 192.168.1.200 80 { delay_loop 6 lb_algo rr lb_kind DR nat_mask 255.255.255.0 persistence_timeout 50 protocol TCP real_server 192.168.1.10 80 { weight 100 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } real_server 192.168.1.11 80 { weight 100 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } real_server 192.168.1.12 80 { weight 100 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } } 注意Lvs-backup端同样配置,只需要修改priority 100为priority 50即可。
7)配置ipvsadm规则 [master slave节点执行]
[root@matser keepalived-1.2.8]# ipvsadm -C [root@matser keepalived-1.2.8]# ipvsadm -A -t 192.168.1.200:80 -s rr -p 600 [root@matser keepalived-1.2.8]# ipvsadm -a -t 192.168.1.200:80 -r 192.168.1.10:80 -g [root@matser keepalived-1.2.8]# ipvsadm -a -t 192.168.1.200:80 -r 192.168.1.11:80 -g [root@matser keepalived-1.2.8]# ipvsadm -a -t 192.168.1.200:80 -r 192.168.1.12:80 -g [root@matser keepalived-1.2.8]# ipvsadm -S
8)配置 VIP [WEB节点也需要配置]
[root@matser Linux]# cd /etc/sysconfig/network-scripts/ [root@matser network-scripts]# cp ifcfg-lo ifcfg-lo:0 [root@matser network-scripts]# vi ifcfg-lo:0 DEVICE=lo:0 IPADDR=192.168.1.200 NETMASK=255.255.255.255 ONBOOT=yes NAME=loopback
9)在WEB节点上配置内核参数
[root@CentOS Linux]# vi /etc/sysctl.conf net.ipv4.conf.lo.arp_ignore = 1 net.ipv4.conf.lo.arp_announce = 2 net.ipv4.conf.all.arp_ignore = 1 net.ipv4.conf.all.arp_announce = 2 [root@CentOS Linux]# sysctl -p
10)在WEB节点添加路由
[root@CentOS network-scripts]# route add -host 192.168.1.254 dev lo:0
11)启动keepalived和apache
[root@CentOS ~]# service httpd restart [root@matser~ ]# service keeplaived restart