首页 > 其他分享 >keepalived高可用服务搭建——单实例

keepalived高可用服务搭建——单实例

时间:2023-07-03 23:35:39浏览次数:57  
标签:00 keepalived 实例 scope ens33 root inet 搭建

准备四台机器

hostnameIP说明
lb01 10.0.0.30 keepalived主服务器(nginx主负载均衡)
lb02 10.0.0.60 Keepalived备服务器(Nginx辅负载均衡)
www01 10.0.0.40 www01
www02 10.0.0.50 www02

安装keepalived(lb01,lb02)

[root@lb01 ~]# yum install keepalived -y
[root@lb01 ~]# rpm -qa keepalived
keepalived-1.3.5-19.el7.x86_64
[root@lb02 ~]# yum install -y keepalived
[root@lb02 ~]# rpm -qa keepalived
keepalived-1.3.5-19.el7.x86_64

配置主keepalived服务器 lb01-master

1.备份旧配置文件,定义新配置文件
[root@lb01 keepalived]# cp keepalived.conf{,.bak} 
[root@lb01 keepalived]# ls
keepalived.conf  keepalived.conf.bak
2.根据自己机器修改配置
vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
#定义虚拟IP,也就是VIP,提供给用户的高可用地址,绑定网络接口ens33,别名ens33:1,主备节点要相同 virtual_ipaddress { 192.168.17.100/24 dev ens33 label ens33:1 } } [root@lb01 keepalived]# systemctl restart keepalived [root@lb01 keepalived]# ps -ef |grep keepalived root 1299 1 0 10:38 ? 00:00:00 /usr/sbin/keepalived -D root 1300 1299 0 10:38 ? 00:00:00 /usr/sbin/keepalived -D root 1301 1299 0 10:38 ? 00:00:00 /usr/sbin/keepalived -D root 1303 1200 0 10:38 pts/0 00:00:00 grep --color=auto keepalived ##检查是否生成新的VIP地址192.168.17.100 [root@lb01 keepalived]# ip addr | grep inet inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host inet 10.0.0.30/24 brd 10.0.0.255 scope global noprefixroute ens33 inet 192.168.17.100/24 scope global ens33:1 inet6 fe80::c537:24bd:f23f:695e/64 scope link noprefixroute

配置keepalived备服务器lb02-BACKUP

注意lb02和lb01的参数区别

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51  #虚拟路由IP跟主节点一致
    priority 100    #优先级备用节点一般比主节点低50
    advert_int 1    #MASTER和BACKUP之间的通信检查时间,默认是1秒
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.17.100/24 dev ens33 label ens33:1
    }
}
[root@lb02 keepalived]# systemctl restart keepalived
[root@lb02 keepalived]# ps -ef |grep keepalived
root       1285      1  0 10:49 ?        00:00:00 /usr/sbin/keepalived -D
root       1286   1285  0 10:49 ?        00:00:00 /usr/sbin/keepalived -D
root       1287   1285  0 10:49 ?        00:00:00 /usr/sbin/keepalived -D
root       1293   1171  0 10:51 pts/0    00:00:00 grep --color=auto keepalived
[root@lb02 keepalived]# ip addr | grep inet
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
    inet 10.0.0.60/24 brd 10.0.0.255 scope global noprefixroute ens33
    inet6 fe80::8d10:27e0:1c5b:e2a1/64 scope link tentative noprefixroute dadfailed 
    inet6 fe80::b5c6:7506:252b:95e/64 scope link noprefixroute

检查配置结果,是否出现虚拟IP(VIP)

  • 正确的结果应该是没有出现VIP
  • 因为lb02位BACKUP,当主节点正常的时候,它不会接管VIP
  • 若是lb02看到了VIP,表示配置出错了!

如果出现lb01、lb02抢夺VIP资源,排查故障思路

  • 主备服务器是否能够通信,可以ping检测
  • 是否没有正确配置防火墙,或是忘记关闭防火墙
  • 两台服务器的keepalived.conf是否写错(例如虚拟路由ID是否不一致)

高可用主备服务切换实践

停止主服务器的keepalived或者直接关机

[root@lb01 keepalived]# ip addr | grep inet
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
    inet 10.0.0.30/24 brd 10.0.0.255 scope global noprefixroute ens33
    inet 192.168.17.100/24 scope global ens33:1
    inet6 fe80::c537:24bd:f23f:695e/64 scope link noprefixroute 
[root@lb01 keepalived]# systemctl stop keepalived
[root@lb01 keepalived]# ip addr | grep inet
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
    inet 10.0.0.30/24 brd 10.0.0.255 scope global noprefixroute ens33
    inet6 fe80::c537:24bd:f23f:695e/64 scope link noprefixroute

可以看到,关闭keepalived服务后,VIP也消失了 

