Rainie999 发表于 2019-1-1 11:03:27

haproxy+pacemaker

  # yum install rpm-build   安装rpm编译工具(有可能需要安装依赖包,根据报错情况)
# rpmbuild-tb haproxy-1.6.11.tar.gz    编译
# cd rpmbuild/
BUILD/   BUILDROOT/ RPMS/      SOURCES/   SPECS/   SRPMS/   
# cd rpmbuild/RPMS/
# ls
x86_64
# cd x86_64/
# rpm -qpl haproxy-1.6.11-1.x86_64.rpm    查看安装文件路径
/etc/haproxy
/etc/rc.d/init.d/haproxy
/usr/sbin/haproxy
/usr/share/doc/haproxy-1.6.11
/usr/share/doc/haproxy-1.6.11/CHANGELOG
/usr/share/doc/haproxy-1.6.11/README
/usr/share/doc/haproxy-1.6.11/architecture.txt
/usr/share/doc/haproxy-1.6.11/configuration.txt
/usr/share/doc/haproxy-1.6.11/intro.txt
/usr/share/doc/haproxy-1.6.11/management.txt
/usr/share/doc/haproxy-1.6.11/proxy-protocol.txt
/usr/share/man/man1/haproxy.1.gz
# rpm -ivh haproxy-1.6.11-1.x86_64.rpm    安装
Preparing...                ###########################################
1:haproxy                ###########################################
# cd
# tar zxf haproxy-1.6.11.tar.gz
# cd haproxy-1.6.11
# ls
CHANGELOG   doc       include      Makefilesrc      VERDATE
contrib       ebtree    LICENSE      README    SUBVERSVERSION
CONTRIBUTINGexamplesMAINTAINERSROADMAP   tests
# find -name *.spec                  #rpmbuild可以编译是因为原文件包有这个工具
./examples/haproxy.spec
# cd examples/
# cp content-sw-sample.cfg/etc/haproxy/haproxy.cfg
# cd/etc/haproxy/
# ls
haproxy.cfg
# grep 200 /etc/passwd
# groupadd -g 200 haproxy
# useradd-u 200 -g 200 -M haproxy
# vim /etc/security/limits.conf
末尾添加:
haproxy          -       nofile          10000
# vim haproxy.cfg
http://i2.运维网.com/images/blog/201808/04/10bf6888fc07f950985073e319214192.pnghttp://i2.运维网.com/images/blog/201808/04/182003067b293bd80083525273823ae6.png
default_backend static

The static backend backend for 'Host: img', /img and /css.
  backend static
balance         roundrobin
server          statsrv1 172.25.135.2:80 check inter 1000
server          statsrv2 172.25.135.3:80 check inter 1000
# /etc/init.d/haproxystart
测试一下网址:
172.25.135.1/amin/statc
安装php实现动态
# yum install php -y
# vim /var/www/html/index.php

http://i2.运维网.com/images/blog/201808/04/a1a160c6e6d4c07ece6c8a6d4cfbdd2b.png
测试172.25.135.1/index.php
# vim haproxy.cfg
http://i2.运维网.com/images/blog/201808/04/b00c99950d96f9b7a9a1e550a78171fb.png
# yum install httpd
# vim /etc/httpd/conf/httpd.conf   改端口为8080
# /etc/init.d/httpd restart
测试 172.25.135.1:8080
# vim haproxy.cfg
http://i2.运维网.com/images/blog/201808/04/f2d9ffa0855ac85ea252d9c178a11aca.png
测试:172.25.135.1/index.php#自动跳到baidu
# vim haproxy.cfg
http://i2.运维网.com/images/blog/201808/04/53119f8004224cf87ae34f6b31c3dd6d.png
在server2和3上安装php下载upload
# mv upload//var/www/html/
# chmod 777 upload/
# mv*..
# vim upload_file.php   将图片默认大小改到合适大小
# /etc/init.d/httpdrestart   #server3同操作
浏览器访问:172.25.135.1/index.php
http://i2.运维网.com/images/blog/201808/04/0c7c90fff760086127aa6712ac865339.png
上传图片则server2无文件server3有上传文件,从而实现读写分离。
# cd upload
# ls
# cd upload
# ls
iso7.gif
从server1将安装包和配置文件发送到server4上
  # scp haproxy-1.6.11-1.x86_64.rpm root@192.168.122.14
  # scp haproxy.cfg root@192.168.122.14:/etc/haproxy/
# pwd
/etc/security
# scp limits.conf root@192.168.122.14:/etc/security/
  # rpm -ivh haproxy-1.6.11-1.x86_64.rpm安装
创建用户,修改文件,开启服务和server1保持一致
# yum install pacemaker corosync -y   安装服务
  #yum install pacemaker corosync -y    安装服务
# vim /etc/corosync/corosync.conf
totem {
version: 2
secauth: off
threads: 0
interface {
ringnumber: 0
bindnetaddr: 172.25.135.0   
mcastaddr: 226.94.1.135       根据情况,如果多用户需改同网络会冲突
mcastport: 5405
ttl: 1
}
}
末尾添加:
service {
name:pacemaker
ver:0
}
# scp corosync.conf server4:/etc/corosync/   发给server4
开启server1,4的服务
# /etc/init.d/corosync start
root@server1 ~]# yum install -y crmsh-1.2.6-0.rc2.2.1.x86_64.rpm pssh-2.3.1-2.1.x86_64.rpm   安装
  # yum install -y crmsh-1.2.6-0.rc2.2.1.x86_64.rpm pssh-2.3.1-2.1.x86_64.rpm   安装
  # crm
  crm(live)# configure
