keepalived+nginx

keepalived+nginx

keepalived是vrrp协议的一种软件实现,可以用做高可用环境当中

实验:

node1:192.168.108.198 centos6
node2:192.168.108.199 centos7

1、安装keepalived

yum install -y keepalived

2、 安装nginx

yum install -y nginx

3、修改主页

vi /usr/share/nginx/html/index.html

centos6:this node1 centos6
centos7:this node2 centos7

4、简单配置

vi /etc/keepalived/keepalived.conf
    ! Configuration File for keepalived

    global_defs {
    #   notification_email {
    #        root@localhost
    #       }
    #   notification_email_from Alexandre.Cassen@firewall.loc
    #   smtp_server 127.0.0.1
    #   smtp_connect_timeout 30
       router_id LVS_DEVEL
       vrrp_skip_check_adv_addr
    #   vrrp_strict
       vrrp_garp_interval 0
       vrrp_gna_interval 0
    }

    vrrp_script chk_nginx {
        script "killall -0 nginx "
        interval 1
        weight -2
    }
    vrrp_instance VI_1 {
        state MASTER
        interface ens160
        virtual_router_id 51
        priority 100
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass lKak
        }
        virtual_ipaddress {
            192.168.108.190/24
        }

    track_script {
            chk_nginx    
        }
    }

这个是主节点的配置文件,备节点只需把 state MASTER 改为 state BACKUP;priority 100 改为priority 99

测试

现在node2为主节点,node1为备节点
node2:
keepalived+nginx_第1张图片

node1:
keepalived+nginx_第2张图片

可以看到node2上产生了虚拟IP,此时访问vip,查看页面结果
这里写图片描述

停止node2 的nginx:
systemctl stop nginx
发现vip漂移成功
keepalived+nginx_第3张图片
keepalived+nginx_第4张图片

继续访问vip网站页面
这里写图片描述

主备自动切换成功

小结

centos7中配置的ip不通信:由于配置文件中配置了vrrp_strict:严格按照vrrp协议,不允许单播模式。
注释掉相关代码后成功

你可能感兴趣的:(linux)