安装:
1、rpmbuild -tb haproxy-1.6.11.tar.gz
2、cd ~/rpmbuild/RPMS/x86_64/
3、rpm -ivh haproxy-1.6.11-1.x86_64.rpm
4、groupadd -g 200 haproxy
5、useradd -u 200 -g 200 haproxy
6、mv /mnt/haproxy.cfg /etc/haproxy
7、vim /etc/security/limits.conf
8、haproxy - nofile 65535
9、vim /etc/sysctl.conf
# Disable netfilter on bridges.
#net.bridge.bridge-nf-call-ip6tables = 0
#net.bridge.bridge-nf-call-iptables = 0
#net.bridge.bridge-nf-call-arptables = 0
10、/etc/init.d/haproxy start
11、vim /etc/rsyslog.conf #设置日志同步
# Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514
*.info;mail.none;authpriv.none;cron.none;local0.none /var/log/messages
local0.* /var/log/haproxy.log
12、vim /etc/haproxy/haproxy.cfg
stats uri /admin/stats
stats auth admin:admin #管理员登陆密码
# The public 'www' address in the DMZ
frontend public
bind 172.25.34.1:80 name clear #监听地址
#bind 192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem
#acl badlist src 172.25.34.250 #黑名单
acl badlist src 172.25.34.250
acl read method GET
acl read method HEAD
acl write method PUT
acl write method POST
#use_backend static if { hdr_beg(host) -i img }
#redirect location http://172.25.34.1:8080 if badlist#重定向
#use_backend static if { path_beg /img /css }
#use_backend dynamic if { path_beg /images }#访问*/images时分离到动态网页
#use_backend dynamic if { path_end .php }
use_backend static if write
default_backend dynamic
# The static backend backend for 'Host: img', /img and /css.
backend static
balance roundrobin
server web1 172.25.34.2:80 check inter 1000
backend dynamic
balance roundrobin
server web2 172.25.34.3:80 check inter 1000
client测试:
server1
1、scp haproxy-1.6.11-1.x86_64.rpm root@server4:/mnt/
2、vim /etc/haproxy/haproxy.cfg
frontend public
bind *:80 name clear
3、scp haproxy.cfg root@server4:/etc/haproxy/
4、scp limits.conf root@server4:/etc/security/
5、cd /etc/corosync/
6、cp corosync.conf.example corosync.conf
7、vim corosync.conf
compatibility: whitetank ##兼容0.8以前的版本
totem {##totem定义集群内各节点间是怎么通信的,totem本是一种协议,专用于corosync专用于各节点间的协议
version: 2
secauth: off##安全认证
threads: 0##用于安全认证开启的并线程数
interface {
ringnumber: 0##回环号码
bindnetaddr: 172.25.0.0##绑定心跳网段,corosync会自动判断本地网卡上配置的哪个ip地址是属于这个网络的,并把这个接口作为多播心跳信息
mcastaddr: 226.94.1.34##心跳信息组播地址,每个节点的组播地址必须为同一个
mcastport: 5405##组播时使用的端口
ttl: 1##只向外一跳心跳信息,避免组播报文回路
}
}
logging {
fileline: off##指定要打印的行
to_stderr: no##日志信息是否发往错误输出(默认否)
to_logfile: yes ##是否记录日志文件
to_syslog: yes##是否记录于syslog日志-->此类日志记录于/var/log/message中
logfile: /var/log/cluster/corosync.log##日志存放位置
debug: off##只要不是为了排错,最好关闭debug,它记录的信息过于详细,会占用大量的磁盘IO
timestamp: on##是否打印时间戳,利于定位错误,但会产生大量系统调用,消耗CPU资源
logger_subsys {
subsys: AMF
debug: off
}
}
amf {
mode: disabled
}
service {#让pacemaker在corosync中以插件方式启动,需要 在corosync.conf文件中加上如下内容
name: pacemaker
ver: 0
}
8、yum install crmsh-1.2.6-0.rc2.2.1.x86_64.rpm pssh-2.3.1-2.1.x86_64.rpm -y
9、/etc/init.d/haproxy stop#应该把所有的服务都交给集群管理,不然会报错
10、/etc/init.d/corosync start
server1测试:
server4
1、rpm -ivh haproxy-1.6.11-1.x86_64.rpm
2、groupadd -g 200 haproxy
3、useradd -u 200 -g 200 haproxy
4、/etc/init.d/haproxy start
5、yum install pacemaker corosync -y
6、yum install -y crmsh-1.2.6-0.rc2.2.1.x86_64.rpm pssh-2.3.1-2.1.x86_64.rpm
7、/etc/init.d/haproxy stop #应该把所有的服务都交给集群管理,不然会报错
8、/etc/init.d/corosync start
server4测试:
server1:
11、[root@server121 ~]# crm
crm(live)# configure
crm(live)configure# property stonith-enabled=false##因为在这里我们还没有对fence进行配置,先将stonith-enabled 设置为 false,表示资源不会迁移
crm(live)configure# commit
crm(live)configure# primitive vip ocf:heartbeat:IPaddr2 params ip=172.25.254.10 cidr_netmask=24 op monitor interval=30s##添加vip资源,params 指定参数 op monitor 监控配置,interval指定执行操作的频率,单位:秒
crm(live)configure# commit #提交
12、crm node standby##将所有资源从节点上移走,执行此命令后我们会看到,server4接替了server1的工作
13在server4上验证,发现server4接管了vip
14、 crm node online #在这里要注意,当另一个节点online后,节点间不会进行回切,这样避免了因资源接管导致的资源丢失
[root@server121 ~]# crm
crm(live)# configure
crm(live)configure# property no-quorum-policy=ignore #我们在这里设置为ignore做实验,这时即使一个节点挂掉了,另一个节点也会正常工作
crm(live)configure# commit
15、 [root@server121 ~]# crm
crm(live)# configure
crm(live)configure# primitive haproxy lsb:haproxy op monitor interval=1min
crm(live)configure# group xinhao vip haproxy #创建集群
crm(live)configure# commit
crm(live)configure# quit
16、[root@server1 network-scripts]# crm
crm(live)# configure
procrm(live)configure# property stonith-enabled=true
crm(live)configure# primitive vmfence stonith:fence_xvm params pcmk_host_map=”server1:server1;server4:server4” op monitor interval=1min #主机名与节点名对应关系
crm(live)configure# commit
16、/etc/init.d/corosync restart
测试:
关闭server1或server4可正常访问: