听闻少年二字,当与平庸相斥。这篇文章主要讲述LVS-DR模式集群构建相关的知识,希望能为你提供帮助。
实验示意图:
文章图片
一、配置负载均衡器LVS-Master& Slave:
关闭网卡守护进程
[root@localhost ~]# service NetworkManager stop
Stopping NetworkManager daemon:[OK]
[root@localhost ~]# chkconfig NetworkManager off
拷贝eth0网卡子接口充当集群入口接口
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# ls
ifcfg-eth0ifdown-ethifdown-pppifup-aliasesifup-isdnifup-routesnet.hotplug
ifcfg-eth1ifdown-ipppifdown-routesifup-bnepifup-plipifup-sitnetwork-functions
ifcfg-loifdown-ipv6ifdown-sitifup-ethifup-plusbifup-tunnelnetwork-functions-ipv6
ifdownifdown-isdnifdown-tunnelifup-ipppifup-postifup-wireless
ifdown-bnepifdown-postifupifup-ipv6ifup-pppinit.ipv6-global
[root@localhost network-scripts]# cp ifcfg-eth0 ifcfg-eth0:0
[root@localhost network-scripts]# vim ifcfg-eth0:0
DEVICE=eth0:0
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=static
IPADDR=100.100.100.100
NETMASK=255.255.255.0
[root@localhost network-scripts]# ifup ifcfg-eth0:0
关闭网卡重定向功能
[root@localhost ~]# vim /etc/sysctl.conf
# 在文件末尾添加下列三行
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.eth0.send_redirects = 0
# 重新加载系统参数
[root@localhost ~]# sysctl -p
安装ipvsadm命令行管理工具
[root@localhost ~]# mount -t iso9660 /dev/cdrom /mnt/cdrom/
mount: block device /dev/sr0 is write-protected, mounting read-only
[root@localhost ~]# yum -y install ipvsadm
.......
?
# 添加ipvs TCP集群
[root@localhost ~]# ipvsadm -A -t 100.100.100.100:80 -s rr
?
# 添加ipvsadm集群子节点
[root@localhost ~]# ipvsadm -a -t 100.100.100.100:80 -r 100.100.100.13:80 -g
[root@localhost ~]# ipvsadm -a -t 100.100.100.100:80 -r 100.100.100.14:80 -g
?
# 查看集群列表
[root@localhost ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:PortForward Weight ActiveConn InActConn
TCP100.100.100.100:80 rr
-> 100.100.100.13:80Route100
-> 100.100.100.14:80Route100
?
# 查看集群访问次数
[root@localhost ~]# ipvsadm -Ln --stats
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:PortConnsInPktsOutPktsInBytes OutBytes
-> RemoteAddress:Port
TCP100.100.100.100:801472098440
-> 100.100.100.13:80737049000
-> 100.100.100.14:80735049440
?
# 保存ipvs集群内容至文件,进行持久化存储
[root@localhost ~]# service ipvsadm save
ipvsadm: Saving IPVS table to /etc/sysconfig/ipvsadm:[OK]
[root@localhost ~]# chkconfig ipvsadm on
二、配置真实服务器RS1
关闭网卡守护进程
[root@localhost ~]# service NetworkManager stop
Stopping NetworkManager daemon:[OK]
[root@localhost ~]# chkconfig NetworkManager off
拷贝回环网卡子接口
[root@localhost html]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-lo ifcfg-lo:0
[root@localhost network-scripts]# vim ifcfg-lo:0
DEVICE=lo:0
IPADDR=100.100.100.100
NETMASK=255.255.255.255
NETWORK=127.0.0.0
# If you\'re having problems with gated making 127.0.0.0/8 a martian,
# you can change this to something else (255.255.255.255, for example)
BROADCAST=127.255.255.255
ONBOOT=yes
NAME=loopback
关闭对应ARP响应及公告功能
[root@localhost ~]# vim /etc/sysctl.conf
# 在配置文件末尾添加下列内容
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
# 重新加载系统内核参数
[root@localhost ~]# sysctl -p
net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
net.bridge.bridge-nf-call-ip6tables = 0
net.bridge.bridge-nf-call-iptables = 0
net.bridge.bridge-nf-call-arptables = 0
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
# 开启回环子接口
[root@localhost ~]# ifup lo:0
添加路由记录
[root@localhost ~]# route add -host 100.100.100.100 dev lo:0
[root@localhost ~]# service httpd start
Starting httpd:
# 保存路由记录到开机启动项中
[root@localhost ~]# echo "route add -host 100.100.100.100 dev lo:0" > > /etc/rc.local
# 查看路由条目
[root@localhost ~]# route -n
Kernel IP routing table
DestinationGatewayGenmaskFlags Metric RefUse Iface
100.100.100.100 0.0.0.0255.255.255.255 UH000 lo
100.100.100.00.0.0.0255.255.255.0U100 eth2
# 开启回环子接口
[root@localhost ~]# ifup lo:0
添加web服务并部署一个简易网站
[root@localhost ~]# service httpd start
Starting httpd: httpd: Could not reliably determine the server\'s fully qualified domain name, using localhost.localdomain for ServerName
[OK]
[root@localhost ~]# chkconfig httpd on
[root@localhost ~]# echo "131111111111111111" > > /var/www/html/index.html
[root@localhost ~]# curl localhost
131111111111111111
[root@localhost ~]#
三、配置真实服务器RS2
关闭网卡守护进程
[root@localhost ~]# service NetworkManager stop
Stopping NetworkManager daemon:[OK]
[root@localhost ~]# chkconfig NetworkManager off
拷贝回环网卡子接口
[root@localhost html]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp ifcfg-lo ifcfg-lo:0
[root@localhost network-scripts]# vim ifcfg-lo:0
DEVICE=lo:0
IPADDR=100.100.100.100
NETMASK=255.255.255.255
NETWORK=127.0.0.0
# If you\'re having problems with gated making 127.0.0.0/8 a martian,
# you can change this to something else (255.255.255.255, for example)
BROADCAST=127.255.255.255
ONBOOT=yes
NAME=loopback
关闭对应ARP响应及公告功能
[root@localhost ~]# vim /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
# 重新加载系统参数
[root@localhost ~]# sysctl -p
net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
net.bridge.bridge-nf-call-ip6tables = 0
net.bridge.bridge-nf-call-iptables = 0
net.bridge.bridge-nf-call-arptables = 0
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
# 开启回环子接口
[root@localhost ~]# ifup lo:0
添加路由记录,当访问VIP时交给lo:0网卡接收
[root@localhost ~]# route add -host 100.100.100.100 dev lo:0
[root@localhost ~]# service httpd start
Starting httpd:
# 保存路由记录到开机启动项中
[root@localhost ~]# echo "route add -host 100.100.100.100 dev lo:0" > > /etc/rc.local
# 查看路由条目
[root@localhost ~]# route -n
Kernel IP routing table
DestinationGatewayGenmaskFlags Metric RefUse Iface
100.100.100.100 0.0.0.0255.255.255.255 UH000 lo
100.100.100.00.0.0.0255.255.255.0U100 eth2
# 开启回环子接口
[root@localhost ~]# ifup lo:0
四、LVS-Master安装keepalived
检查keepalived所需要的依赖包是否已经安装
# (安装keepalived需要popt支持)
[root@localhost ~]# rpm -qa | grep popt
#(安装keepalived需要openssl支持)
[root@localhost ~]# rpm -qa | grep openssl
注意:如不知道keepalived需要哪些依赖包,可到下载后的源码解压目录下查看INSTALL 文件内容,安装需要的依赖包,源码安装任何一个软件都要养成查看源码包文档的习惯,比如INSTALL,README,doc等文档,可以获得很多有用的信息
源码编译安装keepalived
# 安装keepalived相关依赖
[root@localhost ~]# yum -y install kernel-devel openssl-devel popt-devel gcc*
[root@localhost ~]# tar zxvf keepalived-2.1.5.tar.gz
[root@localhost ~]# cd keepalived-2.1.5
[root@localhost keepalived-2.1.5]# ./configure --prefix=/ --with-kernel-dir=/usr/src/kernels/2.6.32-504.el6.x86_64/
[root@localhost keepalived-2.1.5]# make
.......
.......
[root@localhost keepalived-2.1.5]# make install
.......
对keepalived进行相关优化
[root@localhost ~]# whereis keepalived
keepalived: /sbin/keepalived /usr/sbin/keepalived /etc/keepalived /usr/local/keepalived
[root@localhost ~]# cp -a keepalived-2.1.5/keepalived/etc/init.d/keepalived /etc/init.d/
安装keepalived服务并开机自启
[root@localhost ~]# chkconfig --add keepalived
[root@localhost ~]# chkconfig --list keepalived
keepalived0:off1:off2:off3:off4:off5:off6:off
[root@localhost ~]# service keepalived start
Starting keepalived:[OK]
[root@localhost ~]# chkconfig keepalived on
修改keepalived.conf
[root@localhost ~]# vim /etc/keepalived/keepalived.conf
global_defs {
router_id R1
}
?
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 66
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
100.100.100.100
}
}
?
virtual_server 100.100.100.100 80 {
delay_loop 2
lb_algo rr
lb_kind DR
protocol TCP
?
real_server 100.100.100.13 80 {
weight 1
TCP_CHECK {
connect_port 80
connetc_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
real_server 100.100.100.14 80 {
weight 1
TCP_CHECK {
connect_port 80
connetc_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
}
?
五、LVS-SLAVE安装keepalived
源码编译安装keepalived
安装keepalived相关依赖
?
[root@localhost ~]# yum -y install kernel-devel openssl-devel popt-devel gcc*
[root@localhost ~]# tar zxvf keepalived-2.1.5.tar.gz
[root@localhost ~]# cd keepalived-2.1.5
[root@localhost keepalived-2.1.5]# ./configure --prefix=/ --with-kernel-dir=/usr/src/kernels/2.6.32-504.el6.x86_64/
[root@localhost keepalived-2.1.5]# make
.......
.......
[root@localhost keepalived-2.1.5]# make install
.......
?
对keepalived进行相关优化
[root@localhost ~]# whereis keepalived
keepalived: /sbin/keepalived /usr/sbin/keepalived /etc/keepalived /usr/local/keepalived
[root@localhost ~]# cp -a keepalived-2.1.5/keepalived/etc/init.d/keepalived /etc/init.d/
安装keepalived服务并开机自启
[root@localhost ~]# chkconfig --add keepalived
[root@localhost ~]# chkconfig --list keepalived
keepalived0:off1:off2:off3:off4:off5:off6:off
[root@localhost ~]# service keepalived start
Starting keepalived:[OK]
[root@localhost ~]# chkconfig keepalived on
修改keepalived.conf
[root@localhost ~]# vim /etc/keepalived/keepalived.conf
?
! Configuration File for keepalived
?
global_defs {
router_id R2
}
?
vrrp_instance VI_1 {
state SLAVE
interface eth0
virtual_router_id 66
priority 20
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
100.100.100.100
}
}
?
virtual_server 100.100.100.100 80 {
delay_loop 2
lb_algo rr
lb_kind DR
protocol TCP
?
real_server 100.100.100.13 80 {
weight 1
TCP_CHECK {
connect_port 80
connetc_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
?
real_server 100.100.100.14 80 {
weight 1
TCP_CHECK {
connect_port 80
connetc_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
}
?
六、访问web网站地址
访问web页面
文章图片
文章图片
观察主负载均衡调度器LVS-Master状态 刷新前:
[root@localhost ~]# ipvsadm -Ln --stats
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:PortConnsInPktsOutPktsInBytes OutBytes
-> RemoteAddress:Port
TCP100.100.100.100:8019990133950
-> 100.100.100.13:801507100
-> 100.100.100.14:80213015520
刷新web网站页面后:
[root@localhost ~]# ipvsadm -Ln --stats
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:PortConnsInPktsOutPktsInBytes OutBytes
-> RemoteAddress:Port
TCP100.100.100.100:80281440197850
-> 100.100.100.13:80630042600
-> 100.100.100.14:80633043920
假设LVS-Master有故障宕机,通过关闭网卡模拟宕机
关闭主负载均衡调度器的网卡ifcfg-eth0:
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# ifdown ifcfg-eth0
关闭后,仍可访问网页,说明从负载均衡调度器在起作用。
观察从负载均衡调度器LVS-Slave状态 刷新页面前:
[root@localhost network-scripts]# ipvsadm -Ln --stats
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:PortConnsInPktsOutPktsInBytes OutBytes
-> RemoteAddress:Port
TCP100.100.100.100:80211070148140
-> 100.100.100.13:801052070300
-> 100.100.100.14:801155077840
刷新页面后:
[root@localhost network-scripts]# ipvsadm -Ln --stats
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:PortConnsInPktsOutPktsInBytes OutBytes
-> RemoteAddress:Port
TCP100.100.100.100:80311580218750
-> 100.100.100.13:8015780106320
-> 100.100.100.14:8016800112430
通过观察Conns数值,可以知道宕机情况下,从负载均衡调度器起作用了,使仍可访问网站。
【LVS-DR模式集群构建】
推荐阅读
- VMware系列--创建Ubuntu
- 文件系统的几种类型(ext3, s…)
- Altium Designer许可告急,这样的许可优化方案才是最优选择!!!
- 我可以对内容使用max-width,但对背景使用全角吗()
- 我可以使页面在wordpress中显示为博客文章吗()
- 我可以在WordPress中使用其它插件代替主题的默认插件吗
- 我可以更改wordpress主题中弹出的登录窗口吗()
- 我可以在wordpress的自定义链接菜单上添加onClick()事件吗()
- WordPress post可以显示其他自定义post吗()