Keepalived的作用是检测服务器的状态,如果有一台web服务器宕机,或工作出现故障,Keepalived将检测到,并将有故障的服务器从系统中剔除,同时使用其他服务器代替该服务器的工作,当服务器工作正常后Keepalived自动将服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的服务器。
keepalived起初是专门针对LVS设计的一款强大的辅助工具,主要用来提供故障切换和健康检查的功能,用来判断LVS负载调度器、节点服务器的可用性,及时隔离并替换为新的服务器,当故障主机恢复后将其重新加入群集
keepalived采用vrrp(virtual router redundancy protocol,虚拟路由冗余协议)热备份协议,以软件的方式实现Linux服务器的多级热备功能。vrrp是针对路由器的一种备份解决方案,由多台路由器组成一个热备组,通过共用的虚拟IP地址对外提供提供服务,每个热备组内同一时刻只有一台主路由器提供服务,其他路由器处于冗余状态,若主路由器失效,则从其他路由器会自动接替虚拟IP,继续提供服务,虚拟IP被称之为漂移地址
操作系统 | IP地址 | 部署服务 |
---|---|---|
centos7.6 | 192.168.81.210 -------------------VIP:192.168.81.100 | keepalived主 |
centos7.6 | 192.168.81.220 -------------------VIP:192.168.81.100 | keepalive从 |
centos7.6 | 192.168.81.230 | web1 |
centos7.6 | 192.168.81.240 | web2 |
centos7.6 | 192.168.81.250 | NFS |
1.安装支持软件
[root@localhost ~]# yum -y install kernel-devel openssl-devel popt-devel ipvsadm
[root@localhost ~]# tar xvf keepalived-1.2.13.tar.gz
[root@localhost ~]# cd keepalived-1.2.13
[root@localhost ~]# ./configure --prefix=/ --with-kernel-dir=/usr/src/kernels/2.6.32-431.el6.x86_64/ && make && make install
2.修改配置文件/etc/keepalived/keepalived.conf
[root@localhost ~]# vim /etc/keepalived/keepalived.conf
global_defs {
router_id LVS_DEVEL_R1 //本地路由名称,不可冲突
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.81.100 //VIP地址,可以有多个
}
}
virtual_server 192.168.81.100 80 {
delay_loop 15
lb_algo rr
lb_kind DR
!persistence_timeout 60 //不可省略,表示持续时间
protocol TCP
real_server 192.168.81.230 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
real_server 192.168.81.240 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
}
3.重启服务
[root@localhost ~]# service keepalived restart
4.查看VIP
[root@localhost ~]# ip add show
与主服务器配置文件修改有三点不同
修改路由名称
修改优先级
修改热备状态
1.1.安装支持软件
[root@localhost ~]# yum -y install kernel-devel openssl-devel popt-devel ipvsadm
[root@localhost ~]# tar xvf keepalived-1.2.13.tar.gz
[root@localhost ~]# cd keepalived-1.2.13
[root@localhost ~]# ./configure --prefix=/ --with-kernel-dir=/usr/src/kernels/2.6.32-431.el6.x86_64/ && make && make install
2.修改配置文件/etc/keepalived/keepalived.conf
[root@localhost ~]# vim /etc/keepalived/keepalived.conf
global_defs {
router_id LVS_DEVEL_R2 //修改
}
vrrp_instance VI_1 {
state BACKUP //修改
interface eth0
virtual_router_id 51
priority 80 //修改
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.81.100 //VIP地址,可以有多个
}
}
virtual_server 192.168.81.100 80 {
delay_loop 15
lb_algo rr
lb_kind DR
!persistence_timeout 60 //不可省略,表示持续时间
protocol TCP
real_server 192.168.81.230 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
real_server 192.168.81.240 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
}
3.重启服务
[root@localhost ~]# service keepalived restart
1.配置虚拟ip地址
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost ~]# cp ifcfg-lo ifcfg-lo:0
[root@localhost ~]# vim ifcfg-lo:0
DEVICE=lo:0
IPADDR=172.16.16.172
NETMASK=255.255.255.255
ONBOOT=yes
[root@localhost ~]# ifup lo:0
[root@localhost ~]# vim /etc/rc.local
route add -host 172.16.16.172 dev lo:0
2.调整/proc响应
[root@localhost ~]# vim /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
3.添加防火墙策略,允许访问http服务
[root@localhost ~]# firewall-cmd --permanent --add-service=http
[root@localhost ~]# firewall-cmd --reload
1.安装软件
[root@localhost ~]# yum -y install nfs-utils rpcbind
2.设置共享目录
[root@localhost ~]# vim /etc/exports //将/var/www/html做为共享目录
/var/www/html 172.16.16.0/24(rw,sync,no_root_squash)
3.启动nfs服务
[root@localhost ~]# service rpcbind start
[root@localhost ~]# service nfs start
4.showmount -e //查看本机共享目录
5.要给共享目录写入权限
[root@localhost ~]# chmod -R 755 /var/www/html
6.防火墙配置
[root@localhost ~]# firewall-cmd --permanent --add-service=nfs
[root@localhost ~]# firewall-cmd --reload
6.客户端挂载
[root@localhost ~]# mount 192.168.81.250:/var/www/html/ /var/www/html/
不要在nfs启动http服务,将所有的selinux关掉