openstack自动化搭建脚本

Posted cocl888

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了openstack自动化搭建脚本相关的知识,希望对你有一定的参考价值。

Openstack平台部署+节点扩容

1)搭建脚本

 1 #!/bin/bash
 2 #openstack私有云平台部署
 3 #脚本使用前提:三台虚拟机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源正常;本次部署为精简版安装
 4 source /root/moudle.sh
 5 while :
 6 do
 7 echo "#############################_menu_##################################"
 8 echo "1.openstack服务器环境部署与检查"
 9 echo "2.nova环境部署与检查"
10 echo "3.openstack服务安装与登录"
11 echo "4.openstack节点扩容"
12 echo "5.exit"
13 echo "#####################################################################"
14 read -p "请输入您要部署的服务(1|2|3|4|5):" select
15 if [ "$select" == "1" ];then
16     env_openstack
17 elif [ "$select" == "2" ];then
18     env_nova
19 elif [ "$select" == "3" ];then
20     install_openstack
21 elif [ "$select" == "4" ];then
22     ext_nova
23 elif [ "$select" == "5" ];then
24     exit
25 else
26     echo "I AM SORRY"
27     sleep 2
28 fi
29 done

2)脚本模块

  1 #!/bin/bash
  2 #函数定义
  3 env_openstack()
  4 {    
  5     hostnamectl set-hostname openstack
  6     hostname openstack
  7     sleep 1
  8     echo "1.主机名已同步"
  9 
 10     echo "192.168.1.10 openstack
 11 192.168.1.11 nova01
 12 192.168.1.12 nova02
 13 " >> /etc/hosts
 14     sleep 1
 15     echo "2.域名已同步"
 16 
 17     sed -i /search/d /etc/resolv.conf
 18     sleep 1
 19     echo "3.域名解析服务器已指定"
 20 
 21     sed -i 7 s/gateway/192.168.1.254/ /etc/chrony.conf #1.254是真机ip
 22     systemctl restart chronyd 
 23     sleep 5
 24     if [ `chronyc sources -v | tail -1 | awk {print $1}` == ^* ];then
 25          echo "4.时间同步成功"
 26     else
 27          echo ##########!!!时间未同步,请检查
 28     fi
 29 
 30     echo "[local_repo]
 31 name=CentOS-$releasever - Base
 32 baseurl="ftp://192.168.1.254/centos-1804"
 33 enabled=1
 34 gpgcheck=0
 35 [extras]
 36 name=extras
 37 baseurl="ftp://192.168.1.254/extras/"
 38 enabled=1
 39 gpgcheck=0
 40 [openstack1]
 41 name=openstack1
 42 baseurl="ftp://192.168.1.254/openstack/rhel-7-server-openstack-10-rpms"
 43 enabled=1
 44 gpgcheck=0
 45 [openstack2]
 46 name=openstack2
 47 baseurl="ftp://192.168.1.254/openstack/rhel-7-server-openstack-10-devtools-rpms"
 48 enabled=1
 49 gpgcheck=0
 50 " > /etc/yum.repos.d/local.repo
 51     sleep 1
 52     if [ `yum repolist | tail -1 | awk {print $2}` == "10,670" ];then
 53          echo "5.yum源配置成功"
 54     else 
 55          echo "##########!!!yum源有问题"
 56     fi
 57 
 58     yum remove firewalld-* &>/dev/null;yum remove NetworkManager &>/dev/null
 59     sleep 1 
 60     echo "6.已卸载firewalld和NetworkManager"
 61     sleep 1 
 62     if [ `cat /etc/sysconfig/network-scripts/ifcfg-eth0 | awk -F" /ONBOOT/{print $2}` == "yes" ];then
 63          echo "7.network冲突解决"
 64     else
 65          echo "###########!!!network冲突"
 66     fi
 67     sleep 1
 68     ping -c 3 192.168.1.11 &>/dev/null && ping -c 3 192.168.1.12 &>/dev/null && echo "8.网络畅通" || echo "##########!!!请检查网络"
 69     sleep 1
 70     echo "环境依赖包安装,请稍等..."
 71     sleep 1
 72     yum -y install python-setuptools &> /dev/null && echo "9.依赖包安装成功,环境配置完毕,请进行下一步,安装openstack服务"
 73     sleep 1.5
 74 }
 75 
 76 env_nova()
 77 {
 78     read -p "请输入主机名尾号(01|02):" a
 79     hostnamectl set-hostname nova$a
 80         hostname nova$a
 81         sleep 1
 82         echo "1.主机名已同步"
 83 
 84         echo "192.168.1.10 openstack
 85 192.168.1.11 nova01
 86 192.168.1.12 nova02
 87 " >> /etc/hosts
 88         sleep 1
 89         echo "2.域名已同步"
 90 
 91         sed -i /search/d /etc/resolv.conf
 92         sleep 1
 93         echo "3.域名解析服务器已指定"
 94 
 95         sed -i 7 s/gateway/192.168.1.254/ /etc/chrony.conf #1.254是真机ip
 96         systemctl restart chronyd
 97     sleep 5
 98         if [ `chronyc sources -v | tail -1 | awk {print $1}` == "^*" ];then
 99                 echo "4.时间同步成功"
100         else
101                 echo ##########!!!时间未同步,请检查
102         fi
103 
104         echo "[local_repo]
105 name=CentOS-$releasever - Base
106 baseurl="ftp://192.168.1.254/centos-1804"
107 enabled=1
108 gpgcheck=0
109 [extras]
110 name=extras
111 baseurl="ftp://192.168.1.254/extras/"
112 enabled=1
113 gpgcheck=0
114 [openstack1]
115 name=openstack1
116 baseurl="ftp://192.168.1.254/openstack/rhel-7-server-openstack-10-rpms"
117 enabled=1
118 gpgcheck=0
119 [openstack2]
120 name=openstack2
121 baseurl="ftp://192.168.1.254/openstack/rhel-7-server-openstack-10-devtools-rpms"
122 enabled=1
123 gpgcheck=0
124 " > /etc/yum.repos.d/local.repo
125     sleep 1
126         if [ `yum repolist | tail -1 | awk {print $2}` == "10,670" ];then
127                 echo "5.yum源配置成功"
128         else
129                 echo "##########!!!yum源有问题"
130         fi
131     yum remove firewalld-* &>/dev/null;yum remove NetworkManager &>/dev/null
132         sleep 1
133     echo "6.已卸载firewalld和NetworkManager"
134     sleep 1
135         if [ `cat /etc/sysconfig/network-scripts/ifcfg-eth0 | awk -F" /ONBOOT/{print $2}` == "yes" ];then
136                 echo "7.network冲突解决"
137         else
138                 echo "###########!!!network冲突"
139         fi
140     sleep 1
141         ping -c 3 192.168.1.10 &>/dev/null && ping -c 3 192.168.1.12 &>/dev/null && echo "8.网络畅通" || echo "##########!!!请检查网络"
142 
143         echo "环境依赖包安装,请稍等..."
144         yum -y install python-setuptools qemu-kvm libvirt-daemon libvirt-daemon-driver-qemu libvirt-client &> /dev/null && echo "9.依赖包安装成功,环境配置完毕,请进行下一步"
145     sleep 1.5
146 }
147 
148 install_openstack()
149 {
150     yum -y install openstack-packstack
151     cd /root/
152     packstack --gen-answer-file=answer.ini
153     sed -i 42 s/=y/=n/ answer.ini 
154     sed -i 45 s/=y/=n/ answer.ini 
155     sed -i 49 s/=y/=n/ answer.ini 
156     sed -i 53 s/=y/=n/ answer.ini 
157     sed -i 75 s/=/=192.168.1.254/ answer.ini 
158     sed -i 98 s/1.10/1.11/ answer.ini 
159     sed -i 102 s/1.10/1.10,192.168.1.11/ answer.ini 
160     sed -i 333 s/=.*$/=a/ answer.ini 
161     sed -i 840 s/=vx/=flat,vx/ answer.ini 
162     sed -i 910 s/=/=physnet1:br-ex/ answer.ini 
163     sed -i 921 s/=/=br-ex:eth0/ answer.ini 
164     sed -i 1179 s/=y/=n/ answer.ini 
165     echo "开始安装请耐心等待大约30min..."
166     packstack --answer-file=answer.ini
167     sed -i /WSGIProcessGroup apache/a WSGIApplicationGroup %{GLOBAL} /etc/httpd/conf.d/15-horizon-vhost.conf
168     apachectl graceful
169     echo "安装成功!欢迎使用,请访问以下网址http://192.168.1.10进行访问,账户密码为您的应答文件设置"
170 }
171 
172 ext_nova()
173 {
174     cd /root/
175         sed -i 98 s/1.11/1.11,192.168.1.12/ answer.ini
176         sed -i 102 s/1.11/1.11,192.168.1.12/ answer.ini
177         echo "开始安装请耐心等待大约30min..."
178         packstack --answer-file=answer.ini
179         sed -i /WSGIProcessGroup apache/a WSGIApplicationGroup %{GLOBAL} /etc/httpd/conf.d/15-horizon-vhost.conf
180         apachectl graceful
181         echo "扩容成功!欢迎使用,请访问以下网址http://192.168.1.10进行访问,账户密码>为您的应答文件设置"    
182 }

以上是关于openstack自动化搭建脚本的主要内容,如果未能解决你的问题,请参考以下文章

什么是openstack的 metadata

企业运维之 openstack 的介绍以及组件搭建

openstack 之 使用virtualbox 脚本自动安装mirantis openstack

CentOS7下利用cobbler搭建openstack本地源

sh 用于在Openstack上自动部署k8的脚本。

OpenStack——自动安装部署