Build an HA cluster based on keepalived

Source: Internet
Author: User
Tags haproxy

1. Introduction to keepalived

1. the purpose of Keepalived is to check the health status of RealServer. If a RealServer is down, Keepalived detects it and removes the down RealServer from the system, when the RealServer server resumes normal operation, Keepalived automatically adds the RealServer server to the server group.

2. Common keeepalived Models

<1> master-slave model: after the master node is repaired, the service returns to the master node)

Define a VIP over the NIC alias)

Modify vrrp_instance as follows:

StateMASTER # slave node is BACKUP

Priority101 # specify the priority. The greater the number, the higher the priority. The priority of the master node is higher than that of the slave node.


<2> dual-master model:

Create two vrouters with Master nodes and slave nodes

Specify two vrrp_instances as master-slave instances.

Virtual_router_id is not the same

<3> LVS + keepalived

First configure LVS

Configure keepalived, add virtual services, and specify the lvs model and scheduling algorithm.

Practice

System Environment: centos6.4-i386

Yum-yinstallkeepalived

Keepalivedi686-1.2.7-3

Ii. Master-slave model HA cluster

Modify the main configuration file: vim/etc/keepalived. conf

! Configuration File for detail {icationication_email {linuxedu@gmail.commageedu @ 126.com} contains kanotify@magedu.comsmtp _ connect_timeout 3smtp_server 127.0.0.1 # The Mail sender is a local host router_id LVS_DEVEL} vrrp_script failed {script "killall-0 haproxy" interval 1 weight} vrrp_script chk_mantaince_down {script "[-f/etc/keepalived/down] & exit 1 | exit 0" interval 1 weight-2} vrrp_instance VI_1 {# vrrp instance interface eth0state MASTER # The slave node is BACKUPpriority 101 # specify the priority, A greater number indicates a higher priority. virtual_router_id 51garp_master_delay 1 authentication {auth_type PASSauth_pass password} track_interface {eth1} virtual_ipaddress {172.16.15.114/16 dev eth1 label eth1: 0 # specify the VIP and nic alias} track_script {chk_haproxychk_mantaince_down} notify_master "/etc/keepalived/notify. sh master "yy_backup"/etc/keepalived/notify. sh backup "policy_fault"/etc/keepalived/policy. sh fault "}

Y script:

#! /Bin/bashvip = 172.16.15.114 # specify vipcontact = 'root @ localhost' using Y () {mailsubject = "'hostname' to be $1: $ vip floating "mailbody =" 'date' + % F % H: % M: % s': vrrp transition, 'hostname' changed to be $1 "echo $ mailbody | mail-s" $ mailsubject "$ contact} case" $1 "inmaster) Running y master/etc/rc. d/init. d/haproxy startexit 0; backup) y backup/etc/rc. d/init. d/haproxy stopexit 0; fault) failed y faultexit 0; *) echo 'usage: 'basename $ 0' {master | backup | fault} 'exit 1; esac

Access 172.16.15.114 using windows as the client

650) this. width = 650; "title =" Unnamed 1.png "alt =" 161926110.png" src = "http://www.bkjia.com/uploads/allimg/131227/23452529E-0.png"/>

Disable the master service: servicekeepalivedstop

650) this. width = 650; "title =" Unnamed 2.png "alt =" 162020933.png" src = "http://www.bkjia.com/uploads/allimg/131227/23452515V-1.png"/>

Iii. Dual-Master Model

Principle: create two vrouters, and the two nodes are master and slave.

