HA Cluste 的配置:keepalived:vrrp协议 双主模型

keepalived:vrrp协议 双主模型
   第一步配置出始环境:
        准备两节点:
            主节点:172.18.57.7 long1
            备节点:172.18.57.8 long2
    (1) 各节点时间必须同步;      
        同步时间:
            ~]# yum -y install chrony
            ~]# vim /etc/chrony.conf
            ~]# systemctl start chronyd
            ~]# chronyc sources
            ~]# chronyc sourcestats
    (2) 确保iptables及selinux不会成为阻碍;       
        清空防火墙:
            ~]# iptables -F
        关闭SELinux
            ~]# setenforce 0
        清空规则:
            ~]# ipvsadm -C
        重新启动网络:
            ~]#systemctl restart network    
    (3) 各节点之间可通过主机名互相通信(对KA并非必须);
        修改域名解析使用/etc/hosts文件实现;
             ~]#vim /etc/hosts
                172.18.57.7 long1
                172.18.57.8 long2
    第二步:keepalived安装
        long1 ~]#yum -y install keepalived
        long2 ~]#yum -y install keepalived
   第三步:修改配置文件
        [root@long1 ~]# 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 long1
               vrrp_mcast_group4 224.0.101.57
            }

            vrrp_instance myr1 {
                state MASTER
                interface ens33
                virtual_router_id 57
                priority 100
                advert_int 1
                authentication {
                    auth_type PASS
                    auth_pass dAcLuTSt
                }
                virtual_ipaddress {
                    172.18.57.57/16 dev ens33
                }
            }
            vrrp_instance myr2 {
                state BACKUP
                interface ens33
                virtual_router_id 58
                priority 98
                advert_int 1
                authentication {
                    auth_type PASS
                    auth_pass dAcLUTSt
                }
                virtual_ipaddress {
                    172.18.57.157/16 dev ens33
                }
            }
        [root@long2 ~]#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 long2
               vrrp_mcast_group4 224.0.101.57
            }

            vrrp_instance myr1 {
                state BACKUP
                interface ens33
                virtual_router_id 57
                priority 98
                advert_int 1
                authentication {
                    auth_type PASS
                    auth_pass dAcLuTSt
                }
                virtual_ipaddress {
                    172.18.57.57/16 dev ens33
                }
            }
            vrrp_instance myr2 {
                state MASTER
                interface ens33
                virtual_router_id 157
                priority 100
                advert_int 1
                authentication {
                    auth_type PASS
                    auth_pass dAcLUTSt
                }
                virtual_ipaddress {
                    172.18.57.157/16 dev ens33
                }
            }

   第四步:启动服务,查看状态
        [root@long1 ~]#systemctl start keepalived.service
        [root@long2 ~]#systemctl start keepalived.service
        [root@long1 ~]# systemctl status keepalived.service
        [root@long2 ~]# systemctl status keepalived.service
        [root@long1 ~]# tcpdump -i ens33 host 224.0.101.57

   模拟宕机long1:
        [root@long1 ~]# iptables -A OUTPUT -s 172.18.57.7 -d 224.0.101.57 -j REJECT
        [root@long1 ~]# systemctl status keepalived.service
        [root@long1 ~]# iptables -F
   实验节点ip发生转移是发送邮件:通知脚本
        [root@long1 ~]#systemctl stop keepalived
        [root@long2 ~]#systemctl stop keepalived
        [root@long1 ~]# vim /etc/keepalived/notify.sh
            #!/bin/bash
            #
            contact='root@localhost'

            notify () {
                    mailsubject="vrrp:$(hostname) to be $1"
                    mailbody="$(hostname) to be $1, vrrp transition,$(date)."
                    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
        [root@long1 ~]# chmod +x notify.sh
        [root@long1 ~]# vim /etc/keepalived/keepalived.conf
            notify_master "/etc/keepalived/notify.sh master"
            notify_backup "/etc/keepalived/notify.sh backup"
            notify_fault "/etc/keepalived/notify.sh fault"  
        [root@long2 ~]# vim /etc/keepalived/notify.sh
            #!/bin/bsah
            #
            contact='root@localhost'

            notify () {
                    mailsubject="vrrp:$(hostname) to be $1"
                    mailbody="$(hostname) to be $1, vrrp transition,$(date)."
                    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
        [root@long2 ~]# vim /etc/keepalived/keepalived.conf
            notify_master "/etc/keepalived/notify.sh master"
            notify_backup "/etc/keepalived/notify.sh backup"
            notify_fault "/etc/keepalived/notify.sh fault"
        [root@long2 ~]#systemctl start keepalived
        [root@long2 ~]#mail
        [root@long2 keepalived]#mail
            Heirloom Mail version 12.5 7/5/10.  Type ? for help.
            "/var/spool/mail/root": 2 messages 1 new 2 unread
            >N  1 root  Sun May 14 20:53  19/684   "vrrp: long2 to be backup"
             N  2 root  Sun May 14 20:53  18/674   "vrrp: long2 to be master"
        [root@long1 ~]#systemctl start keepalived
        [root@long1 ~]#mail
            Heirloom Mail version 12.5 7/5/10.  Type ? for help.
            "/var/spool/mail/root": 2 messages 2 new
            >N  1 root  Sun May 14 20:53  18/674   "vrrp: long1 to be backup"
             N  2 root  Sun May 14 20:53  18/674   "vrrp: long1 to be master"
        [root@long2 ~]#mail
            Heirloom Mail version 12.5 7/5/10.  Type ? for help.
            "/var/spool/mail/root": 3 messages 1 new 3 unread
             U  1 root   Sun May 14 20:53  19/684   "vrrp: long2 to be backup"
             U  2 root   Sun May 14 20:53  19/684   "vrrp: long2 to be master"
            >N  3 root   Sun May 14 20:54  18/674   "vrrp: long2 to be backup"

原创文章,作者:CL80516000,如若转载,请注明出处:http://www.178linux.com/75758

(0)
CL80516000CL80516000
上一篇 2017-05-15
下一篇 2017-05-15

相关推荐

  • 程序包管理yum

    YUM: YellowdogUpdate Modifier,rpm的前端程序,用来解决软件包相关依赖性,可以在多个库之间定位软件包 存储了众多rpm包,以及包的相关的元数据文件(放置于特定目录下:repodata) 文件服务器:     ftp://     http:// &n…

    Linux干货 2016-08-23
  • vim功能及使用

    vim 简介:   vi命令是UNIX操作系统和类UNIX操作系统中最通用的全屏幕纯文本编辑器。Linux中的vi编辑器叫vim,它是vi的增强版(vi Improved),与vi编辑器完全兼容,而且实现了很多增强功能。 文本编辑种类: 行编辑器: sed全屏编辑器:nano, vivim – Vi Improved 一、基本操作语法 vim [OPTION…

    Linux干货 2016-08-11
  • web服务 初步

    Web Service初步 一、引言 Web服务也是一个C/S架构,服务器端就是一个进程,客户端是一个浏览器。我们打开浏览器的时候,都会看到hppt,https的字样,然后才是输入网址,这两个协议是web服务的应用层协议,用来实现某些具体应用的。像https,前面学习openssl的时候也接触过,https=http+ssl。 web的传输层协议用到了tcp…

    Linux干货 2016-12-09
  • 马哥教育网络班21期+第3周课程练习

    1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 //使用who命令列出列出当明显登录的所有用户,使用cut命令取出用户名,使用sort命令去重即可// [root@localhost ~]# who | cut -d' ' -f1…

    Linux干货 2016-08-08
  • 文件权限管理

    一 、文件属性介绍 1、linux文件权限分为读(read)、写(write)、执行(excute)三种,主要针对三类用户进行定义:ouwer:属主(u) 、group(g):属组、 other(o):其他人。文件的权限组合可以分为两种,一种是由三个字母r 、w、 x组成,一种是由数字组成4、2、1,它们代表的意思是r = 4 表示读权限、w = 2表示写权…

    Linux干货 2016-08-08
  • keepalive高可用haproxy实现URL资源的动静分离

    keepalive高可用haproxy实现动静分离URL资源 实现要点: (1) 动静分离discuzx,动静都要基于负载均衡实现; (2) 进一步测试在haproxy和后端主机之间添加varnish缓存; (3) 给出拓扑设计; (4) haproxy的设定要求:     (a…

    Linux干货 2016-11-12