[Z]haproxy+keepalived高可用群集

时间:2023-03-08 22:15:39

http://blog.51cto.com/13555423/2067131

Haproxy是目前比较流行的一种集群调度工具
Haproxy 与LVS、Nginx的比较
LVS性能最好,但是搭建相对复杂
Nginx的upstream模块支持集群功能,但是对集群节点健康检查功能不强,性能没有Haproxy好
.
性能特性
单进程、事件驱动模型显著降低了上下文切换的开销及内存占用。 
事件检查器(eventchecker)允许其在高并发连接中对任何连接的任何事件实现即时探测。 
在任何可用的情况下,单缓冲(singlebuffering)机制能以不复制任何数据的方式完成读写操作,这会节约大量的CPU时钟周期及内存带宽; 
MRU内存分配器在固定大小的内存池中可实现即时内存分配,这能够显著减少创建一个会话的时长;

.
haproxy与各负载均衡器的区别

  • 与nginx:同样工作在用户空间,nginx是一款轻量级,能实现缓存、webserver、邮件、负载均衡等功能,但nginx的许多功能都需要第三方的模块,
    而haproxy的转发能力比nginx有更强更灵活的定制性,可以运用splice实现0复制的转发,并且有更直观的图形化管理界面,不过通用性不如nginx,并无缓存功能
  • 与varnish:varnish是一款web缓存系统,
  • 与lvs:lvs是工作在内核空间上直接转发的,无缓存功能

.
可以实现

  • 反向代理
  • 基于cookie会话绑定
  • 网页动静分离
    .
    HTTP请求
    请求方式
  • GET方式
  • POST方式
    .
    返回状态码
  • 正常的状态码为2××、3××
  • 异常的状态码为4××、5××
    .

负载均衡常用调度算法

  • RR(Round Robin):轮询调度
  • LC(Least Connections):最小连接数
  • SH(Source Hashing):基于来源访问调度
    .
    环境如下;
    haproxy+keepalived
    haproxy:192.168.1.10
    haproxy:192.168.1.20
    nginx1 :192.168.1.30
    nginx2 :192.168.1.40
    internet:192.168.1.50
    .
    (nginx)
    [root@centos1 /]# yum -y install pcre-devel zlib-devel
