一键部署LVS高可用,本人亲测可用,若有不同可做调整
#!/usr/bin/env bash mkdir lxl cd lxl touch hosts.txt author(){ echo "----------------------------------------------------------------------------------------------------" echo "- keepalived和ipvsadm部署完毕 -" echo "- 作者:lxl -" echo "- 微信:l15565623507 -" echo "----------------------------------------------------------------------------------------------------" } keepalived_ipvsadm(){ index=0 real_n=0 keep=/etc/keepalived/keepalived.conf yum -y -q install ipvsadm keepalived &>/dev/null echo -e "e[1;33m----------------------安装ipvsadm和keepalived成功-------------------------e[0m" sed -i 's/ interface eth0/ interface ens33/g' $keep echo -e "e[1;33m----------------------修改网卡成功---------------------------------------e[0m" sed -i -e '60,$d' -i -e '31,32d' $keep sed -i '44,51d' $keep sed -i '41,48d' $keep echo -e "e[1;33m----------------------删除多余行成功-------------------------------------e[0m" sed -i 's/ vrrp_strict/ #vrrp_strict/g' $keep echo -e "e[1;33m----------------------注释 vrrp_strict成功-------------------------------e[0m" sed -i 's/ persistence_timeout 50/ #persistence_timeout 50/g' $keep echo -e "e[1;33m----------------------注释 persistence_timeout 50成功--------------------e[0m" sed -i 's/ lb_kind NAT/ lb_kind DR/g' $keep echo -e "e[1;33m----------------------修改为DR模式成功-----------------------------------e[0m" read -p "请输入VIP地址:" VIP sed -i 's/ 192.168.200.16/ '"$VIP"'/g' $keep sed -i 's/virtual_server 192.168.200.100 443 {/virtual_server '"$VIP"' 80 {/g' $keep echo -e "e[1;33m----------------------增加VIP地址成功-----------------------------------e[0m" ipvsadm -A -t $VIP:80 -s rr while : do read -p "请输入真实服务器的个数:" n if [ $n -gt 0 ];then while : do read -p "请输入真实服务器的IP地址:" RVIP array[$index]=$RVIP #定义 hosts.txt cat >> hosts.txt << EOF $RVIP EOF index=` expr $index + 1` real_n=`expr $real_n + 1` if [ $real_n -eq $n ]; then break fi done break else echo "真实服务器的个数必须大于0" fi done if [ $index -gt 1 ]; then index=0 a=0 line=39 while : do sed -i ''"$line"'a real_server '"${array[$index]}"' 80 { weight 1 TCP_CHECK { connect_prot 80 connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } }' $keep ipvsadm -a -t $VIP:80 -r ${array[$index]}:80 -g line=` expr $line + 9` index=` expr $index + 1` a=` expr $a + 1 ` if [ $a -eq $n ]; then break fi done else sed -i '39a real_server '"${array[$index]}"' 80 { weight 1 TCP_CHECK { connect_prot 80 connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } }' $keep fi systemctl restart keepalived echo -e "e[1;33m----------------------keepalived启动成功-------------------------------------e[0m" for i in {1..10} do echo "-" done read -p "是否做过免密环境(yes/no):" pass if [ $pass == "no" ]; then rpm -q expect if [ $? -eq 0 ]; then echo -e "e[1;33m--已安装--e[0m" else yum -y install expect &>/dev/null if [ $? -eq 0 ]; then echo -e "e[1;33m--安装成功--e[0m" else echo -e "e[1;33m--安装失败--e[0m" fi fi #本机是否有ssh密钥 没有则生成 if [ ! -f ~/.ssh/id_rsa ]; then echo -e "e[1;33m--请按3次enter健--e[0m" ssh-keygen -t rsa fi ssh_expect() { expect -c "set timeout -1; spawn ssh-copy-id root@$1 expect { "yes/no" { send -- yes ;exp_continue;} "password:" { send -- $2 ;exp_continue;} eof }"; } read -p "请输入真实服务器密码:" passwd for ip in `cat hosts.txt|awk '{print $1}'` do ssh_expect ${ip} ${passwd} done else break fi ipvsadm(){ touch ipvsadm.sh path=`pwd`/ipvsadm.sh rvip=$VIP cat >> ipvsadm.sh<<EOF #!/usr/bin/bash cd /etc/sysconfig/network-scripts/ cp ifcfg-lo ifcfg-lo:0 sed -i 's/DEVICE=lo/DEVICE=lo:0/g' ifcfg-lo:0 sed -i 's/IPADDR=127.0.0.1/IPADDR=$rvip/g' ifcfg-lo:0 sed -i 's/NETMASK=255.0.0.0/NETMASK=255.255.255.255/g' ifcfg-lo:0 sed -i '4,7d' ifcfg-lo:0 sed -i '5d' ifcfg-lo:0 systemctl restart network echo -e "e[1;33m----------------------网卡修改成功-------------------------------------e[0m" echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce echo -e "e[1;33m----------------------响应级别修改成功-------------------------------------e[0m" route add $rvip dev lo echo -e "e[1;33m----------------------路由增加成功-------------------------------------e[0m" EOF ifip=`ifconfig |grep inet|awk 'NR==1{print$2}'` for ip in `cat hosts.txt|awk '{print $1}'` do ssh root@$ip "mkdir -p /opt/lxl" scp root@$ifip:$path $ip:/opt/lxl/ ssh root@$ip "sh /opt/lxl/ipvsadm.sh" done } } keepalived_ipvsadm ipvsadm author