LVS NAT模式
LVS-NAT
三台虚拟机都是centos 6.5
关闭防火墙和selinux
角色 | IP地址 | 备注 |
LVS负载调度器 |
eth0:192.168.119.128(内网) eth1:192.168.94.130(外网) |
|
web01_Real server | 192.168.119.130 | httpd |
web01_Real server | 192.168.119.133 | httpd |
给LVS负载调度器添加一块网卡eth1
- 添加一块新的网卡、启动 eth1还不能直接使用、
[root@lvs01 /]# dmesg | grep -in eth 1581:e1000 0000:02:01.0: eth0: (PCI:66MHz:32-bit) 00:0c:29:de:74:25 1582:e1000 0000:02:01.0: eth0: Intel(R) PRO/1000 Network Connection 1585:e1000 0000:02:05.0: eth1: (PCI:66MHz:32-bit) 00:0c:29:de:74:2f 1586:e1000 0000:02:05.0: eth1: Intel(R) PRO/1000 Network Connection 1614:e1000: eth0 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None 1615:eth0: no IPv6 routers present [root@lvs01 /]# cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth1 [root@lvs01 /]# vim /etc/sysconfig/network-scripts/ifcfg-eth1 DEVICE=eth1 HWADDR=00:0c:29:de:74:2f TYPE=Ethernet ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=static IPADDR=192.168.1.100 NETWORK=255.255.255.0 GATEWAY=192.168.1.1 [root@lvs01 /]# /etc/init.d/network reload Shutting down interface eth0: [ OK ] Shutting down loopback interface: [ OK ] Bringing up loopback interface: [ OK ] Bringing up interface eth0: Determining IP information for eth0... done. [ OK ] Bringing up interface eth1: Determining if ip address 192.168.1.100 is already in use for device eth1... [ OK ]
1、下载软件包
[root@lvs01 /]# wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.26.tar.gz
2、安装需要的依赖包
[root@lvs01 /]# yum -y install kernel-devel gcc gcc-c++ libnl* libpopt* popt-static
3、创建一个软链接,防止后面编译安装ipvsadm时找不到系统内核(2.6.32-642.4.2.el6.x86_64用tab键补全的、如果没有这个文件缺少kernel-devel包)
[root@lvs01 /]# ln -s /usr/src/kernels/2.6.32-642.4.2.el6.x86_64/ /usr/src/linux [root@lvs01 /]# ll /usr/src/ total 8 drwxr-xr-x. 2 root root 4096 Sep 23 2011 debug drwxr-xr-x. 3 root root 4096 Sep 26 06:57 kernels lrwxrwxrwx. 1 root root 43 Sep 26 06:58 linux -> /usr/src/kernels/2.6.32-642.4.2.el6.x86_64/
4、解压、编译、安装
[root@lvs01 /]# tar zxf ipvsadm-1.26.tar.gz [root@lvs01 /]# cd ipvsadm-1.26 [root@lvs01 ipvsadm-1.26]# make && make install [root@lvs01 ipvsadm-1.26]# echo $? 0 [root@lvs01 ipvsadm-1.26]# modprobe ip_vs //加载IP_VS模块 [root@lvs01 ipvsadm-1.26]# lsmod | grep ip_vs ip_vs 125220 0 libcrc32c 1246 1 ip_vs ipv6 317340 142 ip_vs,ip6t_REJECT,nf_conntrack_ipv6,nf_defrag_ipv6 [root@lvs01 ipvsadm-1.26]# cat /proc/net/ip_vs //查看IP_VS版本信息 IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn
5、配置负载调度器
- 配置SNAT转发规则
[root@lvs01 /]# vi /etc/sysctl.conf net.ipv4.ip_forward = 1 [root@lvs01 ipvsadm-1.26]# sysctl -p
net.ipv4.ip_forward = 1 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 error: "net.bridge.bridge-nf-call-ip6tables" is an unknown key error: "net.bridge.bridge-nf-call-iptables" is an unknown key error: "net.bridge.bridge-nf-call-arptables" is an unknown key kernel.msgmnb = 65536 kernel.msgmax = 65536 kernel.shmmax = 68719476736 kernel.shmall = 4294967296 [root@lvs01 /]# iptables -t nat -A POSTROUTING -s 192.168.119.0/24 -o eth0 -j SNAT --to-source 192.168.94.130
- 配置负载分配策略
[root@lvs01 ipvsadm-1.26]# ipvsadm -C //清除原有策略 [root@lvs01 ipvsadm-1.26]# ipvsadm -A -t 192.168.119.94:130 -s rr [root@lvs01 ipvsadm-1.26]# ipvsadm -a -t 192.168.119.94:130 -r 192.168.119.130:80 -m -w 1 [root@lvs01 ipvsadm-1.26]# ipvsadm -a -t 192.168.119.94:130 -r 192.168.119.133:80 -m -w 1 [root@lvs01 ipvsadm-1.26]# service ipvsadm save //保存策略 Saving IPVS table to /etc/sysconfig/ipvsadm: [ OK ] [root@lvs01 ipvsadm-1.26]# chkconfig ipvsadm on
5、配置系欸但服务器
- 安装httpd、创建测试网页
web01配置 [root@web01 /]# yum -y install httpd [root@web01 /]# find / -name httpd.conf /etc/httpd/conf/httpd.conf [root@web01 /]# vim /etc/httpd/conf/httpd.conf Servername www.web01.com:80 [root@web01 /]# echo "web01_192.168.119.130" > /var/www/html/index.html [root@web01 /]# /etc/init.d/httpd start Starting httpd: [ OK ] [root@web01 /]# curl 192.168.119.130 web01_192.168.119.130 web02配置 [root@web02 /]# yum -y install httpd [root@web02 /]# vim /etc/httpd/conf/httpd.conf ServerName www.web02.com:80 [root@web02 /]# echo "web02_192.168.119.133" > /var/www/html/index.html
[root@web02 /]# /etc/init.d/httpd start
Starting httpd: [ OK ]
[root@web02 /]# curl 192.168.119.133
web02_192.168.119.133
6、测试LVS
[root@lvs01 ipvsadm-1.26]# ipvsadm -ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.119.128:80 rr -> 192.168.119.130:80 Masq 1 0 0 -> 192.168.119.133:80 Masq 1 0 0