侧边栏壁纸
博主头像
枕头下放双臭袜子博主等级

今我何功德,曾不事农桑

  • 累计撰写 162 篇文章
  • 累计创建 30 个标签
  • 累计收到 0 条评论

Openstack-nova组件一键部署安装脚本(v1版,命令组合)

枕头下放双臭袜子
2020-01-10 / 0 评论 / 0 点赞 / 714 阅读 / 0 字 / 正在检测是否收录...

  0x01

    刚好有需要,就自己写了一个shell来自动化安装部署openstack-nova组件

    ishells!

  0x02

    踩坑:

      1、nova的配置文件全部是被注释的,有的需要更改的字段是被注释了,有的需要更改的字段根本就没有!这就导致使用sed匹配注释行进行更改配置文件的话,可能有的匹配不到

      2、如果进行[ ]匹配,一定要使用正则把行首行尾匹配上,不然配置文件可能多处出现所匹配字段

      3、在运行nova-compute服务前一定要确认两台虚拟机的防火墙已经关掉!!!一定要确认两台虚拟机的防火墙已经关掉!!!一定要确认两台虚拟机的防火墙已经关掉!!!

      4、脚本已经测试,放心食用,所有1790600080处修改为自己的密码,controller-ZJB修改为自己的主机名

  0x03

    思路:

      1、修改配置文件时使用cat -n首先将配置文件全部输出,然后使用grep + RE匹配到[],在使用awk '{print $1}'获取到[ ]的行数

      2、在使用sed -i根据行数进行直接插入

      3、每插入一行都会导致行数发生变化,所以获取行数要紧挨着sed

      4、多行插入可以使用内置计算器expr行数+1即可

  0x04:(controller节点代码)

#!/bin/bash
#This is a script for editing nova-controller file
# 如果你需要使用此脚本,将主机名controller-ZJB修改为你的主机名
# 所有1790600080处修改为自己的密码
readonly create_domain_pass=1790600080
readonly create_user_pass=1790600080

echo "   这是一个一键配置Nova-Controller组件的脚本:"
echo "   --------------------------------"
echo "   1、下面开始创建数据库并赋予相应权限……"
   mysql -uroot -p1790600080 <<EOF
   CREATE DATABASE nova_api;
   CREATE DATABASE nova;
   CREATE DATABASE nova_cell0;
   CREATE DATABASE placement;
   GRANT ALL PRIVILEGES ON nova_api.* TO 'nova'@'localhost' IDENTIFIED BY '1790600080';
   GRANT ALL PRIVILEGES ON nova_api.* TO 'nova'@'%' IDENTIFIED BY '1790600080';
   GRANT ALL PRIVILEGES ON nova.* TO 'nova'@'localhost' IDENTIFIED BY '1790600080';
   GRANT ALL PRIVILEGES ON nova.* TO 'nova'@'%' IDENTIFIED BY '1790600080';
   GRANT ALL PRIVILEGES ON nova_cell0.* TO 'nova'@'localhost' IDENTIFIED BY '1790600080';
   GRANT ALL PRIVILEGES ON nova_cell0.* TO 'nova'@'%' IDENTIFIED BY '1790600080';
   GRANT ALL PRIVILEGES ON placement.* TO 'placement'@'localhost' IDENTIFIED BY '1790600080';
   GRANT ALL PRIVILEGES ON placement.* TO 'placement'@'%' IDENTIFIED BY '1790600080';
EOF

echo "   --------------------------------"
echo "   数据库已创建成功并赋予了相应权限"
echo "   --------------------------------"
echo "   2、创建Compute服务凭证:"

export OS_PROJECT_DOMAIN_NAME=Default
export OS_USER_DOMAIN_NAME=Default
export OS_PROJECT_NAME=admin
export OS_USERNAME=admin
export OS_PASSWORD=1790600080
export OS_AUTH_URL=http://controller-ZJB:5000/v3
export OS_IDENTITY_API_VERSION=3
export OS_IMAGE_API_VERSION=2


