Haproxy+keepalived (双机热备+高可用)---可用于虚拟云上 注释:配置相对比lvs+keepalived 简单! 设备资源: Haproxy1:172.16.1.165 Haproxy2:172.16.1.196 VIP : 172.16.1.223 Realserver1:172.16.1.195 Realserver2:172.16.1.155 Author::darkyinliang@163.com 一、首先配置Haproxy1和Haproxy2 (配置一样) tar -zxvf haproxy-1.3.20.tar.gz cd haproxy-1.3.20 # 说明:linux下指定内核的版本
make TARGET=linux26 PREFIX=/usr/local/haproxy make install PREFIX=/usr/local/haproxy cd /usr/local/haproxy 在此目录下创建haproxy.cfg文件: [iyunv@asdasda haproxy]# vim /usr/local/haproxy/haproxy.cfg global log 127.0.0.1 local0 #log 127.0.0.1 local1 notice #log loghost local0 info maxconn 4096 chroot /usr/local/haproxy uid 99 #所属运行的用户uid gid 99 #所属运行的用户组 Daemon #后台运行 nbproc 1 # 开启haproxy线程数(大点处理能力强点) pidfile /usr/local/haproxy/run/haproxy.pid #debug #quiet defaults log global log 127.0.0.1 local3 #日志文件的输出定向 mode http #所处理的类别 option httplog #日志类别 option httpclose option dontlognull option forwardfor option redispatch retries 2 #设置多个haproxy并发进程提高性能 maxconn 2000 #( balance roundrobin #负载均衡算法 stats uri /haproxy-stats #haproxy 监控页面的访问地址 stats auth admin:123456 # 是登录这个页面的管理员:密码 # 可通过 http://localhost/haproxy-stats 访问 contimeout 5000 clitimeout 50000 srvtimeout 50000 listen web 0.0.0.0:80 #运行端口及主机--这个vip就是4个0,因为在keepalived进行配置了! mode http option httpchk GET /index.txt #健康检测 server s1 172.16.1.195:80 cookie 1 check inter 2000 rise 2 fall 5 # 后端主机IP &端口80可换 server s2 172.16.1.155:80 cookie 2 check inter 2000 rise 2 fall 5 # 后端主机IP &端口80可换 #—以上配置分别放到两台haproxy的相同目录下即可!——————————— 1 注释:cookie 1表示serverid为1, 2 check inter 1500 是检测心跳频率 3 rise 2是2次正确认为服务器可用 4 fall 5是5次失败认为服务器不可用 5 weight代表权重 二、配置高可用keepalived(两台机器上基本相同只是修改两行:state BACKUP/MASTER 和优先级priority 120/150) 首先安装keepavlied: tar -zxvf keepalived-1.1.17.tar.gz
安装这个之前,有个细节注意/usr/src/kernels/2.6.18-238.el5-i686/这个下看有东西没! 如果没有:执行yum install kernel-devel -y,有则继续! ln -s /usr/src/kernels/2.6.18-238.el5-i686/ /usr/src/linux cd keepalived-1.1.17 ./configure --prefix=/usr/local/keepalived make make install 安装后,把人家提供好的文件进行对应的复制! ld1#cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ ld1#cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ ld1#mkdir -pv /etc/keepalived mkdir: 已创建目录 “/etc/keepalived” ld1#cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ ld1#ln -s /usr/local/keepalived/sbin/keepalived /sbin/ 1、下面是Haproxy1:172.16.1.165 的配置(keepalived.conf) [iyunv@gfhhg keepalived]# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived vrrp_script chk_http_port { script "/etc/keepalived/check_haproxy.sh" interval 2 weight 2 global_defs { router_id LVS_DEVEL } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 80 priority 120 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_http_port } virtual_ipaddress { 172.16.1.223 } } } 2、下面是Haproxy2:172.16.1.196 的配置: [iyunv@asdasda haproxy]# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived vrrp_script chk_http_port { script "/etc/keepalived/check_haproxy.sh" interval 2 weight 2 global_defs { router_id LVS_DEVEL } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 80 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_http_port } virtual_ipaddress { 172.16.1.223 } } } 3、由于上面配置的keepalived.conf 含有一个脚本check_haproxy.sh -----(此脚本目的是启动keepalived的同时就进行对haproxy进行检测---如果发现haproxy没起来就直接拉动haproxy配置文件一起启动!) 下面贴出脚本的内容 如下创建: [iyunv@asdasda haproxy]# vim /etc/keepalived/check_haproxy.sh #!/bin/bash A=`ps -C haproxy --no-header |wc -l` if [ $A -eq 0 ];then /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.cfg echo "启动haproxy" &> /dev/null sleep 3 if [ `ps -C haproxy --no-header |wc -l` -eq 0 ];then /etc/init.d/keepalived stop echo "关闭keepalived" &> /dev/null fi Fi 注意: 这个脚本放在keepalived中对应的目录/etc/keepalived/下,方可生效! Chmod +x /etc/keepalived/check_haproxy.sh 4、启动keepalived Service keepalived start 三、加上日志支持(对haproxy的访问日志支持) 1、编辑/etc/syslog.conf在最下边增加 Vim /etc/syslog.conf 在这文本最后添加以下两行内容: local3.* /var/log/haproxy.log local0.* /var/log/haproxy.log 2、编辑 /etc/sysconfig/syslog修改: Vim /etc/sysconfig/syslog 修改下面这个内容: SYSLOGD_OPTIONS="-r -m 0" 3、重启日志服务 #service syslog restart 查看haproxy日志可用: tail -f /var/log/haproxy.log 四、realserver两台配置一样: 1、如下操作 安装普通的apache ---- Yum httpd install -y Service httpd start 以上两个操作在两台realserver均执行! 2、修改主页 3、在Realserver1:172.16.1.195上 Echo 195 > /var/www/html/index.html 在创建一个haproxy检测的文本: Echo 95 > /var/www/html/index.txt 4、在Realserver2:172.16.1.155上 Echo 155 > /var/www/html/index.html 在创建一个haproxy检测的文本: Echo 55 > /var/www/html/index.txt 测试效果是:两台realserver的内容交互出现!--- 六、查看haproxy的状态网页: 结果图为如下所示: file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\ksohtml\wps_clip_image-32460.png 以上操作便完成了整个架构的搭建!
|