Keepalived实验

一、 LVS+Keepalived

实验:7-1为主; 7-2为备; 7-3和7-4为后端服务器

1.关闭防火墙和selinux

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0

2.配置主设备7-1

1.安装ipvsadm和keepalived

[root@localhost ~]# yum install ipvsadm.x86_64 keepalived.x86_64 -y

2.修改keepalived的配置

[root@localhost ~]# cd /etc/keepalived
[root@localhost keepalived]# ls
keepalived.conf
[root@localhost keepalived]# cp keepalived.conf keepalived.conf.bak
[root@localhost keepalived]# ls
keepalived.conf  keepalived.conf.bak
[root@localhost keepalived]# 

[root@localhost keepalived]# vim keepalived.conf

10    smtp_server 127.0.0.1
12    router_id LVS_01
21     interface ens33
27         auth_pass 123123

把14行注释掉 

参数解析

30         192.168.91.188
34         virtual_server 192.168.91.188 80 {
37         lb_kind DR
38         persistence_timeout 0

41         real_server 192.168.91.103 80 {
43             TCP_CHECK {
44             connect_port  80

 50     real_server 192.168.91.104 80 {
 51         weight 1
 52             TCP_CHECK {
 53             connect_port  80
 54             connect_timeout 3
 55             nb_get_retry 3
 56             delay_before_retry 3
 57         }   
 58     }   

参数解析

3.开启ipvsadm

[root@localhost keepalived]# ipvsadm-save > /etc/sysconfig/ipvsadm
[root@localhost keepalived]# systemctl start ipvsadm

[root@localhost keepalived]# systemctl restart keepalived.service 
[root@localhost keepalived]# systemctl restart ipvsadm.service 
[root@localhost keepalived]# ipvsadm -ln

3.配置7-3Web服务器

1.安装httpd并开启

[root@localhost ~]# yum install httpd -y
[root@localhost ~]# systemctl start httpd

[root@localhost ~]# cd /var/www/html
[root@localhost html]# echo 7-3 > index.html
[root@localhost html]# systemctl restart httpd

4.配置7-4Web服务器

1.安装httpd并开启

[root@localhost ~]# yum install httpd -y
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# cd /var/www/html
[root@localhost html]# echo 7-4 > index.html
[root@localhost html]# systemctl restart httpd

 5.给7-3和7-4做虚拟网卡

[root@localhost html]# ifconfig lo:0 192.168.91.188/32
[root@localhost html]# 
[root@localhost html]# 
[root@localhost html]# ip a

给7-3和7-4添加ARP规则

vim /etc/sysctl.conf

net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2

[root@localhost html]# sysctl -p

[root@localhost html]# systemctl restart httpd
[root@localhost html]# 

6.用7-1传内容

[root@localhost keepalived]# scp keepalived.conf 192.168.91.102:/data

7.配置7-2的keepalived

1.安装keepalived和ipvsadm

[root@localhost ~]# yum install ipvsadm.x86_64 keepalived.x86_64 -y

2.复制文件

[root@localhost ~]# cd /etc/keepalived/
[root@localhost keepalived]# ls
keepalived.conf
[root@localhost keepalived]# cp keepalived.conf keepalived.conf.bak
[root@localhost keepalived]# 
[root@localhost keepalived]# mv /data/keepalived.conf  .

3.改配置文件

12    router_id LVS_02
20     state BACKUP
23     priority 80
vim keepalived.conf

[root@localhost keepalived]# systemctl start keepalived.service 
[root@localhost keepalived]# ipvsadm-save > /etc/sysconfig/ipvsadm
[root@localhost keepalived]# 
[root@localhost keepalived]# systemctl start ipvsadm
[root@localhost keepalived]# 
[root@localhost keepalived]# ipvsadm -ln

8.把7-3和7-4的长连接关掉

[root@localhost html]# vim /etc/httpd/conf/httpd.conf

354 keepalive off

[root@localhost html]# systemctl restart httpd

9.去浏览器访问虚拟IP

7-3和7-4都可以成功访问

二、模式实验

抢占模式、非抢占模式、延迟抢占模式

默认是抢占模式;所以不需要弄

1.抢占模式

1.当主设备7-1keepalived开启时

7-1

7-2

虚拟IP192.168.91.188在主设置7-1上

2.当从设备7-2keepalived关闭时

7-1

7-2

虚拟IP到了从设备7-2上

2.非抢占模式

1.修改7-1的keepalived

vim keepalived.conf

20     state BACKUP
21     nopreempt

 

[root@localhost keepalived]# vim keepalived.conf
[root@localhost keepalived]# 
[root@localhost keepalived]# systemctl restart keepalived.service 

 ip  a###看一下


3.延迟抢占模式

1.修改7-1配置

[root@localhost keepalived]# vim keepalived.conf
[root@localhost keepalived]# 
[root@localhost keepalived]# systemctl restart keepalived.service 

2.修改7-2配置

[root@localhost keepalived]# vim keepalived.conf
[root@localhost keepalived]# 
[root@localhost keepalived]# systemctl restart keepalived.service 

3.去7-1看结果

[root@localhost keepalived]# systemctl stop keepalived.service 
[root@localhost keepalived]# 
[root@localhost keepalived]# hostname -I
192.168.91.100 192.168.122.1 
[root@localhost keepalived]# 

4.去7-2看结果

[root@localhost keepalived]# hostname -I
192.168.91.102 192.168.91.188 192.168.122.1 
[root@localhost keepalived]# 

5.去7-1看延迟抢占的结果

[root@localhost keepalived]# systemctl start keepalived.service 
[root@localhost keepalived]# hostname -I
192.168.91.100 192.168.122.1 
[root@localhost keepalived]# hostname -I
192.168.91.100 192.168.122.1 
[root@localhost keepalived]# 
[root@localhost keepalived]# hostname -I
192.168.91.100 192.168.91.188 192.168.122.1 
[root@localhost keepalived]# 

三、多播修改

1.去7-2上抓包

[root@localhost keepalived]# tcpdump -i ens33 -nn src host  192.168.91.100

2.修改7-1的配置文件

vim keepalived.conf

14    vrrp_mcast_group4 234.6.6.6

[root@localhost keepalived]# vim keepalived.conf
[root@localhost keepalived]# 
[root@localhost keepalived]# systemctl restart keepalived.service 

3.修改7-2的配置文件

vim  keepalived.conf

14    vrrp_mcast_group4 234.6.6.6

systemctl restart keepalived.service

4.去7-2上抓包看结果

[root@localhost keepalived]# tcpdump -i ens33 -nn src host  192.168.91.100

四、单播修改

1.修改7-1配置

 vim keepalived.conf

 31     unicast_src_ip 192.168.91.100
 32     unicast_peer { 
 33     192.168.91.102
 34 }

[root@localhost keepalived]# vim keepalived.conf
[root@localhost keepalived]# 
[root@localhost keepalived]# systemctl restart keepalived.service 

2.修改7-2配置

vim  keepalived.conf

 33     unicast_src 192.168.91.102
 34     unicast_peer {
 35     192.168.91.100
 36 }

[root@localhost keepalived]# vim keepalived.conf
[root@localhost keepalived]# 
[root@localhost keepalived]# systemctl restart keepalived.service 

3.去7-2上抓包

[root@localhost keepalived]# tcpdump -i ens33 -nn src host  192.168.91.100 and dst host 192.168.91.102

五、通知脚本

1.修改7-1配置

[root@localhost keepalived]# cd /opt
[root@localhost opt]# ls
rh
[root@localhost opt]# vim keepalived.sh
[root@localhost opt]# 
[root@localhost opt]# mv keepalived.sh keepalive.sh 
[root@localhost opt]# 
[root@localhost opt]# chmod +x keepalive.sh 
[root@localhost opt]# 
[root@localhost opt]# vim /etc/keepalived/keepalived.conf

vim keepalive.sh

#!/bin/bash
#
contact='2305981334@qq.com'
notify() {
 mailsubject="$(hostname) to be $1, vip floating"
 mailbody="$(date +'%F %T'): vrrp transition, $(hostname) changed to be $1"
 echo "$mailbody" | mail -s "$mailsubject" $contact
}
case $1 in
master)
 notify master
 ;;
backup)
 notify backup
 ;;
fault)
 notify fault
 ;;
