单实例主备切换---keepalived

一:环境说明

  • 1:操作系统rhel7内核   3.10.0-123.el7.x86_64 
    2:keepalived软件版本  keepalived-1.3.9.tar.gz
    

二:环境配置

  • 1:主Keepalived服务器IP地址    192.168.1.10
    2:备Keepalived服务器IP地址   192.168.1.20
    3:Keepalived 虚拟IP地址      192.168.1.15 [局域网中任意未使用IP]
    

三:安装流程

  • 1:上传Keepalived软件到/bankapp目录

  • 2: 解压软件

    tar -zxvf keepalived-1.3.9.tar.gz 
    cd keepalived-1.3.9
    
  • 编译以及编译安装

    ./configure --prefix=/usr/local/keepalived
    make && make install
    
  • 注意:
    configure: error:
    !!! OpenSSL is not properly installed on your system. !!!
    !!! Can not include OpenSSL headers files.

    解决办法:
    openssl version -a 查看一下openssl是否安装
    yum -y install openssl-devel --安装开发包,包含头文件和链接库

  • 3:制作快捷启动,在源文件的目录./keepalived-1.3.5/keepalived/etc/init.d下有两个快捷启动文件和生成/usr/local/keepalived/目录下一个配置文件需要复制

    1:cp  /bankapp/keepalived-1.3.9/keepalived/etc/init.d/keepalived  /etc/rc.d/init.d/
    2:cp  /bankapp/keepalived-1.3.9/keepalived/etc/sysconfig/keepalived   /etc/sysconfig
    #cp  /usr/local/keepalived/etc/sysconfig/keepalived      /etc/sysconfig
    
    3:mkdir -p /etc/keepalived   
     cp  /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
    
    4:cp  /usr/local/keepalived/sbin/keepalived   /usr/sbin
    
    
    ###注意 /etc/init.d是/etc/rc/init.d软链接文件
    
  • 4:设置为服务,开机启动 ---注意给文件赋可执行权限 chmod +x /etc/rc.d/rc.local

    vi /etc/rc.local
    
    # THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
    #
    # It is highly advisable to create own systemd services or udev rules
    # to run scripts during boot instead of using this file.
    #
    # In contrast to previous versions due to parallel execution during boot
    # this script will NOT be run after all other services.
    #
    # Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
    # that this script will be executed during boot.
    
    touch /var/lock/subsys/local
    
    /etc/init.d/keepalived start
    

四:keepalived配置

  • 1:主机修改配置文件

  • cp keepalived.conf keepalived.conf.20200319   #备份原来的
    vi  /etc/keepalived/keepalived.conf 
    ! Configuration File for keepalived
    
    vrrp_instance VI_1 {
        state BACKUP          #mdodel :MASTER-BACKUP,BACKUP-BACKUP
        interface eno16777728 
        virtual_router_id 51   #主备要一致
        priority 150		  #优先级主要比从高50,一般是50为一个分级
        advert_int 1
        nopreempt			#非抢占式,主服务器要配置,备机不配置
        authentication {
            auth_type PASS
            auth_pass 1111
        }
        virtual_ipaddress {
            192.168.1.15
        }
    }
    
  • 2:备机也按照主机那样安装,配置

  • 然后修改配置文件

    ! Configuration File for keepalived
    
    
    vrrp_instance VI_1 {
        state BACKUP        #备机也是BACKUP
        interface eno16777728
        virtual_router_id 51
        priority 100
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass 1111
        }
        virtual_ipaddress {
            192.168.1.15
        }
    }
    

五:启动服务

  • 1:service keepalived start
    #service keepalived   stop
    #service keepalived restart
        
    2:ps - ef | grep keep |grep -v grep   查看进程,有3个进程说明正常启动     
    root       8732      1  0 19:14 ?        00:00:00 /usr/local/keepalived/sbin/keepalived -D
    root       8733   8732  0 19:14 ?        00:00:00 /usr/local/keepalived/sbin/keepalived -D
    root       8734   8732  0 19:14 ?        00:00:00 /usr/local/keepalived/sbin/keepalived -D
    

六:查看网卡信息

  • 主Keepalived网卡信息
# ip addr show eno16777728 
2: eno16777728: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:5d:1a:f0 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.10/24 brd 192.168.1.255 scope global noprefixroute eno16777728
       valid_lft forever preferred_lft forever
    inet 192.168.1.15/32 scope global eno16777728
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe5d:1af0/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
  • 备Keepalived网卡信息
# ip addr show eno16777728 
2: eno16777728: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:5a:67:0e brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.20/24 brd 192.168.1.255 scope global eno16777728
       valid_lft forever preferred_lft forever
    inet 192.168.1.15/32 scope global eno16777728
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe5a:670e/64 scope link 
       valid_lft forever preferred_lft forever
    
    ###我这备机也有虚拟IP,可能裂脑了
    
    
  • 注意error:

  • #eno16777728: <BROADCAST,MULTICAST,UP,LOWER_UP>  这里会显示网卡模式
        #主备都有VIP,裂脑了,通过修改网卡的为混杂模式解决
    
    ifconfig  eno16777728  #临时修改
    eno16777728: flags=4419<UP,BROADCAST,RUNNING,PROMISC,MULTICAST>  mtu 1500
            inet 192.168.1.10  netmask 255.255.255.0  broadcast 192.168.1.255
            inet6 fe80::20c:29ff:fe5d:1af0  prefixlen 64  scopeid 0x20<link>
            ether 00:0c:29:5d:1a:f0  txqueuelen 1000  (Ethernet)
            RX packets 15605  bytes 12116937 (11.5 MiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 8017  bytes 1558236 (1.4 MiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
            
    #永久修改开机执行命令:
    vi /etc/rc.local
    ifconfig eno16777728 promisc
    
    
    #注意关闭防火墙 
    systemctl status firewalld
    systemctl disable firewalld
    
    

七:测试

  • 1:主机
    ip addr show eno16777728 |grep 192.168.1.15
        inet 192.168.1.15/32 scope global eno16777728
        
    2:备机
    ip addr show eno16777728 | grep 192.168.1.15
    无输出
    
    3:停掉主机keepalived服务
    [root@docker log]# service keepalived stop
    Stopping keepalived (via systemctl):                       [  OK  ]
    [root@docker log]# ip addr show eno16777728 |grep 192.168.1.15
    [root@docker log]#   
    
    4:查看备机
    [root@registry log]# ip addr show eno16777728 | grep 192.168.1.15
        inet 192.168.1.15/32 scope global eno16777728
        
     成功!!!
    
    

posted @ 2020-03-19 00:42  Noob52037  阅读(677)  评论(0编辑  收藏  举报