haproxy peacemaker

hHAProxy 提供高可用性、负载均衡以及基于 TCP 和 HTTP 应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。HAProxy 特别适用于那些负载特大的 web 站点, 这些站点通常又需要会话保持或七层处理。HAProxy 运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整 合进您当前的架构中, 同时可以保护你的 web 服务器不被暴露到网络上。

实验环境haproxy部署在host1(172.25.254.1)
后端服务器host2(172.25.254.2) port:80
后端服务器host3(172.25.254.3) port:80
后端服务器host4(172.25.254.4) port:8080

软件下载http://haproxy.1wt.eu/

安装

1.rpm 包方式:

rpmbuild -tb haproxy-1.4.23.tar.gz
rpm -ivh /root/rpmbuild/RPMS/x86_64/haproxy-1.4.23-1.x86_64.rpm

这里写图片描述
这里写图片描述

rpm -qpl rpmbuild/RPMS/x86_64/haproxy-1.6.11-1.x86_64.rpm

这里写图片描述

2源码安装

tar zxf haproxy-1.4.23.tar.gz
cd haproxy-1.4.23
make TARGET=linux26 ARCH=x86_64 USE_PCRE=1 PREFIX=/usr/local/haproxy install

这里采取第一种,但还是要解压tar包,获取配置文件模板

cp /root/haproxy-1.6.11/examples/content-sw-sample.cfg /etc/haproxy/haproxy.cfg

这里写图片描述

二.到这里安装基本完成进行文件配置

1.查看文件,新建haproxy用户

cd /etc/haproxy/
vim haproxy.cfg
   10 global
   11       maxconn       10000
   12       stats socket  /var/run/haproxy.stat mode 600 level admin
   13       log           127.0.0.1 local0
   14       uid             200
   15       gid             200
   16       chroot          /var/empty
   17       daemon

全局设定不用改:这里需要新建haproxy用户并指定uid gid为200

groupadd -g 200 haproxy
useradd -u 200 -g haproxy  haproxy -s /sbin/nologin
id haproxy

这里写图片描述

2.public域是对下边动静的一个管理,这里编写default域

改成下边的代码

 10 global
    11          maxconn         10000
    12          stats socket    /var/run/haproxy.stat mode 600 level admin
    13          log             127.0.0.1 local0
    14          uid             200
    15          gid             200
    16          chroot          /var/empty
    17          daemon
    18  
    19  # The public 'www' address in the DMZ
    20  defaults
    21      mode            http
    22          log             global
    23          option          httplog
    24          option          dontlognull
    25          monitor-uri     /monitoruri
    26          maxconn         8000
    27          timeout client  30s 
    28      
    29          option prefer-last-server
    30          retries         2
    31          option redispatch
    32          timeout connect 5s
    33          timeout server  5s
    34          stats uri       /admin/stats
    35  
    36  frontend public
    37          bind            172.25.254.1:80 name clear
    38          #bind            192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem
    39  
    40          #use_backend     static if { hdr_beg(host) -i img }
    41          #use_backend     static if { path_beg /img /css   }
    42          default_backend static
    43  
    44  # The static backend backend for 'Host: img', /img and /css.
    45  backend static
    46          balance         roundrobin
    47          #option httpchk  HEAD /favicon.ico
    48          server          statsrv1 172.25.254.2:80 check inter 1000
    49          server          statsrv2 172.25.254.3:80 check inter 1000
    50          server          statsrv3 172.25.254.4:8080 check inter 1000
    51  
/etc/init.d/haproxy start
ss -anlpp   ##haproxy 用的80端口

这里写图片描述

实验效果是浏览访问172.25.254.1会轮询后边的3台服务器
curl  172.25.254.1 ##查看

这里写图片描述

haproxy的状态页

172.25.254.1/monitoruri

这里写图片描述
haproxy的监控页

172.25.254.1/admin/stats

这里写图片描述
查看haproxy进程

