openstack-nova源码分析(一)环境搭建

openstack是一个开源的云计算服务解决方案项目,应用相对广泛,值得进行学习一下,首先搭建一套nova环境,对openstack的认识,以及后面的源码分析都很有必要。

(首先准备一台学习主机,个人这里使用个人电脑并搭建一台虚拟机centos7.2)
约定:

  1. 以下所有操作默认都在 root 用户下进行操作,如有特殊将会说明
  2. xxxxxxxx 为密码, 需指定
一. 安装前准备工作
  1. 配置网络,可以访问外网(如果环境是虚拟机,则虚拟机也需要访问外网)

  2. 关闭主机上不用的服务

 systemctl disable NetworkManager
 systemctl stop NetworkManager
 systemctl disable firewalld.service
 systemctl stop firewalld.service
  1. 关闭selinux
vi /etc/selinux/config
将SELINUX=enforcing改为SELINUX=disabled (重启主机生效)
  1. 修改/etc/hosts 将集群所有主机hosts解析加入列表,如:
192.168.137.15  nova
  1. 配置dns:
    注: (如果是本地虚拟机可以更改为本地主机的dns ip)
vi /etc/resolv.conf
nameserver 8.8.8.8
  1. 配置yum源:
    这里配置阿里源
    (若本地有yum源且有安装需要的所有包,则可以不用访问外网)
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum clean all
yum makecache

二. 安装
  1. 配置指定安装的openstack版本:
    注: 一些版本官方可能已经停止维护,因此建议使用最新的列表中的版本, 不建议按照网上的命令直接执行,下面有具体的方法

yum list 查看目前支持的openstack 版本,再选择安装

yum list | grep centos-release-openstack

centos-release-openstack-ocata.noarch
centos-release-openstack-pike.x86_64
centos-release-openstack-queens.noarch
centos-release-openstack-rocky.noarch

这里看到支持最新的 O P Q R 4个版本
我这里安装O版

yum install centos-release-openstack-ocata  -y 

安装client

yum install python-openstackclient  -y

这里openstack 的yum源及一些基本配置已经准备完成,下面是各个组件的具体安装


  1. 安装本地mysql库(若已经有mysql库,则创建对应用户即可)
yum install mariadb mariadb-server python2-PyMySQL -y

systemctl enable mariadb.service
systemctl start  mariadb.service

安装完成后可在/etc/my.cnf修改mysql参数,这里先使用默认配置即可

进入myslq验证

