haproxy_keepalived安装配置

haproxy_keepalived安装配置


参照《ubuntu server最佳方案》,学习配置了haproxy的负载均衡。留个简单的安装配置记录,然后继续深入学习。

安装环境:ubuntu server

网络环境
•	Load Balancer 1: lb1.test.com, IP address: 192.168.1.10 eth0
•	Load Balancer 2: lb2.test.com, IP address: 192.168.1.11 eth0
•	Web Server 1: web1.test.com, IP address: 192.168.1.12 eth0
•	Web Server 2: web2.test.com, IP address: 192.168.1.13 eth0
•	lb1 and lb2共享虚拟IP: 192.168.1.100处理请求


一、 web server的安装配置
以下操作在两台web server上同时做
1. apache安装
sudo apt-get install apache2 libapache2-mod-php5 php5-mysql


2. 修改apache中日志记录,以便可以记录client的ip而不是lb的
vi /etc/apache2/apache2.conf
把logFormat的%h改为%{X-Forwarded-For}i


3. 在你网站目录创建检测文件,用于haproxy检测web server是否存活
echo “It works!” > /var/www/hachecker.php
修改你的虚拟主机配置文件记录access日志部分,不记录hachecker.php的访问日志
SetEnvIf Request_URI “^/hachecker\.php$” dontlog
CustomLog /var/log/apache2/access.log combined env=!dontlog


4. 重启apache
/etc/init.d/apache2 restart



二、 Load Balancer(HAProxy)安装配置
以下操作在两台Load Balancer上同时做
1. 安装HAProxy
sudo apt-get install haproxy


2. 配置HAProxy
sudo mv /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg_BAK
sudo vi /etc/haproxy/haproxy.cfg
加入如下:
global
	log 127.0.0.1	local0
	log 127.0.0.1	local1 notice
	#log loghost	local0 info
	maxconn 4096
	#chroot /usr/share/haproxy
	user haproxy
	group haproxy
	daemon
	#debug
	#quiet

defaults
	log	global
	mode	http
	option	httplog
	option	dontlognull
	retries	3
	option redispatch
	maxconn	2000
	contimeout	5000
	clitimeout	50000
	srvtimeout	50000

listen	webfarm 192.168.1.100:80
	stats  enable
	stats  auth isends:isends
	balance	roundrobin
	cookie	JSESSIONID prefix
	option   forwardfor
	option   httpchk HEAD /hachecker.php
	server	web1 192.168.1.12:80 cookie A check
	server	web2 192.168.1.13:80 cookie B check

	option	httpclose		# disable keep-alive
	#option  checkcache		# block response if set-cookie & cacheable
	
	#errorloc	502	http://192.168.114.58/error502.html
	#errorfile	503	/etc/haproxy/errors/503.http
	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

#retries--web无法访问的重试次数
#cookie JSESSIONID prefix--处理session
#option forwardfor--转发client的IP给web server(X-Forwarded-For)

3. 调整系统参数、开机启动HAProxy
sudo vi /etc/sysctl.conf
加入一行:
net.ipv4.ip_nonlocal_bind=1
执行sudo sysctl –p使之生效
开机启动HAProxy
vi /etc/default/haproxy
设置ENABLED=1
ENABLED=1


4. 启动haproxy
sudo /etc/init.d/haproxy start



三、 Keepalived安装配置
lb1和lb2的HAProxy已经配置好并监听IP地址:192.168.1.100。Keepalived用priority参数把bl1和bl2分配为“主服务器”和“从服务器”。正常情况下由主服务器监听IP地址并提供服务。
以下操作在两台Load Balancer上同时做,但是priority参数主服务器为101,从服务器为100
1. 安装Keepalived
sudo apt-get install keepalived



2. 配置Keepalived
sudo vi /etc/keepalived/keepalived.conf
配置如下:
vrrp_script chk_haproxy {
    script "killall -0 haproxy"
    interval 2
    weight 2
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    virtual_ipaddress {
        192.168.1.100
	# optional label. should be of the form "realdev:sometext" for
	# compatibility with ifconfig.
	#192.168.200.18 label eth0:1
    }
    track_script {
        chk_haproxy
    }
}


3. 启动Keepalived
sudo /etc/init.d/keepalived start
查看ip是否绑定正确,bl1绑定IP:192.168.1.100。bl2不绑定该IP
ip addr sh eth0


四、 其他
HAProxy情况查询:用上面设置的帐号密码登陆 http://192.168.1.100/haproxy?stats
HAProxy中文网站
附件是haproxy的详细配置文档

你可能感兴趣的:(apache,PHP,Web,mysql,ubuntu)