别觉得真懂Openstack: 虚拟机建立的50个步骤和100个知识点(3)

4、Nova-compute

image

步骤17:nova-compute接收到请求后,经过Resource Tracker将建立虚拟机所须要的资源声明占用

步骤18:调用Neutron API配置Network,虚拟机处于Networking的状态

须要注意的是,这一步虽然是配置Network,可是主要是数据结构的准备,真正的设备并无建立。html

因为在建立虚拟机的时候,咱们指定了将虚拟机放到哪一个private network里面,于是在建立真正的设备以前,全部的信息都须要准备好。python

这里的知识点设计Network的建立,然而这一步其实在虚拟机建立以前就应该作好。shell

一个最简单的场景是经过下面的脚本建立网络ubuntu

#!/bin/bash
TENANT_NAME="openstack"
TENANT_NETWORK_NAME="openstack-net"
TENANT_SUBNET_NAME="${TENANT_NETWORK_NAME}-subnet"
TENANT_ROUTER_NAME="openstack-router"
FIXED_RANGE="192.168.0.0/24"
NETWORK_GATEWAY="192.168.0.1"windows

PUBLIC_GATEWAY="172.24.1.1"
PUBLIC_RANGE="172.24.1.0/24"
PUBLIC_START="172.24.1.100"
PUBLIC_END="172.24.1.200"安全

TENANT_ID=$(keystone tenant-list | grep " $TENANT_NAME " | awk '{print $2}')bash

(1) TENANT_NET_ID=$(neutron net-create --tenant_id $TENANT_ID $TENANT_NETWORK_NAME --provider:network_type gre --provider:segmentation_id 1 | grep " id " | awk '{print $4}')网络

(2) TENANT_SUBNET_ID=$(neutron subnet-create --tenant_id $TENANT_ID --ip_version 4 --name $TENANT_SUBNET_NAME $TENANT_NET_ID $FIXED_RANGE --gateway $NETWORK_GATEWAY --dns_nameservers list=true 8.8.8.8 | grep " id " | awk '{print $4}')数据结构

(3) ROUTER_ID=$(neutron router-create --tenant_id $TENANT_ID $TENANT_ROUTER_NAME | grep " id " | awk '{print $4}')架构

(4) neutron router-interface-add $ROUTER_ID $TENANT_SUBNET_ID

(5) neutron net-create public --router:external=True

(6) neutron subnet-create --ip_version 4 --gateway $PUBLIC_GATEWAY public $PUBLIC_RANGE --allocation-pool start=$PUBLIC_START,end=$PUBLIC_END --disable-dhcp --name public-subnet

(7) neutron router-gateway-set ${TENANT_ROUTER_NAME} public

create network

  1. 为这个Tenant建立一个private network,不一样的private network是须要经过VLAN tagging进行隔离的,互相之间broadcast不能到达,这里咱们用的是GRE模式,也须要一个相似VLAN ID的东西,称为Segment ID
  2. 建立一个private network的subnet,subnet才是真正配置IP网段的地方,对于私网,咱们经常用192.168.0.0/24这个网段
  3. 为这个Tenant建立一个Router,才可以访问外网
  4. 将private network链接到Router上
  5. 建立一个External Network
  6. 建立一个Exernal Network的Subnet,这个外网逻辑上表明了咱们数据中心的物理网络,经过这个物理网络,咱们能够访问外网。于是PUBLIC_GATEWAY应该设为数据中内心面的Gateway, PUBLIC_RANGE也应该和数据中心的物理网络的CIDR一致,不然连不通,而之因此设置PUBLIC_START和PUBLIC_END,是由于在数据中心中,不可能全部的IP地址都给Openstack使用,另外可能搭建了VMware Vcenter,可能有物理机器,仅仅分配一个区间给Openstack来用。
  7. 将Router链接到External Network

通过这个流程,从虚拟网络,到物理网络就逻辑上联通了。

然而真正的建立底层的设备,倒是经过具体的命令来的,本人总结了一下:

neutron建立network执行的那些命令 

固然还有更复杂的场景,参考这篇文章

多个router和多个network 

步骤19:生成MAC Address

步骤20: 获取DHCP Server的配置

步骤21:获取Network的信息

步骤22:获取Security Group的信息

步骤23:拿着全部的信息,建立Port对象,是一个Tap device,固然真正的设备尚未建立

步骤24:调用Libvirt Driver建立虚拟机

5、Image

image

在建立Instance以前,固然须要Image,Image后来发现是一个大学问。

在Openstack里面,对于KVM,应用到的Image格式主要是两种RAW和qcow2,

