准备调度器haproxy keeaplived
编辑haproxy配置文件 两边要一致
调度器负责响应用户请求和后端真实web分发 keepalive负责调度器高可用
haproxy 负责调度 提供虚拟服务
keepalive提供浮动ip 用户直接访问活跃的调度器的浮动ip 负责维护两个调取器的心跳 选择一个主调度器监听浮动地址
1.基本环境
hostname master.uplook.com
hostname slave.uplook.com
hostname node1.uplook.com
hostname node2.uplook.com
/etc/init.d/iptabes stop
chkconfig iptables off
setenforce 0
vim /etc/hosts
192.168.1.2 master.uplook.com
192.168.1.3 slave.uplook.com
192.168.1.10 node1.uplook.com
192.168.1.20 node2.uplook.com
haproxy-master 192.168.1.2/24
haproxy-slave 192.168.1.3/24
web1 192.168.1.10
web2 192.168.1.20
vim /etc/yum.repos.d/rhel-source.repo
[LoadBalancer]
name=Red Hat Enterprise Linux
baseurl=file:///mnt/LoadBalancer
enabled=1
gpgcheck=0
[HighAvailability]
name=Red Hat Enterprise Linux
baseurl=file:///mnt/HighAvailability
enabled=1
gpgcheck=0
scp /etc/yum.repos.d/rhel-source.repo slave:/etc/yum.repos.d/
2.Rs配置 配置好web做好测试
node1(web1) node2(web2) --→ http
[root@master ~]# curl http://192.168.1.10
web1
[root@master ~]# curl http://192.168.1.20
web2
[root@slave ~]# curl http://192.168.1.10
web1
[root@slave ~]# curl http://192.168.1.20
web2
3.调度器配置Haproxy(主/备)
[root@master ~]# yum install haproxy -y
[root@slave ~]# yum install haproxy -y
cp -rf /etc/haproxy/haproxy.cfg{,.bak}
sed -i -r '/^[ ]*#/d;/^$/d' /etc/haproxy/haproxy.cfg
vim /etc/haproxy/haproxy.cfg
global
defaults
----------------------------------配置监控[可选]------------------------------
listen stats 起的标签 看ha状态
bind *:1314
stats enable
stats refresh 30s 刷新
stats hide-version 隐藏版本
stats uri /haproxystats 状态地址 自己定义
stats realm Haproxy\ stats 登录提示消息
stats auth uplook:123 用户密码
stats admin if TRUE 成功则为管理员
----------------------------------------------------------------------------
提供虚拟服务
frontend web
mode http
bind *:80
default_backend httpservers
*80 注意提供过个网站要写监听的具体地址
backend httpservers
balance roundrobin
server http1 192.168.1.10:80 maxconn 2000 weight 1 check inter 1s rise 2 fall 2
server http2 192.168.1.20:80 maxconn 2000 weight 1 check inter 1s rise 2 fall 2
server http3 192.168.1.30:80 maxconn 2000 weight 1 check inter 1s rise 2 fall 2
检测间隔和实效的时间 保留defaults和global 追加listen 没有提供具体主页 set list
[root@uplook ~]# service haproxy restart
[root@uplook ~]# chkconfig haproxy on
service haproxy restart
chkconfig haproxy on
4.测试主备调度器
先测试主调度器 一切就绪后把ha_proxy配置复制给备份调度器 至此两台调度器都可以正常调度
用户可以直接访问调度器就可以访问页面
测试主调度器
uplook 123
[root@master ~]# rsync -va /etc/haproxy/haproxy.cfg slave:/etc/haproxy/
[root@master ~]# ssh slave /etc/init.d/haproxy restart
停止 haproxy:[失败]
正在启动 haproxy:[确定]
至此haproxy部署完成 用户可以直接访问两个调度器地址来访问后端真实web 但是用户访问其中任何一台都有可能单点故障 需要做高可用 引入keepalived
二、Keepalived实现调度器HA
注:主/备调度器均能够实现正常调度
1. 主/备调度器安装软件
[root@master ~]# yum -y install keepalived
[root@slave ~]# yum -y install keepalived
2. Keepalived
Master
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id director1 //辅助改为director2
}
vrrp_instance VI_1 {
state BACKUP
nopreempt
interface eth0 //心跳接口,尽量单独连接心跳
virtual_router_id 80 //MASTER,BACKUP一致
priority 100 //辅助改为50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.122.100
}
}
BACKUP
[root@master ~]# rsync -va /etc/keepalived/keepalived.conf slave:/etc/keepalived/
[root@slave ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id salve
}
vrrp_instance VI_1 {
state BACKUP
interface eth1
virtual_router_id 80
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.100
}
}
3. 启动KeepAlived(主备均启动)
[root@master ~]# /etc/init.d/keepalived restart ; chkconfig keepalived on
[root@master ~]# ssh slave /etc/init.d/keepalived restart; chkconfig keepalived on
master上观察多了Ip地址
ip addr
主/备调度器:
正常只有活跃的调度器发送组播
master:
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth1, link-type EN10MB (Ethernet), capture size 65535 bytes
14:13:24.687034 IP master.uplook.com > 224.0.0.18: VRRPv2, Advertisement, vrid 80, prio 100, authtype simple, intvl 1s, length 20
slave:
[root@slave ~]# tcpdump -i eth1 vrrp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth1, link-type EN10MB (Ethernet), capture size 65535 bytes
14:13:05.656157 IP master.uplook.com > 224.0.0.18: VRRPv2, Advertisement, vrid 80, prio 100, authtype simple, intvl 1s, length 20'
活跃的路由器才会发通知消息
至此用户访问的时候,直接访问浮动ip,至于浮动ip在哪个调度器上看vrrp的竞选结果
多业务部署使用default backend不行 采用以下配置
[root@master ~]# vim /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
maxconn 4000
user haproxy
group haproxy
daemon
stats socket /var/lib/haproxy/stats
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
listen stats
bind *:1314
stats enable
stats refresh 30s
stats hide-version
stats uri /haproxystats
stats realm Haproxy\ stats
stats auth uplook:123
stats admin if TRUE
listen www.uplook.com
mode http
bind *:80
balance roundrobin
server http1 192.168.1.10:80 maxconn 2000 weight 1 check inter 1s rise 2 fall 2
server http2 192.168.1.20:80 maxconn 2000 weight 1 check inter 1s rise 2 fall 2
server http3 192.168.1.30:80 maxconn 2000 weight 1 check inter 1s rise 2 fall 2
[root@master ~]# rsync -va /etc/haproxy/haproxy.cfg slave:/etc/haproxy/
[root@master ~]# /etc/init.d/haproxy restart
停止 haproxy: [确定]
正在启动 haproxy: [确定]
[root@master ~]# ssh slave /etc/init.d/haproxy restart
停止 haproxy:[确定]
正在启动 haproxy:[确定]
[root@client1 ~]# curl http://192.168.1.100
web1
[root@client1 ~]# curl http://192.168.1.100
web2
主/备调度器:
4. 扩展对调度器Haproxy健康检查(可选) 如果haproxy挂掉 无法检测 lvs基于内核通过规则可以检测 定期检测haproxy 计划任务和keepalived都可以检测
思路:
让Keepalived以一定时间间隔执行一个外部脚本,脚本的功能是当Haproxy失败,则关闭本机的Keepalived
非0 调度器自己访问自己
注意命令的绝对路径
[root@master ~]# cat /etc/keepalived/check_haproxy_status.sh
#!/bin/bash
/usr/bin/curl -I http://localhost &>/dev/null
if [ $? -ne 0 ];then
/etc/init.d/keepalived stop
fi
[root@master ~]# chmod a+x /etc/keepalived/check_haproxy_status.sh
复制给B机器
b. keepalived使用script 第2步
! Configuration File for keepalived
global_defs {
router_id director1
}
vrrp_script check_haproxy {
script "/etc/keepalived/check_haproxy_status.sh"
interval 5
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
nopreempt
virtual_router_id 90
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass uplook
}
virtual_ipaddress {
192.168.122.100
}
track_script { 调用
check_haproxy
}
}
rsync -va /etc/keepalived/keepalived.conf slave:/etc/keepalived 复制keepalive 配置文件和脚本
故障切换 keepalived 主挂备上 主恢复备接上
配置不抢占
nopreempt
需要BACKUP状态
=================================================================================
listen www.uplook.com
mode http
bind *:80
balance roundrobin
#balance source
server http1 192.168.122.10:80 maxconn 2000 weight 1 check inter 1s rise 2 fall 2
server http2 192.168.122.20:80 maxconn 2000 weight 1 check inter 1s rise 2 fall 2
server http3 192.168.122.30:80 maxconn 2000 weight 1 check inter 1s rise 2 fall 2
定义日志
Haproxy Log:
[root@rhel6 ~]# tcpdump -i lo -nn port 514
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on lo, link-type EN10MB (Ethernet), capture size 65535 bytes
02:48:50.475524 IP 127.0.0.1.41350 > 127.0.0.1.514: SYSLOG local2.info, length: 176
02:48:55.479321 IP 127.0.0.1.41350 > 127.0.0.1.514: SYSLOG local2.info, length: 176
02:49:00.479946 IP 127.0.0.1.41350 > 127.0.0.1.514: SYSLOG local2.info, length: 176
02:49:05.476149 IP 127.0.0.1.41350 > 127.0.0.1.514: SYSLOG local2.info, length: 176
02:49:10.473743 IP 127.0.0.1.41350 > 127.0.0.1.514: SYSLOG local2.info, length: 176
02:49:15.481521 IP 127.0.0.1.41350 > 127.0.0.1.514: SYSLOG local2.info, length: 176
[root@rhel6 ~]# netstat -tunlp |grep :514
[root@uplook ~]# vim /etc/sysconfig/rsyslog
SYSLOGD_OPTIONS="-c 2 -r"
[root@uplook ~]# vim /etc/rsyslog.conf
# Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514
# Provides TCP syslog reception
$ModLoad imtcp
$InputTCPServerRun 514
local2.* /var/log/haproxy.log
[root@uplook ~]# service rsyslog restart
Shutting down system logger: [ OK ]
Starting system logger: [ OK ]
[root@rhel6 ~]# netstat -tunlp |grep :514
tcp 0 0 0.0.0.0:514 0.0.0.0:* LISTEN 2755/rsyslogd
tcp 0 0 :::514 :::* LISTEN 2755/rsyslogd
udp 0 0 0.0.0.0:514 0.0.0.0:* 2755/rsyslogd
udp 0 0 :::514 :::* 2755/rsyslogd
[root@rhel6 ~]# tailf /var/log/haproxy.log
Jan 16 02:51:10 localhost haproxy[2734]: [16/Jan/2016:02:51:10.487] www.uplook.comwww.uplook.com/http3 0/0/0/0/0 200 265 - - ---- 1/1/0/1/0 0/0 "HEAD / HTTP/1.1"
Jan 16 02:51:15 localhost haproxy[2734]: [16/Jan/2016:02:51:15.493] www.uplook.comwww.uplook.com/http1 0/0/0/1/1 200 265 - - ---- 1/1/0/1/0 0/0 "HEAD / HTTP/1.1"
Jan 16 02:51:20 localhost haproxy[2734]: [16/Jan/2016:02:51:20.490] www.uplook.comwww.uplook.com/http2 0/0/0/1/1 200 265 - - ---- 1/1/0/1/0 0/0 "HEAD / HTTP/1.1"