[root@nova ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 10.1.20-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> exit

验证ok,mysql 组件准备完成


  1. 安装rabbitmq:

rabbit是openstack用来传递消息的消息队列,也非常重要

yum install rabbitmq-server  -y


systemctl enable rabbitmq-server.service
systemctl start rabbitmq-server.service

如果有多个controller则需要将rabbitmq配置为集群(这里测试,不做集群,略)

rabbitmqctl add_user  openstack  xxxxx
rabbitmqctl set_permissions openstack  ".*" ".*" ".*"   

  1. 安装memcache
yum install -y memcached

修改配置文件

vi /etc/sysconfig/memcached

PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="4096"
OPTIONS=""

  1. 安装openstack的rpm包:

这里将openstack的一些组件包一次性安装完成

yum install -y openstack-keystone httpd mod_wsgi openstack-glance  openstack-nova-api openstack-nova-conductor openstack-nova-console openstack-nova-novncproxy openstack-nova-scheduler openstack-nova-compute sysfsutils openstack-neutron openstack-neutron-ml2 openstack-neutron-openvswitch python-openvswitch  ebtables ipset openstack-dashboard openstack-cinder lvm2 targetcli python-keystone

以下为openstack的一些组件安装

  1. keystone组件
    在mysql中创建keystone帐号,
    进入mysql,执行如下命令:
MariaDB [(none)]> create database keystone;
Query OK, 1 row affected (0.13 sec)

MariaDB [(none)]>GRANT ALL PRIVILEGES ON keystone.* TO 'keystone'@'localhost' IDENTIFIED BY 'xxxxxxxxxx';
Query OK, 0 rows affected (0.17 sec)
MariaDB [(none)]> GRANT ALL PRIVILEGES ON keystone.* TO 'keystone'@'%' IDENTIFIED BY 'xxxxxxxxxx';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> 
MariaDB [(none)]> GRANT ALL PRIVILEGES ON keystone.* TO 'keystone'@'nova' IDENTIFIED BY 'xxxxxxxxxx';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> 
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> exit

配置keystone

vi /etc/keystone/keystone.conf 
修改对应参数配置
[database]
connection = mysql+pymysql://keystone:[email protected]/keystone

[token]
provider = fernet

同步keystone表到mysql

su -s /bin/sh -c "keystone-manage db_sync" keystone
keystone-manage fernet_setup --keystone-user keystone --keystone-group keystone
keystone-manage credential_setup --keystone-user keystone --keystone-group keystone
keystone-manage bootstrap --bootstrap-password xxxxxxxxxx --bootstrap-admin-url http://192.168.137.15:35357/v3/ --bootstrap-internal-url http://192.168.137.15:35357/v3/ --bootstrap-public-url http://192.168.137.15:5000/v3/ --bootstrap-region-id RegionOne

配置httpd服务

vi  /etc/httpd/conf/httpd.conf

ServerName 192.168.137.15

创建软连接

ln -s /usr/share/keystone/wsgi-keystone.conf /etc/httpd/conf.d/

启动httpd服务

systemctl enable httpd.service
systemctl start httpd.service

创建环境变量文件

vi ~/admin-openrc.sh

export OS_PROJECT_DOMAIN_NAME=Default
export OS_USER_DOMAIN_NAME=Default
export OS_PROJECT_NAME=admin
export OS_USERNAME=admin
export OS_PASSWORD=xxxxxxxxxx
export OS_AUTH_URL=http://192.168.137.15:35357/v3
export OS_IDENTITY_API_VERSION=3
export OS_IMAGE_API_VERSION=2

  1. 创建domain
    加载环境变量,并配置项目
[root@nova ~]# source admin-openrc.sh 
[root@nova ~]# openstack project create --domain default --description "Service Project" service
+-------------+----------------------------------+
| Field       | Value                            |
+-------------+----------------------------------+
| description | Service Project                  |
| domain_id   | default                          |
| enabled     | True                             |
| id          | 9e1056d2ffc64474852d6897d9406c19 |
| is_domain   | False                            |
| name        | service                          |
| parent_id   | default                          |
+-------------+----------------------------------+
[root@nova ~]# openstack role create user
+-----------+----------------------------------+
| Field     | Value                            |
+-----------+----------------------------------+
| domain_id | None                             |
| id        | a967ad0f57fa4d79af0abc91fcf99daf |
| name      | user                             |
+-----------+----------------------------------+

domain创建成功, 验证keystone已经配置ok, 若有keystone请在日志/var/log/keystone/keystone.log 中查看,再排查

  1. glance安装

配置数据库账号:

MariaDB [(none)]> CREATE DATABASE glance;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> GRANT ALL PRIVILEGES ON glance.* TO 'glance'@'localhost' IDENTIFIED BY 'xxxxxxxxxx';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> GRANT ALL PRIVILEGES ON glance.* TO 'glance'@'%' IDENTIFIED BY 'xxxxxxxxxx';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> GRANT ALL PRIVILEGES ON glance.* TO 'glance'@'nova' IDENTIFIED BY 'xxxxxxxxxx';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> exit

创建endpoint
openstack user create --domain default --password-prompt glance 密码xxxxxxxxxx
openstack role add --project service --user glance admin
openstack service create --name glance --description “OpenStack Image” image
openstack endpoint create --region RegionOne image public http://192.168.137.15:9292
openstack endpoint create --region RegionOne image internal http://192.168.137.15:9292
openstack endpoint create --region RegionOne image admin http://192.168.137.15:9292

[root@nova ~]# openstack user create --domain default --password-prompt glance
User Password:
Repeat User Password:
+---------------------+----------------------------------+
| Field               | Value                            |
+---------------------+----------------------------------+
| domain_id           | default                          |
| enabled             | True                             |
| id                  | 84979403bb234c198ad895f5f88a00bc |
| name                | glance                           |
| options             | {}                               |
| password_expires_at | None                             |
+---------------------+----------------------------------+
[root@nova ~]# openstack role add --project service --user glance admin
[root@nova ~]# openstack service create --name glance --description "OpenStack Image" image
+-------------+----------------------------------+
| Field       | Value                            |
+-------------+----------------------------------+
| description | OpenStack Image                  |
| enabled     | True                             |
| id          | e60238f19dc34af1b723298daecf4de8 |
| name        | glance                           |
| type        | image                            |
+-------------+----------------------------------+
[root@nova ~]# openstack endpoint create --region RegionOne image public http://192.168.137.15:9292
+--------------+----------------------------------+
| Field        | Value                            |
+--------------+----------------------------------+
| enabled      | True                             |
| id           | 5b1dfc841ff74b02bcb0839a1f81e3ea |
| interface    | public                           |
| region       | RegionOne                        |
| region_id    | RegionOne                        |
| service_id   | e60238f19dc34af1b723298daecf4de8 |
| service_name | glance                           |
| service_type | image                            |
| url          | http://192.168.137.15:9292       |
+--------------+----------------------------------+
[root@nova ~]# openstack endpoint create --region RegionOne image internal http://192.168.137.15:9292
+--------------+----------------------------------+
| Field        | Value                            |
+--------------+----------------------------------+
| enabled      | True                             |
| id           | d092e986d84449b89d0f8609ea37feed |
| interface    | internal                         |
| region       | RegionOne                        |
| region_id    | RegionOne                        |
| service_id   | e60238f19dc34af1b723298daecf4de8 |
| service_name | glance                           |
| service_type | image                            |
| url          | http://192.168.137.15:9292       |
+--------------+----------------------------------+
[root@nova ~]# openstack endpoint create --region RegionOne image admin http://192.168.137.15:9292
+--------------+----------------------------------+
| Field        | Value                            |
+--------------+----------------------------------+
| enabled      | True                             |
| id           | 6d15bdf0005f4887ac1c155f3966c989 |
| interface    | admin                            |
| region       | RegionOne                        |
| region_id    | RegionOne                        |
| service_id   | e60238f19dc34af1b723298daecf4de8 |
| service_name | glance                           |
| service_type | image                            |
| url          | http://192.168.137.15:9292       |
+--------------+----------------------------------+
[root@nova ~]# 

更新配置文件

vi /etc/glance/glance-api.conf 
[database]
connection = mysql+pymysql://glance:[email protected]/glance

[keystone_authtoken]
auth_uri = http://192.168.137.15:5000
auth_url = http://192.168.137.15:35357
memcached_servers = 192.168.137.15:11211
auth_type = password
project_domain_name = Default
user_domain_name = Default
project_name = service
username = glance
password = xxxxxxxxxx

[paste_deploy]
flavor = keystone

[glance_store]
stores = file,http
default_store = file
filesystem_store_datadir = /var/lib/glance/images/

更新glance registry 配置

vi /etc/glance/glance-registry.conf
[database]
connection = mysql+pymysql://glance:[email protected]/glance

[keystone_authtoken]
auth_uri = http://192.168.137.15:5000
auth_url = http://192.168.137.15:35357
memcached_servers = 192.168.137.15:11211
auth_type = password
project_domain_name = Default
user_domain_name = Default
project_name = service
username = glance
password = xxxxxxxxxx

[paste_deploy]
flavor = keystone

同步glance表到数据库

su -s /bin/sh -c "glance-manage db_sync" glance

启动服务

systemctl enable openstack-glance-api.service openstack-glance-registry.service
systemctl start openstack-glance-api.service openstack-glance-registry.service

#初始这里没有镜像,先不上传,若有可以上传镜像注册,此部分略


  1. nova组件安装

创建nova数据库账户
mysql中执行:

CREATE DATABASE nova_api;
CREATE DATABASE nova;

GRANT ALL PRIVILEGES ON nova_api.* TO 'nova'@'localhost' IDENTIFIED BY 'xxxxxxxxxx';
GRANT ALL PRIVILEGES ON nova_api.* TO 'nova'@'%' IDENTIFIED BY 'xxxxxxxxxx';
GRANT ALL PRIVILEGES ON nova.* TO 'nova'@'localhost' IDENTIFIED BY 'xxxxxxxxxx';
GRANT ALL PRIVILEGES ON nova.* TO 'nova'@'%' IDENTIFIED BY 'xxxxxxxxxx';
GRANT ALL PRIVILEGES ON nova.* TO 'nova'@'nova' IDENTIFIED BY 'xxxxxxxxxx';
GRANT ALL PRIVILEGES ON nova_api.* TO 'nova'@'nova' IDENTIFIED BY 'xxxxxxxxxx';

配置nova endpoint

openstack user create --domain default  --password-prompt nova 密码xxxxxxxxxx
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://192.168.137.15:8774/v2.1/%\(tenant_id\)s
openstack endpoint create --region RegionOne compute internal http://192.168.137.15:8774/v2.1/%\(tenant_id\)s
openstack endpoint create --region RegionOne compute admin http://192.168.137.15:8774/v2.1/%\(tenant_id\)s

[root@nova ~]# openstack user create --domain default  --password-prompt nova
User Password:
Repeat User Password:
+---------------------+----------------------------------+
| Field               | Value                            |
+---------------------+----------------------------------+
| domain_id           | default                          |
| enabled             | True                             |
| id                  | 7aa1c7cf0bd84c4bac974d0949a35b9a |
| name                | nova                             |
| options             | {}                               |
| password_expires_at | None                             |
+---------------------+----------------------------------+

更新nova配置

vi /etc/nova/nova.conf
[DEFAULT]
auth_strategy = keystone
enabled_apis = osapi_compute,metadata
transport_url = rabbit://openstack:[email protected]
my_ip = 192.168.137.15
use_neutron = True
firewall_driver = nova.virt.firewall.NoopFirewallDriver

[api_database]
connection = mysql+pymysql://nova:[email protected]/nova_api

[database]
connection = mysql+pymysql://nova:[email protected]/nova

[keystone_authtoken]
auth_uri = http://192.168.137.15:5000
auth_url = http://192.168.137.15:35357
memcached_servers = 192.168.137.15:11211
auth_type = password
project_domain_name = Default
user_domain_name = Default
project_name = service
username = nova
password = xxxxxxxxxx

[vnc]
vncserver_listen = $my_ip
vncserver_proxyclient_address = $my_ip

[glance]
api_servers = http://192.168.137.15:9292

[oslo_concurrency]
lock_path = /var/lib/nova/tmp

同步nova表及nova-api表到mysql数据库

su -s /bin/sh -c "nova-manage api_db sync" nova
su -s /bin/sh -c "nova-manage db sync" nova

注: 同步过程中可能抛出一些小的异常,可以暂时忽略, 验证db中ok即可
mysql中验证

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| glance             |
| information_schema |
| keystone           |
| mysql              |
| nova               |
| nova_api           |
| performance_schema |
| test               |
+--------------------+
8 rows in set (0.02 sec)

启动nova api等服务

systemctl enable openstack-nova-api.service openstack-nova-consoleauth.service openstack-nova-scheduler.service openstack-nova-conductor.service openstack-nova-novncproxy.service
systemctl start openstack-nova-api.service openstack-nova-consoleauth.service openstack-nova-scheduler.service openstack-nova-conductor.service openstack-nova-novncproxy.service

验证api服务是否正常:

[root@nova ~]# systemctl status openstack-nova-api.service
● openstack-nova-api.service - OpenStack Nova API Server
   Loaded: loaded (/usr/lib/systemd/system/openstack-nova-api.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2019-03-30 08:23:13 EDT; 16s ago
 Main PID: 39937 (nova-api)
   CGroup: /system.slice/openstack-nova-api.service
           ├─39937 /usr/bin/python2 /usr/bin/nova-api
           ├─39989 /usr/bin/python2 /usr/bin/nova-api
           └─39991 /usr/bin/python2 /usr/bin/nova-api

Mar 30 08:22:55 nova systemd[1]: Starting OpenStack Nova API Server...
Mar 30 08:23:13 nova systemd[1]: Started OpenStack Nova API Server.

以下两个服务可暂时不启动

systemctl enable openstack-nova-compute.service libvirtd.service
systemctl start openstack-nova-compute.service libvirtd.service


验证服务:

[root@nova ~]# nova service-list
+----+------------------+------+----------+---------+-------+----------------------------+-----------------+
| Id | Binary           | Host | Zone     | Status  | State | Updated_at                 | Disabled Reason |
+----+------------------+------+----------+---------+-------+----------------------------+-----------------+
| 1  | nova-consoleauth | nova | internal | enabled | up    | 2019-03-30T12:26:46.000000 | -               |
| 2  | nova-scheduler   | nova | internal | enabled | up    | 2019-03-30T12:26:46.000000 | -               |
| 3  | nova-conductor   | nova | internal | enabled | up    | 2019-03-30T12:26:46.000000 | -               |
+----+------------------+------+----------+---------+-------+----------------------------+-----------------+

至此nova api服务测试环境搭建成功

你可能感兴趣的:(openstack源码分析)