Keepalived achieved using hot standby Detailed

In this highly IT era of information technology, enterprise production systems, business operations, sales and support, as well as daily management and other sectors are increasingly dependent on computer and information services, application requirements for high availability (HA) technology continues to improve, in order to provide continuous, uninterrupted computer system or network services.

A, Keepalived hot standby Basics

1.Keepalived Overview

Keepalived was originally designed for a powerful auxiliary tool designed for LVS, mainly used to provide failover and health checks - LVS load balancer determine availability server nodes, timely isolation and replaced with a new server, when a failed host after its recovery rejoin the cluster.

Keepalived's official website: http://www.keepalived.org/ Although mainly used for LVS cluster environment, but in a non-LVS cluster environment, or as a hot backup software.

The hot standby mode 2.Keepalived

Keepalived using VRRP (Virtual Routing Redundancy Protocol) protocol hot backup, a software-based multi-function hot standby Linux servers. VRRP backup solution is directed to a router - a multiple routers form a hot standby group, by sharing a virtual IP address to provide services; the same time in each group has only one hot standby router to provide services in other router redundancy I state, router failure if the current line, the other routers will automatically take over (according to priority) virtual IP address, in order to continue to provide services.

VRRP (Virtual Router Redundancy Protocol) and HSRP (Hot Standby Routing Protocol) principle is almost the same, but the public is VRRP protocol; HSRP is a Cisco proprietary protocol. For VRRP (Virtual Routing Redundancy Protocol) do not understand the principle, you can refer Bowen: HSRP (Hot Standby Routing Protocol) Detailed Bowen has a detailed explanation.

: Each router in the standby group may called hot primary router, the IP address of virtual router (VIP) may be transferred between the routers in the hot standby groups, it is also known drift IP address as
Keepalived achieved using hot standby Detailed
the use Keepalived , to achieve drift address the need to manually establish a virtual interface profile (for example: ens33: 0); Keepalived but by automatically managed based on the profile.

3. Install Keepalived

Installation services Keepalived particularly simple, Centos 7 system tray has a corresponding package can be installed by YUM! In addition, at the time of application environments LVS cluster, also you need to use ipvsadm management tools (see the distribution of load use).

[root@localhost ~]# yum -y install keepalived ipvsadm
//安装Keepalived和ipvsadm
[root@localhost ~]# systemctl start keepalived
//启动Keepalived服务

Second, using dual hot standby Keepalived

Hot standby mode based on the VRRP, Keepalived failover server may be used, each group may have more than one hot standby server - of course, should be the most commonly used of the hot standby. In this embodiment dual hot standby machine, mainly for drift failover virtual IP address is achieved, and therefore can be applied to various application servers (for example: Web, FTP, Mail, SSH, DNS, etc.).

An example to understand the configuration Keepalived hot standby, as shown:
Keepalived achieved using hot standby Detailed
the primary and backup servers need to be installed Keepalived. (! Httpd service are also the best placement for testing) detailed steps are as follows:

1. The main server configuration

Keepalived service configuration file is /etc/keepalived//keepalived.conf. Specific operation is as follows:

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum -y install keepalived ipvsadm httpd
//安装相应的服务、关闭防火墙和SELinux
[root@localhost ~]# vim /etc/keepalived/keepalived.conf 
//编辑Keepalived服务的配置文件
global_defs {
   notification_email {
     [email protected]                                  //收件人地址
   }
   notification_email_from root  [email protected]                     //发件人姓名、地址
   smtp_server 127.0.0.1
   smtp_connect_timeout 30                            //以上信息是关于发送邮件的内容,根据实际情况填写就好
   router_id HA_TEST_R1                               //本路由器(服务器)的名称
}

vrrp_instance VI_1 {                                       //定义VRRP热备实例
    state MASTER                                           //热备状态,MASTER表示为主服务器
    interface ens33                                          //承载VIP的物理网卡接口
    virtual_router_id 1                                     //虚拟路由器的ID,每个热备组保持一致
    priority 100                                                //优先级100,数值越大优先级越高(最大255)
    advert_int 1                                              //通告间隔秒数(心跳频率)
    authentication {                                        //认证信息,每个热备组要保持一致
        auth_type PASS                                  //认证类型
        auth_pass 1111                                   //密码字串
    }
    virtual_ipaddress { 
        192.168.1.254                                    //指定漂移地址(VIP),可以有多个(但必须跟物理接口是同网段的)
    }
}

