操作系统redhat 5.8(2.6.18-308.el5)
通过以下三个例子,理解LVS:
一、LVS基于NAT模型
二、LVS基于DR模型
三、PNMPP持久防火墙标记,利用防火墙定义80端口和443端口之间的烟亲关系
实验拓扑图如下:
实验环境准备:
三台服务器:
Director两块网卡
Director VIP:172.16.0.22(网卡连接方式Bridge)
Director DIP:192.168.10.1(网卡连接方式host-only)
RS1:192.168.0.11(网卡连接方式host-only)
RS2:192.168.0.12 (网卡连接方式host-only)
1、配置RS1,安装httpd
- #yum -y install httpd
- #echo "<h1>RS1</h1>" >/var/www/html/index.html
- #启动httpd服务:service httpd start
2、配置RS2,安装httpd
- #yum -y install httpd
- #echo "<h1>RS2</h1>" >/var/www/html/index.html
- #启动httpd服务:service httpd start
3、安装ipvsadm,定义集群并添加Real Server
- echo 1 >/proc/sys/net/ipv4/ip_forward #打开路由转发功能
- yum -y install ipvsadm #安装ipvsadm
- 添加规则
- ipvsadm -A -t 172.16.0.22:80 -s rr
- ipvsadm -a -t 172.16.0.22:80 -r 192.168.10.2 -m
- ipvsadm -a -t 172.16.0.22:80 -r 192.168.10.3 -m
- 保存规则:/etc/init.d/ipvsadm save
4、测试
请在浏览器中输入http://172.16.0.22/
请再次刷新一下
使用ipvsadm查看一下连接的状态:
- [root@localhost ~]# ipvsadm -Lcn
- IPVS connection entries
- pro expire state source virtual destination
- TCP 01:55 TIME_WAIT 192.168.0.208:56673 172.16.0.22:80 192.168.10.3:80
- TCP 00:07 CLOSE 192.168.0.208:56691 172.16.0.22:80 192.168.10.3:80
- TCP 00:07 CLOSE 192.168.0.208:56694 172.16.0.22:80 192.168.10.2:80
实验环境:
网卡使用桥接
DIP:172.16.0.22
VIP:172.16.0.100
RS1:172.16.0.23
RS2:172.16.0.24
1、配置RS1
- 修改RS1的内核参数
- #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
- 配置RS1的VIP地址并添加路由
- #ifconfig lo:0 172.16.0.100 broadcast 172.16.0.100 netmask 255.255.255.255 up
- #route add -host 172.16.0.100 dev lo:0
- 使用elinks测试RS1的配置的VIP是否生效
- [root@RS1 html]# elinks -dump http://172.16.0.23/index.html
- RS1
- [root@RS1 html]# elinks -dump http://172.16.0.100/index.html
- RS1
- 测试成功
2、配置RS2
- 修改RS2的内核参数
- #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
- 配置RS2的VIP地址并添加路由
- #ifconfig lo:0 172.16.0.100 broadcast 172.16.0.100 netmask 255.255.255.255 up
- #route add -host 172.16.0.100 dev lo:0
- 使用elinks测试RS2的配置的VIP是否生效
- [root@RS2 html]# elinks -dump http://172.16.0.24/index.html
- RS2
- [root@RS2 html]# elinks -dump http://172.16.0.100/index.html
- RS2
- 测试成功
3、配置Director Server
- 配置Director的VIP地址并添加路由
- #ifconfig eth0:0 172.16.0.100 broadcast 172.16.0.100 netmask 255.255.255.255 up
- #route add -host 172.16.0.100 dev eth0:0
- 打开路由转发功能
- #echo 1 >/proc/sys/net/ipv4/ip_forward
- 定义集群服务并添加Real Server
- #ipvsadm -A -t 172.16.0.100:80 -s rr
- #ipvsadm -a -t 172.16.0.100:80 -r 172.16.0.23 -g
- #ipvsadm -a -t 172.16.0.100:80 -r 172.16.0.24 -g
请在浏览器中输入http://172.16.0.100/
刷新一下
- [root@localhost ~]# ipvsadm -Lcn
- IPVS connection entries
- pro expire state source virtual destination
- TCP 01:50 FIN_WAIT 192.168.0.208:57015 172.16.0.100:80 172.16.0.23:80
- TCP 01:53 FIN_WAIT 192.168.0.208:57079 172.16.0.100:80 172.16.0.23:80
- TCP 00:02 CLOSE 192.168.0.208:57032 172.16.0.100:80 172.16.0.24:80
- TCP 01:53 FIN_WAIT 192.168.0.208:57081 172.16.0.100:80 172.16.0.23:80
- TCP 01:52 FIN_WAIT 192.168.0.208:57042 172.16.0.100:80 172.16.0.24:80
Director Sever:172.16.0.22
图跟上面一样采用DR模型
1、配置Director Server
- 配置Director的VIP地址并添加路由,并打开转发功能,步骤同上
- 配置CA服务器
- #vim /etc/pki/tls/openssl.cnf
- #dir=/etc/pki/CA
- #cd /etc/pki/CA
- #mkdir certs newcerts crl
- #touch index.txt
- #echo 01 > serial
- #(umask 077;openssl genrsa -out private/cakey.pem 2048)
- #openssl req -x509 -new -key private/cakey.pem -out cacert.pem -days 365
- 添加防火墙规则,定义80端口和443端口之间的烟亲关系
- #iptables -t mangle -A PREROUTING -d 172.16.0.100 -p tcp --dport 80 -j MARK --set-mark 6
- #iptables -t mangle -A PREROUTING -d 172.16.0.100 -p tcp --dport 443 -j MARK --set-mark 6
- 定义集群服务并添加Real Server
- #ipvsadm -A -f 6 -s rr
- #ipvsadm -a -f 6 -r 172.16.0.23 -g
- #ipvsadm -a -f 6 -r 172.16.0.24 -g
2、配置RS1
- 需要修改内核参数和配置VIP并添加路由,步骤同上
- 为httpd服务生成的密钥
- #yum -y install mod_ssl
- #mkdir /etc/httpd/ssl
- #cd /etc/httpd/ssl/
- #(umask 077;openssl genrsa -out httpd.key 2048)
- #openssl req -x509 -new -key httpd.key -out httpd.csr -days 365 #请填写好你要访问的域名,这里写的是www.test.com
- #CA签署证书(CA服务器上操作)
- #scp httpd.cst 172.16.0.22:/tmp
- #openssl ca -in httpd.csr -out httpd.crt -days 3665
- #scp httpd.crt 172.16.0.23:/etc/httpd/ssl
- #vim /etc/httpd/conf.d/ssl.conf
- DocumentRoot "/var/www/html"
- ServerName www.test.com:443
- SSLCertificateFile /etc/httpd/ssl/httpd.crt
- SSLCertificateKeyFile /etc/httpd/ssl/httpd.key
- #service httpd restart
3、配置RS2
- 需要修改内核参数和配置VIP并添加路由并使用elinks测试,步骤同上
- 复制RS1的/etc/httpd/ssl目录和ssl.conf文件
- #yum -y install mod_ssl
- #scp -rp /etc/httpd/ssl 172.16.0.24:/etc/httpd/ssl
- #scp /etc/httpd/conf.d/ssl.conf 172.16.0.23:/etc/httpd/conf.d/
- #service httpd restart
先测试80端口
测试443端口:注意观察上面浏览器有一把锁的标志
提示:
1、请在windows本地修改好你的hosts文件,以方便解析域名www.test.com---->172.16.0.100
2、客户端证书在CA上下载,并导入浏览器中
- [root@localhost ~]# ipvsadm -Lcn
- IPVS connection entries
- pro expire state source virtual destination
- TCP 01:03 FIN_WAIT 192.168.0.208:55308 172.16.0.100:443 172.16.0.23:443
- TCP 01:03 FIN_WAIT 192.168.0.208:55312 172.16.0.100:443 172.16.0.23:443
- TCP 01:45 FIN_WAIT 192.168.0.208:55349 172.16.0.100:80 172.16.0.23:80
- TCP 01:21 FIN_WAIT 192.168.0.208:55339 172.16.0.100:443 172.16.0.23:443
- TCP 01:03 FIN_WAIT 192.168.0.208:55307 172.16.0.100:443 172.16.0.24:443
- TCP 01:46 FIN_WAIT 192.168.0.208:55355 172.16.0.100:80 172.16.0.23:80
- TCP 01:46 FIN_WAIT 192.168.0.208:55354 172.16.0.100:80 172.16.0.24:80