Linux Haproxy+KeepAlived高可用负载均衡搭建

Haproxy+KeepAlived 高可用负载均衡

安装环境

1
2
3
4
5
6

 

 

系统版本:CentOS release 6.2 (Final) x64 64
HAProxy版本:1.4.8
Keepalived版本:1.2.4
VIP:192.168.207.100
WEB_2:192.168.207.130 8080端口
WEB_3:192.168.207.131 8080端口

 

软件版本:

 

 

配置文件:

keepalived:

global_defs {
	router_id LVS1
}

vrrp_instance V1 {
	state MASTER
	interface eth0
	virtual_router_id 51
	mcast_src_ip 192.168.2.60	
	priority 100
	advert_int 2
	nopreempt
	authentication {
		auth_type PASS
		auth_pass letmein	
	}
	virtual_ipaddress {
		192.168.2.62	
	}
}

haproxy:

# this config needs haproxy-1.1.28 or haproxy-1.2.1
global
	daemon
	log 127.0.0.1 local0 info
	maxconn 4096
	nbproc 1
	pidfile /etc/haproxy/haproxy.pid
defaults
	mode http
	timeout connect 5000ms
	timeout http-keep-alive 10000ms
	timeout client 600000ms
	timeout server 600000ms
listen admin
	bind :1080
	mode http
	log 127.0.0.1 local0 err
	stats uri /admin?stats
listen http_servers
	log 127.0.0.1 local0 info
	bind :80
	mode tcp
	retries 1
	option httpchk OPTIONS * HTTP/1.1\r\nHost:\ www
	balance roundrobin
	stick on src
	stick-table type ip size 1M expire 30m
	server lvs1 192.168.2.60:8080 maxconn 4096 check port 8080 inter 500ms
	server lvs2 192.168.2.61:8080 maxconn 4096 check port 8080 inter 500ms

依赖环境

安装准备

关闭系统防火墙iptables

关闭SELinux

 

安装gcc

1
 

 

yum install gcc

安装ipvsadm

1
 

 

yum install ipvsadm

安装openssl

1
2

 

 

yum install openssl
yum install openssl-devel

 

安装linux kernel 开发包

1
 

 

yum install kernel-devel 

 

创建符号连接

1
 

 

 ln -sv /usr/src/kernels/2.6.18-238.9.1.el5-x86_64/ /usr/src/linux

 

其他依赖

受Linux系统自身原因及操作系统版本和已有环境影响,部署过程中可能还需要其他依赖包,此部分不在文档中累述,遇到时需自行解决处理

KeepAlived安装与配置

安装

1
2
3
4

 

 

tar -zxvf keepalived-1.2.1.tar.gz
cd keepalived-
1.2.1
./configure --prefix=/usr/local/keepalived
make && make install

 

 

配置

安装完成后,进入安装目录的etc目录下,将keepalived相应的配置文件拷贝到系统相应的目录当中。keepalived启动时会从/etc/keepalived目录下查找keepalived.conf配置文件,如果没有找到则使用默认的配置。/etc/keepalived目录安装时默认是没有安装的,需要手动创建。配置文件目录结构如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13

 

 

