keepalived:高可用,保持存活,防单点故障。keepalived主要用作realserver的健康检查以及负载均衡主机和backup主机之间的故障漂移。
1.master ---- slave 操作一样
[root@lvs_master ~]# cat /etc/redhat-release
CentOS release 6.5 (Final)
[root@lvs_master ~]# modprobe -l|grep ipvs -------检查linux内核是否集成lvs模块
kernel/net/netfilter/ipvs/ip_vs.ko
kernel/net/netfilter/ipvs/ip_vs_rr.ko
kernel/net/netfilter/ipvs/ip_vs_wrr.ko
kernel/net/netfilter/ipvs/ip_vs_lc.ko
kernel/net/netfilter/ipvs/ip_vs_wlc.ko
kernel/net/netfilter/ipvs/ip_vs_lblc.ko
kernel/net/netfilter/ipvs/ip_vs_lblcr.ko
kernel/net/netfilter/ipvs/ip_vs_dh.ko
kernel/net/netfilter/ipvs/ip_vs_sh.ko
kernel/net/netfilter/ipvs/ip_vs_sed.ko
kernel/net/netfilter/ipvs/ip_vs_nq.ko
kernel/net/netfilter/ipvs/ip_vs_ ftp.ko
kernel/net/netfilter/ipvs/ip_vs_pe_sip.ko
[root@lvs_master ~]# echo "1">/proc/sys/net/ipv4/ip_forward -------开启内核路由转发
[root@lvs_master ~]# setenforce 0 关闭selinux
[root@lvs_master ~]# service iptables stop 关闭iptables
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Flushing firewall rules: [ OK ]
iptables: Unloading modules: [ OK ]
--------------------------------------------------------------------------------------------------------------------
[root@lvs_master ~]# yum install -y ipvsadm
[root@lvs_master ~]# chkconfig ipvsadm on
[root@lvs_master ~]# service ipvsadm start
[root@lvs_master ~]# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
----------------------------------------------------------------------------------------------------------------------
[root@lvs_master ~]# yum -y install kernel-devel gcc openssl popt popt-devel libnl libnl-devel popt-static openssl openssl-devel wget --------安装依赖包和工具
[root@lvs_master ~]# wget http://www.keepalived.org/software/keepalived-1.2.16.tar.gz
[root@lvs_master ~]# tar -zxvf keepalived-1.2.16.tar.gz -C /usr/local/src
[root@lvs_master keepalived-1.2.16]# cd /usr/local/src/keepalived-1.2.16/
[root@lvs_master keepalived-1.2.16]# ./configure --prefix=/usr/local/keepalived-1.2.16 && make && make install
[root@lvs_master keepalived-1.2.16]# cd
[root@lvs_master ~]# ln -s /usr/local/keepalived-1.2.16 /usr/local/keepalived
[root@lvs_master ~]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
[root@lvs_master ~]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@lvs_master ~]# ln -s /usr/local/keepalived/etc/keepalived /etc/
[root@lvs_master ~]# ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/
---------------------------------------------------------------------------------------------------------------------
将keepalived配置成系统服务
[root@lvs_master ~]# chkconfig --add keepalived
[root@lvs_master ~]# chkconfig keepalived on
[root@lvs_master ~]# chkconfig --list keepalived
keepalived 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@lvs_master ~]# service keepalived start
Starting keepalived: [ OK ]
-----------------------------------------------------------------------------------------------------------------------
keepalived配置
[root@lvs_master ~]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
[root@lvs_master ~]# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
zdj1033096859@qq.com # 接受通知邮件的email地址
}
notification_email_from zhang.dajiang@eisoo.com #发送通知邮件的email地址
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_MASTER
}
vrrp_instance VI_1 {
state MASTER #BACKUP上修改为LVS_BACKUP
interface eth0
virtual_router_id 51 #虚拟路由标识,主从相同
priority 100 #BACKUP上修改为90
advert_int 1 #主从心跳检测时间
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.111 #Web虚拟IP(VTP)
}
}
virtual_server 192.168.1.111 80 { #定义虚拟IP和端口
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.1.33 80 {
weight 1
SSL_GET {
url {
path /
digest ff20ad2481f97b1754ef3e12ecd3a9cc
}
url {
path /mrtg/
digest 9b3a0c85a887a256d6939da88aabd8cd
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.1.44 80 {
weight 1
HTTP_GET {
url {
path /testurl/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334c
}
url {
path /testurl2/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334c
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@lvs_master ~]# service keepalived restart
--------------------------------------------------------------------------------------------------------------
2.web1 web2一样
[root@web1 ~]# yum install httpd php php-fpm -y ------安装Apache和php
[root@web1 ~]# vi /etc/ httpd/conf/httpd.conf
ServerName :80
[root@web1 ~]# service httpd restart
[root@web1 ~]# service php-fpm restart
[root@web1 ~]# vi /var/www/html/phpinfo.php
[root@web1 ~]# more /var/www/html/phpinfo.php
<?php
phpinfo()
?>
[root@web1 ~]# vi /var/www/html/index.html
[root@web1 ~]# cat /var/www/html/index.html
<html>
<head>
<title>web1</title>
</head>
<body>
hostname: Web1<br>ip : 192.168.1.33
</body>
</html>
-----------------------------------------------------------------------------------------
[root@web1 ~]# vi dr-rs.sh ------Real Server启动脚本
#!/bin/bash
#
# Script to start LVS DR real server.
# description: LVS DR real server
#
. /etc/rc.d/init.d/functions
VIP=192.168.1.111
host=`/bin/hostname`
case "$1" in
start)
# Start LVS-DR real server on this machine.
/sbin/ifconfig lo down
/sbin/ifconfig lo up
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev lo:0
echo "LVS RealServer Start OK"
;;
stop)
# Stop LVS-DR real server loopback device(s).
/sbin/ifconfig lo:0 down
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
echo "LVS RealServer Stoped OK"
;;
status)
# Status of LVS-DR real server.
islothere=`/sbin/ifconfig lo:0 | grep $VIP`
isrothere=`netstat -rn | grep "lo:0" | grep $VIP`
if [ ! "$islothere" -o ! "isrothere" ];then
# Either the route or the lo:0 device
# not found.
echo "LVS-DR real server Stopped."
else
echo "LVS-DR real server Running."
fi
;;
*)
# Invalid entry.
echo "$0: Usage: $0 {start|status|stop}"
exit 1
;;
esac
[root@web1 ~]# chmod +x dr-rs.sh
[root@web1 ~]# ./dr-rs.sh start
LVS RealServer Start OK