• lvs 负载均衡 _DR模式 _Python脚本


    import paramiko
    vip='192.168.83.6' # 虚拟IP
    
    # direct_server_information
    ds_info={
        'ip':'192.168.83.5',
        'user':'root',
        'pwd':'root'
    }
    
    # real_server_information
    real_server_ip=['192.168.83.3','192.168.83.4']
    real_server_user=['root','root']
    real_server_pwd=['root','root']
    ds_cmd = '''
    yum install ipvsadm;
    ipvsadm -C;
    ipvsadm -A -t {vip}:80 -s wrr;
    ipvsadm -a -t {vip}:80 -r {rs1}:80 -w 1 -g;
    ipvsadm -a -t {vip}:80 -r {rs2}:80 -w 2 -g;
    ifconfig ens33:0 {vip} broadcast {vip} netmask 255.255.255.255 up;
    route add -host {vip} dev ens33:0;
    service httpd restart;
    systemctl stop firewalld;
    setenforce 0
    '''.format(vip=vip, rs1=real_server_ip[0], rs2=real_server_ip[1])
    
    server_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;
    systemctl stop firewalld;
    setenforce 0
    '''.format(vip=vip)
    
    
    #配置调度器direct_server
    def direct_server():
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(hostname=ds_info['ip'],port=22,username=ds_info['user'],password=ds_info['pwd'])
        stdin, stdout, stderr = ssh.exec_command(ds_cmd)
        print(stderr.read().decode('utf-8'))
        print(stdout.read().decode('utf-8'))
        ssh.close()
        print('dicret_server配置完成!')
    
    
    #配置real_server
    def real_server():
        k=0
        while k<len(real_server_ip):
            ssh = paramiko.SSHClient()
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            ssh.connect(hostname=real_server_ip[k],port=22,username=real_server_user[k],password=real_server_pwd[k])
            stdin, stdout, stderr = ssh.exec_command(server_cmd)
            print(stderr.read().decode('utf-8'))
            print(stdout.read().decode('utf-8'))
            ssh.close()
            k+=1
        print('real_server配置完成!')
    
    
    def main():
        direct_server()
        real_server()
    
    if __name__ == '__main__':
        main()
  • 相关阅读:
    @当你输入一个网址的时候,实际会发生什么?
    @Java魔法类——unsafer应用解析
    !@面试官:说说双亲委派模型?
    @JAVA字符串格式化
    @double精度比float低吗?
    @java类中资源加载顺序
    !@阿里资深架构师浅谈一个Java类的生命周期
    @String对象的那些事,几行代码就解释得清清楚楚
    @final、finally、finalize有什么区别?
    @35个Java代码优化的细节,你知道几个?
  • 原文地址:https://www.cnblogs.com/leeeel/p/10827784.html
Copyright © 2020-2023  润新知