openstack user create --domain default --password $create_domain_pass nova
openstack role add --project service --user nova admin
openstack service create --name nova --description "OpenStack Compute" compute
openstack endpoint create --region RegionOne compute public http://controller-ZJB:8774/v2.1
openstack endpoint create --region RegionOne compute internal http://controller-ZJB:8774/v2.1
openstack endpoint create --region RegionOne compute admin http://controller-ZJB:8774/v2.1
openstack user create --domain default --password $create_user_pass placement
openstack role add --project service --user placement admin
openstack service create --name placement --description "Placement API" placement
openstack endpoint create --region RegionOne placement public http://controller-ZJB:8778
openstack endpoint create --region RegionOne placement internal http://controller-ZJB:8778
openstack endpoint create --region RegionOne placement admin http://controller-ZJB:8778

echo "   --------------------------------"
echo "   Compute服务凭证已创建完成!"

echo "   3、安装软件包……"
yum -y install openstack-nova-api openstack-nova-conductor openstack-nova-console openstack-nova-novncproxy openstack-nova-scheduler openstack-nova-placement-api
echo "   --------------------------------"
if [ $? == 0 ];then
echo "    软件已成功安装!"
else
   echo "   软件安装出错,请手动查看问题!"
fi

echo "   --------------------------------"
echo "   4、修改Nova配置文件:"
echo " "
read -p  "     是否选择一键修改Nova配置文件(y/n)" answer




#判断/etc/nova/nova.conf文件是否存在,不存在直接输出错误
if [ $answer == "y" ];then
   ls /etc/nova/nova.conf  &>/dev/null
   if [ $? == 0 ];then

# 第一次尝试全部插入,多处出现则会插入到多处
#	sed -i '/\[DEFAULT\]/a\enabled_apis = osapi_compute,metadata' /etc/nova/nova.conf
#	sed -i '/\[api_database\]/a\connection = mysql+pymysql://nova:1790600080@controller-ZJB/nova_api' /etc/nova/nova.conf
#	sed -i '/\[database\]/a\connection = mysql+pymysql://nova:1790600080@controller-ZJB/nova' /etc/nova/nova.conf
#	sed -i '/\[placement_database\]/a\connection = mysql+pymysql://placement:1790600080@controller-ZJB/placement' /etc/nova/nova.conf
#   	sed -i '/\[DEFAULT\]/a\transport_url = rabbit://ZJB:1790600080@controller-ZJB' /etc/nova/nova.conf
#	sed -i '/\[api\]/a\auth_strategy = keystone' /etc/nova/nova.conf
#	sed -i '/\[keystone_authtoken\]/a\www_authenticate__uri = http://controller-ZJB:5000/\nauth_url = http://controller-ZJB:5000/v3\nmemcached_servers = controller-ZJB:11211\nauth_type = password\nproject_domain_name = Default\nuser_domain_name = Default\nproject_name = service\nusername = nova\npassword =1790600080' /etc/nova/nova.conf
#	sed -i '/\[DEFAULT\]/a\my_ip = 192.168.52.100\nuse_neutron = true\nfirewall_driver = nova.virt.firewall.NoopFirewallDriver' /etc/nova/nova.conf
#	sed -i '/\[vnc\]/a\enabled = true\nserver_listen = $my_ip\nserver_proxyclient_address = $my_ip' /etc/nova/nova.conf
#	sed -i '/\[glance\]/a\api_servers = http://controller-ZJB:9292' /etc/nova/nova.conf
#	sed -i '/\[placement\]/a\region_name = RegionOne\nproject_domain_name = Default\nproject_name = service\nauth_type = password\nuser_domain_name = Default\nauth_url = http://controller-ZJB:5000/v3\nusername = placement\npassword =1790600080' /etc/nova/nova.conf

#第二次尝试,对照源配置文件,#字段仅出现一次就选择替换,有的#字段在多个【】地方都会出现,也不行
 #   sed -ie '/\[DEFAULT\]/a\enabled_apis = osapi_compute,metadata' /etc/nova/nova.conf
 #   sed -ie 's/^#connection=mysql:\/\/nova:nova@localhost\/nova/connection = mysql+pymysql:\/\/nova:1790600080@controller-ZJB\/nova_api/g'   /etc/nova/nova.conf
	# sed -ie 's/^#connection=mysql:\/\/nova:nova@localhost\/nova/connection = mysql+pymysql:\/\/nova:1790600080@controller-ZJB\/nova_api/g' /etc/nova/nova.conf
 #   sed -ie '/\[placement_database\]/a\connection = mysql+pymysql://placement:1790600080@controller-ZJB/placement' /etc/nova/nova.conf
 #   sed -ie 's/^#transport_url=<None>/transport_url = rabbit:\/\/openstack:1790600080@controller-ZJB' /etc/nova/nova.conf
 #   sed -ie 's/^#auth_strategy=keystone/auth_strategy=keystone/g' /etc/nova/nova.conf
 # 此处[keystone_authtoken]出现了多次,如果使用插入的话会在多处插入!

 # 多次尝试之后,决定通过行数向里添加内容,如sed -ie 'N;8a我要做运维' test.txt  即在test.txt文件第8行后添加"我要做运维",a换成i即为第8行前

 #  sed -i '/\[keystone_authtoken\]/a\www_authenticate__uri = http://controller-ZJB:5000/\nauth_url = http://controller-ZJB:5000/v3\nmemcached_servers = controller-ZJB:11211\nauth_type = password\nproject_domain_name = Default\nuser_domain_name = Default\nproject_name = service\nusername = nova\npassword =1790600080' /etc/nova/nova.conf
 #
   # sed -ie 's/^#www_authenticate_uri=<None>/www_authenticate__uri = http:\/\/controller-ZJB:5000\//g'
   # sed -ie 's/^#auth_uri=<None>/auth_url = http:\/\/controller-ZJB:5000\/v3/g' /etc/nova/nova.conf
   # sed -ie 's/^#memcached_servers=<None>/memcached_servers = controller-ZJB:11211/g' /etc/nova/nova.conf
   # sed -ie 's/^/g' /etc/nova/nova.conf

# 第三次直接根据未修改配置文件的行数进行插入数据,如sed -ie 'N;8a我要做运维' test.txt  即在test.txt文件第8行后添加"我要做运维",a换成i即为第8行前
# 如果根据行数修改的sed -i 'N;'  需要使用变量需要将格式变为 sed -i $变量'a需要添加的字符'
# 所以定义了变量值获取特定行的行数,然后sed使用变量,并根据变量行数值修改文件,如 api_start_line=`cat -n test.txt | grep "\[api\]$" | awk '{print $1}'`
#
# [DEFAULT]也会多次出现,所以也选择先获取行号在根据行号插入

   DEFAULT_line=`cat -n /etc/nova/nova.conf | grep "\[DEFAULT\]$" | awk '{print $1}'`

   sed -i $DEFAULT_line'a\enabled_apis = osapi_compute,metadata' /etc/nova/nova.conf
   sed -i `expr $DEFAULT_line + 1`'a\transport_url = rabbit://ZJB:1790600080@controller-ZJB' /etc/nova/nova.conf
   sed -i `expr $DEFAULT_line + 2`'a\enabled_apis = osapi_compute,metadata' /etc/nova/nova.conf
   sed -i `expr $DEFAULT_line + 3`'a\my_ip = 192.168.52.100\nuse_neutron = true\nfirewall_driver = nova.virt.firewall.NoopFirewallDriver' /etc/nova/nova.conf

