Keepalived + lvs Configuration

Source: Internet
Author: User

Lvs + keepalived:
The master and backup architecture modes are used.
VRRP: vro redundancy protocol. It can be used to avoid spof.
Keepalived detects the status of the server through the vrrp protocol for protocol exchange. And master election.
1. Initialization
2. conduct the election and select the master server
Install keepalived:
1. Compile and install
2. Create a service startup script to control
Cp/usr/local/keepalived/etc/rc. d/init. d/keepalived/etc/init. d/keepalived
Chmod + x/etc/init. d/keepalived
Because we use a non-default path (/usr/local) to install keepalived, we need to modify several paths to ensure that keepalived can be started properly. The files to be modified are as follows:
3. Modify/etc/init. d/keepalived: Search for about 15 rows. /etc/sysconfig/keepalived, modified :. /usr/local/keepalived/etc/sysconfig/keepalived, pointing to the correct File Location
Add the following content under the preceding line (import the PATH of the keepavlied main program to the environment variable PATH ):
PATH = "$ PATH:/usr/local/keepalived/sbin"
Export PATH
4. Modify the/usr/local/keepalived/etc/sysconfig/keepalived file and set the correct service startup parameters.
KEEPALIVED_OPTIONS = "-D-f/usr/local/keepalived/etc/keepalived. conf"
5. After the above modification, the keepalived installation can be completed. Start the test:
Service keepalived restart
6. Do not forget to set this service to boot
Chkconfig keepalived on
Actual Configuration:

Master-slave mode.

Keepalived is installed and configured on the server that provides services. In this mode, a virtual IP Address can only belong to one node at a time, and another node exists as a slave node. When the master node is unavailable, the slave node takes over the virtual IP address to provide normal services.




Master and slave provide web Services. By default, virtual vIP is deployed on the master server, and the master provides web Services. When the master node is shut down due to a fault, the vIP automatically switches to the slave, which provides web Services. In the client, it is the same as sending nothing.

The keepalived configuration of the master is as follows.

Global_defs {
Router_id LVS_MASTER # global configuration, set the local id

Vrrp_script check_web {# define a check script, specifying the script file path and execution Interval
Script "/usr/local/keepalived/bin/"
Interval 2
Vrrp_instance VI_1 {
State MASTER # The slave server is in the master state. Uppercase is required
Interface eth0 # specify the port for virtual ip binding
Virtual_router_id 181 # name of the vrrp group. The master and slave must be the same and cannot be the same as other groups.
Priority 100 # specify the priority. The larger the value, the higher the priority.
Advert_int 1 # interval for sending multicast Information
Track_script {# Run the previously defined check script
Authentication {# verification information, two vertices are the same
Auth_type PASS
Auth_pass 1111
Virtual_ipaddress {# execute virtual IP Address

Virtual_server 80 {# corresponding port of the Virtual Server
Delay_loop 6 # Delay Time
Lb_algo rr # lvs Algorithm
Lb_kind DR # lvs Mode
Persistence_timeout 50 # timeout time. During this time, the same user sends multiple requests and is forwarded to the same server.
Protocol TCP # protocol used
Real_server 80 {# the ip address and port of the real host. The ip address on the slave server is 186, and the following is 185.
Weight 1 # weight, based on the distribution ratio of requests
Connect_timeout 3
Nb_get_retry 3
Delay_before_retry 3
Connect_port 80
Real_server 80 {

Weight 1
Connect_timeout 3
Nb_get_retry 3
Delay_before_retry 3
Connect_port 80

For the same configuration on the slave server, you only need to change the location marked in red. The real_server186 on the slave server should be written before 185.

Attached test script:

Check port 80 of the host. if the service is not enabled, port 80 is not monitored. The service will be restarted. After the service is restarted, The keepalived service is disabled.

#! /Bin/bash
A = 'lsof-I: 80 | wc-l'
If [$ a-eq 0]
Sleep 3
If ['lsof-I: 80 | wc-l'-eq 0]
Service keepalived stop

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: 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.