ps aux

这里写图片描述

3.刚才监控页面是没有加密的,来加密下

vim /etc/haproxy/haproxy.cfg
   34         stats uri       /admin/stats
   35         stats auth     admin:redhat  ##添加认证用户名和密码

这里写图片描述
浏览器测试:
这里写图片描述

3.haproxy日志

vim /etc/rsyslog.conf 

     13 $ModLoad imudp
     14 $UDPServerRun 514

     62 local0.*            /var/log/haproxy.log
/etc/init.d/rsyslog restart

这里写图片描述

客户端访问下

[kiosk@foundation51 Desktop]$ curl  172.25.254.1
<h1>host3:172.25.254.3  port:80</h1>

查看日志

tail -f /var/log/haproxy.log

这里写图片描述

4.备用服务器,haproxy可以用作所有服务器down后自己上

先down后端服务器
这里写图片描述
这里写图片描述
这里写图片描述

编辑配置文件

 52       server          backup    172.25.254.1:8080  backup 

这里写图片描述

haproxy服务器安装httpd些个默认页重启

yum install httpd
echo 'backup server' >/var/www/html/index.html
/etc/init.d/httpd restart

重启报错
这里写图片描述
解决方法,注释掉 /etc/httpd/conf/httpd.conf99行
再重启就ok了
客户端访问

curl 172.25.254.1

这里写图片描述
实验完必须,ok恢复后端的三台服务器

5.访问控制frontend public

vim /etc/haproxy/haproxy.cfg 
 --->
 frontend public
     ...
     40         acl blacklist src    172.25.254.51
     41         http-request deny    if blacklist
     ...
/etc/init.d/haproxy reload

这里写图片描述
客户端测试
这里写图片描述

6访问重定向frontend public

vim /etc/haproxy/haproxy.cfg 
 --->
 frontend public
     ...
     40         acl blacklist src    172.25.254.51
     41         http-request deny    if blacklist
     42         errorloc      403     http://172.25.254.5/
    ...

/etc/init.d/haproxy reload

这里写图片描述
客户端(172.25.254.51)访问被rewrite了
这里写图片描述
客户端(172.25.254.5)访问正常
这里写图片描述
实验完取消设置

7.重写frontend public

vim /etc/haproxy/haproxy.cfg 

 37 frontend public
 38         bind            172.25.254.1:80 name clear

 40         acl blacklist src    172.25.254.51
 41         http-request deny    if blacklist
 42         errorloc      403     http://172.25.254.5/

这里写图片描述

客户端(172.25.254.51)测试,浏览器的话输入172.25.254.1会很快转到172.25.254.5效果不明显
这里写图片描述
客户端(172.25.254.5)测试,没有被定向
这里写图片描述

8.动静分离frontend public

vim /etc/haproxy/haproxy.cfg 

 37 frontend public
 38     bind       172.25.254.1:80 name clear
  ...都是注释
 45     use_backend    dynamic if { path_beg /image   }
 46     default_backend static
 47 

 49 backend static
 50     balance  roundrobin

 52     server  statsrv1 172.25.254.2:80 check inter 1000
 53     server  statsrv2 172.25.254.3:80 check inter 1000



 56 backend dynamic
 57      balance         roundrobin
 58      server          statsrv3 172.25.254.4:8080 check inter 1000

这里写图片描述

172.25.254.4部署

mkdir /var/www/html/image
往 /var/www/html/image目录中放张图片wx.jpg

这里写图片描述

解释下:44行实现了分离,实验效果是当访问172.25.254.1的时候,默认为static静态去转向后台172.25.254.2和172.25.254.3两台服务器,轮询算法,当访问172.25.254.1/image/的时候转向动态172.25.254.4,注意image目录在172.25.254.4 /var/www/html/下

客户端访问:没问题默认是静态,而静态服务器就是host2和host3

curl 172.25.254.1

这里写图片描述