shell> tree -l /usr/local/keepalived/etc
-- keepalived
|   |-- keepalived.conf
|   `-- samples
|       |-- keepalived.conf.status_code
|       |-- keepalived.conf.track_interface
|       |-- keepalived.conf.vrrp
|       |-- 
。。。
|-- rc.d
|   `-- init.d
|       `-- keepalived
`-- sysconfig
    `-- keepalived

 

分别对应系统目录(忽略samples目录)

1
2
3

 

 

/etc/keepalived/keepalived.conf
/etc/rc.d/init.d/keepalived
/etc/sysconfig/keepalived

 

 

将配置完成的keepalived.conf文件放入

192.168.207.130keepalived.conf配置信息:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

 

 

global_defs {
    router_id LVS1
}

vrrp_instance V1 {
    state MASTER
    interface eth0
    virtual_router_id 
51
    mcast_src_ip 192.168.207.130   
    priority 
100
    advert_int 2
    nopreempt
    authentication {
        auth_type PASS
        auth_pass letmein   
    }
    virtual_ipaddress {
        
192.168.207.100    
    }
}

 

192.168.207.131keepalived.conf配置信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

 

 

global_defs {
    router_id LVS1
}

vrrp_instance V1 {
    state MASTER
    interface eth0
    virtual_router_id 
51
    mcast_src_ip 192.168.207.131 
    priority 
100
    advert_int 2
    nopreempt
    authentication {
        auth_type PASS
        auth_pass letmein   
    }
    virtual_ipaddress {
        
192.168.207.100    
    }
}

 

设置keepalived服务开机启动 

1
2
3
4

 

 

shell> chkconfig keepalived on
shell> service keepalived start   #
启动服务
shell> service keepalived stop    #停止服务
shell> service keepalived restart #重启服务

 

另外需要注意的一点是,keepalived启动时不会检查配置文件的语法是否正确,所以我们在编写配置文件时要特别小心,别写错了,否则会出现一些意想不到的现象。

使用service keepalived start命令启动服务时,默认会将/etc/sysconfig/keepalived文件中KEEPALIVED_OPTIONS参数作为keepalived服务启动时的参数,并从/etc/keepalived/目录下加载keepalived.conf配置文件,或用-f参数指定配置文件的位置。

1
2
3
4
5
6
7
8
9
10
11
12
13

 

 

# Options for keepalived. See `keepalived --help' output and keepalived(8and
# keepalived.conf(5) man pages for a list of all options. Here are the most
# common ones :
#
# --vrrp               -P    Only run with VRRP subsystem.
# --check              -C    Only run with Health-checker subsystem.
# --dont-release-vrrp  -V    Dont remove VRRP VIPs & VROUTEs on daemon stop.
# --dont-release-ipvs  -I    Dont remove IPVS topology on daemon stop.
# --dump-conf          -d    Dump the configuration data.
# --
log-detail         -D    Detailed log messages.日志默认输出在/var/log/message文件中
# --log-facility       -S    0-7 Set local syslog facility (default=LOG_DAEMON)
#
KEEPALIVED_OPTIONS=
"-D"

运行keepalived --help可以查看启动时的可选参数,这些可选参数都可以配置在/etc/sysconfig/keepalived文件中的KEEPALIVED_OPTIONS选项中,作为服务启动时的参数。

keepalived正常运行后,会启动3个进程,其中一个是父进程,负责监控其子进程。一个是vrrp子进程,另外一个是checkers子进程。

1
2
3
4

 

 

shell> ps -ef | grep keepalived
root       
831     1  0 11:22 ?        00:00:00 keepalived -D
root       
840   831  0 11:22 ?        00:00:00 keepalived -D
root       
841   831  0 11:22 ?        00:00:00 keepalived -D

 

检查浮动IP地址完成即可

 

HaProxy安装与配置

安装

1
2
3

 

 

make TARGET=linux26
make install
mkdir /etc/haproxy

 

 

配置

将haproxy.cfg上传到/etc/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

 

 

# this config needs haproxy-1.1.28 or haproxy-1.2.1
global
    daemon
    
log 127.0.0.1 local0 info
    maxconn 
4096
    nbproc 1
    pidfile /etc/haproxy/haproxy.pid
defaults
    mode http
    timeout connect 5000ms
    timeout http-keep-alive 10000ms
    timeout client 600000ms
    timeout server 600000ms
listen admin
    bind :
1080
    mode http
    
log 127.0.0.1 local0 err
    stats uri /admin?stats
listen http_servers
    
log 127.0.0.1 local0 info
    bind :
80
    mode tcp
    retries 
1
    option httpchk OPTIONS * HTTP/1.1\r\nHost:\ www
    balance roundrobin
    stick on src
    stick-table 
type ip size 1M expire 30m
    server lvs1 
192.168.207.130:8080 maxconn 4096 check port 8080 inter 500ms
    server lvs2 
192.168.207.131:8080 maxconn 4096 check port 8080 inter 500ms

 

注意:以上配置文件中一般如无特殊需求只需要修改黄色底色部分的应用服务器地址和端口即可

将haproxy复制到/etc/rc.d/init.d/ 下

 

1
 

 

chmod +x /etc/rc.d/init.d/haproxy

 

 

配置日志:

 

1
 

 

# vi /etc/syslog.conf

 

 

在最下边增加

 

1
2

 

 

local3.*         /var/log/haproxy.log
local0.*         /var/log/haproxy.log

 

重启核心日志服务使配置起效

 

1
 

 

# service syslog restart

 

开启防火墙的1080和80端口

 

1
2
3
4
5

 

 

su
/sbin/iptables -I INPUT -p tcp --dport 
1080 -j ACCEPT
/sbin/iptables -I INPUT -p tcp --dport 
80 -j ACCEPT
/etc/rc.d/init.d/iptables save
service iptables restart

 

设置成自启动

 

1
2
3

 

 

#vi /etc/rc.local //里面添加
service keepalived restart
service haproxy restart

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值