High availability—Keepalived installation and deployment

keepalived introduction

  Keepalived is based on the VRRP(Virtual Router Redundancy Protocol, Virtual Router Redundancy Protocol) protocol implementation LVS(LinuxVirtual Server, Linux virtual server) service high availability program. It mainly provides load balancing and high availability functions to avoid single points of failure. Load balancing is achieved through Linux's IPVS (ip virtual server), high availability through VRRP to achieve multi-machine failover.
  Keepalived is generally two nodes running keepalived, one is 主节点(MASTER), one is 备节点(BACKUP), the 对外performance is all 一个虚拟IP, the master node will send a specific message to the standby node, if the standby node does not receive this specific message, it is explained 主节点就宕机, 备节点就会接管虚拟IPproceed Service provision, which achieves high availability.

Keepalived using architecture diagram

Insert picture description here
Deploy keepalived and nginx on one server.

keepalived high availability failover

  Keepalived is based on the VRRP protocol to achieve high availability. There are two modes, one is 抢占模式(used by default), and the other is 非抢占模式to be configured nopreempt. We will talk about it in the detailed keepalived configuration file later. When used 抢占模式, this is a form 竞选机制of communication 主节点优先级大于备节点优先级. When the master node is down, you can switch to the standby node to provide services.

抢占模式原理As follows :

  1. When keepalived is working normally, the MASTER master node will continuously send specific messages ( 多播心跳消息) to the BACKUP standby node , which is a kind 健康检查机制of telling the standby node " 我还活着,虚拟IP我来管就行了!".
  2. When the master node fails and goes down, the heartbeat information cannot be sent to the standby node, and the standby node cannot receive the health check heartbeat information of the master node. At this time, the standby node is finally correct, and the opportunity comes. Service provision.
  3. When the failure of the master node recovers, it continuously sends heartbeats to the standby node, informing " 我现在活着呢,我来管虚拟IP" that the 备节点就会释放master node took over IP资源以及服务silently when it was down 做回一个备胎.

Keepalived installation and deployment

yum install keepalived

$ yum install -y keepalived
Insert picture description here

View keepalived version

[root@keepalived /etc/keepalived]#  keepalived -v
Keepalived v1.3.5 (03/19,2017), git commit v1.3.5-6-g6fa32f2

Copyright(C) 2001-2017 Alexandre Cassen, <[email protected]>

Build options:  PIPE2 LIBNL3 RTA_ENCAP RTA_EXPIRES RTA_PREF FRA_OIFNAME FRA_SUPPRESS_PREFIXLEN FRA_TUN_ID RTAX_CC_ALGO RTAX_QUICKACK LIBIPTC LIBIPSET_DYNAMIC LVS LIBIPVS_NETLINK VRRP VRRP_AUTH VRRP_VMAC SOCK_NONBLOCK SOCK_CLOEXEC FIB_ROUTING INET6_ADDR_GEN_MODE SNMP_V3_FOR_V2 SNMP SNMP_KEEPALIVED SNMP_CHECKER SNMP_RFC SNMP_RFCV2 SNMP_RFCV3 SO_MARK

Change keepalived configuration

$ cd /etc/keepalived
$ vim keepalived.conf
Mainly modify the configuration of the assigned virtual IP address.

Start keepalived

$ systemctl start keepalived.serviceOr $ service keepalived start
other related command
1) Restart:
$ systemctl restart keepalived.service
2) Stop:
$ systemctl stop keepalived.service
3) Status:
$ systemctl status keepalived.service
4) Set boot:
$ chkconfig keepalived on

View keepalived status

$ systemctl status keepalived.service
Insert picture description here

View keepalived process

$ ps -ef | grep keepalived
Insert picture description here

View ip list

$ ip add show
Insert picture description here

access

After the keepalived is started, we can access the service through the VIP of 10.139.1.1.

Detailed explanation of keepalived high availability configuration file

Master node

! Configuration File for keepalived

global_defs {
   # notification_email { # 邮件通知,一般不用
   #   [email protected]
   #   [email protected]
   # }
   # notification_email_from [email protected]
   router_id hostname1 # 标识本节点的字符串,设置为hostname即可
}

vrrp_instance VI_1 {
    state MASTER	# 标识主节点服务(只有MASTER和BACKUP两种,大写)
    interface eth0	# VIP板顶的网卡接口
    virtual_router_id 51	# 虚拟路由id,和备节点保持一致
    priority 100	# 优先级,高于备节点的即可。
    # nopreempt		# 禁止MASTER宕机恢复后抢占服务
    # smtp_alert		# 激活故障时发送邮件告警
    mcast_src_ip 10.139.1.10	# 本机IP地址
    advert_int 1	# MASTER和BACKUP节点之间的同步检查时间间隔,单位为秒
    authentication {	# 验证类型和验证密码
        auth_type PASS	# PAAS(默认),HA
        auth_pass 1111	# MASTER和BACKUP使用相同明文才可以互通
    }
    virtual_ipaddress {	# 虚拟IP地址池,可以多个IP
        10.139.1.1 # 虚拟IP1(VIP)
        10.139.1.2 # 虚拟IP2(VIP)
    }
}

Standby node

Except for the comment #, everything is consistent with the master node.

! Configuration File for keepalived

global_defs {
   router_id hostname2 # 标识本节点的字符串,设置为hostname即可
}

vrrp_instance VI_1 {
    state BACKUP	# 标识主节点服务(只有MASTER和BACKUP两种,大写)
    interface eth0	
    virtual_router_id 51	
    priority 99	# 优先级,高于备节点的即可。
    mcast_src_ip 10.139.1.11	# 本机IP地址
    advert_int 1	
    authentication {	
        auth_type PASS	
        auth_pass 1111	
    }
    virtual_ipaddress {	
        10.139.1.1
        10.139.1.2 
    }
}
Published 118 original articles · Like 150 · Visits 40,000+

Guess you like

Origin blog.csdn.net/Andya_net/article/details/105534290