检查BACKUP机器状态

[root@lb02 keepalived]# ip addr | grep inet
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
    inet 10.0.0.60/24 brd 10.0.0.255 scope global noprefixroute ens33
    inet 192.168.17.100/24 scope global ens33:1
    inet6 fe80::8d10:27e0:1c5b:e2a1/64 scope link tentative noprefixroute dadfailed 
    inet6 fe80::b5c6:7506:252b:95e/64 scope link noprefixroute 

可以看到备节点lb02此时已经接管了VIP,并且这个接管期间,备节点还会主动发送ARP广播,让局域网内所有的客户端更新本地ARP缓存表,以便访问新接管VIP服务的节点。

恢复主节点,查看VIP动态

[root@lb01 keepalived]# systemctl restart keepalived
[root@lb01 keepalived]# ip addr | grep inet
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
    inet 10.0.0.30/24 brd 10.0.0.255 scope global noprefixroute ens33
    inet 192.168.17.100/24 scope global ens33:1
    inet6 fe80::c537:24bd:f23f:695e/64 scope link noprefixroute

 此时会发现,再次启动keepalived主服务,MASTER很快会接管VIP,备服务器正确应该是释放了VIP,此时用户的请求也就再次转移到新机器了。

明确单实例主备模式配置文件区别

区别参数MASTER参数BACKUP参数
router_id lb01 lb02
state MASTER BACKUP
priority 150 100

 

标签:00,keepalived,实例,scope,ens33,root,inet,搭建
From: https://www.cnblogs.com/junwured/p/17524448.html

相关文章

  • day05--23.7.3JDK、JRE、JVM以及开发环境搭建
    JDK、JRE、JVMJDK:JavaDevelopmentKit--JAVA开发者工具--用于程序开发java--编译运行javajavac--编译运行javajavadoc--java生成文档jar--java打包成应用JRE:JavaRuntimeEnvironment--JAVA运行时环境appletlibrariesJVM:JavaVirtualMachine--JAVA虚拟机java开发......
  • 学城项目后端接口搭建2
    1.手机验证码注册接口 /1 短信注册的序列化类classUserRegisterSerializer(serializers.ModelSerializer):code=serializers.CharField(max_length=4,min_length=4,write_only=True)classMeta:model=Userfields=['mobile','password'......
  • 学城项目后端端口搭建2
    1.短信登录接口/1 创建此接口的序列化类1由于短信登录的序列化类和多方式的登录的序列化类中获取token和全局钩子的代码一致所以我们把它们放一起封装为CommonLoginSerializer#全局钩子和获取token的模块封装classCommonLoginSerializer():def......
  • 用 IaC 的方式管理 EC2 实例 - 每天5分钟玩转 GPT 编程系列(1)
    目录写在前面你听我说HelloDevChat第一轮第二轮第三轮成果检验总结一下写在前面咳咳,开个系列吧,就叫《每天5分钟玩转GPT编程》。在这个系列里我们将分享各种借助GPT解决实际工作问题的经验,包括但不限于让GPT辅助写代码。咳咳,敲黑板:本系列文章欢迎大家投稿,分享出你使用G......
  • 网关搭建【黑马头条】
    一、导入依赖<dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency><dependency><group......
  • 搭建SpinalHDL环境心得(Windows)
    主要步骤是基于别人的教程文章。在这里记录一下个人遇到的问题。1、sbt.\build.sbt时的错误,很多时候是由于JDK版本问题导致的。这个问题在sbt后续版本里得到了解决,我的sbt升级到1.9.0之后就没有遇到这个问题了。2、build.sbt里的spinalVersion会更新。可以时不时去spinalHDL的......
  • 微服务网关搭建
    依赖<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency><dependency><groupId......
  • linux搭建LAMP环境
    LAMP环境指在linux系统下搭建Apache服务器+mysql+php(1)安装Apache服务及其扩展包yum-yinstallhttpdhttpd-manualmod_sslmod_perlmod_auth_mysql(2)启动Apache服务systemctlstarthttpd.service(3)安装mysql  wgethttp://dev.mysql.com/get/mysql57-community-release......
  • pgsql 搭建
    1.yum安装yuminstall-yhttps://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpmyuminstall-ypostgresql12-server2.启动pgsql/usr/pgsql-12/bin/postgresql-12-setupinitdbsystemctlenablepostgresql-12system......
  • kubesphere 搭建redis
    一、我们用docker搭建redis时的命令是:dockerrun-d-p6379:6379--restart=always\-v/mydata/redis/conf/redis.conf:/etc/redis/redis.conf\-v/mydata/redis-01/data:/data\--nameredis-01redis:6.2.5\redis-server/etc/redis/redis.conf从中分析出来,我......