# 其余部分可以根据行号插入,首先通过一个变量获取行号,在使用sed根据行数添加
   api_database_line=`cat -n /etc/nova/nova.conf | grep "\[api_database\]$" | awk '{print $1}'`
   sed -i $api_database_line'a\connection = mysql+pymysql://nova:1790600080@controller-ZJB/nova_api' /etc/nova/nova.conf
   database_line=`cat -n /etc/nova/nova.conf | grep "\[database\]$" | awk '{print $1}'`
   sed -i $database_line'a\connection = mysql+pymysql://nova:1790600080@controller-ZJB/nova' /etc/nova/nova.conf

   placement_database_line=`cat -n /etc/nova/nova.conf | grep "\[placement_database\]$" | awk '{print $1}'`
   sed -i $placement_database_line'a\connection = mysql+pymysql://placement:1790600080@controller-ZJB/placement' /etc/nova/nova.conf
   api_line=`cat -n /etc/nova/nova.conf | grep "\[api\]$" | awk '{print $1}'`
   sed -i $api_line'a\auth_strategy = keystone' /etc/nova/nova.conf

   keystone_authtoken_line=`cat -n /etc/nova/nova.conf | grep "\[keystone_authtoken\]$" | awk '{print $1}'`
   sed -i $keystone_authtoken_line'a\auth_url = http://controller-ZJB:5000/v3' /etc/nova/nova.conf
   sed -i `expr $keystone_authtoken_line + 1`'a\memcached_servers = controller-ZJB:11211' /etc/nova/nova.conf
   sed -i `expr $keystone_authtoken_line + 2`'a\auth_type = password' /etc/nova/nova.conf
   sed -i `expr $keystone_authtoken_line + 3`'a\project_domain_name = Default' /etc/nova/nova.conf
   sed -i `expr $keystone_authtoken_line + 4`'a\user_domain_name = Default' /etc/nova/nova.conf
   sed -i `expr $keystone_authtoken_line + 5`'a\project_name = service' /etc/nova/nova.conf
   sed -i `expr $keystone_authtoken_line + 6`'a\username = nova' /etc/nova/nova.conf
   sed -i `expr $keystone_authtoken_line + 7`'a\password = 1790600080' /etc/nova/nova.conf
   sed -i `expr $keystone_authtoken_line + 8`'a\' /etc/nova/nova.conf

   vnc_line=`cat -n /etc/nova/nova.conf | grep "\[vnc\]$" | awk '{print $1}'`
   sed -i $vnc_line'a\enabled = true'  /etc/nova/nova.conf
   sed -i `expr $vnc_line + 1`'a\server_listen = $my_ip'  /etc/nova/nova.conf
   sed -i `expr $vnc_line + 2`'a\server_proxyclient_address = $my_ip'  /etc/nova/nova.conf

   glance_line=`cat -n /etc/nova/nova.conf | grep "\[glance\]$" | awk '{print $1}'`
   sed -i $glance_line'a\api_servers = http://controller-ZJB:9292'  /etc/nova/nova.conf

   oslo_concurrency_line=`cat -n /etc/nova/nova.conf | grep "\[oslo_concurrency\]$" | awk '{print $1}'`
   sed -i $oslo_concurrency_line'a\lock_path = /var/lib/nova/tmp'  /etc/nova/nova.conf

   placement_line=`cat -n /etc/nova/nova.conf | grep "\[placement\]$" | awk '{print $1}'`
   sed -i $placement_line'a\region_name = RegionOne'  /etc/nova/nova.conf
   sed -i `expr $placement_line + 1`'a\project_domain_name = Default'  /etc/nova/nova.conf
   sed -i `expr $placement_line + 2`'a\project_name = service'  /etc/nova/nova.conf
   sed -i `expr $placement_line + 3`'a\auth_type = password'  /etc/nova/nova.conf
   sed -i `expr $placement_line + 4`'a\user_domain_name = Default'  /etc/nova/nova.conf
   sed -i `expr $placement_line + 5`'a\auth_url = http://controller-ZJB:5000/v3'  /etc/nova/nova.conf
   sed -i `expr $placement_line + 6`'a\username = placement'  /etc/nova/nova.conf
   sed -i `expr $placement_line + 7`'a\password = 1790600080'  /etc/nova/nova.conf

	echo "    1、/etc/nova/nova.conf文件已修改完毕"
   else
        echo "/etc/nova/nova.conf文件不存在!"
   fi

#判断/etc/httpd/conf.d/00-nova-placement-api.conf文件是否存在,不存在直接输出错误
   ls /etc/httpd/conf.d/00-nova-placement-api.conf  &>/dev/null
   if [ $? == 0 ]; then

