需要四台虚拟机
【server1/server2】
yum install -y haproxy
【server2】
vim /etc/haproxy/haproxy.cfg
将63行到最后(86行)的语句全部注释掉,并在最后添上以下内容:
listen westos *:80
balance roundrobin
server web1 172.25.45.3:80 check
server web2 172.25.45.4:80 check
/etc/init.d/haproxy start netstat -antlpe | grep :80
测试:
http://172.25.45.2/
【server3/server4】
yum install -y httpd echo server3.example.com > /var/www/html/index.html##server4写入server4.example.com
开启server3的http服务
开启server4的http服务
网页可在server3与server4之间切换
vim haproxy.cfg
将最后的内容修改为以下:
listen westos *:80
balance roundrobin
server web1 172.25.45.3:80 check weight 1
server web2 172.25.45.4:80 check weight 3
/etc/init.d/haproxy reload
网页http://172.25.45.2/
权重改变,server4.example.com 3次,server3.example.com 1次
vim haproxy.cfg
在58行之后添加以下内容:
stats uri /status
/etc/init.d/haproxy reload
网页http://172.25.45.2/status
vim haproxy.cfg
将最后的内容修改为以下:
frontend westos *:80
default_backend web
backend web
balance roundrobin
server web1 172.25.45.3:80 check weight 1
server web2 172.25.45.4:80 check weight 3
/etc/init.d/haproxy reload
网页http://172.25.45.2/status
vim haproxy.cfg
将最后的内容修改为以下:
frontend westos *:80
default_backend web
backend web
balance roundrobin
server web1 172.25.45.3:80 check weight 1
server web2 172.25.45.4:80 check weight 3
server backup 127.0.0.1:8080 backup
/etc/init.d/haproxy reload
vim /etc/httpd/conf/httpd.conf
将136行的80端口改为8080
/etc/init.d/httpd start echo "Please try again later" > /var/www/html/index.html
测试:
网页http://172.25.45.2/
关闭server3的http服务
此时网页显示server4.example.com
再关闭server4的http服务
此时网页显示Please try again later
若是此时开启server3或server4则网页会切到server3或server4节点上
server3/server4都开启http服务
vim haproxy.cfg
修改最后的内容,将backend web下的 balance 后的值改为source
/etc/init.d/haproxy reload
网页http://172.25.45.2/
锁定在当前页面
vim haproxy.cfg
修改最后的内容,将backend web下的 balance 后的值改为leastconn
/etc/init.d/haproxy reload
网页http://172.25.45.2/
不受权重影响,server3和server4可以平均切换
vim haproxy.cfg
修改最后的内容,将backend web下的 balance 后的值改为roundrobin
vim /etc/rsyslog.conf
取消13、14行的注释,并在61行下面添加以下内容:
local2.* /var/log/haproxy.log
/etc/init.d/rsyslog restart
tail -f /var/log/haproxy.log
刷新网页http://172.25.45.2/
查看是否有日志生成
vim haproxy.cfg
在58行之后之前添加的“stats uri /status”之后添加以下内容:
stats auth admin:westos
/etc/init.d/haproxy reload
网页http://172.25.45.2/status
有一个安全认证,用户名为admin,密码为westos
vim haproxy.cfg
将最后的内容修改为以下:
frontend westos *:80
acl bad src 172.25.45.250
block if bad
default_backend web
backend web
balance roundrobin
server web1 172.25.45.3:80 check weight 1
server web2 172.25.45.4:80 check weight 3
server backup 127.0.0.1:8080 backup
/etc/init.d/haproxy reload
网页http://172.25.45.2/
vim haproxy.cfg
将最后的内容修改为以下:
frontend westos *:80
acl bad src 172.25.45.250
block if bad
errorloc 403 http://172.25.45.2:8080
default_backend web
backend web
balance roundrobin
server web1 172.25.45.3:80 check weight 1
server web2 172.25.45.4:80 check weight 3
server backup 127.0.0.1:8080 backup
/etc/init.d/haproxy reload
网页http://172.25.45.2/
vim haproxy.cfg
将最后的内容修改为以下:
frontend westos *:80
acl bad src 172.25.45.250
# block if bad
# errorloc 403 http://172.25.45.2:8080
redirect location http://172.25.45.3:80 if bad
default_backend web
backend web
balance roundrobin
server web1 172.25.45.3:80 check weight 1
server web2 172.25.45.4:80 check weight 3
server backup 127.0.0.1:8080 backup
/etc/init.d/haproxy reload
网页http://172.25.45.2/
会自动跳转到server3的页面
【server3/server4】
cd /var/www/html/ mkdir admin cd admin/ echo hello > index.html
【server2】
vim haproxy.cfg
将最后的内容修改为以下:
frontend westos *:80
acl bad src 172.25.45.250
acl denyfile path /admin/
# block if bad
# errorloc 403 http://172.25.45.2:8080
# redirect location http://172.25.45.3:80 if bad
http-request deny if denyfile bad
default_backend web
backend web
balance roundrobin
server web1 172.25.45.3:80 check weight 1
server web2 172.25.45.4:80 check weight 3
server backup 127.0.0.1:8080 backup
/etc/init.d/haproxy reload
网页http://172.25.45.2/admin
看不到下面的内容
vim haproxy.cfg
将最后的内容修改为以下:
frontend westos *:80
acl bad src 172.25.45.250
acl denyfile path /admin/
# block if bad
# errorloc 403 http://172.25.45.2:8080
# redirect location http://172.25.45.3:80 if bad
# http-request deny if denyfile bad
default_backend web
backend web
balance roundrobin
server web1 172.25.45.3:80 check weight 1
server web2 172.25.45.4:80 check weight 1
server backup 127.0.0.1:8080 backup
/etc/init.d/haproxy reload
网页http://172.25.45.2/admin
会自动切到server3上的/var/www/html/admin/,显示其下的内容
【server3/server4】
cd /var/www/html/admin echo hello by server3 > index.html ##server4上写入echo hello by server4 > index.html
网页http://172.25.45.2/admin
会在hello by server3和hello by server4中来回切换
vim haproxy.cfg
取消“http-request deny if denyfile bad”的注释
/etc/init.d/haproxy reload
网页http://172.25.45.2/
可以在serrver3和server4中来回切换
网页http://172.25.45.2/admin
不显示内容
vim haproxy.cfg
将最后的内容修改为以下:
frontend westos *:80
acl bad src 172.25.45.250
acl denyfile path /admin/
# block if bad
# errorloc 403 http://172.25.45.2:8080
# redirect location http://172.25.45.3:80 if bad
# http-request deny if denyfile bad
acl url_static path_beg -i /static /p_w_picpaths /javascript /stylesheets
acl url_static path_end -i .jpg .gif .png .css .js
use_backend p_w_picpaths if url_static
default_backend upload
backend p_w_picpaths
balance roundrobin
server web1 172.25.45.3:80 check weight 1
server backup 127.0.0.1:8080 backup
backend upload
server web2 172.25.45.4:80 check weight 1
/etc/init.d/haproxy reload
【server3】
cd /var/www/html/ mkdir p_w_picpaths ##下载一张图片到该目录 ls
网页http://172.25.45.2/
只显示server4.example.com
网页http://172.25.45.2/p_w_picpaths/1.png
vim haproxy.cfg
将最后的内容修改为以下:
frontend westos *:80
acl bad src 172.25.45.250
acl denyfile path /admin/
# block if bad
# errorloc 403 http://172.25.45.2:8080
# redirect location http://172.25.45.3:80 if bad
# http-request deny if denyfile bad
acl url_static path_beg -i /static /p_w_picpaths /javascript /stylesheets
acl url_static path_end -i .jpg .gif .png .css .js
acl read method GET
acl read method HEAD
acl write method PUT
acl write method POST
use_backend p_w_picpaths if url_static
use_backend p_w_picpaths if read
use_backend upload if write
default_backend upload
backend p_w_picpaths
balance roundrobin
server web1 172.25.45.3:80 check weight 1
server backup 127.0.0.1:8080 backup
backend upload
server web2 172.25.45.4:80 check weight 1
/etc/init.d/haproxy reload
所需软件:在upload/目录下,将其放到server4的/var/www/html下,赋予权限chmod 777 upload/
【server3/server4】
yum install -y php /etc/init.d/httpd restart
网页http://172.25.45.2/upload/
【server3】
/etc/init.d/httpd stop
网页http://172.25.45.2/upload/
不显示
/etc/init.d/httpd start
【server4】
cd /var/www/html/upload vim upload_file.php
将第5行的大小改成200000000
cd upload/ mkdir upload chmod 777 upload/
【server2】
tail -f /var/log/haproxy.log
##结合实现集群##
【server1】
/etc/init.d/corosync start
【server2】
/etc/init.d/corosync start crm_mon
【server1】
[root@server1 ~]# crm
crm(live)# resource
crm(live)resource# show
crm(live)resource# stop mygroup
crm(live)resource# show ##同上图,没有停止
crm(live)resource# stop sqldataclone
crm(live)resource# show
crm(live)resource# stop sqlfs
crm(live)resource# cd
crm(live)# configure
crm(live)configure# delete mygroup
crm(live)configure# delete sqldataclone
crm(live)configure# delete sqlfs
crm(live)configure# delete sqldata
crm(live)configure# delete mysql
crm(live)configure# show
【server2】
cd /etc/haproxy scp haproxy.cfg 172.25.45.1:/etc/haproxy
crm_mon
【server1】
crm(live)configure# primitive haproxy lsb:haproxy op monitor interval=30s
crm(live)configure# commit
crm(live)configure# group hagroup vip haproxy
crm(live)configure# commit
网页http://172.25.45.100/
http://172.25.45.100/upload/
http://172.25.45.100/status
crm(live)configure# cd
crm(live)# node
crm(live)node# standby server2.example.com
crm(live)node# online server2.example.com