LVS--简单篇之nat
拓扑图:
部署准备:
关闭所有的iptables和selinux,调度器除外。
系统自带光盘
加载ip_vs模块
[root@cjl ~]# ipvsadm -v ipvsadm v1.26 2008/5/15 (compiled with popt and IPVS v1.2.1)
使用ipvsadm管理工具:
[root@cjl ~]# modprobe ip_vs [root@cjl ~]# cat /proc/net/ip_vs IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn
创建虚拟服务器,并添加节点:
[root@cjl ~]# ipvsadm -A -t 192.168.1.10:80 -s rr [root@cjl ~]# ipvsadm -a -t 192.168.1.10:80 -r 99.99.99.16:80 -m -w 1 [root@cjl ~]# ipvsadm -a -t 192.168.1.10:80 -r 99.99.99.17:80 -m -w 1 [root@cjl ~]# 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.1.10:80 rr -> 99.99.99.16:80 Masq 1 0 0 -> 99.99.99.17:80 Masq 1 0 0 [root@cjl ~]
保存负载均衡策略:
[root@cjl ~]# ipvsadm-save > /etc/sysconfig/ipvsadm [root@cjl ~]# cat /etc/sysconfig/ipvsadm -A -t 192.168.1.10:http -s rr -a -t 192.168.1.10:http -r 99.99.99.16:http -m -w 1 -a -t 192.168.1.10:http -r 99.99.99.17:http -m -w 1 [root@cjl ~]#
启用路由,和SNAT
搭建NFS网络文件共享:
[root@cjl ~]# yum -y install nfs-utils rpcbind [root@cjl ~]# chkconfig nfs on [root@cjl ~]# chkconfig rpcbind on [root@cjl ~]# mkdir -p /opt/www [root@cjl ~]# vim /etc/exports
/opt/www 99.99.99.0/24(rw,sync,no_root_squash) ##添加这行
启动服务:
[root@cjl ~]# /etc/init.d/rpcbind start 正在启动 rpcbind: [确定] [root@cjl ~]# /etc/init.d/nfs start 启动 NFS 服务: [确定] 关掉 NFS 配额: [确定] 启动 NFS mountd: [确定] 启动 NFS 守护进程: [确定] 正在启动 RPC idmapd: [确定] [root@cjl ~]#
查看本机发布的nfs共享:
其他服务器安装访问:
[root@cjl ~]# yum -y install nfs-utils rpcbind [root@cjl ~]# chkconfig rpcbind on [root@cjl ~]# /etc/init.d/rpcbind start 正在启动 rpcbind: [确定] [root@cjl ~]# showmount -e 99.99.99.27 Export list for 99.99.99.27: /opt/www 99.99.99.0/24 [root@cjl ~]#
挂载nfs共享,确认挂载结果。
[root@cjl ~]# mount 99.99.99.27:/opt/www /var/www/html [root@cjl ~]# tail -1 /etc/mtab 99.99.99.27:/opt/www /var/www/html nfs rw,vers=4,addr=99.99.99.27,clientaddr=99.99.99.16 0 0 [root@cjl ~]# cat /var/www/html/index.html nfs测试 [root@cjl ~]#
实现开机挂载添加如下行:
vim /etc/fstab 99.99.99.27:/opt/www /var/www/html nfs defaults,netdev 0 0
搭建第一台测试服务器:
[root@cjl ~]# yum -y install httpd [root@cjl ~]# cat /var/www/html/index.html 测试服务器1
搭建第二台测试服务器:
搭建第一台测试服务器: [root@cjl ~]# yum -y install httpd [root@cjl ~]# cat /var/www/html/index.html 测试服务器2
不挂载访问不断刷新:
挂载访问不断刷新:
LVS--简单篇之DR:
配置虚拟网卡:
[root@cjl ~]# cd /etc/sysconfig/network-scripts/ [root@cjl network-scripts]# cp ifcfg-eth0 ifcfg-eth0:0 [root@cjl network-scripts]# vim ifcfg-eth0:0
编辑eth0:0配置文件如下:
DEVICE=eth0:0 ONBOOT=yes IPADDR=99.99.99.28 NETMASK=255.255.255.0
启动网卡:
[root@cjl network-scripts]# ifup eth0:0 Determining if ip address 99.99.99.28 is already in use for device eth0... [root@cjl network-scripts]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 08:00:27:0a:b2:27 brd ff:ff:ff:ff:ff:ff inet 99.99.99.15/24 brd 99.99.99.255 scope global eth0 inet 99.99.99.28/24 brd 99.99.99.255 scope global secondary eth0:0 inet6 fe80::a00:27ff:fe0a:b227/64 scope link valid_lft forever preferred_lft forever [root@cjl network-scripts]#
配置路由文件添加如下行(关闭linux内核的重定向参数响应):
net.ipv4.conf.all.send_redirects = 0 net.ipv4.conf.default.send_redirects = 0 net.ipv4.conf.eth0.send_redirects = 0
启用路由:
[root@cjl ~]# 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 net.ipv4.conf.all.send_redirects = 0 net.ipv4.conf.default.send_redirects = 0 net.ipv4.conf.eth0.send_redirects = 0
配置负载分配策略:
[root@cjl ~]# /etc/init.d/ipvsadm stop [root@cjl ~]# ipvsadm -A -t 99.99.99.28:80 -s rr [root@cjl ~]# ipvsadm -a -t 99.99.99.28:80 -r 99.99.99.16 -g -w 1 [root@cjl ~]# ipvsadm -a -t 99.99.99.28:80 -r 99.99.99.17 -g -w 1 [root@cjl ~]# chkconfig ipvsadm on
配置节点(真实的web)服务器(虚拟VIP):
[root@cjl ~]# cd /etc/sysconfig/network-scripts/ [root@cjl network-scripts]# cp ifcfg-lo ifcfg-lo:0 [root@cjl network-scripts]# vim ifcfg-lo:0 DEVICE=lo:0 IPADDR=99.99.99.28 NETMASK=255.255.255.255 ONBOOT=yes [root@cjl network-scripts]# ifup lo:0 [root@cjl network-scripts]# ifconfig lo:0 lo:0 Link encap:Local Loopback inet addr:99.99.99.28 Mask:255.255.255.255 UP LOOPBACK RUNNING MTU:65536 Metric:1 [root@cjl network-scripts]# route add -host 99.99.99.28 dev lo:0 [root@cjl network-scripts]# echo "/sbin/route add -host 99.99.99.28 dev lo:0" >>/etc/rc.local
编辑路由配置文件(调整内核参数的ARP响应):
[root@cjl ~]# vim /etc/sysctl.conf
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@cjl ~]# 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 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 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@cjl ~]#
访问测试我的网页文件是nfs的所以刷新后还是这个页面: