三臺機器html
[root@lijie-01 ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens37 HWADDR=00:0C:29:21:5E:CA TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=none IPADDR=192.168.95.144 PREFIX=24 DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV4_DNS_PRIORITY=100 IPV6INIT=yes IPV6_AUTOCONF=no IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy IPV6_DNS_PRIORITY=100 NAME=ens37 DEVICE=ens37 ONBOOT=no ZONE=block [root@lijie-01 ~]# ifup ens37 鏈接已成功激活(D-Bus 活動路徑:/org/freedesktop/NetworkManager/ActiveConnection/2) [root@lijie-01 ~]#
隨後咱們從windows中ping一下新建的這個網卡linux
C:\Users\mixuyulv>ping 192.168.75.136 正在 Ping 192.168.75.136 具備 32 字節的數據: 來自 192.168.75.136 的回覆: 字節=32 時間<1ms TTL=64 來自 192.168.75.136 的回覆: 字節=32 時間<1ms TTL=64 來自 192.168.75.136 的回覆: 字節=32 時間<1ms TTL=64 來自 192.168.75.136 的回覆: 字節=32 時間<1ms TTL=64 192.168.75.136 的 Ping 統計信息: 數據包: 已發送 = 4,已接收 = 4,丟失 = 0 (0% 丟失), 往返行程的估計時間(以毫秒爲單位): 最短 = 0ms,最長 = 0ms,平均 = 0ms C:\Users\mixuyulv>
[root@lijie-02 ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet iPROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=ens33 DEVICE=ens33 ONBOOT=yes IPADDR=192.168.75.134 NETMASK=255.255.255.0 GATEWAY=192.168.75.136 DNS1=119.29.29.29
咱們還須要重啓網絡服務,重啓後這臺機器就不能上網了,nginx
[root@lijie-02 ~]# systemctl restart network.service [root@lijie-02 ~]# ping www.qq.com ping: www.qq.com: 未知的名稱或服務 [root@lijie-02 ~]#
咱們如今再來查看網關就變成了75.136算法
[root@lijie-02 ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.75.136 0.0.0.0 UG 100 0 0 ens33 192.168.75.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0 [root@lijie-02 ~]#
[root@lijie-03 ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=ens33 UUID=087b59ca-7e9f-4b76-a456-ada0fa5a7af4 DEVICE=ens33 ONBOOT=yes IPADDR=192.168.75.130 NETMASK=255.255.255.0 GATEWAY=192.168.75.136 DNS1=119.29.29.29
咱們還須要重啓網絡服務,重啓後這臺機器就不能上網了,apache
[root@lijie-03 ~]# systemctl restart network.service [root@lijie-03 ~]# ping www.qq.com ping: www.qq.com: 未知的名稱或服務 [root@lijie-02 ~]#
咱們如今再來查看網關就變成了75.136vim
[root@lijie-03 ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.75.136 0.0.0.0 UG 100 0 0 ens33 192.168.75.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33 [root@lijie-03 ~]#
[root@lijie-01 ~]# systemctl stop firewalld [root@lijie-01 ~]# systemctl disable firewalld Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service. Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service. [root@lijie-01 ~]# iptables -nvL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination [root@lijie-01 ~]#
這裏建議使用centos6裏面的iptables的方式,須要安裝iptables-servicewindows
[root@lijie-01 ~]# yum install -y iptables-services
隨後啓動iptables-service服務後端
[root@lijie-01 ~]# systemctl start iptables-services Failed to start iptables-services.service: Unit not found. [root@lijie-01 ~]#
咱們可使用下面命令查看iptables-services 這個服務是否成功安裝centos
[root@lijie-01 ~]# rpm -ql iptables-services /etc/sysconfig/ip6tables /etc/sysconfig/iptables /usr/lib/systemd/system/ip6tables.service /usr/lib/systemd/system/iptables.service /usr/libexec/initscripts/legacy-actions/ip6tables /usr/libexec/initscripts/legacy-actions/ip6tables/panic /usr/libexec/initscripts/legacy-actions/ip6tables/save /usr/libexec/initscripts/legacy-actions/iptables /usr/libexec/initscripts/legacy-actions/iptables/panic /usr/libexec/initscripts/legacy-actions/iptables/save /usr/libexec/iptables /usr/libexec/iptables/ip6tables.init /usr/libexec/iptables/iptables.init [root@lijie-01 ~]#
從上圖咱們發現使用的命令有誤,開啓iptables服務應使用如下命令瀏覽器
[root@lijie-01 ~]# systemctl start iptables [root@lijie-01 ~]#
隨後使iptables有效
[root@lijie-01 ~]# systemctl enable iptables Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service. [root@lijie-01 ~]#
上面咱們開啓iptables的目的是爲了清空規則,以便後面調用空規則
[root@lijie-01 ~]# iptables -F [root@lijie-01 ~]# service iptables save iptables: Saving firewall rules to /etc/sysconfig/iptables:[ 肯定 ] [root@lijie-01 ~]#
還須要關閉selinux
[root@lijie-01 ~]# setenforce 0 [root@lijie-01 ~]#
保險起見,最好仍是修改配置文件/etc/selinux/config中selinux值從enforcing變爲disabled來關閉selinux
# This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. SELINUX=disabled # SELINUXTYPE= can take one of three two values: # targeted - Targeted processes are protected, # minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection. SELINUXTYPE=targeted
至此,準備工做完畢
[root@lijie-01 ~]# yum install -y ipvsdam
#! /bin/bash # director 服務器上開啓路由轉發功能 echo 1 > /proc/sys/net/ipv4/ip_forward # 關閉icmp的重定向 echo 0 > /proc/sys/net/ipv4/conf/all/send_redirects echo 0 > /proc/sys/net/ipv4/conf/default/send_redirects # 注意區分網卡名字,個人兩個網卡分別爲ens33和ens37 echo 0 > /proc/sys/net/ipv4/conf/ens33/send_redirects echo 0 > /proc/sys/net/ipv4/conf/ens37/send_redirects # director 設置nat防火牆 iptables -t nat -F iptables -t nat -X iptables -t nat -A POSTROUTING -s 192.168.75.0/24 -j MASQUERADE # director設置ipvsadm IPVSADM='/usr/sbin/ipvsadm' $IPVSADM -C #這裏的wlc是算法,算法還有如rr、lc $IPVSADM -A -t 192.168.95.144:80 -s wlc -p 3 $IPVSADM -a -t 192.168.95.144:80 -r 192.168.75.134:80 -m -w 1 $IPVSADM -a -t 192.168.95.144:80 -r 192.168.75.130:80 -m -w 1
咱們來執行下這個腳本,沒有任何輸出,說明腳本沒有語法錯誤
[root@lijie-01 ~]# sh /usr/local/sbin/lvs_nat.sh [root@lijie-01 ~]#
擴展:
lvs 三種模式詳解 http://www.it165.net/admin/html/201401/2248.html
lvs幾種算法 http://www.aminglinux.com/bbs/thread-7407-1-1.html
關於arp_ignore和 arp_announce http://www.cnblogs.com/lgfeng/archive/2012/10/16/2726308.html
lvs原理相關的 http://blog.csdn.net/pi9nc/article/details/23380589
擴展:
lvs 三種模式詳解 http://www.it165.net/admin/html/201401/2248.html
lvs幾種算法 http://www.aminglinux.com/bbs/thread-7407-1-1.html
關於arp_ignore和 arp_announce http://www.cnblogs.com/lgfeng/archive/2012/10/16/2726308.html
lvs原理相關的 http://blog.csdn.net/pi9nc/article/details/23380589