ubuntu搭建keepalived+haproxy高性能WEB服务器
ubuntu搭建keepalived+haproxy高性能WEB服务器
一、架构拓扑图
haproxy
VIP: 192.168.1.100
Master:192.168.1.10
Slave: 192.168.1.11
之间用keepalived实现HA
webserver1:192.168.1.101
webserver2:192.168.1.102
webserver3:192.168.1.103
二、安装keepalived和haproxy
sudo apt-get install keepalived sudo apt-get install haproxy
三、配置haproxy
sudo vim /etc/haproxy/haproxy.cfg
加入下列内容
global log 127.0.0.1 local1 maxconn 50000 #最大连接数 # chroot /usr/local/haproxy #安装目录 uid haproxy #用户haproxy gid haproxy #组haproxy daemon #守护进程运行 nbproc 1 #进程数量 pidfile /var/run/haproxy.pid #haproxy pid defaults log 127.0.0.1 local2 mode http #7层 http;4层tcp option httplog #http 日志格式 option httpclose #主动关闭http通道 option redispatch #强制定向到其他健康的服务器 option forwardfor option dontlognull maxconn 50000 #最大连接数 contimeout 5000 #连接超时(毫秒) clitimeout 50000 #客户端超时(毫秒) srvtimeout 50000 #服务器超时(毫秒) # errorfile 400 /etc/haproxy/errors/400.http # errorfile 403 /etc/haproxy/errors/403.http # errorfile 408 /etc/haproxy/errors/408.http # errorfile 500 /etc/haproxy/errors/500.http # errorfile 502 /etc/haproxy/errors/502.http # errorfile 503 /etc/haproxy/errors/503.http # errorfile 504 /etc/haproxy/errors/504.http #定义前端服务器 frontend web #定义前端服务器(haproxy) bind *:80 #监听地址 acl yxl_www hdr_beg(host) -i www. yinxiulei.cn acl yxl_www hdr_beg(host) -i yinxiulei.cn use_backend www. yinxiulei.cn if yinxiulei.cn #定义后端服务器 backend www. yinxiulei.cn mode http option forwardfor balance source cookie SERVERID option httpchk HEAD / server server1 192.168.1.101:80 cookie server1 check inter 2000 rise 3 fall 3 weight 3 server server2 192.168.1.102:80 cookie server2 check inter 2000 rise 3 fall 3 weight 3 server server3 192.168.1.103:80 cookie server3 check inter 2000 rise 3 fall 3 weight 3 #HAProxy管理页面 listen admin_stat bind *:1158 #管理页面端口 mode http stats refresh 10s #自动刷新时间 stats uri /haproxy #页面名称 stats realm Haproxy\ Statistics #登录提示 stats auth admin:admin #帐号密码 stats hide-version stats admin if TRUE
保存退出
启动haproxy
sudo /usr/sbin/haproxy –f / /etc/haproxy/haproxy.cfg
修改
sudo vim /etc/default/haproxy
把ENABLED=0改成ENABLED=1
就能使用脚本启动
sudo /etc/init.d/haproxy start
四、配置keepalived
主keepalived
sudo vim /etc/keepalived/keepalived.conf
输入下列内容
! Configuration File for keepalived global_defs { router_id haproxy } vrrp_script check_run { script "killall -0 haproxy" interval 5 } vrrp_sync_group VG1 { group { VI_1 } } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass haproxy } track_script { check_run } virtual_ipaddress { 192.168.1.100 } }
保存退出
备keepalived
sudo vim /etc/keepalived/keepalived.conf
加入下列内容
! Configuration File for keepalived global_defs { router_haproxy } vrrp_script check_run { script "killall -0 haproxy" interval 5 } vrrp_sync_group VG1 { group { VI_1 } } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 50 advert_int 1 authentication { auth_type PASS auth_pass haproxy } track_script { check_run } virtual_ipaddress { 192.168.1.100 } }
保存退出
启动keepalived
sudo /etc/init.d/keepalived start
查看IP绑定情况
ip add
五、测试
测试省略