Keepalived工具介绍
一个合格的集群应该具有的特性
健康检查(探针)
VRRP虚拟路由冗余协议
Keepalived采用VRRP热备份协议实现Linux服务器的多机热备功能
双机热备的故障切换是由虚拟IP地址的漂移来实现,适用于各种应用服务器
主服务器:192.168.242.66
备服务器:192.168.242.67
systemctl disable --now firewalld
setenforce 0
vim /etc/selinux/config
SELINUX=disabled
yum -y install keepalived
cd /etc/keepalived/
cp keepalived.conf keepalived.conf.bak
或者 cp keepalived.conf{,.bak}
vim keepalived.conf
......
global_defs { #定义全局参数
smtp_server 127.0.0.1 #邮件服务指向本地
smtp_connect_timeout 30
router_id LVS_01 #指定服务器(路由器)的名称,主备服务器名称须不同,
主为LVS_01, 备为LVS_02
#注释掉,取消严格遵守VRRP协议功能,否则VIP无法被连接
#vrrp_skip_check_adv_addr
#vrrp_strict
#vrrp_garp_interval 0
#vrrp_gna_interval 0
}
vrrp_instance VI_1 { #定义VRRP热备实例参数
state MASTER #指定热备状态,主为MASTER,备为BACKUP
interface ens32 #指定承载vip地址的物理接口
virtual_router_id 51 #指定虚拟路由器的ID号,每个热备组保持一致
#nopreempt
#如果设置非抢占模式,两个节点state必须为BACKUP,并加上配置 nopreempt
priority 100 #指定优先级,数值越大优先级越高,这里设置主为100,备为90
advert_int 1 #通告间隔秒数(心跳频率)
authentication { #定义认证信息,每个热备组保持一致
auth_type PASS #认证类型
auth_pass 1111 #指定验证密码,主备服务器保持一致
}
virtual_ipaddress { #指定群集vip地址
192.168.242.188
}
}
主服务器配置
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_01
}
vrrp_instance VI_1 {
state MASTER
interface ens32
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.242.188
}
}
备服务器
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_02
}
vrrp_instance VI_1 {
state BACKUP
interface ens32
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.242.188
}
}
systemctl restart keepalived
ip addr ##查看到主网卡下面会多出虚拟网卡地址
systemctl disable --now firewalld
setenforce 0
vim /etc/selinux/config
SELINUX=disabled
yum -y install keepalived
cd /etc/keepalived/
cp keepalived.conf keepalived.conf.bak
或者 cp keepalived.conf{,.bak}
配置主服务器的配置,包括keepalived和LVS
vim keepalived.conf
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_01
}
vrrp_instance VI_1 {
state MASTER
interface ens32
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.242.188
}
}
virtual_server 192.168.242.188 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 0
protocol TCP
real_server 192.168.242.68 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.242.69 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
##删除后面多余的配置##
配置文件中LVS配置解释
##指定虚拟服务器地址(VIP)、端口,定义虚拟服务器和Web服务器池参数
virtual_server 192.168.242.188 80 {
delay_loop 6 #健康检查的间隔时间(秒)
lb_algo rr #指定调度算法,轮询(rr)
lb_kind DR #指定群集工作模式,直接路由(DR)
persistence_timeout 0 #连接保持时间(秒)
protocol TCP #应用服务采用的是 TCP协议
real_server 192.168.242.68 80 { ##指定第一个Web节点的地址、端口
weight 1 #节点的权重
#添加以下健康检查方式
TCP_CHECK {
connect_port 80 #添加检查的目标端口
connect_timeout 3 #添加连接超时(秒)
nb_get_retry 3 #添加重试次数
delay_before_retry 3 #添加重试间隔
}
}
real_server 192.168.242.69 80 { #添加第二个 Web节点的地址、端口
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
##删除后面多余的配置##
主服务器配置完成后,部署备服务器
cd /etc/keepalived/
scp keepalived.conf 192.168.242.67:`pwd`
vim keepalived.conf
##改三处
router_id LVS_02
state BACKUP
priority 90
节点服务器1:192.168.242.68
节点服务器2:192.168.242.69
两个节点服务器都需要相同的配置
systemctl disable --now firewalld
setenforce 0
vim /etc/selinux/config
SELINUX=disabled
yum -y install httpd
vim /etc/httpd/conf/httpd.conf
KeepAlive off
cd /var/www/html/
echo 'this is web 1
' > index.html
systemctl restart httpd
配置虚拟IP
cd /etc/sysconfig/network-scripts/
cp ifcfg-lo ifcfg-lo:0
vim ifcfg-lo:0
DEVICE=lo:0
ONBOOT=yes
IPADDR=192.168.242.188
NETMASK=255.255.255.255 #注意:子网掩码必须全为 1
ifup lo:0
ifconfig lo:0
route add -host 192.168.242.188 dev lo:0
route -n
vim /etc/rc.d/rc.local
route add -host 192.168.242.188 dev lo:0
chmod +x /etc/rc.d/rc.local
配置内核参数
vim /etc/sysctl.conf
......
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
sysctl -p
浏览器 访问 http://192.168.242.188
或者
curl 192.168.242.188
Keepalived通过什么判断哪台主机为主服务器,通过什么方式配置浮动IP?
keepalived的抢占与非抢占模式:
主备都是一样的配置
vrrp_instance VI_1 {
state BACKUP
nopreempt
interface ens32
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.242.188
}
}
Master主机一直发送心跳消息给backup主机,如果中间的链路突然断掉,backup主机将无法收到master主机发送过来的心跳消息(也就是vrrp报文),backup主机这时候会立即抢占master的工作,但其实这时候的master是正常工作的,此时就会出现脑裂的现象,即两台主机都为master状态,都有VIP且都正常工作
关闭两个都成为master主机中的一个,一般关闭原本就为master的主机
###Shell脚本判断主机之间是否时链路断开
###主调度器
###判断主从调度器之间链路是否断连
ping -c 3 -i 0.5 -w 2 192.168.58.63 &> /dev/null
if [ $? -ne 0 ];then
ssh 192.168.242.66 ping -c 3 -i 0.5 -w 2 -I 192.168.242.66 192.168.242.67 &> /dev/null
if [ $? -eq 0 ];then
systemctl stop keepalived
fi
fi
主备服务器配置
vim keepalived.conf
......
global_defs {
smtp_server 127.0.0.1
router_id Nginx_01
}
vrrp_script chk_nginx {
###指定监控脚本的路径
script "/etc/keepalived/nginx_check.sh"
###检测的间隔时间
interval 2
###权重
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface ens32
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.242.188
}
track_script { ###加载追踪模块
chk_nginx
}
}
nginx监控脚本
###检测nginx是否启动的脚本
#!/bin/bash
if ! killall -0 nginx &> /dev/null
then
systemctl stop keepalived
fi
vim nginx_check.sh
###给脚本添加执行权限
chmod +x nginx_check.sh
四层代理
yum -y install nginx
cd /etc/nginx/
vim nginx.conf
stream {
upstream backend {
server 192.168.242.68:80;
server 192.168.242.68:80;
}
server {
listen 8080;
proxy_pass backend;
}
}
systemctl restart nginx