#	cat  >> /etc/httpd/conf.d/00-nova-placement-api.conf <<EOF
#	   <Directory /usr/bin>
#  	      <IfVersion >= 2.4>
#      		 Require all granted
#   	      </IfVersion>
#   	      <IfVersion < 2.4>
#      		 Order allow,deny
#      		 Allow from all
#   	      </IfVersion>
#	   </Directory>
#EOF
	sed -i '$a\<Directory /usr/bin>\n \   <IfVersion >= 2.4>\n \      Require all granted\n \   </IfVersion>\n \   <IfVersion < 2.4>\n \      Order allow,deny\n \      Allow from all\n \   </IfVersion>\n \</Directory>' /etc/httpd/conf.d/00-nova-placement-api.conf

	echo "    2、/etc/httpd/conf.d/00-nova-placement-api.conf文件已修改完毕"
   else
	echo "/etc/httpd/conf.d/00-nova-placement-api.conf配置文件不存在"
   fi

   #修改完配置文件,重启httpd
   systemctl restart httpd

   #填充nova-api和 placement的数据库
   su -s /bin/sh -c "nova-manage api_db sync" nova
   su -s /bin/sh -c "nova-manage cell_v2 map_cell0" nova
   su -s /bin/sh -c "nova-manage cell_v2 create_cell --name=cell1 --verbose" nova
   su -s /bin/sh -c "nova-manage db sync" nova
   su -s /bin/sh -c "nova-manage cell_v2 list_cells" nova

   #重启nova相关服务
   systemctl enable openstack-nova-api.service openstack-nova-consoleauth openstack-nova-scheduler.service openstack-nova-conductor.service openstack-nova-novncproxy.service
   systemctl start openstack-nova-api.service openstack-nova-consoleauth openstack-nova-scheduler.service openstack-nova-conductor.service openstack-nova-novncproxy.service
elif [ $answer == "n" ]; then
   echo " "
   echo "    已退出此脚本"
fi

# 配置完nova-compute在controller上发现就算节点的最后两条命令未做,因为执行时间不一致

  0x05:(compute节点代码)

#!/bin/bash
#This is a script for editing nova-compute file
# 如果你需要使用此脚本,将主机名controller-ZJB修改为你的主机名
# 所有1790600080处修改为自己的密码

echo "   这是一个一键配置Nova-Compute组件的脚本:"
echo "   1、下面开始安装openstack-nova-compute……"
yum -y install openstack-nova-compute
if [ $? == 0 ];then
   echo "   success: 组件已安装完毕,下面修改Nova-Compute配置文件:"
else
   echo "   error:组件安装出现了问题!请手动检查是否安装成功!"
fi
echo " "

read -p  "     2、是否选择一键修改Nova配置文件(y/n)" answer

#判断/etc/nova/nova.conf文件是否存在,不存在直接输出错误
if [ $answer == "y" ];then
   ls /etc/nova/nova.conf  &>/dev/null
   if [ $? == 0 ];then

