Keeplived高可用多实例安装配置教程,keepalived负载均衡实战

Centos7.2系统下64位LVS+Keeplived高可用多实例安装部署教程,centos7.2lvs

Centos7.2系统下64位LVS+Keeplived高可用多实例安装部署教程

系统:centos7.2 需要的软件包:mysql-5.6.22.tar.gz

环境准备:服务器:192.10.30.10、服务器:192.10.30.11、服务器:192.10.30.12

一.实践环境

三台服务器,一台做lvs+keepalived,两台是lvs_web,因为只有三台机器环境测试所以lvs+keepalived没有做heartbeat来做备份

DB———————————192.10.30.12

Lvs_web01————————192.10.30.10

Lvs_web02————————192.10.30.11

Web_vip—————————192.10.30.39

Mycat_vip————————–192.10.30.49

二.安装步骤

0、安装LVS前系统需要安装

yum -y install openssl-devel lftplibnl* popt* libnl* libpopt*gcc*
libipset* makepcre pcre-devel kernel-devel libnl-devel popt-devel

1.安装ipvsadm

yum -y install ipvsadm

2.安装keepalived

yum -y install keepalived

  1. 配置keepalived启动:

#systemctl enable keepalived.service

#systemctl start keepalived.service

#systemctl status keepalived.service


2、开启路由转发(路由转发模式需要配置,NAT模式不需要配置此项)

vi /etc/sysctl.conf

修改下面的值,从0修改到1:

net.ipv4.ip_forward = 1

刷新系统变量,使系统文件变更马上生效

sysctl -p


3、配置Keepalived

vi /etc/keepalived/keepalived.conf

删除所有内容,在LVS-MASTER中输入以下内容:

[[email protected]
init.d]# cat /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

notification_email {

[email protected]

}

[email protected]

smtp_server 127.0.0.1

smtp_connect_timeout 30

router_id LVS_10

vrrp_skip_check_adv_addr

vrrp_strict

vrrp_garp_interval 0

vrrp_gna_interval 0

}

vrrp_instance VI_1 {

state BACKUP

interface eno16777728

virtual_router_id 51

priority 90

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

192.10.30.39/24 dev eno16777728 labeleno16777728:1

}

}

virtual_server 192.10.30.39 80 {

delay_loop 6

lb_algo wrr

lb_kind DR

nat_mask 255.255.255.0

persistence_timeout50

protocol TCP

real_server192.10.30.10 80 {

weight 1

TCP_CHECK {

connect_timeout8

nb_get_retry 3

delay_before_retry3

connect_port 80

}

}

real_server 192.10.30.1180 {

weight 1

TCP_CHECK {

connect_timeout8

nb_get_retry 3

delay_before_retry3

connect_port 80

}

}

}

vrrp_instance VI_2 {#只是在这里额外添加了一个模块

state MASTER

interface eno16777728

virtual_router_id 52

priority 90

advert_int 1

authentication {

auth_type PASS

auth_pass 2222

}

virtual_ipaddress {

192.10.30.49/24 deveno16777728 label eno16777728:2

}

}

virtual_server 192.10.30.49 8066 {

delay_loop 6

lb_algo rr

lb_kind DR

nat_mask 255.255.255.0

persistence_timeout50

protocol TCP

real_server 192.10.30.108066 {

weight 1

TCP_CHECK {

connect_timeout8

nb_get_retry 3

delay_before_retry3

connect_port 8066

}

}

real_server192.10.30.11 8066 {

weight 1

TCP_CHECK {

connect_timeout8

nb_get_6165金沙总站,retry 3

delay_before_retry3

connect_port 8066

}

}

}

[[email protected]
init.d]#

删除所有内容,在LVS-BACKUP中输入以下内容:

[[email protected]
mycat]# cat /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

notification_email {

[email protected]

}

[email protected]

smtp_server 127.0.0.1

smtp_connect_timeout 30

router_id LVS_10

vrrp_skip_check_adv_addr

vrrp_strict

vrrp_garp_interval 0

vrrp_gna_interval 0

}

vrrp_instance VI_1 {

state MASTER

interface eno16777728

virtual_router_id 51

priority 100

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

192.10.30.39/24dev eno16777728 label eno16777728:1

}

}

virtual_server 192.10.30.39 80 {

delay_loop 6

lb_algo wrr

lb_kind DR

nat_mask 255.255.255.0

persistence_timeout50

protocol TCP

real_server192.10.30.10 80 {

weight 1

TCP_CHECK {

connect_timeout8

nb_get_retry 3

delay_before_retry3

connect_port 80

}

}

real_server192.10.30.11 80 {

weight 1

TCP_CHECK {

connect_timeout8

nb_get_retry 3

delay_before_retry3

connect_port 80

}

}

}

vrrp_instance VI_2 {#只是在这里额外添加了一个模块

state BACKUP

interface eno16777728

virtual_router_id 52

priority 90

advert_int 1

authentication {

auth_type PASS

auth_pass 2222

}

virtual_ipaddress {

192.10.30.49/24 deveno16777728 label eno16777728:2

}

}

