千家信息网

DR模式搭建LVSkeepalived-多web应用

发表于:2025-01-23 作者:千家信息网编辑
千家信息网最后更新 2025年01月23日,服务器划分注:测试环境因此nginx和Apache都部署在同一台real server上Master和backup和real server服务器公用。VIP172.20.0.210Master172.
千家信息网最后更新 2025年01月23日DR模式搭建LVSkeepalived-多web应用

服务器划分

注:测试环境因此nginxApache都部署在同一台real server

Masterbackupreal server服务器公用。

VIP

172.20.0.210

Master

172.20.0.22

Backup

172.20.0.25

Real_server

172.20.0.22

Real_server

172.20.0.25

Web :nginx

port:8220

Web :Apache

port:8221

Masterbackup部署

安装ipvsadm

创建相应的目录

[root@SALT1 ~]# mkdir software

[root@SALT1 ~]# cd software/

下载安装包:

相应的命令为:

wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz

查看内核参数

[root@SALT1 software]# uname -a

Linux SALT1 2.6.32-431.el6.x86_64 #1 SMP Sun Nov 10 22:19:54 EST 2013 x86_64 x86_64 x86_64 GNU/Linux

创建软连接,不创建此链接,ipvsadm编译安装不过去。

[root@SALT1 software]# ln -s /usr/src/kernels/2.6.32-431.el6.x86_64/ /usr/src/linux

编译安装ipvsadm

[root@SALT1 software]# ls

ipvsadm-1.24.tar.gz

[root@SALT1 software]# tar -xvf ipvsadm-1.24.tar.gz

ipvsadm-1.24/config_stream.c

ipvsadm-1.24/config_stream.h

…….

[root@SALT1 software]# cd ipvsadm-1.24

[root@SALT1 ipvsadm-1.24]# make && make install

make -C libipvs

make[1]: Entering directory `/root/software/ipvsadm-1.24/libipvs'

gcc -Wall -Wunused -Wstrict-prototypes -g -O2 -I/usr/src/linux/include -DHAVE_NET_IP_VS_H

……..

加载LVS IP_VS模块

[root@ccb log]# ipvsadm

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

-> RemoteAddress:Port Forward Weight ActiveConn InActConn

[root@ccb log]# lsmod |grep ip_vs

ip_vs 115643 0

libcrc32c 1246 1 ip_vs

ipv6 321422 31 ip_vs

安装keepalived

下载安装包

相应的命令为:

[root@SALT1 software]# wget http://www.keepalived.org/software/keepalived-1.1.19.tar.gz

--2016-07-13 14:09:03-- http://www.keepalived.org/software/keepalived-1.1.19.tar.gz

Resolving www.keepalived.org... 37.59.63.157, 2001:41d0:8:7a9d::1

Connecting to www.keepalived.org|37.59.63.157|:80... connected.

HTTP request sent, awaiting response... 200 OK

Length: 241437 (236K) [application/x-gzip]

Saving to: "keepalived-1.1.19.tar.gz"

100%[===================================================================================================================>] 241,437 190K/s in 1.2s

2016-07-13 14:09:06 (190 KB/s) - "keepalived-1.1.19.tar.gz" saved [241437/241437]

编译安装keepalived

[root@SALT1 software]# tar -xvf keepalived-1.1.19.tar.gz

keepalived-1.1.19/

keepalived-1.1.19/config.log

keepalived-1.1.19/configure

keepalived-1.1.19/README

keepalived-1.1.19/ChangeLog

keepalived-1.1.19/Makefile.in

……..

[root@SALT1 software]# cd keepalived-1.1.19

[root@SALT1 keepalived-1.1.19]# ./configure --prefix=/usr/local/keepalived

checking for gcc... gcc

……

[root@SALT1 keepalived-1.1.19]#

[root@SALT1 keepalived-1.1.19]# make

make -C lib || exit 1;

make[1]: Entering directory `/root/software/keepalived-1.1.19/lib'

gcc -g -O2 -I. -Wall -Wunused -Wstrict-prototypes -c memory.c

gcc -g -O2 -I. -Wall -Wunused -Wstrict-prototypes -c utils.c

……

Make complete

[root@SALT1 keepalived-1.1.19]# make install

make -C keepalived install

