拓扑图
要求:如图所示,有两台haproxy调度器,
但是所有的流量全部由主调度器获取,由主调度器可以
正常调度web节点。
使用keepalived搭建双机热备
第一步:配置主调度器
[root@lvs_1 ~]# modprobe ip_vs #加载ip_vs模块
[root@server1 ~]# yum -y install ipvsadm.x86_64 #安装管理软件ipvsadm
[root@lvs_1 ~]# yum -y install gcc gcc-c++ make popt-devel kernel-devel openssl-devel #安装编译工具及插件
[root@lvs_1 ~]# tar zxvf keepalived-2.0.13.tar.gz #解压keepalived安装包
[root@server1 ~]# cd keepalived-2.0.13/
[root@lvs_1 keepalived-2.0.13]# ./configure --prefix=/ #配置安装到/目录下
[root@lvs_1 keepalived-2.0.13]# make && make install #编译与安装
[root@lvs_1 keepalived-2.0.13]# cp keepalived/etc/init.d/keepalived /etc/init.d/ #加入系统管理服务,用以支持systemctl管理
[root@lvs_1 keepalived-2.0.13]# systemctl enable keepalived #设置开机启动
[root@lvs_1 keepalived-2.0.13]# vi /etc/keepalived/keepalived.conf #编辑配置文件
! Configuration File for keepalived
global_defs { #区段指定全局参数
router_id lvs_1 #本服务器名称,默认为邮箱设置
}
vrrp_instance VI_1 { #定义VRRP热备实例(组名称)
state MASTER #热备状态,MASTER表示主服务器,BACKUP表示从服务器
interface ens33 #承载VIP地址的物理接口
virtual_router_id 51 #虚拟路由器的id号,每个热备组保持一致
priority 150 #优先级,数值越大优先级越高,主调度器最高,备调度器第二,尽量设置为相邻优先级相差不到10
advert_int 1 #通告间隔秒数(心跳频率)
authentication { #热备认证信息,每个热备组保持一致
auth_type PASS #认证类型
auth_pass 6666 #密码字符串
}
virtual_ipaddress { #指定漂移地址(VIP:VRRP的IP,虚拟服务器地址),可以为多个
20.0.0.100
}
}
virtual_server 20.0.0.100 80 { #虚拟服务器地址(VIP)、端口
delay_loop 6 #健康检查的间隔时间(秒)
lb_algo rr #轮询(rr)调度算法
lb_kind DR #直接路由(DR)群集工作模式
persistence_timeout 6 #连接保持时间
protocol TCP #应用服务器采用的是TCP协议
}
[root@server1 keepalived-2.0.13]# systemctl restart keepalived.service
[root@server1 keepalived-2.0.13]# ip addr show ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:46:3c:ae brd ff:ff:ff:ff:ff:ff
inet 20.0.0.11/24 brd 20.0.0.255 scope global ens33
valid_lft forever preferred_lft forever
inet 20.0.0.100/32 scope global ens33 #虚拟地址
valid_lft forever preferred_lft forever
inet6 fe80::ff21:1304:2937:51b3/64 scope link
valid_lft forever preferred_lft forever
编译安装haproxy
[root@haproxy ~]# yum -y install gcc gcc-c++ make pcre-devel bzip2-devel #解决依赖环境
[root@haproxy ~]# tar zxvf haproxy-1.4.24.tar.gz
[root@haproxy ~]# cd haproxy-1.4.24/
[root@haproxy haproxy-1.4.24]# make TARGET=linux26 && make install
[root@haproxy haproxy-1.4.24]# mkdir /etc/haproxy #创建haproxy目录
[root@haproxy haproxy-1.4.24]# cp examples/haproxy.cfg /etc/haproxy/ #将haproxy配置文件复制到haproxy目录下
[root@haproxy haproxy-1.4.24]# vi /etc/haproxy/haproxy.cfg
global
log /dev/log local0 info #配置日志记录,local0为日志设备,默认存放到系统日志
log /dev/log local0 notice #notice为日志级别,通常有24个级别
#log loghost local0 info
maxconn 4096 #最大连接数,根据应用实际情况进行调整,推荐使用10240
daemon
# chroot /usr/share/haproxy #注释
uid 99 #用户uid
gid 99 #用户gid
#debug
#quiet
[root@server1 keepalived-2.0.13]# systemctl restart keepalived.service
[root@haproxy ~]# vi /etc/rsyslog.d/haproxy.conf #编辑新的haproxy配置文件
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
&~
[root@server1 ~]# cd /var/log/haproxy/
### haproxy-info.log #调度信息
[root@haproxy ~]# systemctl restart haproxy
[root@haproxy ~]# systemctl restart rsyslog.service
第二步:配置备调度器
同主调度器相同,只需在keepalived配置文件里更改本服务器名称为lvs2,热备状态为BACKUP,优先级为145。
配置NFS服务器
[root@server1 ~]# yum -y install nfs*
[root@server1 ~]# yum -y install rpcbind
[root@server1 ~]# mkdir /web1 /web2
[root@server1 ~]# echo "<h1>this is web1</h1>" > /web1/index.html
[root@server1 ~]# echo "<h1>this is web2</h1>" > /web2/index.html
[root@server1 ~]# vi /etc/exports
/web1/ 20.0.0.13(ro)
/web2/ 20.0.0.14(ro)
[root@server1 ~]# systemctl restart nfs-utils
[root@server1 ~]# systemctl restart rpcbind
[root@server1 ~]# showmount -e
Export list for server1:
/web2 20.0.0.14
/web1 20.0.0.13
配置nginx服务器
web1
###首先安装在线源
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm #不安装在线源无法yum安装nginx
yum安装nginx
yum -y install nginx
[root@server1 html]# mount 20.0.0.15:/web1/ /usr/share/nginx/html
[root@server1 html]# df -Th
[root@server1 html]# systemctl restart httpd
[root@server1 html]# curl localhost
<h1>this is web1</h1>
web2
###首先安装在线源
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm #不安装在线源无法yum安装nginx
yum安装nginx
yum -y install nginx
[root@server1 ~]# mount 20.0.0.15:/web2 /usr/share/nginx/html
[root@server1 ~]# df -Th
[root@server1 ~]# systemctl start httpd
[root@server1 ~]# curl localhost
<h1>this is web2</h1>
验证效果
访问VIP:20.0.0.100