动态测试:似乎不明显,但一定会看到wx.jpg关键词

 curl 172.25.254.1/image/

这里写图片描述

浏览器很明显
这里写图片描述

9.读写分离

37 frontend public
 38     bind            172.25.254.1:80 name clear
 39     acl read  method GET
 40     acl read method HEAD
 41     acl write method PUT
 42     acl write method POST
....都是注释
 50     use_backend     dynamic  if write
 51     use_backend     static  if read
 52     default_backend static
 53 

 55 backend static
 56     balance         roundrobin
 58     server          statsrv1 172.25.254.2:80 check inter 1000
 59     server          statsrv2 172.25.254.3:80 check inter 1000

 61     server          backup    172.25.254.1:8080  backup
 62 backend dynamic         
 63     balance         roundrobin
 64     server          statsrv3 172.25.254.4:8080 check inter 1000

这里写图片描述

将读的请求给172.25.254.2和172.25.254.3,写的请求给172.25.254.4默认为static域名(读)
没错,默认为static域,所以read host2和host3
这里写图片描述

二peacemaker+haproxy

端口冲突问题

vim /etc/haproxy/haproxy.cfg
 37 frontend public
 38         bind            *:80 name clear

这里写图片描述

1.host5也要配置haproxy

方法同之前(工作环境host5的家目录)
由于host5没有rpm-build工具,所以yum install rpm-build
rpmbuild -tb haproxy-1.6.11.tar.gz
cd rpmbuild/RPMS/x86_64/
rpm -ivh haproxy-1.6.11-1.x86_64.rpm
把host1的/etc/haproxy/hsproxy.cfg scp一份到host5:/etc/haproxy/(scp haproxy.cfg [email protected]:/etc/haproxy/)
这里写图片描述
基于上边的haproxy配置

2.host1(haproxy服务器)和host5安装corosync心跳组件和pacemaker集群资源管理器

yum install corosync pacemaker

3进入到/etc/corosync/ 复制配置文件

这里写图片描述

cp /etc/corosync/corosync.conf.example /etc/corosync/corosync.conf
vim /etc/corosync/corosync.conf
  1 # Please read the corosync.conf.5 manual page
     2  compatibility: whitetank
     3  
     4  totem {
     5      version: 2
     6      secauth: off
     7      threads: 0
     8      interface {
     9          ringnumber: 0
    10          bindnetaddr: 172.25.254.0  ##两台pacemaker的网段
    11          mcastaddr: 226.94.1.1  ##广播地址
    12          mcastport: 5405
    13          ttl: 1
    14      }
    15  }
    16  
    17  logging {
    18      fileline: off
    19      to_stderr: no
    20      to_logfile: yes
    21      to_syslog: yes
    22      logfile: /var/log/cluster/corosync.log
    23      debug: off
    24      timestamp: on
    25      logger_subsys {
    26          subsys: AMF
    27          debug: off
    28      }
    29  }
    30  
    31  amf {
    32      mode: disabled
    33}
    34  server {              ##默认没有手动添加
    35      name:pacemaker
    36      ver:0    
    37  }

36行ver:0表示pacemaker作为corosync的插件来运行;
1表示pacemaker作为单一的守护进程来运行,也就是说在启动corosync后还必须手动启动pacemaker守护进程;
这里写图片描述
这里写图片描述

4.把这份文件传到host5的/etc/corosync下

scp /etc/corosync/corosync.conf root@172.25.254.5:/etc/corosync/

这里写图片描述
启动corosync ,pacemaker会随着corosync的启动而自启动(ver:0)

/etc/init.d/corosync start

5.host1和host5安装crm管理工具

yum install pssh-2.3.1-2.1.x86_64.rpm (依赖包)
yum install crmsh-1.2.6-0.rc2.2.1.x86_64.rpm 

这两个包分别放到了各自的家目录下
这里写图片描述
这里写图片描述

6.host1 shell命令行直接输入crm进入集群资源管理器

