keepalived简介
Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的web服务器从系统中剔除,当web服务器工作正常后Keepalived自动将web服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的web服务器。
lvs简介
LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统。LVS集群采用IP负载均衡技术和基于内容请求分发技术。调度器具有很好的吞吐率,将请求均衡地转移到不同的服务器上执行,且调度器自动屏蔽掉服务器的故障,从而将一组服务器构成一个高性能的、高可用的虚拟服务器。整个服务器集群的结构对客户是透明的,而且无需修改客户端和服务器端的程序。为此,在设计时需要考虑系统的透明性、可伸缩性、高可用性和易管理性。
lvs-dr:Direct Routing
通过为请求报文的重新封闭一个MAC首部进行转发,源MAC是DIP所在接口的MAC,目标MAC是挑选出某RS的RIP所在接口的MAC地址;IP首部不会发生变化(CIP<-->VIP);
(1)确保前端路径器将目标IP为VIP的请求报文发往Director:
解决方案:
在路由器上静态绑定VIP和Director的MAC地址;
禁止RS响应VIP的ARP请求,禁止RS的VIP进行通告
(a) arptables
(b) 修改RS的内核参数,并把VIP绑定lo的别名上;
arp_ignore, arp_announce
(2)RS的RIP可以使用私有地址,也可以使用公网地址;
(3)RS跟Director必须在同一物理网络;RS的网关必须不能指向DIP;
(4)请求报文必须由Directory调度,但响应报文必须不能经由Director;
(5)不支持端口映射;
(6)RS可以使用大多的OS;
keepalived提供高可用并监测后端服务器健康状态
lvs提供负载均衡
这里使用lvs-dr keepalived的主/主模型来搭建
实验环境:
虚拟机:VMware Workstation 12.1 pro
操作系统:CentOS 7
keepalived-1.2.13-7.el7.x86_64
ipvsadm-1.27-7.el7.x86_64
httpd-2.4.6-40.el7.centos.x86_64
IP规划:
DR-MASTER-DIP:172.18.1.110
VIP:172.18.1.88
DR-BACKUP-DIP:172.18.1.111
VIP:172.18.1.88
RS1:172.18.1.130
RS1-lo:0 :172.18.1.88
RS2:192.168.10.12
RS2-lo:0 :172.18.1.88
实验拓扑图
稍后补...
老规矩,开始前将所有主机的iptables和selinux关闭或者设置允许策略
systemctl stop iptables.servicesystemctl disable iptables.service
setenforce 0
vim /etc/selinux/config
SELINUX=disable
DR中安装keepalived和ipvsadm
yum -y install keepalived ipvsadm
配置DR1-MASTER
! Configuration File for keepalivedglobal_defs { notification_email { root@localhost } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DR1}! DR1为主,DR2为备vrrp_instance VI_1 { state MASTER interface eno16777736 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.18.1.88 dev eno16777736 label eno16777736:0 }}virtual_server 172.18.1.88 80 { delay_loop 6 lb_algo wrr lb_kind DR nat_mask 255.255.0.0 persistence_timeout 50 protocol TCP real_server 172.18.1.130 80 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } real_server 172.18.1.131 80 { weight 2 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } }}! DR1为备,DR2为主vrrp_instance VI_2 { state BACKUP interface eno16777736 virtual_router_id 51 priority 98 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.18.1.88 dev eno16777736 label eno16777736:0 }}virtual_server 172.18.1.88 80 { delay_loop 6 lb_algo wrr lb_kind DR nat_mask 255.255.0.0 persistence_timeout 50 protocol TCP real_server 172.18.1.130 80 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } real_server 172.18.1.131 80 { weight 2 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } }}
配置DR2-BACKUP
! Configuration File for keepalivedglobal_defs { notification_email { root@localhost } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DR1}! DR1为主,DR2为备vrrp_instance VI_1 { state BACKUP interface eno16777736 virtual_router_id 51 priority 98 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.18.1.88 dev eno16777736 label eno16777736:0 }}virtual_server 172.18.1.88 80 { delay_loop 6 lb_algo wrr lb_kind DR nat_mask 255.255.0.0 persistence_timeout 5 protocol TCP real_server 172.18.1.130 80 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } real_server 172.18.1.131 80 { weight 2 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } }}! DR1为备,DR2为主vrrp_instance VI_2 { state MASTER interface eno16777736 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.18.1.88 dev eno16777736 label eno16777736:0 }}virtual_server 172.18.1.88 80 { delay_loop 6 lb_algo wrr lb_kind DR nat_mask 255.255.0.0 persistence_timeout 50 protocol TCP real_server 172.18.1.130 80 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } real_server 172.18.1.131 80 { weight 2 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } }}
RS1和RS2分别安装nginx服务并提供一个测试页面
yum -y install nginx...省略安装过程
分别在/usr/share/nginx/html目录下创建测试页
RS1创建
vim /usr/share/nginx/html/index.html<h1>RS-1</h1>
RS2创建
vim /usr/share/nginx/html/index.html<h1>RS-2</h1>
为RS1和RS2的lo:0接口别名配置VIP,并限制报文转发。编写脚本实现了
#!/bin/bash#vip=172.18.1.88case $1 instart)#限制arp的响应和通告echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignoreecho 1 > /proc/sys/net/ipv4/conf/lo/arp_ignoreecho 2 > /proc/sys/net/ipv4/conf/all/arp_announceecho 2 > /proc/sys/net/ipv4/conf/lo/arp_announce#设置接口IPifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up#添加路由route add -host $vip dev lo:0;;stop)#不限制arp的响应和通告echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignoreecho 0 > /proc/sys/net/ipv4/conf/lo/arp_ignoreecho 0 > /proc/sys/net/ipv4/conf/all/arp_announceecho 0 > /proc/sys/net/ipv4/conf/lo/arp_announce#设置接口IPifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 down;;*)echo "Nothing to do."exit 6;;esac
分别在RS1和RS2执行
./rs-lo.sh start
启动DR1和DR2的keepalived服务,并启动RS1和RS2的nginx服务
在另一主机访问
curl 172.18.1.88
本文出自 “我的学习笔记” 博客,请务必保留此出处http://zhaoyongtao.blog.51cto.com/10955972/1775862