virtual_server 192.10.30.49 8066 {

delay_loop 6

lb_algo rr

lb_kind DR

nat_mask 255.255.255.0

persistence_timeout50

protocol TCP

real_server192.10.30.10 8066 {

weight 1

TCP_CHECK {

connect_timeout8

nb_get_retry 3

delay_before_retry3

connect_port 8066

}

}

real_server192.10.30.11 8066 {

weight 1

TCP_CHECK {

connect_timeout8

nb_get_retry 3

delay_before_retry3

connect_port 8066

}

}

}

[[email protected]
mycat]#

4.自动启动keepalived:

[[email protected]
ipvsadm-1.26]# chkconfig –list | grep keepalived

[[email protected]
ipvsadm-1.26]# chkconfig –add keepalived

[[email protected]
ipvsadm-1.26]# chkconfig –level 235 keepalived on

[[email protected]
ipvsadm-1.26]# chkconfig –list | grep keepalived

#service keepalived start

===下面的脚本是在WEB服务器上执行的=====================================================

在WEB1和WEB2机器上编辑如下文件

#!/bin/bash

# description: Config realserver

#Written by :

VIP0=192.10.30.39

VIP1=192.10.30.49

/etc/rc.d/init.d/functions

case “$1” in

start)

/sbin/ifconfig lo:0$VIP0 broadcast $VIP0 netmask 255.255.255.255 up

/sbin/ifconfig lo:1$VIP1 broadcast $VIP1 netmask 255.255.255.255 up

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

sysctl -p>/dev/null 2>&1

echo”RealServer Start OK”

;;

stop)

/sbin/ifconfig lo:0down

/sbin/ifconfig lo:1 down

echo “0”>/proc/sys/net/ipv4/conf/lo/arp_ignore

echo “0”>/proc/sys/net/ipv4/conf/lo/arp_announce

echo “0”>/proc/sys/net/ipv4/conf/all/arp_ignore

echo “0”>/proc/sys/net/ipv4/conf/all/arp_announce

echo”RealServer Stoped”

;;

*)

echo “Usage:$0 {start|stop}”

exit 1

esac

#exit 0

[[email protected]
init.d]#

赋给执行权限

chmod 755/etc/rc.d/init.d/functions
别人的配置文档中没这个步骤,我不知道为什么总是说执行到这句时没有权限,所以我添加了执行权限

#chmod 755 /etc/init.d/lvs_real.sh

#/etc/init.d/lvs_real.shstart

添加自动启动

echo “/etc/init.d/lvs_real.shstart” >> /etc/rc.local

==============================================================

配置两台LVS_web的apache服务,至此结束

三.测试服务

1.启动两台lvs_web的apache服务

2.启动lvs_keepalived服务器的keepalived和lvs的服务

3.访问

4.停到一台lvs_web的apache服务,在访问

Centos7.2系统下64位LVS+Keeplived高可用多实例安装部署教程 系统:centos7.2
需要…

LVS+keepalived负载均衡实战

       ———————这里非常感谢田逸老师的文档,在我学习lvs的时候给了我很大的帮助!

一、为什么要使用负载均衡技术?

1、系统高可用性

2、  系统可扩展性

3、  负载均衡能力

LVS+keepalived能很好的实现以上的要求,LVS提供负载均衡,keepalived提供健康检查,故障转移,提高系统的可用性!采用这样的架构以后很容易对现有系统进行扩展,只要在后端添加或者减少realserver,只要更改lvs的配置文件,并能实现无缝配置变更!

二、LVS+Keepalived介绍

1、  LVS

LVS是一个开源的软件,可以实现LINUX平台下的简单负载均衡。LVS是Linux Virtual Server的缩写,意思是Linux虚拟服务器。目前有三种IP负载均衡技术(VS/NAT、VS/TUN和VS/DR);八种调度算法(rr,wrr,lc,wlc,lblc,lblcr,dh,sh)。

2、  keepalived

Keepalived
是运行在lvs 之上,它的主要功能是实现真实机的故障隔离及负载均衡器间的失败切换,提高系统的可用性

三、LVS+keepalived负载均衡架构图

 
6165金沙总站 1

四、LVS+keepalived的安装和配置

1.        
配置环境

              System OS:CentOS release
5.4

              Software:ipvsadm-1.24.tar.gz, keepalived-.tar.gz

2.        
信息列表

名称

IP

 LVS-Master

192.168.1.112

 LVS-BACKUP

192.168.1.113

 LVS-VIP

192.168.1.115

 Realserver1

192.168.1.105

 Realserver2

192.168.1.103

Realserver3

192.168.1.104

3.        
安装lvs

分别在backup
lvs和master lvs上安装

wget

ln -s /usr/src/kernels/-164.el5-i686/
/usr/src/linux

tar zxvf ipvsadm-1.24.tar.gz

cd ipvsadm-1.24

make && make install

4.        
安装keepalived

分别在backup
lvs和master lvs上安装

wget

tar zxvf keepalived-1.1.19.tar.gz
cd keepalived-1.1.19
./configure –prefix=/usr/local/keepalived
make
make install
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
mkdir /etc/keepalived

5.        
配置keepadlived