#  设置变量获取行数,使用sed根据行号添加
     DEFAULT_line=`cat -n /etc/nova/nova.conf | grep "\[DEFAULT\]$" | awk '{print $1}'`

     sed -i $DEFAULT_line'a\enabled_apis = osapi_compute,metadata' /etc/nova/nova.conf
     sed -i `expr $DEFAULT_line + 1`'a\transport_url = rabbit://ZJB:1790600080@controller-ZJB' /etc/nova/nova.conf
     sed -i `expr $DEFAULT_line + 2`'a\my_ip = 192.168.52.200' /etc/nova/nova.conf
     sed -i `expr $DEFAULT_line + 3`'a\use_neutron = true' /etc/nova/nova.conf
     sed -i `expr $DEFAULT_line + 4`'a\firewall_driver = nova.virt.firewall.NoopFirewallDriver' /etc/nova/nova.conf


     api_line=`cat -n /etc/nova/nova.conf | grep "\[api\]$" | awk '{print $1}'`
	   sed -i $api_line'a\auth_strategy = keystone' /etc/nova/nova.conf

     keystone_authtoken_line=`cat -n /etc/nova/nova.conf | grep "\[keystone_authtoken\]$" | awk  '{print $1}'`

     sed -i  $keystone_authtoken_line'a\auth_url = http://controller-ZJB:5000/v3' /etc/nova/nova.conf
     sed -i `expr $keystone_authtoken_line + 1`'a\memcached_servers = controller-ZJB:11211'  /etc/nova/nova.conf
     sed -i `expr $keystone_authtoken_line + 2`'a\auth_type = password'  /etc/nova/nova.conf
     sed -i `expr $keystone_authtoken_line + 3`'a\project_domain_name = Default'  /etc/nova/nova.conf
     sed -i `expr $keystone_authtoken_line + 4`'a\user_domain_name = Default'  /etc/nova/nova.conf
     sed -i `expr $keystone_authtoken_line + 5`'a\project_name = service'  /etc/nova/nova.conf
     sed -i `expr $keystone_authtoken_line + 6`'a\username = nova'  /etc/nova/nova.conf
     sed -i `expr $keystone_authtoken_line + 7`'a\password =1790600080'  /etc/nova/nova.conf

     vnc_line=`cat -n /etc/nova/nova.conf | grep "\[vnc\]$" | awk '{print $1}'`
     sed -i $vnc_line'a\enabled = true' /etc/nova/nova.conf
     sed -i `expr $vnc_line + 1`'a\server_listen = 0.0.0.0' /etc/nova/nova.conf
     sed -i `expr $vnc_line + 2`'a\server_proxyclient_address = $my_ip' /etc/nova/nova.conf
     sed -i `expr $vnc_line + 3`'a\novncproxy_base_url = http://controller-ZJB:6080/vnc_auto.html' /etc/nova/nova.conf

     glance_line=`cat -n /etc/nova/nova.conf | grep "\[glance\]$" | awk '{print $1}'`
     sed -i $glance_line'a\api_servers = http://controller-ZJB:9292' /etc/nova/nova.conf

     oslo_concurrency_line=`cat -n /etc/nova/nova.conf | grep "\[oslo_concurrency\]$" | awk '{print $1}'`
     sed -i $oslo_concurrency_line'a\lock_path = /var/lib/nova/tmp' /etc/nova/nova.conf

     placement_line=`cat -n /etc/nova/nova.conf | grep "\[placement\]$" | awk '{print $1}'`
     sed -i $placement_line'a\region_name = RegionOne' /etc/nova/nova.conf
     sed -i `expr $placement_line + 1`'a\project_domain_name = Default' /etc/nova/nova.conf
     sed -i `expr $placement_line + 2`'a\project_name = service' /etc/nova/nova.conf
     sed -i `expr $placement_line + 3`'a\auth_type = password' /etc/nova/nova.conf
     sed -i `expr $placement_line + 4`'a\user_domain_name = Default' /etc/nova/nova.conf
     sed -i `expr $placement_line + 5`'a\auth_url = http://controller-ZJB:5000/v3' /etc/nova/nova.conf
     sed -i `expr $placement_line + 6`'a\username = placement' /etc/nova/nova.conf
     sed -i `expr $placement_line + 7`'a\password =1790600080' /etc/nova/nova.conf


	 echo "    success: Compute节点/etc/nova/nova.conf文件已修改完毕"
   else
        echo "error: Compute节点/etc/nova/nova.conf文件不存在!"
   fi

#确保Compute节点支持硬件虚拟化
   echo "     3、检查Compute节点是否支持虚拟化硬件加速:"
      num=`egrep -c '(vmx|svm)' /proc/cpuinfo`
   if  [ $num -gt 0 ];then
      echo "      该Compute节点支持虚拟化硬件加速! "
   else
      libvirt_line=`cat -n /etc/nova/nova.conf | grep "\[libvirt\]$" | awk '{print $1}'`
   	  sed -i $libvirt_line'a\virt_type = qemu' /etc/nova/nova.conf
   	  echo "     Compute节点不支持虚拟化硬件加速,已配置libvirt 使用  QEMU 取代 KVM"
   fi

#设置compute服务开机自启动,并启动它
   systemctl enable libvirtd  openstack-nova-compute
   systemctl start libvirtd  openstack-nova-compute
   if [ $? == 0 ];then
   echo "      nova-compute已经启动!"
   else
        echo "      error: nova-compute未启动成功,请手动查看原因!"
   fi


elif [ $answer == "n" ]; then
   echo " "
   echo "    已退出此脚本"
fi

  0x06

    当compute脚本执行之后,controller节点需要执行如下命令发现compute节点

# su -s /bin/sh -c "nova-manage cell_v2 discover_hosts --verbose" nova

  0x07

    随后可以进行多项测试,例:

# openstack compute service list
# nova-status upgrade check
0

评论