Nginx+keepalived实现简单切换

2023年 7月 15日 57.3k 0

keepalived是集群管理中保证集群高可用的一个服务软件,其功能类似于heartbeat,用来防止单点故障。keepalived是以VRRP协议为实现基础的,他的切换速度非常迅速!虽然他比较轻量,功能稍弱,单可以通过脚本来实现请输入图片描述

安装keepalived网盘下载keepalived包

http://www.keepalived.org/index.html 
[root@nginx-proxy ~]# ln -s /usr/src/kernels/2.6.32-504.el6.x86_64/ /usr/src/linux
[root@nginx-proxy ~]# tar xf keepalived-1.2.13.tar.gz 
[root@nginx-proxy ~]# cd keepalived-1.2.13
[root@nginx-proxy keepalived-1.2.13]# ./configure 
[root@nginx-proxy keepalived-1.2.13]# make
[root@nginx-proxy keepalived-1.2.13]# make install

生成启动脚本

[root@nginx-proxy keepalived-1.2.13]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/  

复制配置启动脚本

[root@nginx-proxy keepalived-1.2.13]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/ 

创建配置文件路径

[root@nginx-proxy keepalived-1.2.13]# mkdir /etc/keepalived     

配置文件模板

[root@nginx-proxy keepalived-1.2.13]# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/    
[root@nginx-proxy keepalived-1.2.13]# cp /usr/local/sbin/keepalived /usr/sbin/ 
[root@nginx-proxy keepalived-1.2.13]# /etc/init.d/keepalived start
Starting keepalived:                                       [  OK  ]
[root@nginx-proxy local]# ps -ef|grep keep|grep -v grep
root       3355      1  0 05:55 ?        00:00:00 keepalived -D
root       3357   3355  0 05:55 ?        00:00:00 keepalived -D
root       3358   3355  0 05:55 ?        00:00:00 keepalived -D
[root@nginx-proxy local]# 

配置文件说明

[root@nginx-proxy keepalived]# vim /etc/keepalived/keepalived.conf
vrrp_instance VI_1 {
    state MASTER            主/备 
    interface eth0          监听网卡
    virtual_router_id 51      实例id号
    priority 100              优先级
    advert_int 1             心跳间隔时间              
    authentication {
        auth_type PASS         
        auth_pass 1111        密码
    }
    virtual_ipaddress {     VIP
        192.168.200.16
        192.168.200.17
        192.168.200.18
    }
}

配置文件修改

[root@nginx-proxy keepalived]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
   734943463@qq.com   
}
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance VI_1 {
    state MASTER
    interface eth1
    virtual_router_id 51
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
    10.0.0.100/24
    }
}
[root@nginx-proxy keepalived]# /etc/init.d/keepalived restart
Stopping keepalived:                                       [  OK  ]
Starting keepalived:                                       [  OK  ]
[root@nginx-proxy keepalived]# 

启动后vip会在权重高的一端

[root@nginx-proxy keepalived]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:5c:67:b6 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.90/16 brd 10.0.255.255 scope global eth1
    inet 10.0.0.100/24 scope global eth1
    inet6 fe80::20c:29ff:fe5c:67b6/64 scope link 
       valid_lft forever preferred_lft forever
[root@nginx-proxy keepalived]# ip addr|grep 10.0.0.100
    inet 10.0.0.100/24 scope global eth1
[root@nginx-proxy keepalived]# 

nginx-proxy2的配置需要修改1,router_id不能一致2,state MASTER/BACKUP3, priority 权重不能一致这里需要注意的是网卡的信息,两端需要网卡号一致密码一致,其他等

[root@nginx-proxy2 ~]# cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   notification_email {
   734943463@qq.com   
}
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL_2
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth1
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
    10.0.0.100/24
    }
}
[root@nginx-proxy2 ~]# 

相关文章

LeaferJS 1.0 重磅发布:强悍的前端 Canvas 渲染引擎
10分钟搞定支持通配符的永久有效免费HTTPS证书
300 多个 Microsoft Excel 快捷方式
一步步配置基于kubeadmin的kubevip高可用
istio全链路传递cookie和header灰度
REST Web 服务版本控制

发布评论