[root@centos1 /]# tar zxf nginx-1.6.2.tar.gz
[root@centos1 /]# cd nginx-1.6.2
[root@centos1 nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make && make install
[root@centos1 nginx-1.6.2]# useradd -M -s /sbin/nologin nginx
[root@centos1 nginx-1.6.2]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
[root@centos1 nginx-1.6.2]# echo "node_1"> /usr/local/nginx/html/index.html
.
启动nginx服务
[root@centos3 nginx-1.6.2]# nginx
.
建立防火墙规则
[root@centos3 nginx-1.6.2]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT

.
(haproxy)

挂载
[root@centos1 /]# mount /dev/cdrom /media/

.
编译安装haproxy

首先安装两个支持包
[root@centos1 /]# yum -y install pcre-devel bzip2-devel

.
卸载切换光盘haproxy

[root@centos1 /]#  umount /dev/cdrom
[root@centos1 /]# mount /dev/cdrom /media/
[root@centos1 /]# cd /media/
[root@centos1 /]# tar zxf haproxy-1.4.24.tar.gz -C /usr/src/
[root@centos1 /]# cd /usr/src/haproxy-1.4.24/
[root@centos1 /]# make TARGET=linux26
[root@centos1 /]# make install

.
haproxy服务器的配置

首先建立haproxy的配置文件
[root@centos1 /]# mkdir /etc/haproxy

.
拷贝配置文件的样本复制到/etc/haproxy目录下

[root@centos1 /]#  cp examples/haproxy.cfg /etc/haproxy/
.
[root@centos1 /]# vim /etc/haproxy/haproxy.cfg

.

# this config needs haproxy-1.1.28 or haproxy-1.2.1

global
log /dev/log local0 info //为了把info和notice日志分别放到不同的文件方便查看
log /dev/log local0 notice
#log loghost local0 info
maxconn 4096 //最大连接数
#chroot /usr/share/haproxy
uid 99 //用户uid
gid 99 //用户gid
pidfile /var/run/haproxy.pid //添加pid文件的路径以及文件名
daemon 后台运行
#debug
#quiet defaults
log global //应用全局配置日志格式
mode http //模式为http协议
option httplog //检查节点的失败次数
option dontlognull
retries 3 //连续达到三次则认为节点不可达
redispatch
maxconn 2000 //最大连接数2000
contimeout 5000 //连接超时5000
clitimeout 50000 //客户端服务器差事时间都是50000
srvtimeout 50000
option httpclose //关闭客户端请求
listen webcluster 0.0.0.0:80 //家庭地址及端口 0.0.0.0为自动检测
option httpchk GET /index.html //检查index.html文件
balance roundrobin //负载均衡的调度算法采用轮询算法
server inst1 192.168.1.20:80 check inter 2000 fall 3 //定义两个节点的地址和端口健康检查三次
server inst2 192.168.1.30:80 check inter 2000 fall 3

.

[root@centos1 /]#  cp examples/haproxy.init /etc/init.d/haproxy
[root@centos1 /]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
[root@centos1 /]# chmod +x /etc/init.d/haproxy [root@centos1 /]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT
开启haproxy服务
[root@centos1 /]# /etc/init.d/haproxy start

测试高可用
[Z]haproxy+keepalived高可用群集
.

配置haproxy日志
Haproxy的日志默认是保存到系统的syslog中,查看起来不方便,所以我们在生产环境中可以将日志单独存储到不同的文件中,配置如下
首先修改配置文件,主要改下面的部分

[root@centos1 /]#  vim /etc/haproxy/haproxy.cfg
log /dev/log local0 info
log /dev/log local0 notice

.
这两行的作用是将info和notice的日志分别记录到不同的文件中

然后修改rsyslog配置,将haproxy相关的配置独立定义到haproxy.conf,并放到/etc/rsyslog.d下,rsyslog启动时会自动加载此目录下所有的配置文件。

[root@centos1 /]#  touch /etc/rsyslog.d/haproxy.conf
[root@centos1 /]# vim /etc/rsyslog.d/haproxy.conf

.

if ($programname == 'haproxy' and $syslogseverity-text == 'info') then
-/var/log/haproxy/haproxy-info.log
& ~
if ($programname == 'haproxy' and $syslogseverity-text == 'notice') then
-/var/log/haproxy/haproxy-notice.log
& ~

.
以下内容是将info和notice日志放到不同的文件中

然后重新启动rsyslog服务
[root@centos1 /]# service rsyslog restart

.
测试日志信息

在客户机访问网站之后,可以使用tail -f /var/log/haproxy/haproxy-info.log即时查看日志
[root@centos1 /]# tail -l /var/log/haproxy/haproxy-info.log

.
实现haproxy的高可用配置keepalived
.

挂载
[root@centos1 /]# umount /dev/cdrom /media/
[root@centos1 /]# mount /dev/cdrom /media/
.
安装相关软件
[root@centos1 /]# yum -y install kernel-devel openssl-devel popt-devel

.
切换光盘

[root@centos1 /]#  umount /dev/cdrom /media/
[root@centos1 /]# mount /dev/cdrom /media/
[root@centos1 /]# cd /media/
[root@centos1 /]# tar zxf keepalived-1.2.13.tar.gz -C /usr/src/

.
编译安装

[root@centos1 /]#  cd /usr/src/keepalived-1.2.13/
[root@centos1 /]# ./configure --prefix=/ --with-kernel-dir=/usr/src/kernels/2.6.32-431.el6.x86_64/ && make && make install

.
使用keepalived服务

[root@centos1 /]#  chkconfig --add keepalived
[root@centos1 /]# chkconfig keepalived on

.
配置住调度器(注:如果haproxy配置指向了web节点那么这步可不做,#表示注释掉不生效,haproxy指向web比keepalived指向web的切换访问速度快)
.
[root@centos1 /]# vim /etc/keepalived/keepalived.conf 
.

! Configuration File for keepalived

global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id R1
} vrrp_instance VI_1 {
state MASTER
interface eth1
virtual_router_id 1
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.99
}
} #virtual_server 192.168.1.99 80 {
# delay_loop 6
# lb_algo rr # lb_kind DR
# persistence_timeout 50
# protocol TCP
#
# real_server 192.168.1.20 80 {
# weight 1
# TCP_CHECK {
# connect_port 80
# connect_timeout 3
# nb_get_retry 3
# dalay_bofore_retry 3
# }
# }
# real_server 192.168.1.30 80 {
# weight 1
# TCP_CHECK {
# connect_port 80

.

重启keepalived

[root@centos1 /]#  service keepalived start
[root@centos1 /]# ip addr show dev eth1

.
两台haproxy同样操作,从keeplived有三个地方不同:
优先级,调度名称,热备状态
route-id R2
state BACKUP
priority 99
.
为了方便可用scp命令
.
如不生效重启haproxy的命令:
[root@centos1 /]# /etc/init.d/haproxy restart

.
断掉一台haproxy测试,访问依旧

[Z]haproxy+keepalived高可用群集