openstack自动化搭建脚本

jmppok 2020-01-07

Openstack平台部署+节点扩容

1)搭建脚本

#!/bin/bash
#openstack私有云平台部署
#脚本使用前提:三台虚拟机openstack(ip地址:192.168.1.10,至少7G内存,50G硬盘),nova01(ip地址:192.168.1.11至少4G内存,100G硬盘),扩容用nova02(ip地址:192.168.1.12至少4G内存,100G硬盘);yum源正常;本次部署为精简版安装
source /root/moudle.sh
while :
do
echo "#############################_menu_##################################"
echo "1.openstack服务器环境部署与检查"
echo "2.nova环境部署与检查"
echo "3.openstack服务安装与登录"
echo "4.openstack节点扩容"
echo "5.exit"
echo "#####################################################################"
read -p "请输入您要部署的服务(1|2|3|4|5):" select
if [ "$select" == "1" ];then
    env_openstack
elif [ "$select" == "2" ];then
    env_nova
elif [ "$select" == "3" ];then
    install_openstack
elif [ "$select" == "4" ];then
    ext_nova
elif [ "$select" == "5" ];then
    exit
else
    echo "I AM SORRY"
    sleep 2
fi
done

2)脚本模块

#!/bin/bash
#函数定义
env_openstack()
{    
    hostnamectl set-hostname openstack
    hostname openstack
    sleep 1
    echo "1.主机名已同步"

    echo "192.168.1.10 openstack
192.168.1.11 nova01
192.168.1.12 nova02
" >> /etc/hosts
    sleep 1
    echo "2.域名已同步"

    sed -i ‘/search/d‘ /etc/resolv.conf
    sleep 1
    echo "3.域名解析服务器已指定"

    sed -i ‘7 s/gateway/192.168.1.254/‘ /etc/chrony.conf #1.254是真机ip
    systemctl restart chronyd 
    sleep 5
    if [ `chronyc sources -v | tail -1 | awk ‘{print $1}‘` == ^* ];then
         echo "4.时间同步成功"
    else
         echo ‘##########!!!时间未同步,请检查‘
    fi

    echo "[local_repo]
name=CentOS-$releasever - Base
baseurl="ftp://192.168.1.254/centos-1804"
enabled=1
gpgcheck=0
[extras]
name=extras
baseurl="ftp://192.168.1.254/extras/"
enabled=1
gpgcheck=0
[openstack1]
name=openstack1
baseurl="ftp://192.168.1.254/openstack/rhel-7-server-openstack-10-rpms"
enabled=1
gpgcheck=0
[openstack2]
name=openstack2
baseurl="ftp://192.168.1.254/openstack/rhel-7-server-openstack-10-devtools-rpms"
enabled=1
gpgcheck=0
" > /etc/yum.repos.d/local.repo
    sleep 1
    if [ `yum repolist | tail -1 | awk ‘{print $2}‘` == "10,670" ];then
         echo "5.yum源配置成功"
    else 
         echo "##########!!!yum源有问题"
    fi

    yum remove firewalld-* &>/dev/null;yum remove NetworkManager &>/dev/null
    sleep 1 
    echo "6.已卸载firewalld和NetworkManager"
    sleep 1 
    if [ `cat /etc/sysconfig/network-scripts/ifcfg-eth0 | awk -F‘"‘ ‘/ONBOOT/{print $2}‘` == "yes" ];then
         echo "7.network冲突解决"
    else
         echo "###########!!!network冲突"
    fi
    sleep 1
    ping -c 3 192.168.1.11 &>/dev/null && ping -c 3 192.168.1.12 &>/dev/null && echo "8.网络畅通" || echo "##########!!!请检查网络"
    sleep 1
    echo "环境依赖包安装,请稍等..."
    sleep 1
    yum -y install python-setuptools &> /dev/null && echo "9.依赖包安装成功,环境配置完毕,请进行下一步,安装openstack服务"
    sleep 1.5
}