Confirm that the configuration information is not wrong, start Keepalived service. MASTER actual status of the primary server will automatically add ens33 interfaces VIP address, see (ifconfig command can not see) by ip command.

[root@localhost ~]# systemctl restart keepalived
//重启服务
[root@localhost ~]# ip addr show dev ens33
//查看漂移IP地址是否存在
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:00:11:89 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.1/24 brd 192.168.1.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet 192.168.1.254/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::c4bc:2e5a:89b:8729/64 scope link 
       valid_lft forever preferred_lft foreve
[root@localhost ~]# echo “aaaaaaaaa” > /var/www/html/index.html
[root@localhost ~]# systemctl start httpd
//修改httpd服务的主页文件,用于测试!

2. The standby server configuration

In the same Keepalived hot standby groups, all servers Keepalived profile substantially the same, comprising a virtual router ID, authentication information, drift address, heart rate, etc. (must be the same); the main difference that the router name, hot standby state, priority.

  • Router Name: It is recommended to specify a different name for each server involved in hot standby;
  • Hot Standby State: should be at least one host server, the status is set to the MASTER; can have more than one backup server, set the status to the BACKUP;
  • Priority: The higher the value the greater will obtain control of the VIP priority, thus the priority master server should be the highest group is hot standby; descending order of priority may be other replicas, but not identical, to avoid competition for VIP control over a conflict;

When configuring the standby server (which may be more than one), the primary server may refer to the contents keepa.conf profile, simply modify the name of the router, hot standby status, priority can! as follows:

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum -y install keepalived ipvsadm httpd
//安装相应的服务、关闭防火墙和SELinux
[root@localhost ~]# vim /etc/keepalived/keepalived.conf 
//编辑Keepalived服务的配置文件
global_defs {
   notification_email {
     [email protected]
   }
   notification_email_from root  [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id HA_TEST_R2                                 //本路由器(服务器)名称(修改)
}

vrrp_instance VI_1 {
    state BACKUP                                              //热备状态,BACKUP表示备用服务器(修改)
    interface ens33
    virtual_router_id 1
    priority 99                                                     //优先级(修改)
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.254
    }
}

Confirm the configuration is correct, start Keepalived service when the primary server is online. VIP still controlled by the master server, while other servers in the standby state.

[root@localhost ~]# systemctl start keepalived
[root@localhost ~]# ip addr show ens33
//启动Keepalived服务、确认VIP地址(没有VIP地址)
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:2b:56:b5 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.2/24 brd 192.168.1.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::aa26:7be4:3379:130f/64 scope link 
       valid_lft forever preferred_lft forever
[root@localhost ~]# echo "qqqqqqqqqqq" > /var/www/html/index.html
[root@localhost ~]# systemctl start httpd
//启动http服务,设置不一样的主页(用于测试!)

3. Test Hot Standby dual-function

(1) Continuity test

Executed on the client machine "ping -t 192.168.1.254" (VIP address), the normal communication, continuous testing!

[root@localhost ~]# systemctl stop keepalived
//主服务器故意停止Keepalived服务

View client test results, as shown:
Keepalived achieved using hot standby Detailed
switching customers the opportunity to have a delay, it could lose one or two packages.

[root@localhost ~]# ip addr show ens33
//备用服务器查看VIP地址(发现VIP已经自动转移到备用服务器上)
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:2b:56:b5 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.2/24 brd 192.168.1.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet 192.168.1.254/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::aa26:7be4:3379:130f/64 scope link 
       valid_lft forever preferred_lft forever

(2) Web access test

Client access test, as shown:
Keepalived achieved using hot standby Detailed
When the original server priority 100 is turned Keepalived service, client access again, will change the page content.

[root@localhost ~]# systemctl start keepalived
//主服务器启动Keepalived服务

Client access test, as shown:
Keepalived achieved using hot standby Detailed

You can also view the system log (/ var / log / messages), for more details!

By Keepalived service is built high availability cluster, the cluster with load balancing built by LVS, not the same. Information on the type of clusters can introduce reference Bowen: LVS load balancing cluster Detailed

-------- end of this article so far, thanks for reading --------

Guess you like

Origin blog.51cto.com/14157628/2439093