Keepalived工具介绍
- 专为LVS和HA设计的一款健康检查工具
- 支持故障自动切换(Failover)
- 支持节点健康状态检查(Health Checking)
Keepalived实现原理刨析
- Keepalived采用VRRP热备份协议实现Linux服务器的多机热备功能
shell
#主服务器keepalived的安装
[root@localhost ~]# systemctl stop NetworkManager #网络托管 网络管理
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# yum -y install keepalived ipvsadm
[root@localhost ~]# systemctl enable keepalived
#主服务器keepalived的配置
[root@localhost ~]# cd /etc/keepalived/
[root@localhost keepalived]# cp keepalived.conf keepalived.conf.bak
[root@localhost keepalived]# vi keepalived.conf
global_defs { :w#全局参数
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
#vrrp_strict #严格模式 禁用单播模式
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_01 #路由器名称
}
vrrp_instance VI_1 { #示例参数
state MASTER
interface ens33
virtual_router_id 51 #虚拟路由id在群集中需一致
priority 100 #优先级 0~255
advert_int 1 #通告发送间隔
authentication {
auth_type PASS #验证类型
auth_pass 1111 #验证密码
}
virtual_ipaddress {
192.168.10.172
}
}
virtual_server 192.168.10.172 80 { #群集ip
delay_loop 6 #健康检查间隔
lb_algo wrr #负载均衡算法
lb_kind DR #负载均衡类型
nat_mask 255.255.255.0 #非不许
! persistence_timeout 50
protocol TCP #虚拟服务器协议
real_server 192.168.10.103 80 { #真实服务器
weight 1 #权重
TCP_CHECK { #健康检查
connect_port 80
connect_timeout 3
nb_get_retry 3 #超时次数
delay_before_retry 3 #重连时间
}
}
real_server 192.168.10.104 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
#主服务器内核参数的配置
[root@localhost ~]# vi /etc/sysctl.conf
#添加
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
[root@localhost ~]# sysctl -p
#开启主服务器的keepalived服务
[root@localhost keepalived]# systemctl start keepalived
[root@localhost keepalived]# ip add show dev ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:ae:7f:64 brd ff:ff:ff:ff:ff:ff
inet 192.168.10.101/24 brd 172.16.16.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.10.172/32 scope global ens33
valid_lft forever preferred_lft forever
#配置从调度器
#从调度器keepalived安装
[root@localhost ~]# systemctl stop NetworkManager
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# yum -y install keepalived ipvsadm
[root@localhost ~]# systemctl enable keepalived
#从调度器keepalived的配置
[root@localhost ~]# cd /etc/keepalived/
[root@localhost keepalived]# cp keepalived.conf keepalived.conf.bak
[root@localhost keepalived]# vi keepalived.conf
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
#vrrp_strict
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_02
}
vrrp_instance VI_1 {
state BACKUP #备份
interface ens33
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.10.172
}
}
virtual_server 192.168.10.172 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
! persistence_timeout 50
protocol TCP
real_server 192.168.10.101 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.10.102 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
#从服务器内核参数的配置
[root@localhost ~]# vi /etc/sysctl.conf
在末尾添加:
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
[root@localhost ~]# sysctl -p
#开启从服务器的keepalived服务
[root@localhost keepalived]# systemctl start keepalived
#web网络的配置
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-lo ifcfg-lo:0
[root@localhost network-scripts]# vi ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.10.172
NETMASK=255.255.255.255
ONBOOT=yes
[root@localhost network-scripts]# systemctl restrt network
[root@localhost network-scripts]# vi /etc/rc.local
/sbin/route add -host 192.168.10.172 dev lo:0
[root@localhost network-scripts]# route add -host 192.168.10.172 dev lo:0
#httpd服务的安装
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# vi /var/www/html/index.html
test web01
#内核参数的设置
[root@localhost ~]# vi /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
#开启httpd服务
[root@localhost ~]# systemctl start httpd