• 自动化脚本-配置LVS(DR模式)


    一,获取所需配置的主机IP

    cat get_ip.sh

      

    #!/bin/bash
    #将获得到的IP地址写进IP.txt
    >ip.txt
    #将原有的hosts信息清除
    >/root/.ssh/known_hosts
    
    #如果没有公钥,退出建立
    if [ ! -f ~/.ssh/id_rsa.pub ];then
            echo "请使用ssh-kengen建立密钥!!!"
            exit
    fi
    yum -y install expect
    
    for i in {2..254}
    do
            {
            ip=192.168.122.$i
            #ping一次,超时1s
            ping -c1 -W1 $ip &>/dev/null
            if [ $? -eq 0 ];then
                    #ping成功则记录到ip.txt中
                    echo $ip >> ip.txt
                    #使用expect执行下列内容,spawn负责传递公钥给ip,传递yes和密码给可能出现的询问,遇到"#"退出expect
                    /usr/bin/expect <<-EOF
                    set timeout 10
                    spawn ssh-copy-id -i $ip
                    expect {
                            "*yes/no" { send "yes
    "; exp_continue }
                            "*password:" { send "uplooking
    " }
                    }
            
                    expect "#"
                    send "exit
    "
                    expect eof
                    EOF
            fi
            }&
    done
    
    wait
    echo "finish..."

     二,配置RS

    vim lvs_dr_realserver_init.sh

    #!/bin/bash
    #LVS_DR_Realserver初始化
    #设置分隔符为回车
    IFS=$'
    '
    #避免sed修改文件时导致该文件的软连接失效
    alias sed='sed -c --follow-symlinks'
    ntpserver=172.16.8.100
    gw=192.168.122.1
    vip=192.168.122.100
    
    for line in `cat real_serverip.txt`
    #文件格式如下real_serverip.txt
    #old_ip         hostname        newip
    #192.168.122.62 web10           192.168.122.10
    #192.168.122.72 web20           192.168.122.20
    #192.168.122.82 web30           192.168.122.30
    #...
    do
            {
            ip=`echo $line |awk '{print $1}'`
            hostname=`echo $line |awk '{print $2}'`
            newip=`echo $line |awk '{print $3}'`
            }
            #''强引用,变量失效;""变量正常调用
            ssh root@$ip 'chkconfig NetworkManager off'
            ssh root@$ip 'iptables -F; service iptables save'
            #sed c 取代
            ssh root@$ip "sed -r -i "/^SELINUX/cSELINUX=disabled" /etc/selinux/config"
            ssh root@$ip "sed -r -i "/^HOSTNAME/cHOSTNAME=$hostname" /etc/sysconfig/network"
            ssh root@$ip "sed -r -i "/^BOOTPROTO/cBOOTPROTO=none" /etc/sysconfig/network-scripts/ifcfg-eth0"
            #sed 3a 第三行后边新增
            ssh root@$ip "sed -r -i "/3aIPADDR=$newip" /etc/sysconfig/network-scripts/ifcfg-eth0"
            ssh root@$ip "sed -r -i "/3aGATEWAY=$gw" /etc/sysconfig/network-scripts/ifcfg-eth0"
            #ssh root@$ip "wget ftp://172.16.8.100/rhel6.repo -0 /etc/yum.repos.d/rhel6.repo"
            ssh root@$ip "yum -y install lftp tree httpd"
            ssh root@$ip "chkconfig httpd on"
            ssh root@$ip "echo $hostname > /var/www/html/index.html"
            ssh root@$ip "ntpdate -b $ntpserver"
            ssh root@$ip "echo 'echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore' >> /etc/rc.local"
            ssh root@$ip "echo 'echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce' >> /etc/rc.local"
            ssh root@$ip "echo 'echo 'ip addr add dev lo' $vip/32 >> /etc/rc.local"
            ssh root@$ip "reboot"
            }&
    done
    
    wait
    echo "所有主机初始化完成"

     二,调度器初始化

    vim lvs_dr_director_init.sh

    #!/bin/bash
    #LVS_DR_Director初始化
    #设置分隔符为回车
    IFS=$'
    '
    #避免sed修改文件时导致该文件的软连接失效
    alias sed='sed -c --follow-symlinks'
    ntpserver=172.16.8.100
    gw=192.168.122.1
    
    for line in `cat ip.txt`
    #文件格式如下ip.txt
    #old_ip         hostname        newip
    #192.168.122.52 director1       192.168.122.2
    #192.168.122.52 director2       192.168.122.3
    #...
    do
            {
            ip=`echo $line |awk '{print $1}'`
            hostname=`echo $line |awk '{print $2}'`
            newip=`echo $line |awk '{print $3}'`
            #''强引用,变量失效;""变量正常调用
            ssh root@$ip 'chkconfig NetworkManager off'
            ssh root@$ip 'iptables -F; service iptables save'
            #sed c 取代
            ssh root@$ip "sed -r -i "/^SELINUX/cSELINUX=disabled" /etc/selinux/config"
            ssh root@$ip "sed -r -i "/^HOSTNAME/cHOSTNAME=$hostname" /etc/sysconfig/network"
            ssh root@$ip "sed -r -i "/^BOOTPROTO/cBOOTPROTO=none" /etc/sysconfig/network-scripts/ifcfg-eth0"
            #sed 3a 第三行后边新增
            ssh root@$ip "sed -r -i "/3aIPADDR=$newip" /etc/sysconfig/network-scripts/ifcfg-eth0"
            ssh root@$ip "sed -r -i "/3aGATEWAY=$gw" /etc/sysconfig/network-scripts/ifcfg-eth0"
            #ssh root@$ip "wget ftp://172.16.8.100/rhel6.repo -0 /etc/yum.repos.d/rhel6.repo"
            ssh root@$ip "yum -y install ipvsadm keepalived"
            ssh root@$ip "ntpdate -b $ntpserver"
            ssh root@$ip "reboot"
            }&
    done
    
    wait
    echo "所有主机初始化完成"

    三、配置调度器

  • 相关阅读:
    pip:No module named setuptools_rust
    pip:ffi.h: No such file or directory"
    docker一键搭建postgresql脚本(全网精华版原创)
    前端父子页面传递消息
    ubuntu内核系统安装node和npm打包前端代码
    强制刷新dom问题
    关于html中属性设置在代码中取消问题
    mybatis-mapper.xml中调用方法
    mysql内置属性及方法
    swagger入门
  • 原文地址:https://www.cnblogs.com/suminem/p/10695400.html
Copyright © 2020-2023  润新知