LVS NAT模式html
LVS IP Tunnel模式linux
LVS DR模式nginx
NAT模式搭建-準備工做算法
[root@taoyuan ~]# hostnamectl set-hostname qingyun-01 #進入個子shell [root@taoyuan ~]# bash [root@qingyun-01 ~]#
[root@qingyun-02 html]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.0.130 0.0.0.0 UG 100 0 0 ens33 192.168.0.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
[root@qingyun-03 html]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.0.130 0.0.0.0 UG 100 0 0 ens33 192.168.0.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
#因爲3臺是從新,都關閉防火牆 [root@qingyun-01 ~]# systemctl stop firewalld [root@qingyun-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@qingyun-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@qingyun-03 ~]# yum install -y iptables-services #若是安裝很慢,能夠取消epel #把/etc/yum.repos.d/目錄下epel.repo 改一下名字 #查看包安裝的文件 [root@qingyun-01 yum.repos.d]# rpm -ql iptables-services /etc/sysconfig/ip6tables /etc/sysconfig/iptables /usr/lib/systemd/system/ip6tables.service /usr/lib/systemd/system/iptables.service #啓動iptables.service [root@qingyun-01 yum.repos.d]# systemctl start iptables [root@qingyun-01 yum.repos.d]# systemctl enable iptables Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
#開啓的目的是爲了調用一個空的規則 [root@qingyun-03 ~]# iptables -F [root@qingyun-03 ~]# service iptables save iptables: Saving firewall rules to /etc/sysconfig/iptables:[ 肯定 ] #關閉selinux [root@qingyun-01 yum.repos.d]# setenforce 0 [root@qingyun-01 yum.repos.d]# vi /etc/selinux/config #SELINUX=disabled #查看網關 [root@qingyun-03 ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.0.1 0.0.0.0 UG 100 0 0 ens33 192.168.0.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
NAT模式搭建shell
[root@qingyun-01 yum.repos.d]# yum install -y ipvsadm
[root@qingyun-01 ~]# vim /usr/local/sbin/lvs_nat.sh #! /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 # dirrector 設置nat防火牆 iptables -t nat -F iptables -t nat -X iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -j MASQUERADE # director設置ipvsadm IPVSADM='/usr/sbin/ipvsadm' $IPVSADM -C $IPVSADM -A -t 192.168.147.144:80 -s wlc -p 3 $IPVSADM -a -t 192.168.147.144:80 -r 192.168.0.132:80 -m -w 1 $IPVSADM -a -t 192.168.147.144:80 -r 192.168.0.133:80 -m -w 1
[root@qingyun-01 ~]# sh /usr/local/sbin/lvs_nat.sh #沒有輸出,說明沒有錯誤
NAT模式效果測試vim
兩臺rs上都安裝nginx後端
[root@qingyun-01 ~]# curl 192.168.147.144 qingyun03-132 [root@qingyun-01 ~]# curl 192.168.147.144 qingyun03-133 [root@qingyun-01 ~]# curl 192.168.147.144 qingyun03-132 [root@qingyun-01 ~]# curl 192.168.147.144 qingyun03-133 [root@qingyun-01 ~]# cat /usr/local/sbin/lvs_nat.sh ........... $IPVSADM -C $IPVSADM -A -t 192.168.147.144:80 -s wlc .............
[root@qingyun-01 ~]# 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.147.144:80 wlc -> 192.168.0.132:80 Masq 1 0 0 -> 192.168.0.133:80 Masq 1 0 4