标签:静态路由 两台 系统 dmi simple 怎么办 out 操作 因此
keepalived高可用对之间是通过VRRP通信的,因此,我从VRRP开始给您讲起.
1)VRRP,全称Virtual Router Reduancy Protocol,中文名为虚拟路由器冗余协议,VRRP的出现是为了解决静态路由的单点故障;
2)VRRP是通过一种竞选协议来将路由任务交给某台VRRP路由器的;
3)VRRP用IP多播的方式,(默认多播地址(224.0.0.18))实现高可用对之间通信;
4)工作时主节点发包,备节点接包,当备节点接收不到主节点发的包的时候,就启动接管程序接管主节点的资源。备节点可以有多个,通过优先级竞选,但一般keepalived系统运维工作中都是一对。
5)VRRP使用了加密协议加密数据,但keepalived官方目前还是推荐用明文的方式配置认证类型和密码。
介绍完了VRRP,接下来我在介绍一下keepalived服务的工作原理;
keepalived高可用对之间是通过VRRP进行通信的,VRRP是通过竞选机制来确定主备的,主的优先级高于备,因此,工作时会优先获得所有的资源,备节点处于等待状态,当主挂了的时候,备节点就会接管主节点的资源,然后顶替主节点对外提供服务。
在keepalived服务对之间,只有作为主的服务器会一直发送VRRP广播包,告诉备它还活着,此时备不会抢占主。当主不可用时,即备监听不到主发送的广播包时,就会启动相关服务接管资源,保证业务的连续性,接管速度最快可以小于一秒。
[root@lb02 ~]# yum install keepalived -y
[root@lb02 ~]# rpm -qa keepalived
keepalived-1.3.5-8.el7_6.5.x86_64
[root@lb02-e ~]# systemctl start keepalived
[root@lb02-e ~]# ps -ef|grep keepalived
root 10039 1 0 12:21 ? 00:00:00 /usr/sbin/keepalived -D
root 10040 10039 0 12:21 ? 00:00:00 /usr/sbin/keepalived -D
root 10041 10039 0 12:21 ? 00:00:03 /usr/sbin/keepalived -D
###提示:启动后有三个keepalived进程表示安装正确
[root@lb02 ~]# ip a | grep eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
inet 10.0.0.6/24 brd 10.0.0.255 scope global noprefixroute eth0
inet 192.168.200.16/32 scope global eth0
inet 192.168.200.17/32 scope global eth0
inet 192.168.200.18/32 scope global eth0
###提示:默认情况会启动三个VIP地址
[root@lb02-e ~]# systemctl stop keepalived
###提示: 测试完毕后关闭服务,上述测试需要在lb01和lb02两台服务器上进行
[root@lb02 ~]# rpm -qc keepalived
/etc/keepalived/keepalived.conf
/etc/sysconfig/keepalived
[root@lb02 ~]# cat -n /etc/keepalived/keepalived.conf
1 ! Configuration File for keepalived
2
3 global_defs {
4 notification_email {
5 acassen@firewall.loc
6 failover@firewall.loc
7 sysadmin@firewall.loc
8 }
9 notification_email_from Alexandre.Cassen@firewall.loc
10 smtp_server 192.168.200.1
11 smtp_connect_timeout 30
12 router_id LVS_DEVEL
13 vrrp_skip_check_adv_addr
14 vrrp_strict
15 vrrp_garp_interval 0
16 vrrp_gna_interval 0
17 }
?
第1行是注释,!开头和#号开头一样,都是注释。
第2行是空行。
第3行—8行是定义故障服务报警的Email地址。作用是服务发生切换或RS节点等有故障时,需要发送的Email地址,可以有多个,每行一个。
第9行是制定发送邮件的发送人,即发送人地址,也是可选配置。
第10行smtp_server指定发送邮件的smtp服务器,如果本机开启了sendmail或postfix。就可以使用上面默认配置实现邮件发送,也是可选配置。
第11行smtp_connect_timeout是连接smtp的超时时间,也是可选配置。
第12行是Keepalived服务器的路由标识{route_id}。在一个局域网内,这个标识{route_id}应该是唯一的。
大括号{}用来区分区块,要成对出现。如果漏写了半个大括号,keepalived运行时,不会报错,但是也不会得到预期的结果。另外,由于区块间存在多层嵌套关系,因此很容易遗漏区块结尾处的大括号,需要特别注意。
19 vrrp_instance VI_1 {
20 state MASTER
21 interface eth0
22 virtual_router_id 51
23 priority 100
24 advert_int 1
25 authentication {
26 auth_type PASS
27 auth_pass 1111
28 }
29 virtual_ipaddress {
30 192.168.200.16
31 192.168.200.17
32 192.168.200.18
33 }
34 }
?
第19行表示定义一个vrrp_instance实例,名字为VI_1,每个vrrp_instance实例可以认为是Keepalived服务的一个实例或者作为一个业务服务,在Keepalived服务配置中,这样的vrrp_instance实例可以有多个。 注意,存在于主节点中的vrrp_isntance实例在备节点也要存在,这样才能实现故障切换接管。
第20行state MASTER表示当前示例VI_1的角色状态,当前角色为MASTER,这个状态只能有MASTER和BACKUP两种状态,并且需要大写这些字符。其中MASTER为正式工作的状态,BACKUP为备用的状态。当MASTER所在的服务器故障或失效时,BACKUP所在的服务器会接管故障的MASTER继续提供服务。
第21行interface为网路通信接口。为对外提供服务的网络接口,如eth0,eth1当前主流的服务器都有2~4个网络接口,在选择服务接口时,要搞清楚。
第22行virtual_router_id为虚拟路由ID标识,这个标识最好是一个数字。
第23行priority为优先级,其后面的数值也是一个数字,数字越大,表示实例优先级越高。在同一个vrrp_instance实例里,MASTER的优先级配置要高于BACKUP的。若MASTER的priority值为150,那么BACKUP的priority必须小于150,一般建议隔50以上为佳。
第24行advent_int为同步通知间隔。MASTER与BACKUP之间通信检查的时间间隔,默认为1秒。
第25-27行authentication为权限认证配置。包含认证类型(auth_type)和认证密码(auth_pass) 。认证类型有PASS(simple passwd),AH(IPSEC)两种,官方推荐使用的类型为PASS。验证密码为明文方式,最好长度不能超过8个字符,建议四位数字,同一vrrp实例的MASTER与BACKUP使用相同的密码才能正常通信。
第29-32行virtual_ipaddress为虚拟IP地址。可以配置多个IP地址,每个地址占一行,配置时最好明确指定子网掩码以及虚拟IP绑定的网络接口。否则,子网掩码默认是32位,绑定的接口和前面的interface参数配置的一致。注意,这里的虚拟IP就是在工作中需要和域名绑定的IP,即和配置的高可用服务监听的IP要保持一致。
#优化基本源
[root@lb01-e ~]# vim /etc/yum.repos.d/CentOS-Base.repo
[root@lb02-e ~]# vim /etc/yum.repos.d/CentOS-Base.repo
[root@lb03-e ~]# vim /etc/yum.repos.d/CentOS-Base.repo
?
[root@lb01 ~]# yum install keepalived -y
?
[root@lb02 ~]# yum install keepalived -y
?
[root@lb01 ~]# rpm -qc keepalived
?
/etc/keepalived/keepalived.conf
?
/etc/sysconfig/keepalived
?
#配置keepalived主服务器lb01 MASTER
[root@lb01 ~]# vim /etc/keepalived/keepalived.conf
?
global_defs { #全局配置
router_id lb01 #keepalived服务器的路由标识(route_id)
}
?
vrrp_instance VI_1 { #vrrp实例,命名叫VI_1
state MASTER #当前实例VI_1的角色状态
interface eth0 #是对外提供服务的网络接口
virtual_router_id 50 #虚拟路由ID标识
priority 150 #优先级
advert_int 1 #同步通知间隔时间
authentication { #权限认证配置
auth_type PASS #认证方式
auth_pass 1111 #认证密码
}
virtual_ipaddress {
10.0.0.4 #虚拟的VIP地址
}
}
?
#启动keepalived(lb01)
[root@lb01 ~]# systemctl enable keepalived
Created symlink from /etc/systemd/system/multi-user.target.wants/keepalived.service to /usr/lib/systemd/system/keepalived.service.
[root@lb01 ~]# systemctl start keepalived
[root@lb01-e ~]# ip addr | egrep 10.0.0.4
inet 10.0.0.4/32 scope global eth0
#实战配置keepalived备服务器lb02 BACKUP
[root@lb02 ~]# vim /etc/keepalived/keepalived.conf
global_defs {
router_id lb02
}
?
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 50
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.4
}
}
?
#启动lb02的keepalived
[root@lb02 ~]# systemctl enable keepalived
Created symlink from /etc/systemd/system/multi-user.target.wants/keepalived.service to /usr/lib/systemd/system/keepalived.service.
[root@lb02 ~]# systemctl start keepalived
#可以看到地址被lb02接管,即IP地址已发生漂移
[root@lb02 ~]# ip addr | grep 10.0.0.4
?
[root@lb01 keepalived]# ip addr|egrep 10.0.0.3
inet 10.0.0.3/24 scope global secondary eth0:1
[root@lb01 ~]# systemctl start keepalived
[root@lb01 keepalived]# ip addr|egrep 10.0.0.3
[root@LVS-6 ~]# ip addr|egrep 10.0.0.3
[root@LVS-6 ~]# ip addr|egrep 10.0.0.3
inet 10.0.0.3/24 scope global secondary eth0:1
[root@lb01 keepalived]# /etc/init.d/keepalived start
Starting keepalived: [ OK ]
[root@lb01 keepalived]# ip addr|egrep 10.0.0.3
inet 10.0.0.3/24 scope global secondary eth0:1
[root@LVS-6 ~]# ip addr|egrep 10.0.0.3
Keepalived配置参数 | MASTER节点特殊参数 | BACKUP节点特殊参数 |
---|---|---|
router_id(唯一标识) | router_id lb01 | router_id lb02 |
state(角色状态) | state MASTER | state BACKUP |
priority(竞选优先级) | priority 150 | priority 100 |
高可用服务器对之间心跳线链路发生故障,导致无法正常通信
心跳线坏了(包括断了,老化)
网卡及相关驱动坏了,IP配置及冲突问题(网卡直连)
心跳线之间连接的设备故障(网卡及交换机)
仲裁的机器出问题了(采用总裁的方案)
高可用服务器上开启了iptables防火墙阻挠了心跳信息传输
高可用服务器上心跳网卡地址等信息配置不正确,导致发送心跳失败
其他服务配置不当等原因,如心跳方式不同,心跳广播冲突,软件bug等。
tcpdump -nn -c 20 -i any host 224.0.0.18
[root@lb02 scripts]# firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --in-interface eth0 --destination 224.0.0.18 --protocol vrrp -j ACCEPT
[root@lb02 scripts]# firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --in-interface eth1 --destination 224.0.0.18 --protocol vrrp -j ACCEPT
iptables -I INPUT -i eth0 -d 224.0.0.0/8 -p vrrp -j ACCEPT
iptables -I OUTPUT -o eth0 -d 224.0.0.0/8 -p vrrp -j ACCEPT
tcpdump -nn -c 20 -i any host 224.0.0.18
HOSTNAME | IP | 说明 |
---|---|---|
lb01 | 10.0.0.5 | VIP:10.0.0.12(用于绑定A服务www.etiantian.org域名) |
lb02 | 10.0.0.6 | VIP:10.0.0.13(用于绑定B服务bbs.etiantian.org域名) |
web01 | 10.0.0.8 | Nginx web服务器1 |
web02 | 10.0.0.7 | Nginx web服务器2 |
[root@lb01 ~]# cat /etc/keepalived/keepalived.conf
global_defs {
router_id LVS_01
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3/24 dev eth0 label eth0:1
}
}
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.4/24 dev eth0 label eth0:2
}
}
[root@lb01 ~]# cat /etc/keepalived/keepalived.conf
global_defs {
router_id LVS_02
}
?
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3/24 dev eth0 label eth0:1
}
}
?
vrrp_instance VI_2 {
state MASTER
interface eth0
virtual_router_id 52
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.4/24 dev eth0 label eth0:2
}
}
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
log_format main ‘$remote_addr - $remote_user [$time_local] "$request" ‘
‘$status $body_bytes_sent "$http_referer" ‘
‘"$http_user_agent" "$http_x_forwarded_for"‘;
upstream server_pools {
server 10.0.0.7;
server 10.0.0.8;
server 10.0.0.9;
}
?
server {
listen 10.0.0.3:80;
server_name www.etiantian.org;
location / {
proxy_pass http://server_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
access_log logs/access_www.log main;
}
server {
listen 10.0.0.4:80;
server_name blog.etiantian.org;
location / {
proxy_pass http://server_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
access_log logs/access_blog.log main;
}
}
systemctl restart keepalived
systemctl restart nginx
10.0.0.3 www.etiantian.org
10.0.0.4 blog.etiantian.org
#!/bin/bash
nginxpid=$(ps -C nginx --no-header|wc -l)
#1.判断 Nginx 是否存活,如果不存活则尝试启动 Nginx
if [ $nginxpid -eq 0 ];then
systemctl start nginx
sleep 3
#2.等待 3 秒后再次获取一次 Nginx 状态
nginxpid=$(ps -C nginx --no-header|wc -l)
#3.再次进行判断, 如 Nginx 还不存活则停止 Keepalived,让地址进行漂移,并退出脚本
if [ $nginxpid -eq 0 ];then
systemctl stop keepalived
fi
fi
global_defs {
router_id lb01
}
?
vrrp_script check_web {
script "/server/scripts/check_web.sh"
interval 5
weight 50
}
?
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 50
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
?
virtual_ipaddress {
10.0.0.3
}
}
?
vrrp_instance VI_2 {
state BACKUP
interface eth1
virtual_router_id 55
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 2222
}
?
virtual_ipaddress {
172.16.1.4
}
?
#2.调用并运行该脚本
track_script {
check_web
}
}
global_defs {
router_id lb01
}
?
vrrp_script check_web {
script "/server/scripts/check_web.sh"
interval 5
weight 50
}
?
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 50
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
?
virtual_ipaddress {
10.0.0.3
}
}
?
vrrp_instance VI_2 {
state BACKUP
interface eth1
virtual_router_id 55
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 2222
}
?
virtual_ipaddress {
172.16.1.4
}
?
#2.调用并运行该脚本
track_script {
check_web
}
}
[root@lb02 /server/scripts]# cat check_brain.sh
#!/bin/bash
?
lb01_vip=10.0.1.3
lb01_nginx=$(curl -x 10.0.1.5:80 -I -s -w "%{http_code}\n" -o /dev/null blog.oldzhang.com)
?
#ping -c 1 -W 1 ${lb01_ip} &>/dev/null
#如果lb01的IP能ping通,但是我自己也存在VIP,我就认为发生裂脑了,我就把自己的keep干掉
if [ ${lb01_nginx} -eq 200 -a `ip add|grep "$lb01_vip"|wc -l` -eq 1 ];then
echo "ha is bad" >> /tmp/check.txt
systemctl stop keepalived
else
echo "ha is ok" >> /tmp/check.txt
fi
#lb01存在vip地址
[root@lb01 ~]# ip addr | grep 10.0.0.4
inet 10.0.0.4/32 scope global eth0
?
#停止lb01上的keepalived,检测vip已不存在
[root@lb01 ~]# systemctl stop keepalived
[root@lb01 ~]# ip addr | grep 10.0.0.4
?
global_defs {
router_id lb02
}
?
vrrp_script check_web {
script "/server/scripts/check_web.sh"
interval 5
weight 50
}
?
vrrp_script check_brain {
script "/server/scripts/check_split_brain.sh"
interval 5
weight 50
}
?
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 50
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
?
virtual_ipaddress {
10.0.0.3
}
}
?
vrrp_instance VI_2 {
state MASTER
interface eth1
virtual_router_id 55
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 2222
}
?
virtual_ipaddress {
172.16.1.4
}
?
#2.调用并运行该脚本
track_script {
check_web
check_brain
}
}
标签:静态路由 两台 系统 dmi simple 怎么办 out 操作 因此
原文地址:https://www.cnblogs.com/dabai-wang09/p/11461187.html