day15.3
目录
nginx负载均衡高可用
- # keepalived概述
- Keepalived是一个高可用软件,可以和任何应用配合使用
复制代码- # 什么是高可用
- 一般是指2台机器启动着完全相同的业务系统,当有一台机器down机了,另外一台服务器就能快速的接管,对于访问的用户是无感知的。
复制代码- # 高可用软件
- 1.硬件 F5
- 2.软件 keepalived heartbeat
- 3.Mysql MGR MHA
- 4.redis Redis-Cluster Sentinel
复制代码 keepailved实现原理
- # keepalived底层协议:VRRP(虚拟路由冗余协议)
复制代码
keepailved核心概念
- 1.通过选举投票,决定谁是主节点谁是备节点(选举)
- 2.如果Master故障,Backup自动接管,那么Master恢复后会夺权吗(抢占试、非抢占式)
- 3.两台服务器都认为自己是master,那么会出现一个故障(脑裂)
复制代码 keepailved安装配置
环境准备
主机名waniplanip角色应用lb0110.0.0.5172.16.1.5Master keepalived主节点keepalivedlb0210.0.0.6172.16.1.6Backup keepalived备节点keepalived部署keepalive
- # 1.安装Keepalived
- [root@lb01 ~]# yum -install -y keepalived
- # 2.查找Keepalived配置文件
- [root@lb01 ~]# rpm -ql keepalived
- /etc/keepalived/keepalived.conf
- # 3.修改Keepalived配置文件
- [root@lb01 conf.d]# vim /etc/keepalived/keepalived.conf
- global_defs { #全局配置
- router_id lb01 #标识身份->名称
- }
- vrrp_instance VI_1 {
- state MASTER #标识角色状态
- interface eth0 #网卡绑定接口
- virtual_router_id 50 #虚拟路由id
- priority 150 #优先级
- advert_int 1 #监测间隔时间
- authentication { #认证
- auth_type PASS #认证方式
- auth_pass 1111 #认证密码
- }
- virtual_ipaddress {
- 10.0.0.3 #虚拟的VIP地址
- }
- }
- # 4.修改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.3
- }
- }
复制代码 Keepalived配置区别master节点配置backup节点配置router_idlb01lb02statemasterbackuppriority150100- # 1.启动master上的Keepalived
- [root@lb01 ~]# systemctl start keepalived
- [root@lb01 ~]# systemctl enable keepalived
- 注意:只要停止掉Keepalived,VIP会漂移到另一个节点
复制代码 非抢占式配置
- # 配置需求
- 1、两个节点的state都必须配置为BACKUP
- 2、两个节点都必须加上配置 nopreempt
- 3、其中一个节点的优先级必须要高于另外一个节点的优先级
- # mast而节点配置
- [root@lb01 ~]# !vim
- vim /etc/keepalived/keepalived.conf
- global_defs { #全局配置
- router_id lb01 #标识身份->名称
- }
- vrrp_instance VI_1 {
- state BACKUP #标识角色状态
- interface eth0 #网卡绑定接口
- nopreempt
- virtual_router_id 50 #虚拟路由id
- priority 150 #优先级
- advert_int 1 #监测间隔时间
- authentication { #认证
- auth_type PASS #认证方式
- auth_pass 1111 #认证密码
- }
- virtual_ipaddress {
- 10.0.0.3 #虚拟的VIP地址
- }
- }
- # backup节点配置
- [root@lb02 ~]# !vim
- vim /etc/keepalived/keepalived.conf
- global_defs {
- router_id lb02
- }
- vrrp_instance VI_1 {
- state BACKUP
- interface eth0
- nopreempt
- virtual_router_id 50
- priority 100
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 10.0.0.3
- }
- }
复制代码 脑裂的原因
- 1、服务器网线松动等网络故障
- 2、服务器硬件故障发生损坏现象而崩溃
- 3、主备都开启rewalld防火墙
- # 解决脑裂故障
- [root@lb02 ~]# vim check_split_brain.sh
- #!/bin/sh
- vip=10.0.0.3
- lb01_ip=10.0.0.5
- while true;do
- ping -c 2 $lb01_ip &>/dev/null
- if [ $? -eq 0 -a `ip add|grep "$vip"|wc -l` -eq 1 ];then
- echo "ha is split brain.warning."
- else
- echo "ha is ok"
- fi
- sleep 5
- done
复制代码 Keepalived结合nginx做高可用
环境准备
主机名waniplanip角色应用lb0110.0.0.5172.16.1.5Master keepalived主节点keepalived、nginxlb0210.0.0.6172.16.1.6Backup keepalived备节点keepalived、nginxweb0110.0.0.7172.16.1.7web网站nginx、phpweb0210.0.0.8172.16.1.8web网站nginx、php关联nginx
- # 公司使用脚本
- [root@lb01 ~]# vim check_web.sh
- #!/bin/sh
- nginx_count=$(ps -ef|grep [n]ginx|wc -l)
- #1.判断Nginx是否存活,如果不存活则尝试启动Nginx
- if [ $nginx_count -eq 0 ];then
- systemctl start nginx
- sleep 3
- #2.等待3秒后再次获取一次Nginx状态
- nginx_count=$(ps -ef|grep [n]ginx|wc -l)
- #3.再次进行判断, 如Nginx还不存活则停止Keepalived,让地址进行漂移,并退出脚本
- if [ $nginx_count -eq 0 ];then
- systemctl stop keepalived
- fi
- fi
- # 上课脚本
- [root@lb01 ~]# vim check_web.sh
- nginx_count=$(ps -ef|grep [n]ginx|wc -l)
- if [ $nginx_count -eq 0 ];then
- systemctl stop keepalived
- fi
- # 关闭lb01上的nginx
- [root@lb01 ~]# systemctl stop nginx
- # 查看lb02
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |