- 准备工作,配置高可用yum源
-
# Main rhel6.5 server[base]name=Instructor Server Repositorybaseurl=http://172.25.254.250/rhel6.5gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release # LoadBalancer packages [LoadBalancer] name=Instructor LoadBalancer Repository baseurl=http://172.25.254.250/rhel6.5/LoadBalancer gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release # ResilientStorage [ResilientStorage] name=Instructor ResilientStorage Repository baseurl=http://172.25.254.250/rhel6.5/ResilientStorage gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release # ScalableFileSystem [ScalableFileSystem] name=Instructor ScalableFileSystem Repository baseurl=http://172.25.254.250/rhel6.5/ScalableFileSystem gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
- 两台keepalived安装相关组件?
yum install ipvsadm kernel--xen--devel kernel-devel -y
wget http://www.keepalived.org/software/keepalived-1.1.17.tar.gz
tar zxf keepalived-1.1.17.tar.gz
cd keepalived-1.1.17
./configure ----prefix=/usr/local/keepalived ----with--kernel--dir=/kernels/2.6.18-92.。。。 这个目录要安装kernel-devel 哦自己tab
make && make instal
ln --s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/
ln --s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
ln --s /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
3.vim /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 //备用机写BACKUP interface eth0 virtual_router_id 51 priority 150 //权值 BACKUP的要比这小 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.25.254.100 } } virtual_server 172.25.254.100 80 { delay_loop 6 lb_algo rr lb_kind DR //采用路由直连 nat_mask 255.255.255.0 protocol TCP real_server 172.25.254.1 80 { weight 1 TCP_CHECK{ connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } real_server 172.25.254.2 80 { weight 1 TCP_CHECK{ connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } }
4.后端realserver上执行脚本
#!/bin/bash # description: Config realserver lo and apply noarp SNS_VIP=172.25.254.100 /etc/rc.d/init.d/functions case "$1" in start) ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP /sbin/route add -host $SNS_VIP dev lo:0 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 sysctl -p >/dev/null 2>&1 echo "RealServer Start OK" ;; stop) ifconfig lo:0 down route del $SNS_VIP >/dev/null 2>&1 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 "RealServer Stoped" ;; *) echo "Usage: $0 {start|stop}" exit 1 esac exit 0
分别启动realserver的httpd服务整个主页随便hostname什么的测试就可以了
测试方法:在有VIP的主机上 ipvsadm 查看
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.25.254.100:http rr
-> server1.example.com:http Route 1 0 0
-> server2.example.com:http Route 1 0 0
web访问vip 主页来回切换
关闭server2的httpd服务
web访问VIP 直接转到了server1
关闭一台机子的keepalived服务 vip迁移 另一台机子 ipvsadm 出现数据
就是这样keepalived+lvs负载均衡集群到此搭建完成