*)
 echo "Usage: $(basename $0) {master|backup|fault}"
 exit 1
 ;;
esac

[root@localhost opt]# vim /etc/keepalived/keepalived.conf

先把刚刚加的这段删掉

 31     notify_master "/opt/keepalive.sh master"
 32     notify_backup "/opt/keepalive.sh backup"
 33     notify_fault "/opt/keepalive.sh fault"

systemctl restart keepalived.service
###重启

[root@localhost ~]# killall keepalived

2.去QQ邮箱的垃圾箱,看一下

六、日志功能

1.去7-1上去修改

[root@localhost ~]# vim /etc/sysconfig/keepalived 


KEEPALIVED_OPTIONS="-D -S 6"
 74 local6.*                                                /data/keepalive.log
[root@localhost ~]# vim /etc/sysconfig/keepalived 
[root@localhost ~]# 
[root@localhost ~]# vim /etc/rsyslog.conf
[root@localhost ~]# 
[root@localhost ~]# systemctl restart rsyslog.service

[root@localhost ~]# ls /data/
ls: 无法访问/data/: 没有那个文件或目录
[root@localhost ~]# mkdir /data
[root@localhost ~]# ls /data/
[root@localhost ~]# systemctl restart keepalived.service 
[root@localhost ~]# 
[root@localhost ~]# ls /data/
keepalive.log
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# cat keepalive.log
cat: keepalive.log: 没有那个文件或目录
您在 /var/spool/mail/root 中有新邮件
[root@localhost ~]# cat /data/keepalive.log

