python程序—一键部署keepalived+lvs
一个DS 两个RS
keepalived端在/root下准备好已经修改好的配置文件
import paramiko # keepalived端 需要修改的信息 keepalived_ip='192.168.150.128' keepalived_user='root' keepalived_pwd='root' # server端lvs 需要修改的信息 vip='192.168.150.150' #虚拟IP real_server_ip=['192.168.150.130','192.168.150.131'] real_server_user=['root','root'] real_server_pwd=['root','root'] keepalived_cmd = ''' cp keepalived.conf /etc/keepalived/keepalived.conf; systemctl restart keepalived; systemctl stop firewalld; setenforce 0 ''' 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; systemctl restart httpd; systemctl stop firewalld; setenforce 0 '''.format(vip=vip) # 配置keepalived def keepalived_server(): ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname=keepalived_ip,port=22,username=keepalived_user,password=keepalived_pwd) stdin, stdout, stderr = ssh.exec_command(keepalived_cmd) print(stderr.read().decode('utf-8')) print(stdout.read().decode('utf-8')) ssh.close() print('keepalived配置完成!') # 配置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() print('real_server %s 配置完成!'% real_server_ip[k]) k+=1 def main(): keepalived_server() real_server() if __name__ == '__main__': main()