lvs-nat算法 nat模型样式 vs服务器:公网地址 172.18.250.253 私网地址 192.168.57.129 (DIP) 各私网服务器网关指向服务器DIP 各私网服务器做集群:web (telnet,ssh) 私网服务器使用httpd实现 rs私网服务器1 centos7 ip:192.168.57.132 rs私网服务器2 centos6 ip:192.168.57.128 设置网卡属性: rs1[root@CentOS7 ~]#nmtui 开启图形界面编辑IP地址 Addresses:192.168.57.132/24 Gateway:192.168.57.129 DNS servers:172.18.0.1 rs1[root@CentOS7 ~]#systemctl restart network.service rs2[root@CentOS6 ~]#setup 开启图形界面编辑IP地址 Static IP: 192.168.57.128 Netmask: 255.255.255.0 gateway IP: 192.168.57.129 DNS servers:172.18.0.1 rs2[root@CentOS6 ~]#service network restart vs[root@CentOS7 ~]#yum -y install ipvsadm rs1[root@CentOS7 ~]# yum -y install httpd telnet-server tftp-server rs1[root@CentOS7 ~]#vim /var/www/html/index.html <h1>RS1:192.168.57.132</h1> rs1[root@CentOS7 ~]# systemctl start httpd.service rs1[root@CentOS6 ~]#ss -tnl rs2[root@CentOS6 ~]#yum -y install httpd telnet-server tftp-server rs2[root@CentOS6 ~]#vim /var/www/html/index.html <h1>RS2:192.168.57.128</h1> rs2[root@CentOS6 ~]#service httpd start rs2[root@CentOS6 ~]#ss -tnl vs访问rs: vs[root@CentOS7 ~]#curl http://192.168.57.132 <h1>RS1:192.168.57.132</h1> vs[root@CentOS7 ~]#curl http://192.168.57.128 <h1>RS2:192.168.57.128</h1> 定义集群服务: vs[root@CentOS7 ~]#ipvsadm -Ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn vs[root@CentOS7 ~]# echo 1 > /proc/sys/net/ipv4/ip_forward vs[root@CentOS7 ~]#ipvsadm -A -t 172.18.250.253:80 -s rr vs[root@CentOS7 ~]#ipvsadm -Ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 172.18.250.253:80 rr vs[root@CentOS7 ~]#ipvsadm -a -t 172.18.250.253:80 -r 192.168.57.132 -m vs[root@CentOS7 ~]#ipvsadm -a -t 172.18.250.253:80 -r 192.168.57.128 -m vs[root@CentOS7 ~]#ipvsadm -Ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 172.18.250.253:80 rr -> 192.168.57.128:80 Masq 1 0 0 -> 192.168.57.132:80 Masq 1 0 0 ~]#for i in {1..10};do curl http://172.18.252.253;done <h1>RS1:192.168.57.132</h1> <h1>RS2:192.168.57.128</h1> <h1>RS1:192.168.57.132</h1> <h1>RS2:192.168.57.128</h1> 修改权重: vs[root@CentOS7 ~]# ipvsadm -e -t 172.18.252.253:80 -r 192.168.57.132 -m -w 2 vs[root@CentOS7 ~]# ipvsadm -E -t 172.18.252.253:80 -s wrr vs[root@CentOS7 ~]# ipvsadm -Ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 172.18.250.253:80 wrr -> 192.168.57.128:80 Masq 1 0 5 -> 192.168.57.132:80 Masq 2 0 5 ~]#for i in {1..10};do curl http://172.18.250.253; done <h1>RS1:192.168.57.132</h1> <h1>RS2:192.168.57.128</h1> <h1>RS1:192.168.57.132</h1> <h1>RS1:192.168.57.132</h1> <h1>RS2:192.168.57.128</h1> <h1>RS1:192.168.57.132</h1> <h1>RS1:192.168.57.132</h1>
telnet: 添加用户: rs1[root@CentOS7 ~]# for i in {1..3}; do useradd user$i;echo "mageedu" | passwd --stdin user$i; done rs1[root@CentOS7 ~]# systemctl start telnet.socket rs1[root@CentOS7 ~]# ss -tnl LISTEN 0 128 :::23 :::* rs2[root@CentOS6 ~]# for i in {1..3}; do useradd user$i;echo "mageedu" | passwd --stdin user$i; done rs2[root@CentOS6 html]#chkconfig telnet on rs2[root@CentOS6 html]#service xinetd restart Stopping xinetd: [FAILED] Starting xinetd: [ OK ] rs2[root@CentOS6 html]#ss -tnl LISTEN 0 64 :::23 :::* vs[root@CentOS7 ~]# ipvsadm -A -t 172.18.250.253:23 -s lc vs[root@CentOS7 ~]# ipvsadm -a -t 172.18.250.253:23 -r 192.168.57.132:23 -m -w 1 vs[root@CentOS7 ~]# ipvsadm -a -t 172.18.250.253:23 -r 192.168.57.128:23 -m -w 3 vs[root@CentOS7 ~]# ipvsadm -ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 172.18.252.253:23 lc -> 192.168.57.128:23 Masq 3 0 0 -> 192.168.57.132:23 Masq 1 0 0 ~]#telnet 172.18.252.253 login: user1 Password: mageedu [user1@CentOS6 ~]$ ip a inet 192.168.57.128/24 [user2@CentOS7 ~]$ ip a inet 192.168.57.132/24 [user3@CentOS6 ~]$ ip a inet 192.168.57.128/24 vs[root@CentOS7 ~]# ipvsadm -E -t 172.18.252.253:23 -s wlc vs[root@CentOS7 ~]# ipvsadm -ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 172.18.252.253:23 wlc -> 192.168.57.128:23 Masq 3 0 1 -> 192.168.57.132:23 Masq 1 0 0
把wrr修改为sh算法: [root@CentOS7 ~]# ipvsadm -E -t 172.18.250.253:80 -s sh [root@CentOS7 ~]# ipvsadm -ln TCP 172.18.252.253:80 sh -> 192.168.57.128:80 Masq 1 0 0 -> 192.168.57.132:80 Masq 2 0 0 [root@CentOS7 ~]#for i in {1..10};do curl http://172.18.252.253;done <h1>RS2:192.168.57.128</h1> <h1>RS2:192.168.57.128</h1> <h1>RS2:192.168.57.128</h1> <h1>RS2:192.168.57.128</h1> <h1>RS2:192.168.57.128</h1> rs2宕机: vs[root@CentOS7 ~]# ipvsadm -E -t 172.18.250.253:80 -s rr rs2[root@CentOS6 ~]#service httpd stop Stopping httpd: [ OK ] ~]#for i in {1..10};do curl http://172.18.252.253;done curl: (7) Failed connect to 172.18.252.253:80; Connection refused <h1>RS1:192.168.57.132</h1> curl: (7) Failed connect to 172.18.252.253:80; Connection refused <h1>RS1:192.168.57.132</h1> curl: (7) Failed connect to 172.18.252.253:80; Connection refused <h1>RS1:192.168.57.132</h1>
原创文章,作者:CL80516000,如若转载,请注明出处:http://www.178linux.com/75646