七、脑裂

1.去7-2模拟脑裂

[root@localhost keepalived]# iptables -A INPUT -s 172.168.91.100 -j REJECT
[root@localhost keepalived]# 
[root@localhost keepalived]# ip a

2.去主设备7-1上看一下

当主设备7-1和从设备7-2上都有虚拟IP;代表脑裂;两台设备都认为自己是主

八、VRRP Script解决Nginx高可用问题

[root@localhost ~]# killall -0 nginx
nginx: no process found
[root@localhost ~]# 
[root@localhost ~]# echo $?
1
[root@localhost ~]# 

1.把7-1和7-2的ipvsadm关掉

[root@localhost ~]# systemctl stop ipvsadm.service

2.安装并开启7-1和7-2的Nginx

yum install epel-release -y

yum install nginx -y

systemctl start nginx

3.去7-1的Nginx主配置文件中做反向代理

[root@localhost ~]# vim /etc/nginx/nginx.conf
[root@localhost ~]# 
[root@localhost ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost ~]# nginx -s reload
[root@localhost ~]# 
[root@localhost ~]# systemctl restart nginx

[root@localhost ~]# vim /etc/nginx/nginx.conf

     upstream  web {
        server 192.168.91.103;
        server 192.168.91.104;
}


      location / {
        proxy_pass  http://web;
}

4.curl看一下

[root@localhost ~]# curl 192.168.91.100
7-3
[root@localhost ~]# curl 192.168.91.100
7-4

 5.7-1直接复制给7-2

[root@localhost ~]# scp /etc/nginx/nginx.conf  192.168.91.102:/etc/nginx/nginx.conf
root@192.168.91.102's password: 
nginx.conf                                                       100% 2448   200.2KB/s   00:00    
[root@localhost ~]# 

6.去7-2curl看一下

[root@localhost keepalived]# systemctl restart nginx
[root@localhost keepalived]# curl 192.168.91.102
7-3
[root@localhost keepalived]# curl 192.168.91.102
7-4

7.去7-1修改keepalive

[root@localhost ~]# vim /etc/keepalived/keepalived.conf

18 vrrp_script check_down {
 19         script "/etc/keepalived/ng.sh"
 20         interval 1
 21         weight -30
 22         fall 1 
 23         rise 2
 24         timeout 2
 25 }


 40     track_script {
 41     check_down
 42 }  

[root@localhost ~]# vim /etc/keepalived/keepalived.conf
[root@localhost ~]# 
[root@localhost ~]# scp /etc/keepalived/keepalived.conf 192.168.91.102:/etc/keepalived/
root@192.168.91.102's password: 
keepalived.conf                                                  100%  888   503.4KB/s   00:00    
[root@localhost ~]# systemctl restart keepalived.service 

8.去7-2

[root@localhost keepalived]# systemctl restart keepalived.service 
[root@localhost keepalived]# 

9.浏览器访问一下

不能直接把主设备7-1关机;关机之后,7-2从设备直接成为主设备;我们就测试不出,刚刚写的脚本的作用

10.把主设备nginx停掉

[root@localhost ~]# systemctl stop nginx
[root@localhost ~]# 

11.7-2看一下结果

[root@localhost keepalived]# ip a

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

阿珊解说Vue中`$route`和`$router`的区别

🤍 前端开发工程师、技术日更博主、已过CET6 🍨 阿珊和她的猫_CSDN博客专家、23年度博客之星前端领域TOP1 🕠 牛客高级专题作者、打造专栏《前端面试必备》 、《2024面试高频手撕题》 🍚 蓝桥云课签约作者、上架课程《Vue.js 和 E…

可视化图表:柱坐标系与对应图表详解

一、柱坐标系及其构成 柱状坐标系是一种常见的可视化图表坐标系,用于显示柱状图(也称为条形图)的数据。它由两个相互垂直的轴组成,一个是水平轴(X轴),另一个是垂直轴(Y轴&#xff0…

评测本地部署的语音识别模型

1 引言 最近,朋友给我发来了一段音频,想转录成文字,并使用大型润色文本。音频中的普通话带有一定的口音,并且讲解内容较为专业,所以一般的语音识别工具很难达到较高的识别率。 于是试用了两个大模型。Whisper 是目前…

AIOps常见问题

AIOps的自动化通常指什么? AIOps 平台的自动化一般包括以下几个方面: 数据收集和整合:AIOps 平台可以从多个 IT 基础架构组件、应用需求与性能监视工具以及服务工单系统等数据源中收集并整合运维数据,形成一个全面的数据平台。数…

Access AR Foundation 5.1 in Unity 2022

如果已经下载安装了ARF但版本是5.0.7 可以通过下面的方式修改 修改后面的数字会自动更新 更新完成后查看版本 官方文档 Access AR Foundation 5.1 in Unity 2021 | AR Foundation | 5.1.2

YOLOv9中train.py与train_dual.py的异同!

专栏介绍:YOLOv9改进系列 | 包含深度学习最新创新,主力高效涨点!!! 首先,train.py(左)与train_dual.py(右)中的损失函数是不一样的,这也解释了为什么使用train.py除了填入…

Visual Studio 2022之Release版本程序发送到其它计算机运行

目录 1、缺少dll​ 2、应用程序无法正常启动 3、This application failed to start because no Qt platform plugin could be initialized. 代码在Debug模式下正常运行,然后切换到Release模式下,也正常运行,把第三方平台的dll拷贝到exe所在…

Chrome浏览器好用的几个扩展程序

Chrome好用的扩展程序 背景目的介绍JsonHandle例子未完待续。。。。。。 背景 偶然在往上看到Chrome有很多好用的扩展程序,比较好用,因此记录下比较实用的扩展程序。 目的 记录Chrome浏览器好用的插件。 介绍 JsonHandle下载以及无法扩展插件的解决…

Vue3_2024_6天【回顾上篇watch常见的前三种场景】另两种待补

第一种情况:监视【ref】定义(基本数据类型) 1.引入watch2.格式:watch(基本数据类型数据,监视变化的回调函数) 注意点: 2.1.watch里面第一个参数,是数据~~【监视的基本类…

基于深度学习的三维重建MVSNet系列

2019年4月15日下午6时50分左右,一场大火席卷了法国巴黎圣母院,持续长达14小时。幸而巴黎圣母院有着高分辨率的3D模型,研究人员可以了解圣母院本身的建造结构,以便修复工程的开展。 多视图立体几何(Multi-View Stereo&a…

unity-urp:视野雾

问题背景 恐怖游戏在黑夜或者某些场景下,需要用雾或者黑暗遮盖视野,搭建游戏氛围 效果 场景中,雾会遮挡场景和怪物,但是在玩家视野内雾会消散,距离玩家越近雾越薄。 当前是第三人称视角,但是可以轻松的…

Linux:kubernetes(k8s)探针LivenessProbe的使用(9)

他做的事情就是当我检测的一个东西他不在规定的时间内存在的话,我就让他重启,这个检测的目标可以是文件或者端口等 我这个是在上一章的基础之上继续操作,我会保留startupProbe探针让后看一下他俩的执行优先的一个效果 Linux:kuber…

【QT】QDialog/ QMessageBox/提示对话框/颜色(文字)------对话框

QDialog—对话框 什么是对话框,如下样式 非模态对话框,即打开以后,我还可以对其他框进行操作。 模态对话框,打开以后,其他框都不能再操作了 模态对话框是阻塞对话框 QDialog dig(this);//显示模态对话框dig.exec();…

ROS2中nav_msgs/msg/Path 数据含义及使用

目录 ROS2中nav_msgs/msg/Path数据含义及使用ROS官方消息说明使用ros2中Path生成路径并显示案例使用ROS2命令创建功能包修改创建功能包中的CMakeLists.txt如下创建发布话题的main函数编译与运行rviz可视化发布的路径 ROS2中nav_msgs/msg/Path数据含义及使用 ROS2官方关于nav_m…

vue面试--9, 1 ObjectProperty与vue3Proxy区别。2 MVVM的理解 3 双向绑定原理?

1 ObjectProperty与vue3Proxy区别 2 MVVM的理解 3 双向绑定原理?

Spring源码:手写AOP

文章目录 一、概念1、AOP是什么?2、相关概念1)目标对象Target2)通知Advice3)连接点Joinpoint4)切点Pointcut5)切面Aspect6)织入Weaving 二、分析三、实现1、实现Advice1)前置通知2&a…

Jmeter高效组织接口自动化用例

1、善用“逻辑控制器”中的“简单控制器”。可以把简单控制器像文件夹一样使用,通过它来对用例进行分类归档,方便后续用例的调试和执行。 2、同编写测试用例一样,这里的接口测试用例应该进行唯一性编号,这样在运行整个用例计划出现…

wince+gprs拨号上网总结

一、硬件连接 本次调试的GPRS模块引脚定义 三星主板全功能扩展串口2引脚定义 因GPRS模块可以和pc机直连进行数据通讯,那么收发肯定内部交叉,故主板和GPRS的连接也采用直连方式。如果接线不对则出现没有回应现象,拨号时出现端口不可用&#xf…

【C++】十大排序算法之 桶排序 基数排序

本次介绍内容参考自:十大经典排序算法(C实现) - fengMisaka - 博客园 (cnblogs.com) 排序算法是《数据结构与算法》中最基本的算法之一。 十种常见排序算法可以分为两大类: 比较类排序:通过比较来决定元素间的相对次序…

吴恩达deeplearning.ai:机器学习项目的完整周期伦理

以下内容有任何不理解可以翻看我之前的博客哦:吴恩达deeplearning.ai专栏 文章目录 语音识别部署公平、偏见、伦理 这节博客中,我们主要看看构建一个机器学习的完整周期是什么,也就是说,当你想构建一个有价值的机器学习系统时&am…