Keepalived(二)

news2024/11/26 13:25:53

实验一

Keepalived+Lvs+Nginx 实现高可用集群,保证nginx服务正常运行

系统centos7

IP主机名作用
192.168.28.141/24klmaster配置Keepalived(Master)、配置LVS,作为负载均衡器
192.168.28.143/24klbackup配置Keepalived(Backup)、配置LVS,作为负载均衡器
192.168.28.144/24nginx1配置nginx,作为RS web服务器测试
192.168.28.145/24nginx2配置nginx,作为RS web服务器测试

klmaster配置

通过Keepalived自动配置lvs,无需单独配置lvs。

[root@klmaster ~]# yum -y install keepalived ipvsadm
[root@klmaster ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id Directory01    #标识Keepalived,作为Backup的Keepalived此处应不同
   vrrp_skip_check_adv_addr
#  vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_instance VI_1 {
    state MASTER    #定义Keepalived角色,并非决定因素,起决定作用的是priority优先级
    interface ens33    #检测的网络接口
    virtual_router_id 66    #虚拟路由器标识,主备Keepalived一致
    priority 100    #优先级
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {    #设置VIP
        192.168.28.150 dev ens33 label ens33:0
    }
}

virtual_server 192.168.28.150 80 {    #配置lvs的VIP和端口
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 192.168.28.144 80 {    #作为RS的nginx1的真实IP
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
        }
    
    real_server 192.168.28.145 80 {    #作为RS的nginx2的真实IP
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
        }
    }

klbackup配置

[root@klbackup ~]# yum -y install keepalived ipvsadm
[root@klbackup ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id Directory02
   vrrp_skip_check_adv_addr
#  vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 66
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.28.150 dev ens33 label ens33:0
    }
}

virtual_server 192.168.28.150 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 192.168.28.144 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
        }
    
    real_server 192.168.28.145 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
        }
    }

nginx1配置

[root@nginx1 ~]# yum -y install nginx
[root@nginx1 ~]# vim /usr/share/nginx/html/index.html
This is nginx1 page!
[root@nginx1 ~]# systemctl enable --now nginx
[root@nginx1 ~]# cp /etc/sysconfig/network-scripts/ifcfg-lo /etc/sysconfig/network-scripts/ifcfg-lo:0
[root@nginx1 ~]# vim /etc/sysconfig/network-scripts/ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.28.150
NETMASK=255.255.255.255
ONBOOT=yes
NAME=loopback
[root@nginx1 ~]# systemctl restart network
[root@nginx1 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.28.144  netmask 255.255.255.0  broadcast 192.168.28.255
        inet6 fe80::20c:29ff:febf:6bb8  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:bf:6b:b8  txqueuelen 1000  (Ethernet)
        RX packets 609  bytes 56028 (54.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 880  bytes 236014 (230.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 36  bytes 3060 (2.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 36  bytes 3060 (2.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo:0: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 192.168.28.150  netmask 255.255.255.255
        loop  txqueuelen 1000  (Local Loopback)

[root@nginx1 ~]# vim /etc/sysctl.conf
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_announce = 2
[root@nginx1 ~]# systemctl -p

nginx2配置

[root@nginx2 ~]# yum -y install nginx
[root@nginx2 ~]# vim /usr/share/nginx/html/index.html
This is nginx2 page!
[root@nginx2 ~]# systemctl enable --now nginx
[root@nginx2 ~]# cp /etc/sysconfig/network-scripts/ifcfg-lo /etc/sysconfig/network-scripts/ifcfg-lo:0
[root@nginx2 ~]# vim /etc/sysconfig/network-scripts/ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.28.150
NETMASK=255.255.255.255
ONBOOT=yes
NAME=loopback
[root@nginx2 ~]# systemctl restart network
[root@nginx2 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.28.144  netmask 255.255.255.0  broadcast 192.168.28.255
        inet6 fe80::20c:29ff:febf:6bb8  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:bf:6b:b8  txqueuelen 1000  (Ethernet)
        RX packets 609  bytes 56028 (54.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 880  bytes 236014 (230.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 36  bytes 3060 (2.9 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 36  bytes 3060 (2.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo:0: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 192.168.28.150  netmask 255.255.255.255
        loop  txqueuelen 1000  (Local Loopback)

[root@nginx2 ~]# vim /etc/sysctl.conf
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_announce = 2
[root@nginx2 ~]# systemctl -p

查看各项配置

启动Keepalived

[root@klmaster ~]# systemctl enable --now keepalived
[root@klbackup ~]# systemctl enable --now keepalived

查看lvs规则

[root@klmaster ~]# ipvsadm -Ln
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.28.150:80 rr persistent 50
  -> 192.168.28.144:80            Route   1      0          0
  -> 192.168.28.145:80            Route   1      0          0


[root@klbackup ~]# ipvsadm -Ln
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.28.150:80 rr persistent 50
  -> 192.168.28.144:80            Route   1      0          0
  -> 192.168.28.145:80            Route   1      0          0

分别在主备Keepalived主机上抓包查看组播信息

此时主备Keepalived都正常运行,但klmaster优先级priority高,klmaster的Keepalived在提供服务

[root@klmaster ~]# tcpdump -i ens33 -nn | grep 224.0.0.18
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
20:24:47.755711 IP 192.168.28.141 > 224.0.0.18: VRRPv2, Advertisement, vrid 66, prio 100, authtype simple, intvl 1s, length 20
20:24:48.757495 IP 192.168.28.141 > 224.0.0.18: VRRPv2, Advertisement, vrid 66, prio 100, authtype simple, intvl 1s, length 20
20:24:49.758650 IP 192.168.28.141 > 224.0.0.18: VRRPv2, Advertisement, vrid 66, prio 100, authtype simple, intvl 1s, length 20
20:24:50.759840 IP 192.168.28.141 > 224.0.0.18: VRRPv2, Advertisement, vrid 66, prio 100, authtype simple, intvl 1s, length 20
20:24:51.761223 IP 192.168.28.141 > 224.0.0.18: VRRPv2, Advertisement, vrid 66, prio 100, authtype simple, intvl 1s, length 20
20:24:52.762540 IP 192.168.28.141 > 224.0.0.18: VRRPv2, Advertisement, vrid 66, prio 100, authtype simple, intvl 1s, length 20

[root@klbackup ~]# tcpdump -i ens33 -nn | grep 224.0.0.18
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
20:43:35.348367 IP 192.168.28.141 > 224.0.0.18: VRRPv2, Advertisement, vrid 66, prio 100, authtype simple, intvl 1s, length 20
20:43:36.349916 IP 192.168.28.141 > 224.0.0.18: VRRPv2, Advertisement, vrid 66, prio 100, authtype simple, intvl 1s, length 20
20:43:37.351849 IP 192.168.28.141 > 224.0.0.18: VRRPv2, Advertisement, vrid 66, prio 100, authtype simple, intvl 1s, length 20
20:43:38.353368 IP 192.168.28.141 > 224.0.0.18: VRRPv2, Advertisement, vrid 66, prio 100, authtype simple, intvl 1s, length 20
20:43:39.354468 IP 192.168.28.141 > 224.0.0.18: VRRPv2, Advertisement, vrid 66, prio 100, authtype simple, intvl 1s, length 20
20:43:40.355704 IP 192.168.28.141 > 224.0.0.18: VRRPv2, Advertisement, vrid 66, prio 100, authtype simple, intvl 1s, length 20

先关闭Keepalived(Master),等待两三秒再开启Keepalived(Master);此时抓包信息产生变化

[root@klmaster ~]# systemctl stop keepalived

[root@klbackup ~]# tcpdump -i ens33 -nn | grep 224.0.0.18
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
[root@klbackup ~]# tcpdump -i ens33 -nn | grep 224.0.0.18
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes

20:49:39.034550 IP 192.168.28.141 > 224.0.0.18: VRRPv2, Advertisement, vrid 66, prio 100, authtype simple, intvl 1s, length 20
20:49:40.036474 IP 192.168.28.141 > 224.0.0.18: VRRPv2, Advertisement, vrid 66, prio 100, authtype simple, intvl 1s, length 20
20:49:41.038048 IP 192.168.28.141 > 224.0.0.18: VRRPv2, Advertisement, vrid 66, prio 100, authtype simple, intvl 1s, length 20
20:49:41.332438 IP 192.168.28.141 > 224.0.0.18: VRRPv2, Advertisement, vrid 66, prio 0, authtype simple, intvl 1s, length 20
#此时Keepalived切换为backup
20:49:41.981848 IP 192.168.28.143 > 224.0.0.18: VRRPv2, Advertisement, vrid 66, prio 90, authtype simple, intvl 1s, length 20
20:49:42.983526 IP 192.168.28.143 > 224.0.0.18: VRRPv2, Advertisement, vrid 66, prio 90, authtype simple, intvl 1s, length 20
20:49:43.984625 IP 192.168.28.143 > 224.0.0.18: VRRPv2, Advertisement, vrid 66, prio 90, authtype simple, intvl 1s, length 20
20:49:44.985372 IP 192.168.28.143 > 224.0.0.18: VRRPv2, Advertisement, vrid 66, prio 90, authtype simple, intvl 1s, length 20
#此时Keepalived切换为master
20:49:49.988233 IP 192.168.28.141 > 224.0.0.18: VRRPv2, Advertisement, vrid 66, prio 100, authtype simple, intvl 1s, length 20
20:49:50.988865 IP 192.168.28.141 > 224.0.0.18: VRRPv2, Advertisement, vrid 66, prio 100, authtype simple, intvl 1s, length 20
20:49:51.990114 IP 192.168.28.141 > 224.0.0.18: VRRPv2, Advertisement, vrid 66, prio 100, authtype simple, intvl 1s, length 20
20:49:52.991064 IP 192.168.28.141 > 224.0.0.18: VRRPv2, Advertisement, vrid 66, prio 100, authtype simple, intvl 1s, length 20

 测试

前提:Keepalived主备、Lvs都已正常运行,nginx1/2都正常运行

访问VIP 192.168.28.150

关闭Keepalived(Master),再访问VIP,业务正常,抓包发现Keepalived(Backup)已启用

关闭nginx2,再访问VIP

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/940941.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

数据分析 | 特征重要性分析 | 树模型、SHAP值法

前言 在分析特征重要性的时候&#xff0c;相关性分析和主成分分析往往是比较简单的方法&#xff0c;相关性分析是通过计算特征与目标变量之间的相关系数来评估特征的重要性。它可以告诉我们特征和目标变量之间的线性关系程度&#xff0c;但对于非线性关系就无能为力了&#xff…

微前端:重塑大型项目的前沿技术

引言 随着互联网技术的飞速发展&#xff0c;前端开发已经从简单的页面制作逐渐转变为复杂的应用开发。在这个过程中&#xff0c;传统的前端开发模式已经难以满足大型项目的需求。微前端作为一种新的前端架构模式&#xff0c;应运而生&#xff0c;它旨在解决大型项目中的前端开…

C语言基础语法——数据类型

数据类型介绍 数据类型&#xff1a;数据所属的类型 数据类型的作用&#xff1a;编译器预算数据分配的内存空间大小 变量 变量的语法 在计算机程序中&#xff0c;变量是用来存储数据的一个内存区域&#xff0c;并用一个名字来表示这个区域。 在程序运行过程中&#xff0…

Android事件分发

Android事件分发是指触摸屏幕的事件分发&#xff0c;在手指触摸屏幕后所产生的一系列事件中&#xff0c;典型的事件类型有如下几种: MotionEvent.ACTION_DOWN ——手指刚接触屏幕MotionEvent.ACTION_MOVE——手指在屏幕上面滑动MotionEvent.ACTION_UP——手指从屏幕上松开的一…

C语言每日一题 ----计算日期到天数转换(Day 2)

本专栏为c语言练习专栏&#xff0c;适合刚刚学完c语言的初学者。本专栏每天会不定时更新&#xff0c;通过每天练习&#xff0c;进一步对c语言的重难点知识进行更深入的学习。 &#x1f493;博主csdn个人主页&#xff1a;小小unicorn ⏩专栏分类&#xff1a;C语言天天练 &#x…

m3u8 blob视频免费下载

F12点开找到这个视频url最后是.m3u8结尾 http://blog.luckly-mjw.cn/tool-show/m3u8-downloader/index.html 在上边的网址转Mp4下载即可

HBase集群环境搭建与测试

&#x1f947;&#x1f947;【大数据学习记录篇】-持续更新中~&#x1f947;&#x1f947; 个人主页&#xff1a;beixi 本文章收录于专栏&#xff08;点击传送&#xff09;&#xff1a;【大数据学习】 &#x1f493;&#x1f493;持续更新中&#xff0c;感谢各位前辈朋友们支持…

【第四阶段】kotlin语言的List创建与元素获取

1.list创建 val list listOf("jave","kotlin","c","c")2.list集合获取的三种方式 开发过程中尽量使用getOrElse()或者getOrNull&#xff0c;因为他可以防止崩溃取值 package Stage4fun main() {//list 创建val list listOf("jav…

m4s格式转换mp4

先安装 ffmpeg&#xff0c;具体从官网可以查到&#xff0c;https://ffmpeg.org&#xff0c;按流程走。 转换代码如下&#xff0c;可以任意选择格式导出 import subprocess import osdef merge_audio_video(input_audio_path, input_video_path, output_mp4_path):# 构建 FFmpe…

ViT论文Pytorch代码解读

ViT论文代码实现 论文地址&#xff1a;https://arxiv.org/abs/2010.11929 Pytorch代码地址&#xff1a;https://github.com/lucidrains/vit-pytorch ViT结构图 调用代码 import torch from vit_pytorch import ViTdef test():v ViT(image_size 256, patch_size 32, num_cl…

【第四阶段】kotlin语言可变list集合

1.可变list集合 完整写法 var list:MutableList<String> mutableListOf<String>("java","kotlin","c","c") 省略写法 var list mutableListOf("java","kotlin","c","c")fun ma…

CobaltStrike提权

攻击机&#xff1a;Kali Linux 靶 机&#xff1a;Windows 7 一、上线CS 复制命令&#xff0c;在靶机执行上线CS 2.安装插件&#xff0c;获取shell https://github.com/rsmudge/ElevateKit 上线CS 右击shell&#xff0c;选择插件 有七个模块可以利用&#xff0c;可以逐一…

C++实现YOLOP

C实现YOLOP 一、简介 使用OpenCV部署全景驾驶感知网络YOLOP&#xff0c;可同时处理交通目标检测、可驾驶区域分割、车道线检测&#xff0c;三项视觉感知任务&#xff0c;依然是包含C和Python两种版本的程序实现 onnx文件从百度云盘下载&#xff0c;链接&#xff1a;https://…

【AutoLayout案例07-如何通过代码添加约束 Objective-C语言】

一、那么,接下来,我们就给大家介绍一下,如何通过代码,来实现这个AutoLayout 1.咱们之前是不是都是通过,storyboard、来拖、拉、拽、的方式实现的吧 现在给大家介绍一下,如何通过代码,来实现 在继续介绍,如何通过代码,来实现AutoLayout之前呢, 我们先要给大家补充一…

基于Java+SpringBoot+Vue前后端分离疫苗发布和接种预约系统设计和实现

博主介绍&#xff1a;✌全网粉丝30W,csdn特邀作者、博客专家、CSDN新星计划导师、Java领域优质创作者,博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和毕业项目实战✌ &#x1f345;文末获取源码联系&#x1f345; &#x1f447;&#x1f3fb; 精彩专…

前端开发之Element Plus的分页组件el-pagination显示英文转变为中文

前言 在使用element的时候分页提示语句是中文的到了element-plus中式英文的&#xff0c;本文讲解的就是怎样将英文转变为中文 效果图 解决方案 如果你的element-plus版本为2.2.29以下的 import { createApp } from vue import App from ./App.vue import ElementPlus from …

ruoyi-vue-plus 配置邮箱

ruoyi-vue-plus 配置邮箱 &#x1f4d4; 千寻简笔记介绍 千寻简笔记已开源&#xff0c;Gitee与GitHub搜索chihiro-notes&#xff0c;包含笔记源文件.md&#xff0c;以及PDF版本方便阅读&#xff0c;且是用了精美主题&#xff0c;阅读体验更佳&#xff0c;如果文章对你有帮助请…

Java实现根据短连接获取1688商品详情数据,1688淘口令接口,1688API接口封装方法

要通过1688的API获取商品详情数据&#xff0c;您可以使用1688开放平台提供的接口来实现。以下是一种使用Java编程语言实现的示例&#xff0c;展示如何通过1688开放平台API获取商品详情属性数据接口&#xff1a; 首先&#xff0c;确保您已注册成为1688开放平台的开发者&#xf…

网络工程师的尽头是……

大家好&#xff0c;我是许公子。 最近工作挺忙&#xff0c;很久没有给你们输出文章了&#xff0c;抽空和大家唠嗑唠嗑。 前两天&#xff0c;一个实习生问了我这个问题&#xff1a; “网络工程师的尽头是什么&#xff1f;” 我当时一下子&#xff0c;脑子空白了&#xff0c;…

stackoverflow问题

Stack Overflow requires external JavaScript from another domain, which is blocked or failed to load. stackoverflow引用了谷歌中被屏ajax.googleapis.com的jquery.min.js文件。“https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js” 方案1.打开网站…