LVS-Master的配置文件如下

[root@linux5 ~]# cat
/etc/keepalived/keepalived.conf

global_defs {

notification_email {

               jimo291@gmail.com    #email 通知

        }

        notification_email_from
jimo291@gmail.com

        smtp_server 127.0.0.1

        smtp_connect_timeout 30

        router_id LVS1          # 设置lvs的id,在一个网络内应该是唯一的

}

vrrp_sync_group test {           #设置vrrp组

group {

        loadbalance

}

}

vrrp_instance loadbalance {

        state MASTER       #设置lvs的状态,报错MASTER和BACKUP两种,必须大写

        interface eth0     #设置对外服务的接口

        lvs_sync_daemon_inteface eth0  
#设置lvs监听的接口

        virtual_router_id 51                    
#设置虚拟路由表示

        priority 180            #设置优先级,数值越大,优先级越高

        advert_int 5           #设置同步时间间隔

authentication {                    #设置验证类型和密码

        auth_type PASS

        auth_pass 1111

}

virtual_ipaddress {                #设置lvs vip

        192.168.1.115

}

}

virtual_server 192.168.1.115 80 {

        delay_loop 6          #健康检查时间间隔

        lb_algo rr               #负载均衡调度算法

        lb_kind DR            #负载均衡转发规则

        #persistence_timeout 20  #设置会话保持时间,对bbs等很有用

        protocol TCP                #协议

        real_server 192.168.1.105 80 {

        weight 3                #设置权重

        TCP_CHECK {

        connect_timeout 3

        nb_get_retry 3

        delay_before_retry 3

        connect_port 80

}

}

        real_server 192.168.1.103 80 {

        weight 3

        TCP_CHECK {

        connect_timeout 3

        nb_get_retry 3

        delay_before_retry 3

        connect_port 80

}

}

        real_server 192.168.1.104 80 {

        weight 3

        TCP_CHECK {

        connect_timeout 3

        nb_get_retry 3

        delay_before_retry 3

        connect_port 80

}

}

}

LVS-backup的配置文件如下

global_defs {

notification_email {

               jimo291@gmail.com

        }

        notification_email_from
jimo291@gmail.com

        smtp_server 127.0.0.1

        smtp_connect_timeout 30

        router_id LVS2

}

vrrp_sync_group test {

group {

        loadbalance

}

}

vrrp_instance loadbalance {

        state BACKUP

        interface eth0

        lvs_sync_daemon_inteface eth0

        virtual_router_id 51

        priority 150

        advert_int 5

authentication {

        auth_type PASS

        auth_pass 1111

}

virtual_ipaddress {

        192.168.1.115

}

}

virtual_server 192.168.1.115 80 {

        delay_loop 6

        lb_algo rr

        lb_kind DR

        #persistence_timeout 20

        protocol TCP

        real_server 192.168.1.105 80 {

        weight 3

        TCP_CHECK {

        connect_timeout 3

        nb_get_retry 3

        delay_before_retry 3

        connect_port 80

}

}

        real_server 192.168.1.103 80 {

        weight 3

        TCP_CHECK {

        connect_timeout 3

        nb_get_retry 3

        delay_before_retry 3

        connect_port 80

}

}

        real_server 192.168.1.104 80 {

        weight 3

        TCP_CHECK {

        connect_timeout 3

        nb_get_retry 3

        delay_before_retry 3

        connect_port 80

}

}

}

      

6.        
Realserver的配置

三台客户端的脚本都一样!

cat /etc/rc.d/init.d/realserver.sh

#!/bin/bash

# description: Config realserver lo and apply
noarp

SNS_VIP=192.168.1.115

/etc/rc.d/init.d/functions

case “$1” in

start)

       ifconfig lo:0 $SNS_VIP netmask
255.255.255.255 broadcast $SNS_VIP

       /sbin/route add -host $SNS_VIP dev
lo:0

       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

       sysctl -p >/dev/null 2>&1

       echo “RealServer Start OK”

       ;;

stop)

       ifconfig lo:0 down

       route del $SNS_VIP >/dev/null
2>&1

       echo “0”
>/proc/sys/net/ipv4/conf/lo/arp_ignore

       echo “0”
>/proc/sys/net/ipv4/conf/lo/arp_announce

       echo “0”
>/proc/sys/net/ipv4/conf/all/arp_ignore

       echo “0”
>/proc/sys/net/ipv4/conf/all/arp_announce

       echo “RealServer Stoped”

       ;;

*)

       echo “Usage: $0 {start|stop}”

       exit 1

esac

exit 0

7.        
测试

1)       
首先测试各个readserver,确定各个readserver都能正常访问,测试realserver.sh脚本,看启动后能不能绑定lvs vip,停止后能不能去除绑定的vip

2)       
测试lvs能否进行负载均衡转发,利用ipvsadm命令查看,如果想只当当前的请求转发到哪个服务器去了,可以用ipvsadm加选项来查看,其完整形式为:ipvsadm –lcn | grep
192.168.1.115

3)       
停掉主lvs看lvs backup是否接管!

4)       
更详细信息请查看日志,tail -f /var/log/messages