本文共 6529 字,大约阅读时间需要 21 分钟。
web3=192.168.158.118
img2=192.168.158.120
安装配置过程
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 1、安装配置过程,(两台Haproxy的安装配置步骤一样) haproxy下载地址:http: //haproxy .1wt.eu /download/1 .4 /src/haproxy-1 .4.24. tar .gz #cd /usr/local/src #wget http://haproxy.1wt.eu/download/1.4/src/haproxy-1.4.24.tar.gz #tar xf haproxy-1.4.24.tar.gz #cd haproxy-1.4.24 #make TARGET=linux26 ARCH=x86_64 #编译参数参考README,其中TARGET是指定内核版本,ARCH指定CPU架构, #make install 2、安装完毕后,创建配置文件和启动文件。 #mkdir /etc/haproxy #cp examples/haproxy.cfg /etc/haproxy #cp examples/haproxy.init /etc/init.d/haproxy #chmod +x /etc/init.d/haproxy #ln -s /usr/local/sbin/haproxy /usr/sbin/ |
3、编辑配置文件(两台Haproxy配置文件相同)
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 | # vim /etc/haproxy/haproxy.cfg # this config needs haproxy-1.1.28 or haproxy-1.2.1 global log 127.0.0.1 local0 #日志输出配置,所有日志都记录在本机,通过local0输出 log 127.0.0.1 local1 notice #log loghost local0 info maxconn 65535 #最大连接数 chroot /usr/local/ #改变当前工作目录。 uid 99 #所属用户的uid gid 99 #所属运行的gid daemon #以后台形式运行haproxy defaults log global mode http #默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK option dontlognull option httplog retries 3 #两次连接失败就认为是服务器不可用 option redispatch #当serverId对应的服务器挂掉后,强制定向到其他健康的服务器 option abortonclose #当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接 maxconn 65535 #默认的最大连接数 #timeout http-keep-alive 10s # timeout queue 1m contimeout 5000 #连接超时 clitimeout 50000 #客户端超时 srvtimeout 50000 #服务器超时 timeout check 5s #心跳检测超时 stats refresh 30s #统计页面自动刷新时间 stats uri /stats #统计页面url stats realm baison- test -Haproxy #统计页面密码框上提示文本 stats auth admin:admin123 #统计页面用户名和密码设置 stats hide-version #隐藏统计页面上HAProxy的版本信息 frontend www bind :80 #这里建议使用bind *:80的方式,要不然做集群高可用的时候有问题,vip切换到其他机器就不能访问了。 acl web hdr(host) -i www.baison.com.cn #acl后面是规则名称,-i是要访问的域名,如果访问www.baison.com这个域名就分发到下面的webserver 的作用域。 acl img hdr(host) -i img.baison.com.cn #如果访问img.baison.com.cn就分发到imgserver这个作用域。 use_backend webserver if web use_backend imgserver if img backend webserver #webserver作用域 mode http balance roundrobin #banlance roundrobin 轮询,balance source 保存session值,支持static-rr,leastconn,first,uri等参数 option httpchk /index .html #检测文件,如果分发到后台index.html访问不到就不再分发给它。 server web01 192.168.158.116:80 check inter 2000 fall 3 weight 20 server web02 192.168.158.117:80 check inter 2000 fall 3 weight 10 server web03 192.168.158.118:80 check inter 2000 fall 3 weight 10 backend imgserver mode http option httpchk /index .php balance roundrobin server img01 192.168.149.119:80 check inter 2000 fall 3 server img02 192.168.149.120:80 check inter 2000 fall 3 |
4、启动Haproxy服务,查看状态。
1 | #service haproxy start |
1 2 3 4 5 6 7 8 9 10 | #tar zxvf - 1.2 . 7 .tar.gz #cd keepalived- 1.2 . 7 #./configure --prefix=/usr/local/keepalived #make #make install #cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ #cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ #cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ #mkdir /etc/keepalived #cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ |
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 | #vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { router_id LVS_DEVEL } vrrp_script chk_haproxy { script "/usr/local/keepalived/check_haproxy.sh" interval 2 weight 2 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 5555 } track_script { chk_haproxy #监测haproxy进程状态 } virtual_ipaddress { 192.168.150.222 } } vrrp_instance VI_2 { state BACKUP interface eth0 virtual_router_id 52 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.150.223 } } |
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 | #vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { router_id LVS_DEVEL } vrrp_script chk_haproxy { script "/usr/local/keepalived/check_haproxy.sh" interval 2 weight 2 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 5555 } track_script { chk_haproxy #监测haproxy进程状态 } virtual_ipaddress { 192.168.150.222 } } vrrp_instance VI_2 { state MASTER interface eth0 virtual_router_id 52 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.150.223 } } |
1 2 3 4 5 6 7 8 9 | #vim /usr/local/keepalived/check_haproxy.sh #!/bin/bash if [ $( ps -C haproxy --no-header | wc -l) - eq 0 ]; then /etc/init .d /haproxy start fi sleep 2 if [ $( ps -C haproxy --no-header | wc -l) - eq 0 ]; then /etc/init .d /keepalived stop fi |
1 2 | #servcie haproxy start #service keepalived start |
1 | #ip addr |
1 2 3 4 5 6 7 8 9 10 11 | echo 1024 60999 > /proc/sys/net/ipv4/ip_local_port_range echo 30 > /proc/sys/net/ipv4/tcp_fin_timeout echo 4096 > /proc/sys/net/ipv4/tcp_max_syn_backlog echo 262144 > /proc/sys/net/ipv4/tcp_max_tw_buckets echo 262144 > /proc/sys/net/ipv4/tcp_max_orphans echo 300 > /proc/sys/net/ipv4/tcp_keepalive_time echo 1 > /proc/sys/net/ipv4/tcp_tw_recycle echo 0 > /proc/sys/net/ipv4/tcp_timestamps echo 0 > /proc/sys/net/ipv4/tcp_ecn echo 1 > /proc/sys/net/ipv4/tcp_sack echo 0 > /proc/sys/net/ipv4/tcp_dsack |
转载地址:http://vygda.baihongyu.com/