crm(live)configure# show
node server1
node server4
property $id="cib-bootstrap-options" \
dc-version="1.1.10-14.el6-368c726" \
cluster-infrastructure="classic openais (with plugin)" \
expected-quorum-votes="2"
crm(live)configure# property stonith-enabled=false##先关掉fence
crm(live)configure# commit
  # crm_verify -LV   ##关掉fence后不报错
# crm
crm(live)# configure
shoecrm(live)configure# show
node server1
node server4
property $id="cib-bootstrap-options" \
dc-version="1.1.10-14.el6-368c726" \
cluster-infrastructure="classic openais (with plugin)" \
expected-quorum-votes="2" \
stonith-enabled="false"
crm(live)configure# primitive vip ocf:heartbeat:IPaddr2 params ip=172.25.18.100 cidr_netmask=24 op monitor interval=1min
##配置vip heartbeat工具 vip netmask   健康监测时间
crm(live)configure# commit
crm(live)configure# bye
  # vim /etc/haproxy/haproxy.cfg
frontend public
bind            *:80    ##监听所有的80端口(为了监听vip)
# crm_mon ##动态监听
Last updated: Tue Apr 17 13:18:05 2018
Last change: Tue Apr 17 13:12:45 2018 via cibadmin on server4
Stack: classic openais (with plugin)
Current DC: server4 - partition with quorum
Version: 1.1.10-14.el6-368c726
2 Nodes configured, 2 expected votes
1 Resources configured
  Online: [ server1 server4 ]
  vip   (ocf::heartbeat:IPaddr2):   Started server1
# crm
crm(live)# configure
crm(live)configure# show
node server1
node server4
primitive vip ocf:heartbeat:IPaddr2 \
params ip="172.25.18.100" cidr_netmask="24" \
op monitor interval="1min"
property $id="cib-bootstrap-options" \
dc-version="1.1.10-14.el6-368c726" \
cluster-infrastructure="classic openais (with plugin)" \
expected-quorum-votes="2" \
stonith-enabled="false"
crm(live)configure# property no-quorum-policy=ignore    ##宕机后vip可以浮动
crm(live)configure# commit
byecrm(live)configure# bye
# /etc/init.d/corosync stop
# crm_mon##server1转移到server4
  # cd rpmbuild/RPMS/x86_64/
# scp haproxy-1.6.11-1.x86_64.rpm server4:~/
# rpm -ivh haproxy-1.6.11-1.x86_64.rpm
# /etc/init.d/haproxy start
# scp /etc/haproxy/haproxy.cfg server4:/etc/haproxy/
# /etc/init.d/haproxy stop   ##启动集群管理1时一定要关闭haproxy和取消开机自启
Shutting down haproxy:                                    
# chkconfig --list haproxy
haproxy         0:off   1:off   2:off   3:off   4:off   5:off   6:off
  # crm
  crm(live)# configure
crm(live)configure# show
node server1
node server4
primitive vip ocf:heartbeat:IPaddr2 \
params ip="172.25.18.100" cidr_netmask="24" \
op monitor interval="1min"
property $id="cib-bootstrap-options" \
dc-version="1.1.10-14.el6-368c726" \
cluster-infrastructure="classic openais (with plugin)" \
expected-quorum-votes="2" \
stonith-enabled="false" \
no-quorum-policy="ignore"
crm(live)configure# primitive haproxy lsb:haproxy op monitor interval=1min
crm(live)configure# commit
crm(live)configure# group lbgroup vip haproxy       ##建立组,使crm_mon中vip和haproxy在同一server上
crm(live)configure# commit
crm(live)configure# bye
  # crm node standby   ##控制下线
# crm node online      ##控制上线
  # stonith_admin -I   ##检验fence
  # stonith_admin -M -a fence_xvm    ##查看
  # systemctl status fence_virtd.service   ##物理机上开启fence服务
拷贝/etc/cluster/fence_xvm.key 文件给server1和server4,server1和4的cluster为自建的文件夹把fencekey和物理机保持一致,不然虚拟机fence启动不了
  # crm
crm(live)# configure
crm(live)configure# primitive vmfence stonith:fence_xvm params pcmk_host_map="server1:host1;server4:host4" op monitor interval=1min ##设置fence
crm(live)configure# commit
crm(live)configure# bye
  # crm
crm(live)# configure
crm(live)configure# property stonith-enabled=true   ##开启fence
crm(live)configure# commit
crm(live)configure# bye

# crm
crm(live)# resource
crm(live)resource# cleanup vmfence      ##配错fence后清除更新
Cleaning up vmfence on server1
Cleaning up vmfence on server4
Waiting for 1 replies from the CRMd. OK
crm(live)resource# show
Resource Group: lbgroup
vip    (ocf::heartbeat:IPaddr2):   Started
haproxy    (lsb:haproxy):Started
vmfence    (stonith:fence_xvm):    Started
  检测:
# echo c > /proc/sysrq-trigger   ##看fence机制是否成功
echo c > /proc/sysrq-trigger



页: [1]
查看完整版本: haproxy+pacemaker