raw格式简单,容易转换为其余的格式。须要文件系统的支持才能支持sparse file,性能相对较高。

qcow2是动态的,相对于raw来讲,有下列的好处:

  • 即使文件系统不支持sparse file,文件大小也很小
  • Copy on write
  • Snapshot
  • 压缩
  • 加密

具体的格式和特色,参考下面的文章

QEMU KVM libvirt手册(4) – images

[转] The QCOW2 Image Format

建立一个image,有多种方法

一种方法是经过virt-install,讲hard disk设为一个image文件, 从CDROM启动一个虚拟机,按照正常的安装流程来,最后操做系统安装好,image再通过qemu-img进行处理,压缩,最终造成image。

参考文章

QEMU KVM libvirt 手册(1) 

固然如今有了更先进的方法,就是libguestfs,它能够轻松基于已有版本的image建立一个你想要的image,就是virt-builder

参考文章

libguestfs手册(3): virt命令 

固然一个能够在Openstack里面使用的image,毫不是仅仅安装一个操做系统那么简单。

OpenStack Virtual Machine Image Guide中详细写了一个Linux Image的各类需求

  • Disk partitions and resize root partition on boot (cloud-init)
  • No hard-coded MAC address information
  • Ensure ssh server runs
  • Disable firewall
  • Access instance by using ssh public key (cloud-init)
  • Use cloud-init to fetch the public key
  • Process user data and other metadata (cloud-init)
  • Ensure image writes boot log to console

另外加几条:

  • 包含MBR和bootloader,能够自启动
  • 支持virtio的disk和network driver
  • 使用DHCP分配IP

当一个Linux的Image安装完毕后,总要测试一下:

  • 能经过key ssh上去吗
  • 可以文件注入吗
  • cloud-init是否运行了
  • 文件系统被resize为flavor大小了吗
  • hostname设为文件名吗
  • timezone设的对吗
  • /etc/fstab干净正确吗
  • 虚拟机的log被正确输出了吗
  • /tmp路径下干净吗
  • 能打snapshot吗
  • block storage添加后能正常看到和使用吗

对于windows image,却要复杂的多,windows真的不是对cloud友好的。

  • 首先必须用virt-install将windows系统安装好。
  • windows要想使用virtio,就必须将windows版本的virtio安装好。即使安装好,还要在device manager里面update driver才好。
  • Remote Access要打开,要不怎么远程桌面啊。
  • 安装一个SSH Server吧,有时候须要建立虚拟机后,自动执行脚本安装东西,不能经过远程桌面来。
  • 安装windows版本的cloud-init cloudbase-init
  • 别忘了运行windows update来更新windows,不然会有不少安全漏洞。
  • 在device manager里面添加一个serial console的device
  • windows的硬盘占用一般很大,运行一下disk cleanup tool能够清空一些硬盘。
  • 微软有一个SDelete工具,能够讲未分配的空间设为0,从而能够压缩硬盘。
  • 别忘了License问题。
  • 有时候windows配置网络的时候,会弹出对话框,这是家庭网络,工做网络,仍是公共网络?这会使得网络配置死在哪里,在注册表里干掉他。
  • 运行sysprep

对于cloud-init,参考下面的文章

http://cloudinit.readthedocs.org/en/latest/index.html

http://www.scalehorizontally.com/2013/02/24/introduction-to-cloud-init/

在ubuntu中,cloud-init主要包括

配置文件在/etc/cloud下面,默认的cloud.cfg以下

root@dfasdfsdafasdf:/etc/cloud# cat cloud.cfg
# The top level settings are used as module
# and system configuration.

# A set of users which may be applied and/or used by various modules
# when a 'default' entry is found it will reference the 'default_user'
# from the distro configuration specified below
users:
   - default

# If this is set, 'root' will not be able to ssh in and they
# will get a message to login instead as the above $user (ubuntu)
disable_root: true

# This will cause the set+update hostname module to not operate (if true)
preserve_hostname: false

# Example datasource config
# datasource:
#    Ec2:
#      metadata_urls: [ 'blah.com' ]
#      timeout: 5 # (defaults to 50 seconds)
#      max_wait: 10 # (defaults to 120 seconds)

# The modules that run in the 'init' stage
cloud_init_modules:
- migrator
- seed_random
- bootcmd
- write-files
- growpart
- resizefs
- set_hostname
- update_hostname
- update_etc_hosts
- ca-certs
- rsyslog
- users-groups
- ssh

