网站服务器双机,LVS-DR+Keepalived网站服务器双机热备配置(示例代码)

keepalived是一个类似于layer3, 4 & 7交换机制的软件,也就是我们平时说的第3层、第4层和第7层交换。Keepalived是自动完成,不需人工干涉。

调度服务器:

需要在LVS的基础上面做。

参考上两节:

负载均衡群集之一LVS-DR:http://blog.51cto.com/13572519/2084197

负载均衡群集之—LVS-NAT:http://blog.51cto.com/13572519/2084181

一块网卡:

Vmnet1:192.168.80.100

主与从调度器上都做下面的3步的:加载、安装ipvsadm包

下载地址:http://www.keepalived.org/download.html

[[email protected] ~]# modprobe ip_vs

[[email protected] ~]# rpm -ivh /mnt/Packages/ipvsadm-1.27-7.el7.x86_64.rpm

---------keepalived安装配置(主、从都要配置的)----

[[email protected] ~]# yum -y install popt-devel kernel-devel openssl-devel

WinSCP上传keepalived到主从服务器

02498d675844b862de9b9ccfaedf2a2f.png

[[email protected] ~]# tar fx keepalived-1.4.2.tar.gz -C /opt

[[email protected] opt]# cd /opt/keepalived-1.4.2/

[[email protected] keepalived-1.4.2]# yum -y install \

gcc \

gcc-c++ \

make

[[email protected] keepalived-1.4.2]# ./configure --prefix=/

[[email protected] keepalived-1.4.2]# make && make install

[[email protected] keepalived]# cp -r /opt/keepalived-1.4.2/keepalived/etc/keepalived/ /etc/init.d

[[email protected] keepalived]# systemctl enable keepalived

--------------------------------修改配置文件(主调度器)-------------

[[email protected] keepalived]# vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

notification_email {br/>[email protected]

[email protected]br/>[email protected]

}

notification_email_from [email protected]

smtp_server 192.168.200.1

smtp_connect_timeout 30

router_id LVS_01

}

vrrp_instance VI_1 {

state MASTER

interface ens33

virtual_router_id 51

priority 110

advert_int 1

authentication {

auth_type PASS

auth_pass 6666

}

virtual_ipaddress {

192.168.80.188

}

}

virtual_server 192.168.80.188 80 {

delay_loop 6

lb_algo rr

lb_kind DR

persistence_timeout 60

protocol TCP

real_server 192.168.80.101 80 {

weight 1

TCP_CHECK {

connect_port 80

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

}

}

real_server 192.168.80.102 80 {

weight 1

TCP_CHECK {

connect_port 80

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

}

}

}

[[email protected] keepalived]# service keepalived start

[[email protected] keepalived]# ip addr show dev ens33 //验证绑定了虚拟地址

inet 192.168.80.188/32 scope global ens33

[[email protected] keepalived]# ipvsadm -L

[[email protected] keepalived]# tail -f /var/log/messages //对业务服务器和主调度器进行关闭验证热备及健康检查效果。

MASTER STATE代表主服务器

BACKUP STATE代表备用服务器

---------------------------------修改配置文件(从调度器)-----------------

[[email protected] ~]# modprobe ip_vs

[[email protected] ~]# rpm -ivh /mnt/Packages/ipvsadm-1.27-7.el7.x86_64.rpm

[[email protected] ~]# yum -y install \

gcc \

gcc-c++ \

make

[[email protected] ~]# tar fx keepalived-1.4.2.tar.gz -C /opt

[[email protected] ~]# cd /opt/keepalived-1.4.2/

[[email protected] keepalived-1.4.2]# ./configure --prefix=/

[[email protected] keepalived-1.4.2]# make && make install

[[email protected] keepalived-1.4.2]# cp -r keepalived/etc/init.d/keepalived /etc/init.d/

[[email protected] keepalived-1.4.2]# systemctl enable keepalived

[[email protected] keepalived-1.4.2]# vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

notification_email {

[email protected]

}

notification_email_from benet [email protected]

smtp_server 127.0.0.1

smtp_connect_timeout 30

router_id LVS_02 //主从要不相同

}

vrrp_instance VI_1 {

state BACKUP //从调度器的热备状态

interface ens33

virtual_router_id 51 //编号主与从的两边要一样的

priority 105 //从调度器的优先级

advert_int 1

authentication { //主、从热备认证信息

auth_type PASS

auth_pass 6666

}

virtual_ipaddress { //指定群集VIP地址(虚拟服务器地址),可以指定多个的

192.168.80.188

}

}

virtual_server 192.168.80.188 80 { //虚拟服务器地址(VIP)、端口

delay_loop 6 //健康检查的间隔时间(秒)正常情况下。

lb_algo rr //轮询(rr)调度算法

lb_kind DR //直接路由(DR)群集工作模式

persistence_timeout 60 //连接保持时间,(秒),若注释掉加个"!"在这个时间内同一客户端的请求被同一服务器处理

protocol TCP //应用服务采用的是TCP协议

real_server 192.168.80.101 80 { //第一个web节点的地址、端口

weight 1 //节点的权重

TCP_CHECK { //健康检查方式

connect_port 80 //检查的目标端口

connect_timeout 3 //连接超时(秒)

nb_get_retry 3 //重试次数

delay_before_retry 3 //重试间隔(秒)

}

}

real_server 192.168.80.102 80 { //第二个web节点的地址、端口

weight 1

TCP_CHECK {

connect_port 80

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

}

}

}

[[email protected] keepalived-1.4.2]# service keepalived start

[[email protected] keepalived-1.4.2]# ip addr show dev ens33

[[email protected] keepalived-1.4.2]# tail -f /var/log/messages

MASTER STATE代表主服务器

BACKUP STATE代表备用服务器

f8ad4d80f84a4d96d3c4fdbfc31c603e.png

以下是WEB服务器:

192.168.80.101服务器:

[[email protected] ~]# yum install -y httpd

[[email protected] ~]# vi /etc/httpd/conf/httpd.confServerName www.aa.com

[[email protected] ~]# vi /var/www/html/index.html

Server aa

192.168.80.102服务器:

[[email protected] ~]# yum install -y httpd

[[email protected] ~]# vi /etc/httpd/conf/httpd.conf

ServerName www.bb.com

[[email protected] ~]# vi /var/www/html/index.html

Server bb

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值