十五周四次课
18.1集群介绍
18.2keepalived介绍
18.3/18.4/18.5用keepalived配置高可用集群
18.1集群介绍
Linux集群概述
根据功能划分为两大类:高可用和负载均衡
实现负载均衡的开源软件有LVS、keepalived、haproxy、nginx,商业的有F5、Netscaler
商业的负载均衡器,优势在于有更高的并发量,也有很好的稳定性
商业的负载均衡器,相当于是一台设备,成本高昂
实现高可用的开源软件有:heartbeat、keepalived
heartbeat软件,在centos6中有很多bug,而且很久没有更新版本了,不建议继续使用
keepalived软件,不仅有高可用的功能,还有负载均衡的功效,配置也非常简单
1.高可用集群通常为两台服务器,一台工作,另外一台作为冗余,当提供服务的机器宕机,冗余将接替继续提供服务
高可用衡量标准,4个九,就是99.99%,表示一个比例,在一年时间内99.99%的时间都是在线的,不允许宕机,不允许服务不可用。另外还有的是5个九,6个九。
2.负载均衡集群,需要有一台服务器作为分发器,它负责把用户的请求分发给后端的服务器处理,在这个集群里,除了分发器外,就是给用户提供服务的服务器了,这些服务器数量至少为2
18.2keepalived介绍
keepalived介绍
在这里使用keepalived来实现高可用集群,因为heartbeat在centos6上有一些问题,影响实验效果
heartbeat 切换的时候,会不是很及时——>比如:主已经宕机了,但从却一直启动不了,通信会不顺畅,所以不使用 heartbeat
keepalived通过VRRP(Virtual Router Redundancy Protocl 中文为:虚拟路由冗余协议)来实现高可用。
虚拟路由冗余协议(Virtual Router Redundancy Protocol,简称VRRP)是由IETF提出的解决局域网中配置静态网关出现单点失效现象的路由协议,1998年已推出正式的RFC2338协议标准。VRRP广泛应用在边缘网络中,它的设计目标是支持特定情况下IP数据流量失败转移不会引起混乱,允许主机使用单路由器,以及及时在实际第一跳路由器使用失败的情形下仍能够维护路由器间的连通性。
在这个协议里,会将多台功能相同的路由器组成一个小组,这个小组里会有1个master角色和N(N>=1)个backup角色。
master会通过组播的形式向各个backup发送VRRP协议的数据包,当backup收不到master发来的VRRP数据包时,就会认为master宕机了。此时就需要根据各个backup的优先级来决定谁成为新的master。
Keepalived要有三个模块,分别是core、check和vrrp。其中core模块为keepalived的核心,负责主进程的启动、维护以及全局配置文件的加载和解析,check模块负责健康检查,vrrp模块是来实现VRRP协议的。
18.3/18.4/18.5用keepalived配置高可用集群
用keepalived配置高可用集群
准备两台机器130和132,130作为master,132作为backup
两台机器都执行yum install -y keepalived
两台机器都安装nginx,其中130上已经编译安装过nginx,132上需要yum安装nginx: yum install -y nginx
设定vip为100
编辑130上keepalived配置文件,
130编辑监控脚本,
给脚本755权限
systemctl start keepalived 130启动服务
132上编辑配置文件,
132上编辑监控脚本,
给脚本755权限
132上也启动服务 systemctl start keepalived
1.首先准备两台机器,都安装keepalived(130为master,129为backup)
keepalived,实际是包含一个服务的,也就是说这个服务用来实现高可用
A机器安装keepalived
[root@tianqi-01 ~]# yum install -y keepalived
B机器安装keepalived
[root@tianqi-02 ~]# yum install -y keepalived
2.这里使用 nginx ,把它作为一个高可用的对象——>使用nginx作为演示对象的原因,因为nginx在工作中,在生产环境中,很多企业把nginx做一个负载均衡器
假设nginx一旦挂掉,那么后端所有的web,即使说是正常的,那也无法访问到
3.若是A、B机器没有装nginx服务,可以直接 yum安装
若是lnmp安装过nginx,则不需要安装了(源码包安装的nginx)
直接yum安装的nginx,两者很容易区分(PS:有时直接yum安装不了,需要安装yum扩展源——>yum install -y epel-release)
A机器安装nginx
B机器安装nginx
[root@tianqi-02 ~]# yum install -y nginx
A机器上启动并查看nginx服务
[root@tianqi-01 ~]# systemctl start nginx
[root@tianqi-01 ~]# ps aux |grep nginx
root 952 0.0 0.0 20496 628 ? Ss 21:04 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 954 0.0 0.3 22940 3468 ? S 21:04 0:00 nginx: worker process
nobody 955 0.0 0.3 22940 3212 ? S 21:04 0:00 nginx: worker process
root 1321 0.0 0.0 112660 972 pts/0 R+ 21:08 0:00 grep --color=auto nginx
[root@tianqi-01 ~]#
4.更改keepalived配置文件了,
默认的配置文件路径在/etc/keepalived/keepalived.conf
清空文件的快捷键方法 > !$
A机器更改配置文件
[root@tianqi-01 ~]# ls /etc/keepalived/keepalived.conf
/etc/keepalived/keepalived.conf
[root@tianqi-01 ~]# > /etc/keepalived/keepalived.conf //直接清空文件内容
[root@tianqi-01 ~]# vim !$ //去文件地址下载内容,只需要改网卡名字和IP
vim /etc/keepalived/keepalived.conf
global_defs { //global_defs 全局配置标识
notification_email { //notification_email用于设置报警邮件地址
aming@aminglinux.com //可以设置多个,每行一个
}
notification_email_from root@aminglinux.com //设置邮件发送地址(实际上没用)
smtp_server 127.0.0.1 //设置邮件的smtp server地址,若为127.0.0.1则使用本机自带邮件服务器发送
smtp_connect_timeout 30 //设置连接smtp sever超时时间
router_id LVS_DEVEL
}
vrrp_script chk_nginx { //chk_nginx为自定义文件名字,后面还会用到
script "/usr/local/sbin/check_ng.sh" //检查服务是否正常,通过写脚本(该脚本为监控Nginx服务的脚本)实现,脚本检查服务健康状态
interval 3 //检查的时间间断是3秒
}
vrrp_instance VI_1 { //VRRP配置标识 VI_1是实例名称
state MASTER //定义master相关
interface ens33 //通过vrrp协议去通信、去发广播。配置时,需注意自己的网卡名称
virtual_router_id 51 //定义路由器ID ,配置的时候和从机器一致
priority 100 //权重,主角色和从角色的权重是不同的
advert_int 1 //设定MASTER与BACKUP主机质检同步检查的时间间隔,单位为秒
authentication { //认证相关信息
auth_type PASS //这里认证的类型是PASS
auth_pass aminglinux>com //密码的形式是一个字符串
}
virtual_ipaddress { //设置虚拟IP地址 (VIP),又叫做漂移IP地址
192.168.23.100
}
track_script { //加载脚本
chk_nginx
}
virtual_ipaddress:简称VIP,这个vip,两台机器,一个主,一个从,正常的情况是主在服务,主宕掉了,从起来了,从启动服务,从启动nginx以后,访问哪个IP呢?把域名解析到哪个IP上呢?假如解析到主上,主宕掉了,所以这个,需要定义一个公有IP(主上用的IP,从上也用的IP),这个IP是随时可以夏掉,去配置的。
5.定义监控脚本,
脚本路径在keepalived配置文件中有定义,路径为/usr/local/sbin/check_ng.sh
A机器定义监控脚本
[root@tianqi-01 ~]# vim /usr/local/sbin/check_ng.sh
#!/bin/bash
#时间变量,用于记录日志
d=`date --date today +%Y%m%d_%H:%M:%S`
#计算nginx进程数量
n=`ps -C nginx --no-heading|wc -l`
#如果进程为0,则启动nginx,并且再次检测nginx进程数量,
#如果还为0,说明nginx无法启动,此时需要关闭keepalived
if [ $n -eq "0" ]; then
/etc/init.d/nginx start
n2=`ps -C nginx --no-heading|wc -l`
if [ $n2 -eq "0" ]; then
echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log
systemctl stop keepalived
fi
fi
保存退出
“脑裂”,在高可用(HA)系统中,当联系2个节点的“心跳线”断开时,本来为一整体、动作协调的HA系统,就分裂成为2个独立的个体。由于相互失去了联系,都以为是对方出了故障。两个节点上的HA软件像“裂脑人”一样,争抢“共享资源”、争起“应用服务”,就会发生严重后果——或者共享资源被瓜分、2边“服务”都起不来了;或者2边“服务”都起来了,但同时读写“共享存储”,导致数据损坏
如何判断脑裂?
分别在两台机查看当前服务器是否拥有虚拟IP,如果两台服务器都拥有,则说明发生了脑裂,证明目前双机通信出现问题,产生此问题的原有在于 两台服务器都探测不到组内其他服务器的状态(心跳请求无法正常响应),私自判定另一台服务器挂起,则抢占虚拟IP,脑裂的出现是不被允许的,解决此问题的方法为检查防火墙设置(关闭防火墙)或者使用串口通信。
6.脚本创建完之后,还需要改变脚本的权限(不更改权限,就无法自动加载脚本,那就无法启动keepalived服务)
[root@tianqi-01 ~]# chmod 755 /usr/local/sbin/check_ng.sh
[root@tianqi-01 ~]#
7.启动keepalived服务,并查看是否启动成功(PS:启动不成功,有可能是防火墙未关闭或者规则限制导致的)
systemctl stop firewalld 关闭firewalld
iptables -nvL
setenforce 0 临时关闭selinux
getenforce命令查看是否为Permissive
这时再来启动keepalived,就会看到keepalived进程服务了
[root@tianqi-01 ~]# systemctl start keepalived
[root@tianqi-01 ~]# ps aux |grep keepalived
root 3941 0.0 0.1 120740 1408 ? Ss 21:48 0:00 /usr/sbin/keepalived -D
root 3942 0.0 0.3 127476 3272 ? S 21:48 0:00 /usr/sbin/keepalived -D
root 3944 0.0 0.3 131780 3128 ? S 21:48 0:00 /usr/sbin/keepalived -D
root 4011 0.0 0.0 112660 976 pts/0 R+ 21:49 0:00 grep --color=auto keepalived
[root@tianqi-01 ~]#
8.查看nginx服务进程
[root@tianqi-01 ~]# ps aux |grep nginx
root 952 0.0 0.0 20496 628 ? Ss 21:04 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 954 0.0 0.3 22940 3468 ? S 21:04 0:00 nginx: worker process
nobody 955 0.0 0.3 22940 3212 ? S 21:04 0:00 nginx: worker process
root 1469 0.0 0.0 112664 976 pts/0 S+ 21:22 0:00 grep --color=auto nginx
[root@tianqi-01 ~]#
9.这时停止nginx服务
/etc/init.d/nginx stop
[root@tianqi-01 ~]# /etc/init.d/nginx stop
Stopping nginx (via systemctl): [ OK ]
[root@tianqi-01 ~]#
10.再来查看nginx服务进程,会看到自动加载了
[root@tianqi-01 ~]# !ps
ps aux |grep nginx
root 952 0.0 0.0 20496 628 ? Ss 21:04 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 954 0.0 0.3 22940 3468 ? S 21:04 0:00 nginx: worker process
nobody 955 0.0 0.3 22940 3212 ? S 21:04 0:00 nginx: worker process
root 1469 0.0 0.0 112664 976 pts/0 S+ 21:22 0:00 grep --color=auto nginx
[root@tianqi-01 ~]# date
Tue Apr 10 21:24:11 CST 2018
[root@tianqi-01 ~]#
11.keepalived日志文件路径
/var/log/messages
12.查看ip地址,使用 ip add 命令,而不能使用ifconfig命令,因为 ifconfig命令是无法查看到vip192.168.23.100这个IP的
[root@tianqi-01 ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:25:28:af brd ff:ff:ff:ff:ff:ff
inet 192.168.23.130/24 brd 192.168.23.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.11.139/24 brd 192.168.11.255 scope global ens33:0
valid_lft forever preferred_lft forever
inet 192.168.188.100/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::1eb9:8f9e:264a:7159/64 scope link
valid_lft forever preferred_lft forever
[root@tianqi-01 ~]#
13.检查A、B 机器下防火墙和selinux是否关闭了,若没有关闭,可能会导致实验失败
systemctl stop firewalld 关闭firewalld
iptables -nvL
setenforce 0 临时关闭selinux
getenforce命令查看是否为Permissive
A机器上
[root@tianqi-01 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
1805 137K ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0
1 84 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
1 52 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
1130 106K REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT 1894 packets, 238K bytes)
pkts bytes target prot opt in out source destination
[root@tianqi-01 ~]# iptables -F
[root@tianqi-01 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 6 packets, 396 bytes)
pkts bytes target prot opt in out source destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 5 packets, 440 bytes)
pkts bytes target prot opt in out source destination
[root@tianqi-01 ~]# getenforce
Disabled
[root@tianqi-01 ~]#
B机器上
[root@tianqi-02 ~]# iptables -nvL
发现防火墙未关闭
[root@tianqi-02 ~]# systemctl stop firewalld
[root@tianqi-02 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
[root@tianqi-02 ~]# getenforce
Disabled
[root@tianqi-02 ~]#
以上就是主机器A的配置
backup 机器配置
1.在B机器yum安装nginx和keepalived
[root@tianqi-02 ~]# yum install -y epel-release
[root@tianqi-02 ~]# yum install -y nginx
2.关闭B机器的防火墙和selinux
iptables -F 清空规则
setenforce 0 临时关闭selinux
3.自定义B机器keepalived配置文件,,更改虚拟IP和主一样的
首先清空B机器keepalived里面自带的配置文件
[root@tianqi-02 ~]# > /etc/keepalived/keepalived.conf
[root@tianqi-02 ~]# cat !$
cat /etc/keepalived/keepalived.conf
[root@tianqi-02 ~]#
然后复制配置文件并粘贴进去,更改虚拟IP和主一样的
[root@tianqi-02 ~]# vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
aming@aminglinux.com
}
notification_email_from root@aminglinux.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_nginx {
script "/usr/local/sbin/check_ng.sh"
interval 3
}
vrrp_instance VI_1 {
state BACKUP //这里和master不一样的名字
interface eno16777736 //网卡和当前机器一致,否则无法启动keepalived服务
virtual_router_id 51 //和主机器保持一致
priority 90 //权重,要比主机器小的数值
advert_int 1
authentication {
auth_type PASS
auth_pass aminglinux>com
}
virtual_ipaddress {
192.168.23.100
}
track_script {
chk_nginx
}
}
保存退出
4.定义监控脚本,路径在keepalived里面已定义过,
这个脚本和主上的脚本有一点区别,启动nginx的命令不同,因为一个是yum安装,一个是源码包安装
[root@tianqi-02 ~]# vim /usr/local/sbin/check_ng.sh
#!/bin/bash
#时间变量,用于记录日志
d=`date --date today +%Y%m%d_%H:%M:%S`
#计算nginx进程数量
n=`ps -C nginx --no-heading|wc -l`
#如果进程为0,则启动nginx,并且再次检测nginx进程数量,
#如果还为0,说明nginx无法启动,此时需要关闭keepalived
if [ $n -eq "0" ]; then
systemctl start nginx
n2=`ps -C nginx --no-heading|wc -l`
if [ $n2 -eq "0" ]; then
echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log
systemctl stop keepalived
fi
fi
保存退出
5.改动脚本的权限,设置为755权限
[root@tianqi-02 ~]# chmod 755 !$
chmod 755 /usr/local/sbin/check_ng.sh
[root@tianqi-02 ~]#
6.B机器启动keepalived服务
systemctl start keepalived
[root@localhost ~]# systemctl start keepalived
[root@localhost ~]# ps aux |grep keep
root 2772 12.7 0.1 120668 1416 ? Ss 14:54 0:00 /usr/sbin/keepalived -D
root 2773 0.0 0.2 120668 2720 ? S 14:54 0:00 /usr/sbin/keepalived -D
root 6773 0.0 0.0 112644 952 pts/0 R+ 14:54 0:00 grep --color=auto keep
[root@localhost ~]# systemctl restart keepalived
[root@localhost ~]# ps aux |grep keep
root 50874 14.0 0.1 120668 1420 ? Ss 14:55 0:00 /usr/sbin/keepalived -D
root 50875 0.0 0.2 120668 2712 ? S 14:55 0:00 /usr/sbin/keepalived -D
root 52278 0.0 0.0 112644 948 pts/0 R+ 14:55 0:00 grep --color=auto keep
[root@localhost ~]#
如何区分主和从上的nginx?
A机器,是源码包安装的nginx(PS:这是lnmp配置好的环境虚拟主机内容)
[root@tianqi-01 ~]# cat /usr/local/nginx/conf/vhost/aaa.com.conf
server
{
listen 80 default_server;
server_name aaa.com;
index index.html index.htm index.php;
root /data/wwwroot/default;
location ~ \.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/tianqi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/wwwroot/default$fastcgi_script_name;
}
}
[root@tianqi-01 ~]# cat /data/wwwroot/default/index.html
This is the default site.
[root@tianqi-01 ~]# vim /data/wwwroot/default/index.html
master This is the default site.
B机器是yum安装的nginx
默认的索引页在 /usr/share/nginx/html/index.html
[root@localhost ~]# cat /usr/share/nginx/html/index.html
[root@localhost ~]# vim /usr/share/nginx/html/index.html
backup backup.
访问192.168.23.100这个VIP会看到和主机器(即A机器相同的内容),说明现在访问到的是机器master,VIP在master上
[root@localhost ~]# ps aux |grep nginx
root 2738 0.0 0.2 122864 2104 ? Ss 13:38 0:00 nginx: master process /usr/sbin/nginx
nginx 2739 0.0 0.3 123248 3572 ? S 13:38 0:00 nginx: worker process
root 6016 0.0 0.0 112644 952 pts/1 R+ 14:05 0:00 grep --color=auto nginx
[root@localhost ~]# systemctl stop nginx
[root@localhost ~]# !ps
ps aux |grep nginx
root 6093 0.0 0.2 122864 2108 ? Ss 14:05 0:00 nginx: master process /usr/sbin/nginx
nginx 6094 0.0 0.3 123248 3120 ? S 14:05 0:00 nginx: worker process
root 6099 0.0 0.0 112644 952 pts/1 R+ 14:05 0:00 grep --color=auto nginx
[root@localhost ~]#
这里没有停掉nginx服务是因为启动了keepalived服务,会自动加载nginx服务。
问题-B机器无法调用nginx服务?
B机器关闭nginx服务,keepalived无法拉动nginx服务起来
解决方法:
再次设置755权限,就可以拉动nginx服务了
测试高可用
1.模拟线上生产环境,主机器宕机环境,最简单直接的方法,就是直接关闭keepalived服务
2.关闭master机器(即A机器)上的keepalived服务
[root@tianqi-01 ~]# systemctl stop keepalived
[root@tianqi-01 ~]#
3.查看A机器上的VIP被已经释放掉了
[root@tianqi-01 ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:25:28:af brd ff:ff:ff:ff:ff:ff
inet 192.168.23.130/24 brd 192.168.23.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.11.139/24 brd 192.168.11.255 scope global ens33:0
valid_lft forever preferred_lft forever
inet6 fe80::1eb9:8f9e:264a:7159/64 scope link
valid_lft forever preferred_lft forever
[root@tianqi-01 ~]#
4.查看backup机器(即B机器)在监听VIP
[root@localhost ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:53:9d:62 brd ff:ff:ff:ff:ff:ff
inet 192.168.23.129/24 brd 192.168.23.255 scope global eno16777736
valid_lft forever preferred_lft forever
inet 192.168.23.100/32 scope global eno16777736
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe53:9d62/64 scope link
valid_lft forever preferred_lft forever
[root@localhost ~]#
5.查看B机器日志
[root@localhost ~]# tail /var/log/messages
Apr 8 14:14:10 localhost Keepalived_vrrp[2784]: Sending gratuitous ARP on eno16777736 for 192.168.23.100
Apr 8 14:14:10 localhost Keepalived_vrrp[2784]: Sending gratuitous ARP on eno16777736 for 192.168.23.100
Apr 8 14:14:10 localhost Keepalived_vrrp[2784]: Sending gratuitous ARP on eno16777736 for 192.168.23.100
Apr 8 14:14:10 localhost Keepalived_vrrp[2784]: Sending gratuitous ARP on eno16777736 for 192.168.23.100
Apr 8 14:14:15 localhost Keepalived_vrrp[2784]: Sending gratuitous ARP on eno16777736 for 192.168.23.100
Apr 8 14:14:15 localhost Keepalived_vrrp[2784]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eno16777736 for 192.168.23.100
Apr 8 14:14:15 localhost Keepalived_vrrp[2784]: Sending gratuitous ARP on eno16777736 for 192.168.23.100
Apr 8 14:14:15 localhost Keepalived_vrrp[2784]: Sending gratuitous ARP on eno16777736 for 192.168.23.100
Apr 8 14:14:15 localhost Keepalived_vrrp[2784]: Sending gratuitous ARP on eno16777736 for 192.168.23.100
Apr 8 14:14:15 localhost Keepalived_vrrp[2784]: Sending gratuitous ARP on eno16777736 for 192.168.23.100
[root@localhost ~]#
6.浏览器访问vip,会看到已经变成backup机器上的了
7.在master机器(即A机器)启动keepalived服务,会看到vip这个IP立刻回来了
[root@tianqi-01 ~]# systemctl start keepalived
[root@tianqi-01 ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:25:28:af brd ff:ff:ff:ff:ff:ff
inet 192.168.23.130/24 brd 192.168.23.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.11.139/24 brd 192.168.11.255 scope global ens33:0
valid_lft forever preferred_lft forever
inet 192.168.23.100/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::1eb9:8f9e:264a:7159/64 scope link
valid_lft forever preferred_lft forever
[root@tianqi-01 ~]#
8.查看B机器日志变化
[root@localhost ~]# tail /var/log/messages
Apr 8 14:14:10 localhost Keepalived_vrrp[2784]: Sending gratuitous ARP on eno16777736 for 192.168.23.100
Apr 8 14:14:15 localhost Keepalived_vrrp[2784]: Sending gratuitous ARP on eno16777736 for 192.168.23.100
Apr 8 14:14:15 localhost Keepalived_vrrp[2784]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eno16777736 for 192.168.23.100
Apr 8 14:14:15 localhost Keepalived_vrrp[2784]: Sending gratuitous ARP on eno16777736 for 192.168.23.100
Apr 8 14:14:15 localhost Keepalived_vrrp[2784]: Sending gratuitous ARP on eno16777736 for 192.168.23.100
Apr 8 14:14:15 localhost Keepalived_vrrp[2784]: Sending gratuitous ARP on eno16777736 for 192.168.23.100
Apr 8 14:14:15 localhost Keepalived_vrrp[2784]: Sending gratuitous ARP on eno16777736 for 192.168.23.100
Apr 8 14:20:31 localhost Keepalived_vrrp[2784]: VRRP_Instance(VI_1) Received advert with higher priority 100, ours 90
Apr 8 14:20:31 localhost Keepalived_vrrp[2784]: VRRP_Instance(VI_1) Entering BACKUP STATE
Apr 8 14:20:31 localhost Keepalived_vrrp[2784]: VRRP_Instance(VI_1) removing protocol VIPs.
[root@localhost ~]#
##总结
在生产环境中,可能会用到2-3台backup角色, vim /etc/keepalived/keepalived.conf 这里面的权重调成不通级别,权重越高优先级越高!除了nginx服务的话,还可以做MySQL的高可用集群服务。(做mysql的高可用,一定要保证两边的数据一致)
友情链接:阿铭linux