別以為真懂Openstack: 虛擬機器建立的50個步驟和100個知識點(3)

hxw2ljj發表於2015-11-18

四、Nova-compute

image

步驟17:nova-compute接收到請求後,透過Resource Tracker將建立虛擬機器所需要的資源宣告佔用

步驟18:呼叫Neutron API配置Network,虛擬機器處於Networking的狀態

需要注意的是,這一步雖然是配置Network,但是主要是資料結構的準備,真正的裝置並沒有建立。

由於在建立虛擬機器的時候,我們指定了將虛擬機器放到哪個private network裡面,因而在建立真正的裝置之前,所有的資訊都需要準備好。

這裡的知識點設計Network的建立,然而這一步其實在虛擬機器建立之前就應該做好。

一個最簡單的場景是透過下面的指令碼建立網路

#!/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"

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}')

(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建立虛擬機器

五、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,參考下面的文章

在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: 
         security: http://security.ubuntu.com/ubuntu
       search:
         primary:
           - 
           - 
         security: []
     - arches: [armhf, armel, default]
       failsafe:
         primary: 
         security: 
   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:配置檔案注入

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/18796236/viewspace-1840121/,如需轉載,請註明出處,否則將追究法律責任。

相關文章