让host5监控,在host1上配置

host5 shell命令
crm_mon

这里写图片描述

6.1host1配置vip 172.25.254.111/24

先查看集群状态,host1和host5两个节点都在线

[root@host1 haproxy]# crm
crm(live)# status

这里写图片描述

crm(live)# configure 
crm(live)configure# primitive vip ocf:heartbeat:IPaddr2 params ip=172.25.254.111 cidr_netmask=24 op monitor interval=20s
crm(live)configure# commit
提交报错了,先yes保存在说

这里写图片描述
解决错误:

crm(live)configure# property stonith-enabled=false
crm(live)configure# commit

这里写图片描述
进入到resource 查看下vip状态,online host1上

crm(live)configure# cd   
crm(live)# resource   
crm(live)resource# status vip  

这里写图片描述
host5的监控也是这样
这里写图片描述
exit退出crm shell 查看vip却是在host1上
这里写图片描述

6.2添加haproxy服务
crm(live)configure# primitive haproxy lsb:haproxy op monitor interval=30s
crm(live)configure# commit 
crm(live)configure# cd
crm(live)# status    ##查看状态

这里写图片描述

6.3配置组资源web
crm(live)configure# group web vip haproxy 
crm(live)configure# commit 
crm(live)configure# cd
crm(live)# status

这里写图片描述

实验:

目前客户端可以访问172.25.254.111
这里写图片描述
web服务在host1上
这里写图片描述

host1关机,发现vip没有转移(我这里是的)
解决方法

crm(live)configure# property no-quorum-policy="ignore" #只有两个节点所以不竞选
crm(live)configure# commit

这样就好了参考
这里写图片描述

host1关机后web资源组转移host5了
这里写图片描述

host1重新启动,web服务不会在转到host5,host1只是作为备机,关闭host5,web又转移host1上
这里写图片描述
这里写图片描述

7fence机制的应用

关于fence机制的知识自行脑补
由于虚拟机是受宿主机的控制的,所以要从宿主机入手,已经安装了,并且在启用中

貌似安装这些fence-virtd
fence-virtd-libvirt
fence-virtd-multicast
fence-virt
或者yum install fence-*

配置fence

fence_virtd -c

之前做过,所以这里大略的说下
这里写图片描述

systemctl status fence_virtd.service

这里写图片描述
查看端口1229

ss -anplute | grep fence 
#host1和host5两个节点安装 fence-virt.x86_64
yum install fence-virt.x86_64

这里写图片描述

在宿主机的/etc/cluster/下有个认证文件,要把它给两个节点各一份到各自的/etc/cluster下

@@host5

mkdir /etc/cluster
scp root@172.25.254.51:/etc/cluster/* /etc/cluster/

这里写图片描述
@@host1

mkdir /etc/cluster
scp root@172.25.254.51:/etc/cluster/* /etc/cluster/

这里写图片描述

ok,开始部署fence

查看下可获取的devices

stonith_admin -I

这里写图片描述

stonith_admin -M -a fence_xvm    ##添加fence代理进入到crm,将fence配置添加进去,会产生一长串html语言

这里写图片描述

crm
crm(live)configure# primitive vmfence stonith:fence_xvm params pcmk_host_map="host1:host1;host5:host5" op monitor interval=20s
crm(live)configure# commit
crm(live)configure# show

这里写图片描述

不可忽略得两个设置:

property stonith-enabled=true
property no-quorum-policy=ignore

查看配置信息
crm(live)# configure
crm(live)configure# show

实验:关闭host5的网络,很快host5被断电重启
所有服务到host1上了,host5重启后,vmfence在host5上
这里写图片描述

host1制造内核崩溃,所有服务跳到host5上,host1自动重启,重启后,vmfence在host1上

echo c >/proc/sysrq-trigger 

这里写图片描述

猜你喜欢

转载自blog.csdn.net/xixlxl/article/details/80148342
今日推荐