keepalived高可用集群
keepalived是一个类似于layer3, 4 & 5交换机制的软件,也就是我们平时说的第3层、第4层和第5层交换。Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的web服务器从系统中剔除,当web服务器工作正常后Keepalived自动将web服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的web服务器。
准备工作;
准备四台虚拟机、RS1、RS2、HA1、HA2
修改IP地址:RS1:172.16.26.10 RS2:172.16.26.11
HA1:172.16.26.7 HA2:172.16.26.8
VIP:172.16.26.1
首先配置realserver1
复制一个脚本到starts.sh
vim starts.sh
编辑以下内容
#!/bin/bash## Script to start LVS DR real server.# description: LVS DR real server#. /etc/rc.d/init.d/functionsVIP=192.168.0.219host=`/bin/hostname`case "$1" instart) # 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;;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;;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
修改它的执行权限;chmod +x startrs.sh
执行这个脚本 ./startrs.sh start
在RS2上使用上面同样的命令
可以直接复制这个脚本
scp startrs.sh 172.16.26.11:/root/
然后配置主从节点
准备:1、时间同步
2、SSH双机互信
3、主机名和uname -n 一致,并通过/etc/hosts解析
不再演示
配置好后,下载并安装keepalived包
yum -y --nogpgcheck localinstall keepalived-1.2.7-5.el5.i386.rpm
修改配置文件
vim /etc/keepalived/keepalived.conf
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 99 priority 101 advert_int 1 authentication { auth_type PASS auth_pass password } virtual_ipaddress { 172.16.26.1 } }virtual_server 172.16.26.1 80 { delay_loop 6 lb_algo wlc lb_kind DR nat_mask 255.255.0.0 protocol TCP real_server 172.16.26.10 80 { weight 1 HTTP_GET { url { path / status_code 200 } connect_timeout 2 nb_get_retry 3 delay_before_retry 1 }} real_server 172.16.26.11 80 { weight 1 HTTP_GET { url { path / status_code 200 } connect_timeout 2 nb_get_retry 3 delay_before_retry 1 }}}
将这个配置文件复制到节点2上一份
scp keepalived.conf node2:/etc/keepalived/
只需修改两个内容
states MASTER 改为 states BACKUP
priority 101 改为 priority 100
在两个节点上启动keepalived
service keepalived start
安装ipvs规则
yum -y install ipvsadm