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

相关推荐

  • 文件属性、文件管理命令及glob 博客作业—-21期网络班第二周

    1、Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。    cp  mv  rm      命令格式:      cp [options] … SOURCE DEST    …

    Linux干货 2016-07-16
  • 堡垒机-麒麟堡垒机动态口令使用手册

      一.管理员部分 1.在其它-licenses菜单查看动态口令许可是否打开,如果未打开联系厂商重新生成许可   2.找厂商生成密钥文件,密钥文件中包含令牌种子,在其它–动态令牌菜单将密钥文件导入即可看到所有的令牌种子,每个令牌可以绑定给多个用户     3.令牌绑定可以在 资源管理–…

    安全运维 2016-05-29
  • 0805课堂练习与作业

    练习1 1、找出ifconfig命令结果中本机的所有IPv4地址 2、查出分区空间使用率的最大百分比值 3、查出用户UID最大值的用户名、UID及shell类型 4、查出/tmp的权限,以数字方式显示 5、统计当前连接本机的每个远程主机IP的连接数,并按从大 到小排序 练习2 1、显示/proc/meminfo文件中以大小s开头的行;(要求:使 用两种方式)…

    Linux干货 2016-08-07
  • vim编辑器

                                                      &nbsp…

    Linux干货 2015-12-19
  • 运维学习笔记-Puppet之Hiera初探

    为什么使用Hiera? Puppet中的manifest同时包含静态的代码(判断/循环逻辑,依赖关系,类定义,资源类型定义等等)和动态的数据(类声明时的参数值和资源声明时的属性值)。说代码是静态的是因为如果在设计阶段考虑比较全面,代码写成之后是很少变化的。但是数据要根据具体情况赋予不同的值。如果manifest设计的不是很灵活,比如某些数据被固化(hardc…

    Linux干货 2016-07-07
  • N25-第二周作业

    1、linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示 linux上处理目录的常用命令:     ls :列出目录     cd :切换目录     pwd:显示当前工作路径    &nbs…

    Linux干货 2016-12-13