在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
第一章:keepalived介绍 VRRP协议 第二章: keepalived工作原理 2.1 作为系统网络服务的高可用功能(failover) keepalived高可用功能实现的基本原理为: 当角色为Master的主机失效或出现故障时 而当角色为Master的主机故障修复后,又会自动接管回他原来处理的工作 2.2 什么是VRRP VRRP通过竞选机制来实现虚拟路由器的功能,所有的协议报文都是通过IP多播(Multicast)包(默认的多播地址224.0.0.18)形式发送的 在一组虚拟路由器中,只有作为Master的VRRP路由器会一直发送VRRP广播包,此时Backup不会抢占Master 2.3 面试的时候怎么说 第三章: VRRP协议 VIP前提条件: 第四章:keepalived安装配置 1.安装keepalived yum install keepalived -y 2.配置文件解释 global_defs { router_id lb01 #设置路由ID,每个主机不一样 } vrrp_instance VI_1 { #设置VRRP组名,同一组组名相同 state MASTER #设置角色状态,分为MASTER BACKUP interface eth0 #VIP绑定的网卡 virtual_router_id 50 #虚拟路由id,同一组一样 priority 150 #权重,权重越高,优先级越高 advert_int 1 #发送组播间隔 authentication { #设置验证,密码为明文 auth_type PASS auth_pass 1111 } virtual_ipaddress { #设定的虚拟IP,这个虚拟IP必须是存在且合法且没有被使用的。 10.0.0.3 } } 3.lb01配置 [root@lb01 ~]# 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 } } 4.lb02配置 [root@lb02 ~]# 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 } } 5.启动 systemctl start keepalived 6.测试 第五章:脑裂现象 1.安装抓包工具 yum install tcpdump -y 2.lb02抓包查看 tcpdump -nn -i any host 224.0.0.18 3.lb02新开一个终端,然后开启防火墙 systemctl start firewalld.service 4.lb02观察抓包现象 5.添加放行规则 firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --in-interface eth0 --destination 224.0.0.18 --protocol vrrp -j ACCEPT firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --in-interface eth1 --destination 224.0.0.18 --protocol vrrp -j ACCEPT systemctl reload firewalld 6.lb02观察抓包现象 第六章:keepalived双主实验 [root@lb01 ~]# 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 } } vrrp_instance VI_2 { state BACKUP interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 2222 } virtual_ipaddress { 10.0.0.4 } } 2.lb02配置文件 [root@lb02 ~]# 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 } } vrrp_instance VI_2 { state MASTER interface eth0 virtual_router_id 51 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 2222 } virtual_ipaddress { 10.0.0.4 } } 3.重启keepalived并观察现象 systemctl restart keepalived 第七章:keepalived结合nginx反向代理负载均衡 1.备份原有配置 mkdir /backup cd /etc/nginx/conf.d mv * /backup 2.编写Nginx配置文件 [root@lb01 /etc/nginx/conf.d]# cat proxy.conf upstream web_pools { server 172.16.1.7; server 172.16.1.8; } server { listen 80; server_name (www|bbs).mysun.com ; location / { proxy_pass http://web_pools; include proxy_params; } } 3.测试并重启nginx nginx -t systemctl restart nginx lb服务器的keepalived配置: 1.lb01的keepalived配置 [root@lb01 ~]# 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 } } 2.lb02的keepalived配置 [root@lb02 ~]# 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 } } web服务器配置: 注意!两台web服务器配置一模一样 1.nginx配置 [root@web01 ~]# cat /etc/nginx/conf.d/www.conf server { listen 80; server_name www.mysun.com; location / { root /code; index www.html; } } 2.写入测试文件 echo "$(hostname)" >/code/index.html 第八章: 防裂脑脚本 1.问题现象: 2.思路:
2.keepalived定时去调用这个脚本 3.实现: 1.命令如何实现 systemctl start nginx 2.检查nginx进程 [root@lb01 ~]# ps -ef|grep nginx|grep -v "grep" root 1210 1 0 11:21 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf nginx 1211 1210 0 11:21 ? 00:00:00 nginx: worker process [root@lb01 ~]# ps -ef|grep nginx|grep -v "grep"|wc -l 2 [root@lb01 ~]# ps -ef|grep nginx|grep -v "grep"|wc -l 0 脚本内容: [root@lb01 ~]# cat check_web.sh !/bin/bash nginx_status=$(ps -C nginx --no-header|wc -l) if [[ ${nginx_status} == 0 ]] then systemctl start nginx &> /dev/null sleep 1 nginx_status=$(ps -C nginx --no-header|wc -l) if [[ ${nginx_status} == 0 ]] then systemctl stop keepalived fi fi keepalived调用脚本: [root@lb01 ~]# cat /etc/keepalived/keepalived.conf 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 } track_script { check_web } } 4.第二个问题:脑裂问题 现象: 对面的MASTER的Nginx还活着 curl -I -s -w "%{http_code}\n" -o /dev/null 10.0.0.5 但是我又有了VIP ip a |grep "10.0.0.3"|wc -l 我就把自己干掉 systemctl stop nginx systemctl stop keepalived 脚本内容: [root@lb02 /server/scripts]# cat check_vip.sh #!/bin/bash master_status=$(curl -I -s -w "%{http_code}\n" -o /dev/null 10.0.0.5) my_vip=$(ip a |grep "10.0.0.3"|wc -l) if [ ${master_status} == 200 -a ${my_vip} == 1 ] then systemctl stop nginx systemctl stop keepalived fi keepalived配置: [root@lb02 ~]# cat /etc/keepalived/keepalived.conf global_defs { router_id lb02 } vrrp_script check_web { script "/server/scripts/check_web.sh" interval 5 weight 50 } vrrp_script check_vip { script "/server/scripts/check_vip.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 } track_script { check_web check_vip } } 到此这篇关于Keepalived实现Nginx负载均衡高可用的示例代码的文章就介绍到这了,更多相关Keepalived Nginx负载均衡高可用内容请搜索极客世界以前的文章或继续浏览下面的相关文章希望大家以后多多支持极客世界! |
请发表评论