|
1
|
[[email protected] ~]# yum install haproxy
|
|
1
|
mode {tcp|http}
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend service *:80
default_backend app
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend app
balance roundrobin
server app1 192.168.2.7:80 maxconn 3000 weight 2
server app2 192.168.2.18:80 maxconn 3000 weight 1
server app3 127.0.0.1:8080 backup
|
|
1
2
|
[[email protected] ~]# vim /web/index.html
<h2>server node1</h2>
|
|
1
2
|
[[email protected] ~]# vim /web/index.html
<h2>server node2</h2>
|
|
1
2
3
4
5
|
backend <name>
balance uri
hash-type consistent
server ....
server ...
|
|
1
2
3
4
5
|
backend app
balance roundrobin
server app1 192.168.2.7:80 maxconn 3000 weight 2 check inter 1 rise 1 fall 2
server app2 192.168.2.18:80 maxconn 3000 weight 1 check inter 1 rise 1 fall 2
server app3 127.0.0.1:8080 backup
|
|
1
2
3
4
5
6
7
8
9
10
|
listen stats
mode http
bind 192.168.1.116:1080 #监听端口
stats enable #启用state功能
stats scope app #统计报告的报告区段,不启动这项则报告所有区段
stats hide-version #隐藏HAProxy版本号
stats uri /haproxyadmin?stats #state页面的访问路径
stats realm Haproxy\ Statistics #认证时提示信息
stats auth baby:baby #认证的账号密码
stats admin if TRUE #启用管理功能
|
|
1
2
|
[[email protected] web]# service nginx stop
Stopping nginx: [ OK ]
|
|
1
2
3
4
5
|
backend app
balance roundrobin
cookie babyserver insert nocache indirect
server app1 192.168.2.17:80 check port 80 cookie app1
server app2 192.168.2.16:80 check port 80 cookie app2
|
|
1
2
3
|
backend app
.......
option forwardfor header X-Client
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
[[email protected] haproxy]# vim haproxy.cfg
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000 #最大并发连接数
user haproxy #运行haproxy的用户
group haproxy #运行haproxy的组
daemon #后台运行
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http #工作模式
log global #使用全局日志
option httplog
option dontlognull
option http-server-close #允许客户端关闭连接
option forwardfor except 127.0.0.0/8
option redispatch #某上游服务器故障,重新将发往该服务器的请求发往其他的server。
retries 3 #请求重试次数
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
?#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend service 192.168.1.200:80
option httpclose #允许服务器端被动关闭连接
option logasap
capture request header Host len 20
capture request header Referer len 60
#定义ACL,以下两个ACL是获取静态请求
acl url_static path_beg -i /static /images /javascript /stylesheets acl url_static path_end -i .jpg .gif .png .css .js
use_backend static if url_static #将静态请求定向至static
default_backend app #非静态请求定向至app
option forwardfor header X-Client #添加内容为客户端IP的首部
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
balance roundrobin
server first 192.168.2.7:80 check port 80 maxconn 3000
server second 192.168.2.18:80 check port 80 maxconn 3000
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
balance roundrobin
option forwardfor header X-Client
server app1 192.168.2.17:80 check port 80 maxconn 3000
server app2 192.168.2.16:80 check port 80 maxconn 3000
server app3 127.0.0.1:8080 backup
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
[[email protected] ~]# vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1234
}
virtual_ipaddress {
192.168.1.200
}
notify_master "/etc/init.d/haproxy start" #提升为主节点时,启动haproxy服务
notify_backup "/etc/init.d/haproxy stop" #降级为备节点时,关闭haproxy服务
notify_fault "/etc/init.d/haproxy stop" #运行出错时,关闭haproxy服务
}
|
|
1
2
3
4
5
6
|
vrrp_instance VI_1 {
state BACKUP #备用节点
......
priority 99 #服务器优先级
.......
}
|
|
1
2
3
4
|
[[email protected] ~]# ansible haproxy -m shell -a 'service haproxy start'
[[email protected] ~]# ansible haproxy -m shell -a 'service keepalived start'
[[email protected] ~]# ansible webstatic -m shell -a 'service nginx start'
[[email protected] ~]# ansible webdynamic -m shell -a 'service httpd start'
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
####server1#####
[[email protected] ~]# vim /var/www/html/index.php
<h1>dynamic server1</h1>
<?php
phpinfo();
?>
####server2#####
[[email protected] ~]# vim /var/www/html/index.php
<h1>dynamic server2</h1>
<?php
phpinfo();
?>
|
|
1
2
3
4
5
6
7
|
####server1#####
[[email protected] web]# vim images/abc.html
<h1>static node1</h1>
####server2#####
[[email protected] web]# vim images/abc.html
<h1>static node2</h1>
|