二:项目案例-如图
LVS/DR+Keepalived 实现lvs高可用,并提供web服务负载均衡
案例环境:版本及相及IP地址的分配
|
名称(Name) |
Ip_address |
|
LVS-DR-DIP:eth0 LVS-DR-DIP:eth0 |
172.16.88.88/16 172.16.88.66/16 |
|
LVS-DR-VIP:eth0:0 |
172.16.88.100/16 |
|
WEB1-Real-Server:eth0 |
172.16.88.10/16 |
|
WEB2-Real-Server:eth0 |
172.16.88.20/16 |
|
软件及版本:Vmware Station-RHEL 5.8(2.6.18) |
|
实验拓扑图如下:
配置过程如下:
Realserver1:172.16.88.10/16配置
第一步:设置主机名、IP地址、hosts文件IP地址和主机名对应关系,及关闭selinux
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
[root@localhost ~]# sed -i 's@\(HOSTNAME=\).*@\1Real1.example.com@g' /etc/sysconfig/network[root@localhost ~]# hostname Real1.example..com[root@localhost ~]# vim /etc/hosts172.16.88.10 Real1.example.com Real1172.16.88.20 Real2.example.com Real2[root@Real1 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0# Advanced Micro Devices [AMD] 79c970 [PCnet32 LANCE]# Real Service Ip_address InformationDEVICE=eth0BOOTPROTO=staticIPADDR=172.16.88.10NETMASK=255.255.0.0GATEWAY=172.16.0.1ONBOOT=yesHWADDR=00:0c:29:4b:88:1f[root@Real1 ~]# service network restart |
第二步:建立Realserver之间建立SSH互信及时间同步:
|
1
2
3
4
5
|
[root@Real1 ~]# ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ''[root@Real1 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@172.16.88.20[root@Real1 ~]# service ntpd stop && chkconfig ntpd off && ntpdate 172.16.0.1[root@Real1 ~]# crontab -e*/3 * * * * /sbin/ntpdate 172.16.0.1 &> /dev/null |
第三��:创建Shell_Scripts,在这里,我们的RealServer提供的是web服务器,所以在这里我们采用脚本的方式来对Realserver进行配置,脚本内容如下:
[root@Real1 ~]# vim /scripts/realserver.sh ##创建脚本实现vip地址以和默认路由的设置以及对外网ARP请求的相关设置
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
#!/bin/bash## Script to start LVS DR real server.# chkconfig: - 90 10# description: LVS DR real server#. /etc/rc.d/init.d/functionsVIP=172.16.88.1host=`/bin/hostname`case "$1" instart) # Start LVS-DR real server on this machine. /sbin/ifconfig lo down /sbin/ifconfig lo 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 /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up /sbin/route add -host $VIP dev lo:0;;stop) # Stop LVS-DR real server loopback device(s). /sbin/ifconfig lo:0 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;;status) # Status of LVS-DR real server. islothere=`/sbin/ifconfig lo:0 | grep $VIP` isrothere=`netstat -rn | grep "lo:0" | grep $VIP` if [ ! "$islothere" -o ! "isrothere" ];then # Either the route or the lo:0 device # not found. echo "LVS-DR real server Stopped." else echo "LVS-DR real server Running." fi;;*) # Invalid entry. echo "$0: Usage: $0 {start|status|stop}" exit 1;;esac |
第四步:给此脚本执行权限并安装httpd二进制软件包,提供web服务
|
1
2
3
4
5
6
7
8
|
[root@Real1 ~]# chmod +x /scripts/realserver.sh[root@Real1 ~]# .realserver.sh start ##运行此脚本,传递一个参数为start,即可设置vip地址、默认路由、对与外网arp请求参数[root@Real1 ~]# service ntpd stop && ntpdate 172.16.0.1 ##这里实现时间同步,使用的ntpdate+crontab来实现,也可以通过system-config-date来做相关设置即可。[root@Real1 ~]# crontab -e*/3 * * * * /sbin/ntpdate 172.16.0.1 &>/dev/null[root@Real1 ~]# yum install httpd -y[root@Real1 ~]# echo "Real1 Server-1" > /var/www/html/index.txt ##提供网页内容,用于测试[root@Real1 ~]# service httpd restart |
[root@Real1 ~]# netstat -ntupl | grep 80 && curl httpd://172.16.88.10

这样Realserver1的基本设置就完成了,Realserver2的设置和Realserver1完全相同,所以笔者在这里就不再演示。
[root@Real2 ~]# curl http://172.16.88.20

Director1 Server Configure
Keepalived在这里主要用作RealServer的健康状态检查以及LoadBalance主机(172.16.88.88/16)和Backup主机(172.16.88.66/16)之间failover的实现
下面为二台lvs调度服务器,安装lvs+keepalived软件包。安装lvs软件包,因为keepalived是运行在lvs之上的,因此lvs及keepalived必须装在一个系统里面。
第一步:设置主机名和/etc/hosts对照关系及两台Director调度器之间建立SSH互信:
Director Router1操作步骤
|
1
2
3
4
5
6
7
8
9
|
[root@localhost ~]# setup ##设置IP地址为172.16.88.88/16[root@localhost ~]# sed -i 's@\(HOSTNAME=\).*@\1Director1.example.com@g' /etc/sysconfig/network ##修改主机名[root@localhost ~]# hostname Director1.example.com[root@localhost ~]# logout ##可以选择注销和重启来使配置文件生效[root@Director1 ~]# vim /etc/hosts ##添加IP地址和主机名的映射关系172.16.88.88 Director1.example.com Director1172.16.88.66 Director2.example.com Director2[root@Director1 ~]# ssh-keygen -t rsa -f ~/.ssl/id_rsa -P '' ##建立两台调度服务器之间的SSH互信,用于Director服务之间复制文件,也可以使用跳板级来实现部署。[root@Director1 ~]# ssh-copy-id -i ./.ssl/id_rsa.pub Director2 |
第二步:IP地址信息及开路由转发功能:


[root@Director ~]# sysctl -p ##重新加载/etc/sysctl.conf配置文件