make[1]: Entering directory `/root/software/keepalived-1.1.19/keepalived'

install -d /usr/local/keepalived/sbin

……

创建相应的目录

[root@ccb keepalived-1.1.19]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

[root@ccb keepalived-1.1.19]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

[root@ccb keepalived-1.1.19]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/

[root@ccb keepalived-1.1.19]# mkdir /etc/keepalived/

[root@ccb keepalived-1.1.19]#cd

[root@ccb ~]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/


至此lvskeepalived安装完成。


Keepalived配置

Master配置:

[root@localhost ~]# vi /etc/keepalived/keepalived.conf

#global define

global_defs {

router_id node-A

}

vrrp_sync_group test {

group {

test

}

}

vrrp_instance test {

state MASTER

interface eth2

lvs_sync_daemon_interface eth2

virtual_router_id 50

priority 180

advert_int 3

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

172.20.0.210

}

}

virtual_server 172.20.0.210 8220 {

delay_loop 3

lb_algo rr

lb_kind DR

persistence_timeout 1

protocol TCP

real_server 172.20.0.22 8220 {

weight 1

TCP_CHECK {

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

connect_port 8220

}

}

real_server 172.20.0.25 8220 {

weight 1

TCP_CHECK {

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

connect_port 8220

}

}

}

virtual_server 172.20.0.210 8221 {

delay_loop 3

lb_algo rr

lb_kind DR

persistence_timeout 1

protocol TCP

real_server 172.20.0.25 8221 {

weight 1

TCP_CHECK {

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

connect_port 8221

}

}

real_server 172.20.0.22 8221 {

weight 1

TCP_CHECK {

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

connect_port 8221

}

}

}

Backup配置


[root@localhost ~]# vi /etc/keepalived/keepalived.conf

#global define

global_defs {

router_id node-B

}

vrrp_sync_group test {

group {

test

}

}

vrrp_instance test {

state BACKUP

interface eth2

lvs_sync_daemon_interface eth2

virtual_router_id 50

priority 150

advert_int 3

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

172.20.0.210

}

}

virtual_server 172.20.0.210 8220 {

delay_loop 3

lb_algo rr

lb_kind DR

persistence_timeout 1

protocol TCP

real_server 172.20.0.22 8220 {

weight 1

TCP_CHECK {

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

connect_port 8220

}

}

real_server 172.20.0.25 8220 {

weight 1

TCP_CHECK {

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

connect_port 8220

}

}

}

virtual_server 172.20.0.210 8221 {

delay_loop 3

lb_algo rr

lb_kind DR

persistence_timeout 1

protocol TCP

real_server 172.20.0.25 8221 {

weight 1

TCP_CHECK {

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

connect_port 8221

}

}

real_server 172.20.0.22 8221 {

weight 1

TCP_CHECK {

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

connect_port 8221

}

}

}

Web应用配置:

确认启动nginxapache

[root@localhost sbin]# lsof -i:8220

COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME

nginx 22005 root 6u IPv4 86789 0t0 TCP *:8220 (LISTEN)

nginx 22006 nobody 6u IPv4 86789 0t0 TCP *:8220 (LISTEN)

[root@localhost sbin]# lsof -i:8221

COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME

httpd 22012 root 4u IPv6 86820 0t0 TCP *:8221 (LISTEN)

httpd 22015 apache 4u IPv6 86820 0t0 TCP *:8221 (LISTEN)

httpd 22016 apache 4u IPv6 86820 0t0 TCP *:8221 (LISTEN)

httpd 22017 apache 4u IPv6 86820 0t0 TCP *:8221 (LISTEN)

httpd 22018 apache 4u IPv6 86820 0t0 TCP *:8221 (LISTEN)

httpd 22019 apache 4u IPv6 86820 0t0 TCP *:8221 (LISTEN)

httpd 22020 apache 4u IPv6 86820 0t0 TCP *:8221 (LISTEN)

httpd 22021 apache 4u IPv6 86820 0t0 TCP *:8221 (LISTEN)

httpd 22022 apache 4u IPv6 86820 0t0 TCP *:8221 (LISTEN)

在两台real_server上分别创建lvsDR_node脚本:

[root@localhost ~]# vi /etc/init.d/lvsDR_node

#!/bin/bash

# descript : start real server DR

VIP=172.20.0.210

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

case "$1" in

start)

echo "start LVS of RealServer DR"

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

echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore

echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce

;;

stop)

/sbin/ifconfig lo:0 down

echo "close LVS of RealServer DR"

echo "0" > /proc/sys/net/ipv4/conf/all/arp_ignore

echo "0" > /proc/sys/net/ipv4/conf/all/arp_announce

;;

*)

echo "Usage : $0 {start|stop}"

exit 1

esac

启动keepalived:

分别启动masterbackup服务器上面的keepalived:

[root@localhost ~]# /etc/init.d/keepalived start

Starting keepalived: [ OK ]

[root@localhost ~]#

启动real_server上面的lvsDR_node

[root@localhost ~]# /etc/init.d/lvsDR_node start

start LVS of RealServer DR

验证:

查看启动状态:

Master



backup

Web切换验证

Apache验证


Nginx验证:


现在VIP是在master服务器(172.20.0.22)服务器上,为了确保web应用切换之后应用也能正常访问,将master服务器上的nginxapache停掉。:


可以看到22服务器上nginxApache都不能访问了

Nginx:


Apache:


查看状态:


使用VIP访问:




如上图可以看到web应用切换之后web应用依然可以通过VIP正常访问。

VIP切换验证:

切换VIP

Master:


Backup

Nginx验证:


Apache验证:


注:当master上的keepalived再次启动,VIP又会立刻切换到master上,而backup会自动切换为backup state

如图:


0