! Configuration File for detail {icationication_email {linuxedu@gmail.commageedu @ 126.com} contains kanotify@magedu.comsmtp _ connect_timeout 3smtp_server detail _ id LVS_DEVEL} vrrp_script detail {script "killall-0 haproxy" interval 1 weight 2} vrrp_script detail {script" [[-f/etc/keepalived/down] & exit 1 | exit 0 "interval 1 weight 2} vrrp_instance VI_1 {# instance 1 interface eth1state MASTER # BACKUP for slave routerspriority 101 #100 for BACKUPvirtual_router_id 51garp_master_delay 1 authentication {auth_type PASSauth_pass password} track_interface {eth1} virtual_ipaddress {# VIP1172.16.15.115/16 dev eth1 label eth1: 1} track_script {chk_haproxychk_mantaince_down} yy_master "/etc/keepalived/notify. sh master "yy_backup"/etc/keepalived/notify. sh backup "policy_fault"/etc/keepalived/policy. sh fault "} vrrp_instance VI_2 {# instance 2 interface eth1state BACKUP # BACKUP for slave routerspriority 100 #100 for authentication 1 authentication {auth_type PASSauth_pass password} track_interface {eth1} virtual_ipaddress {# region/ 16 dev eth1 label eth1: 2} track_script {chk_haproxychk_mantaince_down }}

Access 172.16.15.115172.16.15.116 from the client

650) this. width = 650; "title =" Unnamed 3.png "alt =" 162517180.png" src = "http://www.bkjia.com/uploads/allimg/131227/23452553T-2.png"/>

View IP

650) this. width = 650; "title =" Unnamed 4.png "src =" http://www.bkjia.com/uploads/allimg/131227/2345253513-3.png "alt =" 164934163.png"/>

650) this. width = 650; "title =" Unnamed 5.png "src =" http://www.bkjia.com/uploads/allimg/131227/2345252525-4.png "alt =" 165000572.png"/>

4. Implement LVS + keepalivedLVS -- DR)

1. First configure the LVS Server Load balancer Cluster

Note: LVS will be detailed in subsequent blog posts !!!

Add ipvsadm rules

# Yum-y install ipvsadm # view ipvsadm information and rules # ipvsadm-A-t 172.16.15.117: 80-s rr # specify the ctor Scheduler)

Note: A (Add Rules)

-T (specify the Protocol as TCP)

172.16.15.117: 80 (specify the VIP and port)

-Srr (specify the scheduling algorithm as rr)

# Add real_server

# Ipvsadm-a-t 172.16.15.117: 80-r 172.16.15.5-g # ipvsadm-a-t 172.16.15.117: 80-r 172.16.15.14-g # ifconfig lo: 0 $ WEB_VIP netmask 255.255.255.255 broadcast $ WEB_VIP # configure the VIP and only allow local loopback #/sbin/route add-host $ WEB_VIP dev lo: 0 # Add a local route # echo "1">/proc/sys/net/ipv4/conf/lo/arp_ignore # echo "2">/proc/sys/net/ipv4/ conf/lo/arp_announce # echo "1">/proc/sys/net/ipv4/conf/all/arp_ignore # echo "2">/proc/sys/net/ipv4/ conf/all/arp_announce

Note: Only two nodes are added here.

-A: Add a service

-R172.16.15.5: Specify the service IP Address

-G: Specify the default DR value for the lvs model)

2. configure a master-slave high-availability cluster based on keepalived.

! Configuration File for detail {icationication_email {linuxedu@gmail.commageedu @ 126.com} detail kanotify@magedu.comsmtp _ connect_timeout 3smtp_server detail _ id LVS_DEVEL} vrrp_script chk_schedown {script "[[-f/etc/keepalived/down] & amp; exit 1 | exit 0 "interval 2 weight-2} vrrp_instance VI_1 {interface eth1state MASTERpriority authentication 1 authentication {auth_type PASSauth_pass password} track_interface {eth1} virtual_ipaddress {172.16.15.117/16 # specifies the VIP, the VIP here is the same as that of lvs, therefore, no Nic} track_script {chk_schedown} virtual_server 172.16.15.117 80 {# specify the virtual service delay_loop limit 50 protocol limit 172.16.15.14 80 # error page real_server limit 80 {# specify rserver1weight 1HTTP_GET {path/status_code 200 # status code} connect_timeout timeout 3} real_server 172.16.15.14 80 {# specify rserver2weight 1HTTP_GET {url {path/status_code 200} connect_timeout 3 # connection timeout time nb_get_retry 3 # number of connection attempts delay_before_retry 3 # waiting time before each attempt }}}

Access 172.16.15.117 on the client

650) this. width = 650; "title =" Unnamed 1.png "src =" http://www.bkjia.com/uploads/allimg/131227/2345255118-5.png "alt =" 165123558.png"/>

650) this. width = 650; "title =" Unnamed 2.png "src =" http://www.bkjia.com/uploads/allimg/131227/2345256412-6.png "alt =" 165139941.png"/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.