aolishuai 2019-12-24
[ conf.d]# cat s.yangdan.com.conf server { listen 80; server_name s.yangdan.com; root /data; set $ip 0; if ($remote_addr ~* "10.0.0.1|10.0.0.100"){ set $ip 1; } if ($ip = "0"){ rewrite ^(.*)/$ /wh.png break; } location / { index index.html; } }
两台机器启动着相同的业务系统,当有一台机器宕机,另外一台服务器能快速的接管,对于访问的用户是无感知的。 减少系统不能提供服务的时间。
软件: keepalived
通过vrrp协议实现, 虚拟路由冗余协议.
https://blog.51cto.com/13108127/2159619 https://www.cnblogs.com/mengfanrong/p/3911181.html
https://www.cnblogs.com/cash-su/p/9964380.html http://image.baidu.com/search/index?tn=baiduimage&ct=201326592&lm=-1&cl=2&ie=gb18030&word=keepalived%B8%DF%BF%C9%D3%C3%CA%B9%D3%C3%B3%A1%BE%B0%3F&fr=ala&ala=1&alatpl=adress&pos=0&hs=2&xthttps=000000
https://www.jianshu.com/p/5dcd6d63c45d
lb01 -----> 10.0.0.5 lb02 -----> 10.0.0.6 #1.安装 yum install keepalived -y #2.配置 ( master backup ) [ ~]# cat /etc/keepalived/keepalived.conf global_defs { router_id lb01 } 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 } } ------------------------------------------- [ ~]# cat /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.3 } } #3.启动 [ ~]# systemctl start keepalived [ ~]# systemctl enable keepalived #4.验证 [ ~]# ip addr|grep 10.0.0.3
1.抓包看看 1.10.0.0.3 发送 组播数据包---> 2.抓包查看arp缓存表 --->
1.master故障--->backup顶上--->master恢复--->backup 抢占式 默认 2.master故障--->backup顶上--->master恢复--->backup继续工作 非抢占式 1、两个节点的state都必须配置为BACKUP(官方建议) 2、两个节点都在vrrp_instance中添加nopreempt参数 3、其中一个节点的优先级必须要高于另外一个节点的优先级。 两台服务器都角色状态启用nopreempt后,必须修改角色状态统一为BACKUP,唯一的区分就是优先级。 1.抢占: 硬件配置不一 2.非抢占: 硬件配置一致,业务不允许多次切换
地址漂移实现高可用 nginx和keeplaived没有关系? nginx需要借助keeplaived VIP 地址漂移 实现 高可用.
1.判断nginx进程是否存在 ps aux|grep nginx|grep -v grep 2.判断nginx的端口是否存在 netstat -lntp|grep :80|wc -l 3.通过curl来模拟访问,判断访问结果是否ok curl -H Host:url.oldxu.com http://10.0.0.3 #1.编写脚本 [ ~]# mkdir /scripts [ ~]# vim /scripts/check_web.sh #!/usr/bin/bash nginx_port=$(netstat -lntp|grep :80|wc -l) if [ $nginx_port -ne 1 ];then systemctl start nginx &>/dev/null rc=$? sleep 3 if [ $rc -ne 0 ];then systemctl stop keepalived fi fi [ ~]# chmod +x /scripts/check_web.sh #2.keeplaived调用该脚本 [ ~]# cat /etc/keepalived/keepalived.conf global_defs { router_id lb01 } 定义脚本名称,以及脚本所在的路径 vrrp_script check_web { script "/scripts/check_web.sh" interval 5 } vrrp_instance VI_1 { state MASTER #nopreempt interface eth0 virtual_router_id 50 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.0.0.3 } #调用脚本名称 track_script { check_web } } #3.模拟nginx停止,检查nginx是否会被拉起 #4.模拟nginx故障,检查keeplaived的VIP是否会漂移至备节点
脑裂(split-brain),指在一个高可用(HA)系统中,当联系着的两个节点断开联系时,本来为一个整体的系统,分裂为两个独立节点,这时两个节点开始争抢共享资源,结果会导致系统混乱,数据损坏。 对于无状态服务的HA,无所谓脑裂不脑裂; 但对有状态服务(比如MySQL)的HA,必须要严格防止脑裂。 (但有些生产环境下的系统按照无状态服务HA的那一套去配置有状态服务,结果可想而知...) master 10.0.0.3 backup 10.0.0.3 #1.在备上编写检测脚本, 测试如果能ping通主并且备节点还有VIP的话则认为产生了脑列 [ conf.d]# cat /scripts/check_spilt.sh vip=10.0.0.3 master_ip=10.0.0.5 ping -c2 $master_ip &>/dev/null if [ $? -eq 0 ];then ip_check=$(ip addr | grep "$vip" | wc -l) if [ $ip_check -eq 1 ];then echo "脑列" systemctl stop keepalived fi fi ---------------------------------------------------------------> [ conf.d]# cat /etc/keepalived/keepalived.conf global_defs { router_id lb02 } vrrp_script check_spilt { script "/scripts/check_spilt.sh" interval 3 } vrrp_instance VI_1 { state BACKUP nopreempt interface eth0 virtual_router_id 50 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.0.0.3 } track_script { check_spilt } } keeplaived使用: 1.不能在公有云上使用 2.公有云要想实现负载均衡高可用-----> 购买的SLB 自带高可用 3.虚拟IP咋使--->真实的硬件环境: