python部署lvs

Posted zhou2019

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python部署lvs相关的知识,希望对你有一定的参考价值。

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()

 

以上是关于python部署lvs的主要内容,如果未能解决你的问题,请参考以下文章

LVS负载均衡之LVS-NAT实例部署(配置篇)

LVS 部署(上

LVS+Keepalived 群集部署

LVS+Keepalived 高可用群集实战部署

LVS负载均衡群集DR模式+Keepalived部署!

LVS负载均衡群集DR模式+Keepalived部署!