env_nova()
{
    read -p "请输入主机名尾号(01|02):" a
    hostnamectl set-hostname nova$a
        hostname nova$a
        sleep 1
        echo "1.主机名已同步"

        echo "192.168.1.10 openstack
192.168.1.11 nova01
192.168.1.12 nova02
" >> /etc/hosts
        sleep 1
        echo "2.域名已同步"

        sed -i ‘/search/d‘ /etc/resolv.conf
        sleep 1
        echo "3.域名解析服务器已指定"

        sed -i ‘7 s/gateway/192.168.1.254/‘ /etc/chrony.conf #1.254是真机ip
        systemctl restart chronyd
    sleep 5
        if [ `chronyc sources -v | tail -1 | awk ‘{print $1}‘` == "^*" ];then
                echo "4.时间同步成功"
        else
                echo ‘##########!!!时间未同步,请检查‘
        fi

        echo "[local_repo]
name=CentOS-$releasever - Base
baseurl="ftp://192.168.1.254/centos-1804"
enabled=1
gpgcheck=0
[extras]
name=extras
baseurl="ftp://192.168.1.254/extras/"
enabled=1
gpgcheck=0
[openstack1]
name=openstack1
baseurl="ftp://192.168.1.254/openstack/rhel-7-server-openstack-10-rpms"
enabled=1
gpgcheck=0
[openstack2]
name=openstack2
baseurl="ftp://192.168.1.254/openstack/rhel-7-server-openstack-10-devtools-rpms"
enabled=1
gpgcheck=0
" > /etc/yum.repos.d/local.repo
    sleep 1
        if [ `yum repolist | tail -1 | awk ‘{print $2}‘` == "10,670" ];then
                echo "5.yum源配置成功"
        else
                echo "##########!!!yum源有问题"
        fi
    yum remove firewalld-* &>/dev/null;yum remove NetworkManager &>/dev/null
        sleep 1
    echo "6.已卸载firewalld和NetworkManager"
    sleep 1
        if [ `cat /etc/sysconfig/network-scripts/ifcfg-eth0 | awk -F‘"‘ ‘/ONBOOT/{print $2}‘` == "yes" ];then
                echo "7.network冲突解决"
        else
                echo "###########!!!network冲突"
        fi
    sleep 1
        ping -c 3 192.168.1.10 &>/dev/null && ping -c 3 192.168.1.12 &>/dev/null && echo "8.网络畅通" || echo "##########!!!请检查网络"

        echo "环境依赖包安装,请稍等..."
        yum -y install python-setuptools qemu-kvm libvirt-daemon libvirt-daemon-driver-qemu libvirt-client &> /dev/null && echo "9.依赖包安装成功,环境配置完毕,请进行下一步"
    sleep 1.5
}

install_openstack()
{
    yum -y install openstack-packstack
    cd /root/
    packstack --gen-answer-file=answer.ini
    sed -i ‘42 s/=y/=n/‘ answer.ini 
    sed -i ‘45 s/=y/=n/‘ answer.ini 
    sed -i ‘49 s/=y/=n/‘ answer.ini 
    sed -i ‘53 s/=y/=n/‘ answer.ini 
    sed -i ‘75 s/=/=192.168.1.254/‘ answer.ini 
    sed -i ‘98 s/1.10/1.11/‘ answer.ini 
    sed -i ‘102 s/1.10/1.10,192.168.1.11/‘ answer.ini 
    sed -i ‘333 s/=.*$/=a/‘ answer.ini 
    sed -i ‘840 s/=vx/=flat,vx/‘ answer.ini 
    sed -i ‘910 s/=/=physnet1:br-ex/‘ answer.ini 
    sed -i ‘921 s/=/=br-ex:eth0/‘ answer.ini 
    sed -i ‘1179 s/=y/=n/‘ answer.ini 
    echo "开始安装请耐心等待大约30min..."
    packstack --answer-file=answer.ini
    sed -i ‘/WSGIProcessGroup apache/a WSGIApplicationGroup %{GLOBAL}‘ /etc/httpd/conf.d/15-horizon-vhost.conf
    apachectl graceful
    echo "安装成功!欢迎使用,请访问以下网址http://192.168.1.10进行访问,账户密码为您的应答文件设置"
}

ext_nova()
{
    cd /root/
        sed -i ‘98 s/1.11/1.11,192.168.1.12/‘ answer.ini
        sed -i ‘102 s/1.11/1.11,192.168.1.12/‘ answer.ini
        echo "开始安装请耐心等待大约30min..."
        packstack --answer-file=answer.ini
        sed -i ‘/WSGIProcessGroup apache/a WSGIApplicationGroup %{GLOBAL}‘ /etc/httpd/conf.d/15-horizon-vhost.conf
        apachectl graceful
        echo "扩容成功!欢迎使用,请访问以下网址http://192.168.1.10进行访问,账户密码>为您的应答文件设置"    
}

相关推荐