# The modules that run in the 'config' stage
cloud_config_modules:
# Emit the cloud config ready event
# this can be used by upstart jobs for 'start on cloud-config'.
- emit_upstart
- disk_setup
- mounts
- ssh-import-id
- locale
- set-passwords
- grub-dpkg
- apt-pipelining
- apt-configure
- package-update-upgrade-install
- landscape
- timezone
- puppet
- chef
- salt-minion
- mcollective
- disable-ec2-metadata
- runcmd
- byobu

# The modules that run in the 'final' stage
cloud_final_modules:
- rightscale_userdata
- scripts-vendor
- scripts-per-once
- scripts-per-boot
- scripts-per-instance
- scripts-user
- ssh-authkey-fingerprints
- keys-to-console
- phone-home
- final-message
- power-state-change

# System and/or distro specific settings
# (not accessible to handlers/transforms)
system_info:
   # This will affect which distro class gets used
   distro: ubuntu
   # Default user name + that default users groups (if added/used)
   default_user:
     name: ubuntu
     lock_passwd: True
     gecos: Ubuntu
     groups: [adm, audio, cdrom, dialout, dip, floppy, netdev, plugdev, sudo, video]
     sudo: ["ALL=(ALL) NOPASSWD:ALL"]
     shell: /bin/bash
   # Other config here will be given to the distro class and/or path classes
   paths:
      cloud_dir: /var/lib/cloud/
      templates_dir: /etc/cloud/templates/
      upstart_dir: /etc/init/
   package_mirrors:
     - arches: [i386, amd64]
       failsafe:
         primary: http://archive.ubuntu.com/ubuntu
         security: http://security.ubuntu.com/ubuntu
       search:
         primary:
           - http://%(ec2_region)s.ec2.archive.ubuntu.com/ubuntu/
           - http://%(availability_zone)s.clouds.archive.ubuntu.com/ubuntu/
         security: []
     - arches: [armhf, armel, default]
       failsafe:
         primary: http://ports.ubuntu.com/ubuntu-ports
         security: http://ports.ubuntu.com/ubuntu-ports
   ssh_svcname: ssh

工做文件夹在/var/lib/cloud

root@dfasdfsdafasdf:/var/lib/cloud/instance# ls
boot-finished     datasource  obj.pkl  sem            user-data.txt.i  vendor-data.txt.i
cloud-config.txt  handlers    scripts  user-data.txt  vendor-data.txt

另外就是cloud-init的命令

/usr/bin/cloud-init

若是咱们打开它,发现他是python文件,若是运行/usr/bin/cloud-init init,则会运行cloud_init_modules:下面的模块,咱们以resizefs为例子

/usr/bin/cloud-init 中会调用main_init,里面会调用run_module_section

这就调用到python代码里面去了,因此cloud-init另外一个部分就是python代码部分

/usr/lib/python2.7/dist-packages/cloudinit

咱们发现里面有这个文件/usr/lib/python2.7/dist-packages/cloudinit/config/cc_resizefs.py

里面是

def _resize_btrfs(mount_point, devpth):  # pylint: disable=W0613
    return ('btrfs', 'filesystem', 'resize', 'max', mount_point)

def _resize_ext(mount_point, devpth):  # pylint: disable=W0613
    return ('resize2fs', devpth)

def _resize_xfs(mount_point, devpth):  # pylint: disable=W0613
    return ('xfs_growfs', devpth)

def _resize_ufs(mount_point, devpth):  # pylint: disable=W0613
    return ('growfs', devpth)

哈哈,终于找到resize的根源了。

说完了建立image,还须要了解修改image,咱们的文件注入,就是对image的修改。

有三种方式:经过mount一个loop device,经过qemu的network block device,或者最早进的,经过libguestfs

总结成了一篇文章

如何修改image文件 

对于qemu-nbd,有文章

QEMU KVM Libvirt手册(6) – Network Block Device 

对于libguestfs,我也写了一些笔记

 

libguestfs手册(1): 架构

libguestfs手册(2):guestfish command

libguestfs手册(3): virt命令

对于文件注入,有文章

nova file injection 

对于如何打snapshot,分多种,有文章

QEMU KVM Libvirt手册(5) – snapshots 

Snapshot Types

External Snapshot management

[转] External(and Live) snapshots with libvirt

[转] Snapshotting with libvirt for qcow2 images

步骤26:从Glance下载Image,做为base

步骤27:基于base image,建立qcow2的image

步骤28:resize image的大小,和filesystem的大小无关

步骤29:配置configuration drive

步骤30:配置文件注入

相关文章
相关标签/搜索