专注系统运维、网络架构,研究技术凯发app官方网站的解决方案,记录我的思想轨迹、工作学习、生活和关注的领域
分类: 架构设计与优化
2013-11-14 10:25:04
#cd /usr/local/src #wget #tar xf haproxy-1.4.24.tar.gz #cd haproxy-1.4.24 #make target=linux26 arch=x86_64 #target是指定内核版本,arch指定cpu架构,我使用的是64bit系统 #make install2、安装完毕后,创建配置文件和启动文件。
#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/ #mkdir /usr/share/haproxy3、编辑配置文件(两台haproxy配置文件相同)
#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 4096 #最大连接数
chroot /usr/share/haproxy #改变当前工作目录。
uid 99 #所属用户的uid
gid 99 #所属运行的gid
daemon #以后台形式运行haproxy
#debug
#quiet
defaults
log global
mode http
#默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回ok
option httplog
option dontlognull
option redispatch
#当serverid对应的服务器挂掉后,强制定向到其他健康的服务器
option abortonclose
#当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接
retries 3 #两次连接失败就认为是服务器不可用
maxconn 2000 #默认的最大连接数
#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
#acl后面是规则名称,-i是要访问的域名,如果访问这个域名就分发到下面的webserver 的作用域。
acl img hdr(host) -i img.zhirs.com
#如果访问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.137.201:80 check inter 2000 fall 3 weight 30
server web01 192.168.137.202:80 check inter 2000 fall 3 weight 20
server web01 192.168.137.203:80 check inter 2000 fall 3 weight 10
backend imgserver
mode http
option httpchk /index.php
balance roundrobin
server img01 192.168.137.101:80 check inter 2000 fall 3
server img02 192.168.137.102:80 check inter 2000 fall 3
4、启动haproxy服务,查看状态。
#service haproxy start
#cd /root/software #wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.33.tar.gz #tar -zxvf pcre-8.33.tar.gz #cd pcre-8.33 #./configure #make && make install #wget #tar -zxvf nginx-1.2.9.tar.gz #cd nginx-1.2.9 #./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_sub_module --with-http_stub_status_module --with-http_gzip_static_module #make && make install6、第一台keepalived配置文件。第一台做192.168.137.200vip的master,192.168.137.100的backup。
#vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
admin@centos.bz
}
notification_email_from keepalived@domain.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id lvs_devel
}
vrrp_script chk_http_port {
#script "/opt/nginx_pid.sh"
script "/opt/check_haproxy.sh"
interval 2
weight 2
}
vrrp_instance vi_1 {
state master ############ 辅机为 backup
interface eth0
virtual_router_id 51
mcast_src_ip 192.168.137.254
priority 100 ########### 权值要比 back 高
advert_int 1
authentication {
auth_type pass
auth_pass 1111
}
track_script {
chk_http_port ### 执行监控的服务
}
virtual_ipaddress {
192.168.137.200
}
}
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.137.100
}
}
7、第二台keepalived的配置文件。
#vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
admin@centos.bz
}
notification_email_from keepalived@domain.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id lvs_devel
}
vrrp_script chk_http_port {
#script "/opt/nginx_pid.sh"
script "/opt/check_haproxy.sh"
interval 2
weight 2
}
vrrp_instance vi_1 {
state backup
interface eth0
virtual_router_id 51
mcast_src_ip 192.168.137.253
priority 99 ##########权值 要比master低
advert_int 1
authentication {
auth_type pass
auth_pass 1111
}
track_script {
chk_http_port
}
virtual_ipaddress {
192.168.137.200
}
}
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.137.100
}
}
8、检测脚本,为了防止haproxy服务关闭导致keepalived不自动切换。
#vim /opt/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 fi9、启动两台keepalived和haproxy服务。
#servcie haproxy start #service keepalived start10、查看两台服务器的vip 查看
2: eth0:查看192.168.137.253:mtu 1500 qdisc pfifo_fast qlen 1000 link/ether 00:0c:29:dc:01:3e brd ff:ff:ff:ff:ff:ff
inet 192.168.137.254/24 brd 192.168.137.255 scope global eth0
inet 192.168.137.200/32 scope global eth0
2: eth0:11、测试,关闭任意一台keepalived,img和www域名访问正常。 后续会对nginx keepalived 双主结果做测试。mtu 1500 qdisc pfifo_fast qlen 1000 link/ether 00:0c:29:d5:e7:3d brd ff:ff:ff:ff:ff:ff
inet 192.168.137.253/24 brd 192.168.137.255 scope global eth0
inet 192.168.137.100/32 scope global eth0
inet6 fe80::20c:29ff:fed5:e73d/64 scope link
valid_lft forever preferred_lft forever
simendavid2020-05-17 22:27:04
kexl:不好意思我有个疑问想问下 ,一般做vip都是一个ip 这样在出现故障的时候能切刀back上 一般back闲置中 ,你这用了两个vip地址 第一台负载均衡器主要分发www.zhirs.com的请求,第二台负载均衡器主要分发img.zhirs.com的请求。 一般时候两台同时使用不会闲置 出故障之后再互相切换,,要是back故障能切到master???
正常情况下是其中一台处理一个域名,如果一台挂掉了,另外一台就接管两个域名了,如果挂掉的复活了,则又是一台一个域名了,样不会浪费资源。
|simendavid2020-05-17 22:26:35
kexl:不好意思我有个疑问想问下 ,一般做vip都是一个ip 这样在出现故障的时候能切刀back上 一般back闲置中 ,你这用了两个vip地址 第一台负载均衡器主要分发www.zhirs.com的请求,第二台负载均衡器主要分发img.zhirs.com的请求。 一般时候两台同时使用不会闲置 出故障之后再互相切换,,要是back故障能切到master???
正常情况下是其中一台处理一个域名,如果一台挂掉了,另外一台就接管两个域名了,如果挂掉的复活了,则又是一台一个域名了,样不会浪费资源。
|kexl2014-04-24 10:55:40
不好意思我有个疑问想问下 ,一般做vip都是一个ip 这样在出现故障的时候能切刀back上 一般back闲置中 ,你这用了两个vip地址 第一台负载均衡器主要分发www.zhirs.com的请求,第二台负载均衡器主要分发img.zhirs.com的请求。 一般时候两台同时使用不会闲置 出故障之后再互相切换,,要是back故障能切到master???