keepAlived 集群安装通过动态IP 实现服务高可用

目标:

实现nginx 集群高可用,通过访问虚拟IP 地址完成nginx服务的请求,在 主nginx 服务器不能正常提供服务时,备份nginx服务器能自动完成服务接管。并邮件提醒管理员(此功能后续实现)

 

 

 

 

前提:

两台服务器IP 分别 为 192.168.1.137, 192.168.1.138,分别在每个服务器上部署nginx 服务,并完成集群配置。

 

 

准备:

安装 open SSL

sudo yum install openssl-devel 

 

安装过程中要交互一下 y

 

 

 

 

安装libnl/libnl-3依赖

sudo yum -y install libnl libnl-devel

 

 

 

 

 

 

安装keepalived

下载keepalived

https://www.keepalived.org/

 

 

 将下载的gz包上传到Linux 服务器

 

 

 解压到/opt/module/

 

 

 cd 到解压后的目录 执行 ./congigure

 

 

执行后是这样的就对了。。

 

 

 

sudo  make

不提示错误---ok

sudo make install

不提示错误---ok

安装成功后会生成以下两个目录;分别存放的是 执行文件和配置文件

/usr/local/sbin/keepalived

/usr/local/etc/keepalived

配置keepalived

创建目录

/etc/keepalived/ 在目录内创建 keepalived.conf

创建配置文件

配置文件内容如下:

192.168.1.138为备份BACKUP

global_defs{
    router_id lb138
}
vrrp_instance nacos{
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 50
    advert_int 1
    authentication{
        auth_type:PASS
        auth_pass:1234
    }
    virtual_ipaddress{
        192.168.1.254
    }

}

virtual_server 192.168.1.254 80 {
    delay_loop 6
    lb_algo rr
    lb_kind NAT
    persistence_timeout 50
    protocol TCP

    real_server 192.168.1.138 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            retry 3
            connect_port 80
            delay_before_retry 3
        }
    }
}
 

192.168.1.137 为master主机

global_defs {
    router_id lb137
}
vrrp_instance nacos {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type:PASS
        auth_pass:1234
    }
    virtual_ipaddress {
        192.168.1.254
    }

}
 virtual_server 192.168.1.254 80 {
    delay_loop 6
    lb_algo rr
    lb_kind NAT
    persistence_timeout 50
    protocol TCP

    real_server 192.168.1.137 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            retry 3
            connect_port 80
            delay_before_retry 3
        }
    }
}
 

对上面的配置文件需要注意:

1、router_id: #此处注意router_id为负载均衡标识  keepalived 服务器的标识,在局域网内应该是唯一的。 

2、vrrp_instance :vrrp 的实例编码 相同的vrrp ,vrrp_instance 相同

3、state: #设置 lvs 的状态,MASTER 和 BACKUP 两种,必须大写

4、virtual_router_id:这个标识是一个数字,同一个 vrrp 实例使用相同标识

 

5、priority:优先级 值越大,优先级越高,标识为MASTER的 vrrp  要比 BACKUP 的vrrp 要大

6、virtual_ipaddress:这个IP 地址要与客户端(调用者)在同一个网络中(同一网段,或者是在防火墙或路由器配置了不同网络之间互联策略) 这里使用了同一个网段的地址

启动服务  service keepalived start

[root@machine138 keepalived]# service  keepalived start
Redirecting to /bin/systemctl start keepalived.service
[root@machine138 keepalived]# service  keepalived status
Redirecting to /bin/systemctl status keepalived.service
● keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
   Active: active (running) since 一 2023-02-20 13:40:51 CST; 18s ago
     Docs: man:keepalived(8)
           man:keepalived.conf(5)
           man:genhash(1)
           https://keepalived.org
  Process: 7846 ExecStart=/usr/local/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 7847 (keepalived)
    Tasks: 3
   CGroup: /system.slice/keepalived.service
           ├─7847 /usr/local/sbin/keepalived -D
           ├─7848 /usr/local/sbin/keepalived -D
           └─7849 /usr/local/sbin/keepalived -D

 

把服务注册为系统 服务,开机自启动 systemctl enable keepalived

[root@machine138 keepalived]# systemctl enable keepalived
Created symlink from /etc/systemd/system/multi-user.target.wants/keepalived.service to /usr/lib/systemd/system/keepalived.service.
[root@machine138 keepalived]# systemctl enable keepalived

 

 

 

 

 

 

 

 

 

 

 

把 1.137reboot

此时观察 138的IP地址及 客户端ping的情况

 

 

 虚拟IP 192.168.1.254 已经漂移到了 138服务器上

 

 

 

测试nginx

 

 

附:服务常用命令

 以下以keepalived服务为例:

systemctl start keepalived            开启服务
systemctl stop keepalived            关闭服务
systemctl status keepalived     显示状态
systemctl restart keepalived        重启服务
systemctl enable keeplived       开机启动服务
systemctl disable keeplived        禁止开机启动
systemctl list-units              查看系统中所有正在运行的服务
systemctl list-unit-files        查看系统中所有服务的开机启动状态
systemctl list-dependencies keepalived          查看系统中服务的依赖关系
systemctl mask keepalived                        冻结服务
systemctl unmask keepalived                     解冻服务
systemctl set-default multi-user.target     开机时不启动图形界面
systemctl set-default graphical.target      开机时启动图形界面
  修改服务配置文件后需要
  systemctl daemon-reload
 
  设置服务开机自启动
  systemctl enable keepalived
 
  查询是否自启动服务
  systemctl is-enabled keepalived
  或
  systemctl list-units |grep keepalived
    取消服务器开机自启动   systemctl disable keepalived# 显示某个 Unit 是否正在运行 $ systemctl is-active keepalived# 显示某个 Unit 是否处于启动失败状态 $ systemctl is-failed keepalived# 显示某个 Unit 服务是否建立了启动链接 $ systemctl is-enabled keepalived

 

posted on 2023-02-20 15:22  hztech  阅读(92)  评论(0编辑  收藏  举报

导航