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
3. 配置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中输入以下内容:
[root@PC11 init.d]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
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_retry 3
delay_before_retry3
connect_port 8066
}
}
}
[root@PC11 init.d]#
删除所有内容,在LVS-BACKUP中输入以下内容:
[root@PC10 mycat]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
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
}
}
}
[root@PC10 mycat]#
4.自动启动keepalived:
[root@hx192 ipvsadm-1.26]# chkconfig --list | grep keepalived
[root@hx192 ipvsadm-1.26]# chkconfig --add keepalived
[root@hx192 ipvsadm-1.26]# chkconfig --level 235 keepalived on
[root@hx192 ipvsadm-1.26]# chkconfig --list | grep keepalived
#service keepalived start
===下面的脚本是在WEB服务器上执行的=====================================================
在WEB1和WEB2机器上编辑如下文件
#!/bin/bash
# description: Config realserver
#Written by : http://kerry.blog.51cto.com
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
[root@PC10 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.访问http://192.10.30.39/并不断刷新页面开是否平均落到两台服务器上
4.停到一台lvs_web的apache服务,在访问http://192.10.30.39,看是否不是给停掉的lvs_web分请求,如果是测试成功,一切结束!!!