云社区 博客 博客详情
云社区 博客 博客详情

keepalived双主以及nginx+keepalived实现高可用

cherylchang 发表于 2019-12-02 20:47:11 12-02 20:47
cherylchang 发表于 2019-12-02 20:47:11 2019-12-02
0
0

【摘要】 一、keepalive实现双主:1.1实验准备2台主机地址分别为:172.20.10.9,172.20.10.10,vip1为172.20.10.10,vip2为172.20.10.111.2通知脚本:[root@node5/]#vim/etc/keepalived/notify.sh#!/bin/bashcontact='root@localhost'notify(){loc...

一、keepalive实现双主:
1.1实验准备
2台主机地址分别为:172.20.10.9,172.20.10.10,
vip1为172.20.10.10,vip2为172.20.10.11
1.2通知脚本:

[root@node5 /]# vim /etc/keepalived/notify.sh 
#!/bin/bash
contact='root@localhost'
notify() {
    local mailsubject="$(hostname) to be $1, vip floating"
    local 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

1.3配置node5为vip1地址的主节点,vip2地址的备节点

[root@node5 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
      root@localhost
   }
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id node5
   vrrp_mcast_group4 224.0.100.19
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.20.10.10/28 dev ens33 label ens33:0
    }
   notify_master "/etc/keepalived/notify.sh master"
   notify_backup "/etc/keepalived/notify.sh backup"
   notify_fault "/etc/keepalived/notify.sh fault"
}
vrrp_instance VI_2 {
    state BACKUP
    interface ens33
    virtual_router_id 52
    priority 98
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.20.10.11/28 dev ens33 label ens33:1
    }
   notify_master "/etc/keepalived/notify.sh master"
   notify_backup "/etc/keepalived/notify.sh backup"
   notify_fault "/etc/keepalived/notify.sh fault"
}

1.4配置node6为vip1地址的备节点,vip2地址的主节点

[root@node6 ~]# vim /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   notification_email {
      root@localhost
   }
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id node5
   vrrp_mcast_group4 224.0.100.19
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 98
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.20.10.10/28 dev ens33 label ens33:0
    }
   notify_master "/etc/keepalived/notify.sh master"
   notify_backup "/etc/keepalived/notify.sh backup"
   notify_fault "/etc/keepalived/notify.sh fault"
}
vrrp_instance VI_2 {
    state MASTER
    interface ens33
    virtual_router_id 52
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.20.10.11/28 dev ens33 label ens33:1
    }
   notify_master "/etc/keepalived/notify.sh master"
   notify_backup "/etc/keepalived/notify.sh backup"
   notify_fault "/etc/keepalived/notify.sh fault"
}

1.5验证:
(1)先开启node6节点的keepalived服务,查看IP地址,状态:
2个VIP地址都在node6节点上:
keepalived双主以及nginx+keepalived实现高可用
keepalived双主以及nginx+keepalived实现高可用

(2)再开启node5节点keepalived服务,查看IP地址和状态:
VIP1地址已经飘到了node5节点上了
keepalived双主以及nginx+keepalived实现高可用
keepalived双主以及nginx+keepalived实现高可用
二,Nginx+Keepalived实现站点高可用
2.1实验准备:
2台主机:node5地址为172.20.10.9,node6的节点为172.20.10.8
vip地址为172.20.10.10
2.2nginx配置
(1)node5节点

[root@node5 ~]# yum install nginx -y
[root@node5 /]# vim /usr/share/nginx/html/index.html 
node5 web page
[root@node5 ~]# systemctl  start nginx

(2)node6节点

[root@node6 /]# yum install -y  nginx
[root@node6 /]# vim /usr/share/nginx/html/index.html 
node6 web page
[root@node6 ~]# systemctl  start nginx

2.3配置node5为vip的主节点,配置检查nginx的监控脚本:

[root@node5 /]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
   notification_email {
      root@localhost
   }
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id node5
   vrrp_mcast_group4 224.0.100.19
}
vrrp_script chk_nginx {
    script "/usr/bin/killall -0 nginx &&  exit 0 || exit 1 "
    interval 1
    weight -5
    fall 2
    rise 1
}
vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.20.10.10/28 dev ens33 label ens33:0
    }
    track_script {
        chk_nginx
    }
    track_interface {
        ens33
    }
   notify_master "/etc/keepalived/notify.sh master"
   notify_backup "/etc/keepalived/notify.sh backup"
   notify_fault "/etc/keepalived/notify.sh fault"
}
[root@node5 keepalived]# systemctl  start   keepalived

2.4配置node6为vip的备节点,配置检查nginx的监控脚本:

[root@node6 /]# vim /etc/keepalived/keepalived.conf 
! Configuration File for keepalived
global_defs {
   notification_email {
      root@localhost
   }
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id node5
   vrrp_mcast_group4 224.0.100.19
}
vrrp_script chk_nginx {
    script "/usr/bin/killall -0 nginx &&  exit 0 || exit 1 "
    interval 1
    weight -5
    fall 2
    rise 1
}
vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 51
    priority 98
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.20.10.10/28 dev ens33 label ens33:0
    }
    track_script {
        chk_nginx
    }
    track_interface {
        ens33
    }
   notify_master "/etc/keepalived/notify.sh master"
   notify_backup "/etc/keepalived/notify.sh backup"
   notify_fault "/etc/keepalived/notify.sh fault"
}
[root@node6 html]# systemctl  start   keepalived

3.验证:
3.1当服务都正常时,vip地址在node5节点上,测试访问172.20.10.10的情况:
keepalived双主以及nginx+keepalived实现高可用
keepalived双主以及nginx+keepalived实现高可用
3.2当down掉node5节点上nginx服务时,观察vip地址和其访问情况:
vip地址飘到node6节点上,curl172.20.10.10,则访问的时node6节点
keepalived双主以及nginx+keepalived实现高可用

keepalived双主以及nginx+keepalived实现高可用
3.3当恢复node5节点上nginx服务时,vip地址飘到node5节点上,访问172.20.10.10则是node5上nginx
keepalived双主以及nginx+keepalived实现高可用
keepalived双主以及nginx+keepalived实现高可用

文章来源: blog.51cto.com,作者:不会运维,版权归原作者所有,如需转载,请联系作者。

原文链接:https://blog.51cto.com/14418331/2455143

登录后可下载附件,请登录或者注册

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件至:huaweicloud.bbs@huawei.com进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容。
评论文章 //点赞 收藏 0
点赞
分享文章到微博
分享文章到朋友圈

评论 (0)


0/1000
评论

登录后可评论,请 登录注册

评论

您没有权限执行当前操作

温馨提示

您确认删除评论吗?

确定
取消
温馨提示

您确认删除评论吗?

删除操作无法恢复,请谨慎操作。

确定
取消
温馨提示

您确认删除博客吗?

确定
取消

确认删除

您确认删除博客吗?

确认删除

您确认删除评论吗?

温馨提示

登录超时或用户已下线,请重新登录!!!

确定
取消