haproxy + fence + pacemaker实现高可用负载均衡

1.将server1和server4之前做过RHCS的相关服务停止开机自启并关闭
在网页luci界面中将两个节点删除
在server1中 chkconfig modcluster|ricci|luci|iscsi off
/etc/init.d/modcluster|ricci|luci|iscsi  stop
在server4中 chkconfig modcluster|ricci|iscsi off
/etc/init.d/modcluster|ricci|iscsi  stop
###############如果是新建的两个虚拟机,可以忽视上面的步骤####################
2.在server1中:下载haproxy的gz包,这里用的是1.6版本的
将下载好的包放入server1的家目录下
3.制作rpm包:
下载 rpm-build : yum install rpm-build -y
执行:rpmbuild -tb haproxy-1.6.11.tar.gz  (编译haproxy的gz包)


cd rpmbuild/RPMS/
cd x86_64/
rpm -qpl haproxy-1.6.11-1.x86_64.rpm  (查看下载后得到的相关文件)
rpm -ivh haproxy-1.6.11-1.x86_64.rpm (安装)


4.解压haproxy的gz包
cd
tar zxf haproxy-1.6.11.tar.gz
cd haproxy-1.6.11
find -name *.spec
cd examples/
cp content-sw-sample.cfg /etc/haproxy/haproxy.cfg(文件名必须这样写,因为/etc/init.d/haproxy要调用haproxy.cfg文件)

groupadd -g 200 haproxy

useradd -u 200 -g 200 -M haproxy

vim /etc/security/limits.conf

最后一行写入:

haproxy - nofile 10000


vim /etc/haproxy/haproxy.cfg
 19 defaults
 20         mode            http
 21         log             global
 22         option          httplog
 23         option          dontlognull
 24         monitor-uri     /monitoruri
 25         maxconn         8000
 26         timeout client  30s
 27
 28         stats uri       /admin/stats
 29
 30         option prefer-last-server
 31         retries         2
 32         option redispatch
 33         timeout connect 5s
 34         timeout server  5s
 35
 36 # The public 'www' address in the DMZ
 37 frontend public
 38         bind            *:80 name clear
 39         #bind            192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem
 40         #use_backend     static if { hdr_beg(host) -i img }
 41         #use_backend     static if { path_beg /img /css   }
 42
 43         acl blacklist src 172.25.6.250 (将172.25.6.250加入黑名单,禁止250访问)
 44
 45         #http-request deny if blacklist
 46
 47         #errorloc 403 http://172.25.6.1:8080    (在出现403错误时,重定向到172.25.6.1的8080端口 )

                          #####需要在172.25.66.1中下载http服务


 48         #redirect location http://www.baidu.com  (在访问172.25.6.1时,重定向到百度)
 49
 50         use_backend     static2 if { path_end -i .php   }#####动静分离######在访问.php结尾的路径时,跳到static2(server3)

##########需要在server3中下载php,下载后重启httpd服务,在/var/www/html/目录中,编辑index.php文件,写入:

<?php

phpinfo();

?>


 51         default_backend static1
 52
 53 # The static backend backend for 'Host: img', /img and /css.
 54 backend static1
 55         balance         roundrobin       (算法)
 56         #balance                source
 57         server          statsrv1 172.25.6.2:80 check inter 1000
 58 backend static2      ####动静分离######
 59         balance         roundrobin
 60         server          statsrv2 172.25.6.3:80 check inter 1000

测试动静分离:在浏览器中输入172.25.66.1/index.php

################################pacemaker###########################################
1.在server1和server4中:下载 pacemaker  corosync


2.在server4中:
cd /etc/corosync
cp corosync.conf.example  corosync.conf


vim corosync.conf
  4 totem {
  5         version: 2
  6         secauth: off
  7         threads: 0
  8         interface {
  9                 ringnumber: 0
 10                 bindnetaddr: 172.25.6.0
 11                 mcastaddr: 226.94.1.6
 12                 mcastport: 5405
 13                 ttl: 1


 35 service {
 36         name: pacemaker
 37         ver:0
 38 }

3.scp /etc/corosync/corosync.conf server4:/etc/corosync
4./etc/init.d/corosync start


5.在server1中:
/etc/init.d/corosync start


6.在srver1和server4中下载crmsh-1.2.6-0.rc2.2.1.x86_64.rpm
pssh-2.3.1-2.1.x86_64.rpm


7.在server4中:
crm_verify -VL
会报错,继续执行下面会解决


执行crm
输入:configure
再输入:property stonith-enabled=false
commit


8.再次crm_verify -VL 就会成功


9.执行crm

configure

crm(live)configure# primitive vip ocf:heartbeat:IPaddr2 params ip=172.25.66.100 cidr_netmask=24 op monitor interval=1min
crm(live)configure# commit
crm(live)configure#

10.执行完这一步,server1中会自动出现172.25.66.100这个ip

11.在server1或者server4中可以使用crm_mon命令监控server1和server4的状态

此时,如果停用server1中的corosync服务,负载均衡将会失效,下面将解决这个问题

12.

13.,现在如果停用其中一个节点,另一个节点将会接替

14.将server4中的haproxy服务配置好

15.在server1中:

16.

17.在server4中用监控命令查看

18.我们可以用命令直接控制正在启用的节点

crm node standby   ##停用正在开启的节点

crm node online      ##开启停用的节点

比如用crm_mon查看正在使用的节点是server4,我们可以在server4中执行停用命令

然后用监控命令查看,server1已经接替,且节点server4处于standby状态

我们可以用启用命令打开节点server4

现在server4就会重新处于online状态了

跟上次做的RHCS一样,做到现在的程度,还不能达到真正的高可用,因为我们一旦将网络关闭或者电源关闭,整个服务就会出问题,因此,与RHCS一样,我们需要配置fence来增强pacemaker的高可用性

19.开启物理机中的fence_virtd服务

20.在server4中:

再将之前设置的false改为true

property stonith-enabled=true

21.用监控命令查看:

此时我们就将pacemaker高可用负载均衡完美的完成了,现在节点server1正在启用,我们可以将server1的内核崩溃,看其是否重启,日过重启成功,则配置成功

猜你喜欢

转载自blog.csdn.net/gd0306/article/details/81415664