lvs-dr-rr
import paramiko vip = '192.168.254.250' ds = '192.168.254.17' rs1 = '192.168.254.37' rs2 = '192.168.254.19' ds_cmd = ''' ipvsadm -C; ipvsadm -A -t {vip}:80 -s rr; ipvsadm -a -t {vip}:80 -r {rs1}:80 -g; ipvsadm -a -t {vip}:80 -r {rs2}:80 -g; ifconfig ens33:0 {vip} broadcast {vip} netmask 255.255.255.255 up; route add -host {vip} dev ens33:0; '''.format(vip=vip, rs1=rs1, rs2=rs2) rs1_cmd = ''' ifconfig lo:0 {vip} broadcast {vip} netmask 255.255.255.255 up; route add -host {vip} dev lo:0; echo "1">/proc/sys/net/ipv4/conf/lo/arp_ignore; echo "2">/proc/sys/net/ipv4/conf/lo/arp_announce; echo "1">/proc/sys/net/ipv4/conf/all/arp_ignore; echo "2">/proc/sys/net/ipv4/conf/all/arp_announce; service httpd restart '''.format(vip=vip) rs2_cmd = ''' ifconfig lo:0 {vip} broadcast {vip} netmask 255.255.255.255 up; route add -host {vip} dev lo:0; echo "1">/proc/sys/net/ipv4/conf/lo/arp_ignore; echo "2">/proc/sys/net/ipv4/conf/lo/arp_announce; echo "1">/proc/sys/net/ipv4/conf/all/arp_ignore; echo "2">/proc/sys/net/ipv4/conf/all/arp_announce; service httpd restart '''.format(vip=vip) ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) def direct_server(): ssh.connect('{ds}'.format(ds=ds), 22, 'root', 'root',timeout=3) stdin, stdout, stderr = ssh.exec_command(ds_cmd) print(stderr.read().decode('utf-8')) print(stdout.read().decode('utf-8')) print('dicret_server配置完成!!!') def real_server1(): ssh.connect('{rs1}'.format(rs1=rs1), 22, 'root', 'root', timeout=3) stdin, stdout, stderr = ssh.exec_command(rs1_cmd) print(stderr.read().decode('utf-8')) print(stdout.read().decode('utf-8')) print('real_server1配置完成!!!') def real_server2(): ssh.connect('{rs2}'.format(rs2=rs2), 22, 'root', 'root', timeout=3) stdin, stdout, stderr = ssh.exec_command(rs2_cmd) print(stderr.read().decode('utf-8')) print(stdout.read().decode('utf-8')) print('real_server2配置完成!!!') def main(): direct_server() real_server1() real_server2() ssh.close() if __name__ == '__main__': main()
lvs-dr-wrr
import paramiko ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) def direct_server(): ssh.connect('192.168.254.17', 22, 'root', 'root',timeout=3) stdin, stdout, stderr = ssh.exec_command('ipvsadm -C;' 'ipvsadm -A -t 192.168.254.250:80 -s wrr;' 'ipvsadm -a -t 192.168.254.250:80 -r 192.168.254.37:80 -w 1 -g;' 'ipvsadm -a -t 192.168.254.250:80 -r 192.168.254.19:80 -w 2 -g;' 'ifconfig ens33:0 192.168.254.250 broadcast 192.168.254.250 netmask 255.255.255.255 up;' 'route add -host 192.168.254.250 dev ens33:0') print(stderr.read().decode('utf-8')) print(stdout.read().decode('utf-8')) print('dicret_server配置完成!!!') def real_server1(): ssh.connect('192.168.254.37', 22, 'root', 'root', timeout=3) stdin, stdout, stderr = ssh.exec_command('ifconfig lo:0 192.168.254.250 broadcast 192.168.254.250 netmask 255.255.255.255 up;' 'route add -host 192.168.254.250 dev lo:0;' 'echo "1">/proc/sys/net/ipv4/conf/lo/arp_ignore;' 'echo "2">/proc/sys/net/ipv4/conf/lo/arp_announce;' 'echo "1">/proc/sys/net/ipv4/conf/all/arp_ignore;' 'echo "2">/proc/sys/net/ipv4/conf/all/arp_announce;' 'service httpd restart') print(stderr.read().decode('utf-8')) print(stdout.read().decode('utf-8')) print('real_server1配置完成!!!') def real_server2(): ssh.connect('192.168.254.19', 22, 'root', 'root', timeout=3) stdin, stdout, stderr = ssh.exec_command( 'ifconfig lo:0 192.168.254.250 broadcast 192.168.254.250 netmask 255.255.255.255 up;' 'route add -host 192.168.254.250 dev lo:0;' 'echo "1">/proc/sys/net/ipv4/conf/lo/arp_ignore;' 'echo "2">/proc/sys/net/ipv4/conf/lo/arp_announce;' 'echo "1">/proc/sys/net/ipv4/conf/all/arp_ignore;' 'echo "2">/proc/sys/net/ipv4/conf/all/arp_announce;' 'service httpd restart') print(stderr.read().decode('utf-8')) print(stdout.read().decode('utf-8')) print('real_server2配置完成!!!') def main(): direct_server() real_server1() real_server2() ssh.close() if __name__ == '__main__': main()