首先提到LVS这要说到我的另一个博客:
LVS简介
那什么又是keepalived?
Keepalived是一款用c语言编写的路由软件,主要目标是为Linux系统和基于Linux的基础设施提供简单的负载均衡和高可用性的工具。loadbalance框架依赖于众所周知且广泛使用的Linux虚拟服务器(IPVS)内核模块,提供分层的负载平衡。Keepalived实现了一组检查器,根据负载均衡服务器池的健康状况动态地、自适应地维护和管理它们,而VRRP协议实现了高可用性。VRRP是用于外部故障转移的基本块。
Keepalived的作用是检测服务器的状态,如果有一台web服务器宕机,或工作出现故障,Keepalived将检测到,并将有故障的服务器从系统中剔除,同时使用其他服务器代替该服务器的工作,当服务器工作正常后Keepalived自动将服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的服务器。
Keepalived是集群管理中保证集群高可用的一个服务软件,用来防止单点故障。Keepalived起初就是为了LVS调度节点高可用而设计的,专门用来监控集群系统中各个服务节点的状态。如果某个服务节点出现异常,或工作出现故障,Keepalived将检测到,并将出现故障的服务节点从集群系统中剔除,也就是替LVS做了对后端realserver的健康状态监测。而当故障节点恢复正常后,Keepalived又可以自动将此服务节点重新加入到服务器集群中。这些工作全部自动完成,不需要人工干涉,需要人工完成的只是修复出现故障的服务节点。
Keepalived后来实现了VRRP协议的功能,基于VRRP协议来实现的LVS服务高可用方案,可以利用其来避免单点故障。一个LVS服务会有2台服务器运行Keepalived,一台为主服务器(MASTER),一台为备份服务器(BACKUP),但是对外表现为一个虚拟IP,主服务器会发送特定的消息给备份服务器,当备份服务器收不到这个消息的时候,即主服务器宕机的时候, 备份服务器就会接管虚拟IP,继续提供服务,从而保证了高可用性。Keepalived是VRRP的完美实现,因此在介绍keepalived之前,先介绍一下VRRP的原理。
VRRP工作机制?
先看两个概念:VRRP路由器和VRRP虚拟路由器
VRRP工作流程?
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
HWADDR=00:0C:29:2D:13:AE
TYPE=Ethernet
UUID=634de9e9-932d-45af-b3c1-de6a19a0417a
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=dhcp
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth1
HWADDR=00:0C:29:2D:13:B8
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=1.1.1.1
NETMASK=255.0.0.0
[root@localhost ~]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:2D:13:AE
inet addr:192.168.234.141 Bcast:192.168.234.255 Mask:255.255.255.0
eth1 Link encap:Ethernet HWaddr 00:0C:29:2D:13:B8
inet addr:1.1.1.1 Bcast:1.255.255.255 Mask:255.0.0.0
[root@localhost ~]# yum -y install gcc* openssl-devel libn13-devel net-snmp-devel kernel-devel popt-devel //使用本地yum源安装所需要的插件
[root@localhost ~]# vim /etc/sysctl.conf //调整内核参数
net.ipv4.ip_forward = 1
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.eth1.send_redirects = 0
[root@localhost ~]# sysctl -p
[root@localhost ~]# modprobe ip_vs
[root@localhost ~]# yum -y install /gz/Packages/ipvsadm-1.26-2.el6.x86_64.rpm
[root@localhost ~]# service ipvsadm stop
[root@localhost ~]# ipvsadm -A -t 192.168.234.100:80 -s rr
[root@localhost ~]# ipvsadm -a -t 192.168.234.100:80 -r 1.1.1.3:80 -m -w 1
[root@localhost ~]# ipvsadm -a -t 192.168.234.100:80 -r 1.1.1.4:80 -m -w 1
[root@localhost ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.234.100:80 rr
-> 1.1.1.3:80 Masq 1 0 0
-> 1.1.1.4:80 Masq 1 0 0
[root@localhost ~]# service ipvsadm save
ipvsadm: Saving IPVS table to /etc/sysconfig/ipvsadm: [确定]
[root@localhost ~]# chkconfig ipvsadm on
[root@localhost ~]# service ipvsadm start
ipvsadm: Clearing the current IPVS table: [确定]
ipvsadm: Applying IPVS configuration: [确定]
[root@localhost keepalived-1.2.13]# tar -zxvf keepalived-1.2.13.tar.gz -C /usr/src/
[root@localhost keepalived-1.2.13]# cd /usr/src/keepalived-1.2.13/
[root@localhost keepalived-1.2.13]# ./configure --prefix=/ --with-kernel-dir=/usr/src/kernels/2.6.32-431.el6.x86_64/ && make -j4 && make install
[root@localhost keepalived-1.2.13]# chkconfig --add keepalived
[root@localhost keepalived-1.2.13]# chkconfig keepalived on
[root@localhost keepalived-1.2.13]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
[root@localhost ~]# vim /etc/keepalived/keepalived.conf
配置文件详解:
**global_defs { //全局定义
notification_email { //通知电子邮件,收到通知邮件的电子邮件账户
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected] //使用电子邮件
smtp_server 192.168.200.1 //用于发送邮件通知的服务器
smtp_connect_timeout 30 //为SMTP处理指定超时时间
router_id LVS_DEVEL //指定LVS的名称
}
vrrp_instance VI_1 { //标识VRRP实例定义块
state MASTER //在标准使用中指定实例状态
interface eth0 //指定要在其上运行的实例的网络接口
virtual_router_id 51 //指定实例所属的VRRP路由器id
priority 100 //在VRRP路由器中指定实例优先级
advert_int 1 //以秒为单位指定间隔(设置为1)
authentication { //标识VRRP身份验证定义块
auth_type PASS //指定要使用哪种身份验证(PASS=AH)
auth_pass 1111 //指定要使用的密码字符串
}
virtual_ipaddress { //识别VRRP VIP定义块
192.168.200.16
192.168.200.17
192.168.200.18
}
}
virtual_server 192.168.200.100 443 { //标识虚拟服务器
delay_loop 6 //延迟回路,以秒为单位
lb_algo rr //选择一个特定的调度算法
lb_kind NAT //转发方法NAT
nat_mask 255.255.255.0
persistence_timeout 50 //为持久连接指定一个超时值
protocol TCP //指定协议种类TCP
sorry_server @IP PORT //如果所有实际服务器都关闭,则将服务器添加到池中
real_server 192.168.201.100 443 { //指定真正的服务器成员
weight 1 //为负载平衡决策指定真正的服务器权重
SSL_GET { //使用SSLGET请求检查实际服务器可用性
url { //标识url定义块
path / //指定url路径
digest ff20ad2481f97b1754ef3e12ecd3a9cc //为特定的url路径指定摘要
}
url {
path /mrtg/
digest 9b3a0c85a887a256d6939da88aabd8cd
}
connect_timeout 3 //使用超时连接远程服务器
nb_get_retry 3 //最大重试次数
delay_before_retry 3 //连续三次重试之间的延迟
}
}
键入“path”是指正在调用的脚本的完整路径。注意,对于需要参数的脚本,路径和参数必须用双引号(“)括起来。**
更改配置文件:
global_defs {
router_id LVS_DEVEL_R1
}
vrrp_instance outside {
state MASTER
nopreempt
interface eth0
virtual_router_id 1
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.234.100
}
}
vrrp_instance inside {
state MASTER
nopreempt
interface eth1
virtual_router_id 1
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
1.1.1.100
}
}
virtual_server 192.168.234.100 80 {
delay_loop 0
lb_algo rr
lb_kind NAT
nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 1.1.1.3 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 1.1.1.4 80{
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@localhost ~]# service keepalived start
正在启动 keepalived:
[root@localhost ~]# ip a //查看网卡配置信息
inet 192.168.234.141/24 brd 192.168.234.255 scope global eth0
inet 192.168.234.100/32 scope global eth0
inet 1.1.1.1/8 brd 1.255.255.255 scope global eth1
inet 1.1.1.100/32 scope global eth1
第三台:安装步骤和第二台一样:
前面操作一样:
配置文件不同:
[root@localhost keepalived-1.2.13]# vim /etc/keepalived/keepalived.conf
global_defs {
router_id LVS_DEVEL_R2
}
vrrp_instance outside {
state BACKUP
nopreempt
interface eth0
virtual_router_id 1
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.234.100
}
}
virtual_instance inside {
state BACKUP
nopreempt
insterface eth1
virtual_router_id 1
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
1.1.1.100
}
}
virtual_server 192.168.234.100 80 {
delay_loop 0
lb_algo rr
lb_kind NAT
nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 1.1.1.3 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 1.1.1.4 80{
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@localhost keepalived-1.2.13]# service keepalived restart
[root@localhost keepalived-1.2.13]# ip a
当断掉一个keepalived之后可以正常运行,ip地址是可以正常漂移
第四台:安装web服务进行验证:
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
HWADDR=00:0C:29:BB:8E:CF
TYPE=Ethernet
UUID=11889ffd-2f8b-4e38-9994-828c5ad0a0ac
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=1.1.1.3
NETMASK=255.0.0.0
GATEWAY=1.1.1.100
[root@localhost ~]# service network restart
[root@localhost ~]# ping 1.1.1.100 //是可以ping通的
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# echo "This is the first web service. Congratulations!" > /var/www/html/index.html
[root@localhost ~]# service httpd start
[root@localhost ~]# service iptables stop
第五台:和上一台操作一样:
[root@localhost 桌面]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
HWADDR=00:0C:29:A1:02:3C
TYPE=Ethernet
UUID=3ea69cdf-3cf4-457e-b7b6-8fb31d58f01d
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=1.1.1.4
NETMASK=255.0.0.0
GATEWAY=1.1.1.100
[root@localhost 桌面]# service network restart
[root@localhost 桌面]# yum -y install httpd
[root@localhost 桌面]# echo "This is the second web service. Congratulations on your successful connection!" > /var/www/html/index.html
[root@localhost 桌面]# service httpd start
[root@localhost 桌面]# service iptables stop
验证:我用第一台客户机访问漂移IP地址:192.168.234.100,看能不能找到负载均衡
[root@localhost ~]# service keepalived stop
停止 keepalived: [确定]
keepalived官网文档