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

相关推荐

  • 制作epel源

    在linux的使用过程中,ISO镜像的rpm包并不是十分的齐全。这个时候就需要去epel源去下载安装rpm包安装。这里简单的描述下epel源的配置方法。仅供参考。 1.关闭SELinuxsed -i.bak ‘s/SELINUX=enforcing/SELINUX=permissive/’ /etc/selinux/config se…

    2017-12-18
  • Mariadb数据库复制系列(三):半同步复制

       实验三:半同步复制的实现 默认情况下,主从复制的结构中,主从节点之间复制操作是异步的,这样就有可能造成主从节点之间数据不一致的情况发生,所谓版同步复制就是指在一主多从的场景中,我们设定主节点与其中一个或多个从节点(一般是一个)的数据复制是同步进行的,从而保证了该从节点和主节点之间的数据的一致性。故当主节点发生故障时,就可以基于其他方式将该从节点提升为主…

    Linux干货 2016-11-24
  • Recycled和Recycler的故事

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://jeffyyko.blog.51cto.com/28563/156012      首先说明一下,这里不是英语课堂,本文也不是来辨析Recycled和Recycler这两个单词的含义。Recy…

    Linux干货 2015-03-25
  • CentOS 系统启动流程

    CentOS 系统启动流程 一、Linux系统的组成部分:内核+根文件系统 1.内核: 进程管理:进程之间的通信为:IPC(Inter Process Communication)机制,有消息队列、semerphor、shm、socket(跨主机之间的通信) 内存管理; 网络管理; 文件系统; 驱动程序; 安全功能; 2.运行中的系统环境可分为两层:内核空间…

    Linux干货 2016-09-13
  • shell脚本编写-2

    1、条件判断if语句       1)、 单分支 if  判断条件;then     条件为真的分支代码     fi 2)、双分支     if  判断条件; then    &…

    Linux干货 2016-08-15
  • 磁盘配额

    磁盘配额 1 启用磁盘配额 首先创建新的分区 /dev/sd5,并创建文件系统。 [root@local ~]# mkfs.ext4 /dev/sda5 由于xfs 不磁盘配额能成功,这里使用ext4. 然后把/dev/sda5分区挂载到 /home [root@local ~]# mount /dev/sda5 /home [root@local ~]# …

    Linux干货 2017-05-02