Openstack之路创建云主机镜像

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Openstack之路创建云主机镜像相关的知识,希望对你有一定的参考价值。

  • 准备CentOS-7.x镜像,可以从网上直接下载,我这里准备的镜像是CentOS-7.2-x86_64
[[email protected] ~]# ls -l /tmp/CentOS-7-x86_64-DVD-1511.iso
-rw-r--r-- 1 root root 4329570304 Jan  7  2016 /tmp/CentOS-7-x86_64-DVD-1511.iso
  • 安装相关软件
[[email protected] ~]# yum -y install qemu-kvm libvirt virt-install
[[email protected] ~]# rpm -qa qemu-kvm libvirt virt-install
libvirt-3.2.0-14.el7_4.5.x86_64
qemu-kvm-1.5.3-141.el7_4.4.x86_64
virt-install-1.4.1-7.el7.noarch
  • 启动libvirtd,会自动创建虚拟网卡virbr0,默认地址为192.168.122.1
[[email protected] ~]# systemctl start libvirtd
[[email protected] ~]# systemctl status libvirtd
[[email protected] ~]# ifconfig virbr0
virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 00:00:00:00:00:00  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  • 创建虚拟磁盘,”-f”指定磁盘格式qcow2,存放路径/opt/CentOS-7.2_x86_64.qcow2,磁盘大小10G
[[email protected] ~]# qemu-img create -f qcow2 /opt/CentOS-7.2-x86_64.qcow2 10G
Formatting ‘/opt/CentOS-7.2-x86_64.qcow2‘, fmt=qcow2 size=10737418240 encryption=off cluster_size=65536 lazy_refcounts=off refcount_bits=16
[[email protected] ~]# ls -l /opt/CentOS-7.2-x86_64.qcow2
-rw-r--r-- 1 root root 196768 Jan 23 14:39 /opt/CentOS-7.2-x86_64.qcow2
  • 创建云主机
[[email protected] ~]# virt-install --virt-type kvm --name CentOS-7.2 --ram 1024 --disk /opt/CentOS-7.2-x86_64.qcow2,format=qcow2 --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole --os-type=linux --os-variant=centos7.0 --location=/tmp/CentOS-7-x86_64-DVD-1511.iso
  • 通过TightVNS工具连接192.168.56.11:5900(默认端口是5900),接下来的步骤和我们平时安装系统没有什么区别,注意,只分根分区,不需要分交换分区

技术分享图片

  • 启动云主机,并查看状态
[[email protected] ~]# virsh start CentOS-7.2
Domain CentOS-7.2 started
[[email protected] ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 2     CentOS-7.2                     running
  • 通过TightVNS工具连接192.168.56.11:5900,配置云主机网络

DNS配置

[[email protected] ~]# echo -e "nameserver 114.114.114.114\nnameserver 202.96.128.86" > /etc/resolv.conf
[[email protected] ~]# cat /etc/resolv.conf
nameserver 114.114.114.114
nameserver 202.96.128.86

网卡配置

[[email protected] ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=dhcp
IPV4_FAILURE_FATAL=no
NAME=eth0
DEVICE=eth0
ONBOOT=yes

[[email protected] ~]# systemctl stop NetworkManager
[[email protected] ~]# systemctl status NetworkManager
[[email protected] ~]# systemctl disable NetworkManager

[[email protected] ~]# systemctl restart netwrok
[[email protected] ~]# systemctl status netwrok
  • 关闭firewalld,selinux,postfix等服务
[[email protected] ~]# systemctl stop firewalld
[[email protected] ~]# systemctl status firewalld
[[email protected] ~]# systemctl disable firewalld

[[email protected] ~]# setenforce 0
[[email protected] ~]# getenforce
Permissive
[[email protected] ~]# sed -i ‘s#SELINUX=enforcing#SELINUX=disabled#g‘ /etc/selinux/config

[[email protected] ~]# systemctl stop postfix
[[email protected] ~]# systemctl status postfix
[[email protected] ~]# systemctl disable postfix
  • 设置系统文件描述符数
[[email protected] ~]# ulimit -SHn 65535
[[email protected] ~]# ulimit -n
65535
[[email protected] ~]# echo "*               -       nofile          65535" >> /etc/security/limits.conf
  • 设置系统字符集
[[email protected] ~]# vi /etc/locale.conf
LANG="en_US.UTF-8"
  • 更新国内yum源

aliyun

[[email protected] ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

epel

[[email protected] ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
  • 安装必要的软件包
[[email protected] ~]# yum -y install lrzsz vim dos2unix telnet nmap nc net-tools ntpdate wget screen tree
  • 安装ACPI服务,支持关闭和重启云主机实例
[[email protected] ~]# yum -y install acpid
[[email protected] ~]# systemctl enable acpid
  • 同步网络时间服务器
[[email protected] ~]# ntpdate 0.pool.ntp.org
[[email protected] ~]# hwclock
[[email protected] ~]# crontab -e
####Synchronization Network Time Server####
*/5 * * * * /usr/sbin/ntpdate 0.pool.ntp.org &>/dev/null
  • 编写镜像初始化脚本
[[email protected] ~]# vim /tmp/system_init.sh
#!/bin/bash
# Name:system_init.sh
# Version:V1.0
# Type:system_init
# Language:Bash Shell
# Date:2018-01-25
# Author:LinBin
# Email:[email protected]

set_key() {
# Determine whether the file exists
if [ ! -d /root/.ssh ]
  then
    mkdir -p /root/.ssh
    chmod 700 /root/.ssh
fi

# Fetch public key using HTTP
for ((i=1;i<6;i++))
do
  if [ ! -f /root/.ssh/authorized_keys ]
    then
      curl -f http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key > /tmp/metadata-key 2>/dev/null
      if [ $? -eq 0 ]
        then
          cat /tmp/metadata-key >> /root/.ssh/authorized_keys
          chmod 0600 /root/.ssh/authorized_keys
          restorecon /root/.ssh/authorized_keys
          rm -f /tmp/metadata-key
          echo "Successfully retrieved public key from instance metadata"
          echo "*****************"
          echo "AUTHORIZED KEYS"
          echo "*****************"
          cat /root/.ssh/authorized_keys
          echo "*****************"
     fi
  else
    break;
  fi
done
}

# Set the system host name
set_hostname() {
    Hostname=$(curl -s http://169.254.169.254/latest/meta-data/hostname)
    echo "$Hostname" > /etc/hostname
    hostnamectl set-hostname $(echo "$Hostname")
}

# Set static ip address
set_static_ip() {
    IP=$(curl -s http://169.254.169.254/latest/meta-data/local-ipv4)
    cat > /etc/sysconfig/network-scripts/ifcfg-eth0 <<EOF
TYPE=Ethernet
BOOTPROTO=static
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=$IP
PREFIX=24
GATEWAY=192.168.56.2
DNS1=114.114.114.114
DNS2=202.96.128.86
EOF
}

# Reboot the instance
reboot_instance() {
    rm -f /tmp/system_init.sh
    sed -i ‘$d‘ /etc/rc.local
    reboot
}

# Main function
main() {
    set_key;
    set_hostname;
    set_static_ip;
    reboot_instance;
}

# Executive main function
main
  • 设置/etc/rc.local文件的执行权限,并添加镜像初始化脚本
[[email protected] ~]# ls -l /etc/rc.local
lrwxrwxrwx 1 root root 13 Jan 20 13:24 /etc/rc.local -> rc.d/rc.local
[[email protected] ~]# ls -l /etc/rc.d/rc.local
-rw-r--r-- 1 root root 473 Oct 20 11:07 /etc/rc.d/rc.local
[[email protected] ~]# chmod +x /etc/rc.d/rc.local
[[email protected] ~]# ls -l /etc/rc.d/rc.local
-rwxr-xr-x 1 root root 473 Oct 20 11:07 /etc/rc.d/rc.local

[[email protected] ~]# echo "/bin/bash /tmp/system_init.sh" >> /etc/rc.local
[[email protected] ~]# tail -1 /etc/rc.local
/bin/bash /tmp/system_init.sh
  • 关闭云主机,并查看状态
[[email protected] ~]# virsh shutdown CentOS-7.2
Domain CentOS-7.2 is being shutdown

[[email protected] ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 -     CentOS-7.2                     shut off
  • 获得admin凭证来获取只有管理员能执行的命令的访问权限
[[email protected] ~]# source admin-openrc
  • 上传镜像到镜像服务并设置公共可见,这样所有的项目都可以访问它
[[email protected] ~]# openstack image create "CentOS-7.2-x86_64" --file /opt/CentOS-7.2-x86_64.qcow2 --disk-format qcow2 --container-format bare --public
+------------------+------------------------------------------------------+
| Field            | Value                                                |
+------------------+------------------------------------------------------+
| checksum         | 9a6d3efdd6874d4aa8ad555e9752b012                     |
| container_format | bare                                                 |
| created_at       | 2018-01-25T11:44:35Z                                 |
| disk_format      | qcow2                                                |
| file             | /v2/images/18587404-efaa-4c9d-bd88-682a835933db/file |
| id               | 18587404-efaa-4c9d-bd88-682a835933db                 |
| min_disk         | 0                                                    |
| min_ram          | 0                                                    |
| name             | CentOS-7.2-x86_64                                    |
| owner            | 14055178975d417987c5a94f030c7acf                     |
| protected        | False                                                |
| schema           | /v2/schemas/image                                    |
| size             | 1196359680                                           |
| status           | active                                               |
| tags             |                                                      |
| updated_at       | 2018-01-25T11:44:57Z                                 |
| virtual_size     | None                                                 |
| visibility       | public                                               |
+------------------+------------------------------------------------------+
  • 确认镜像的上传并验证属性
[[email protected] ~]# openstack image list
+--------------------------------------+-------------------+--------+
| ID                                   | Name              | Status |
+--------------------------------------+-------------------+--------+
| 18587404-efaa-4c9d-bd88-682a835933db | CentOS-7.2-x86_64 | active |
| cd96090c-87ca-4eb3-b964-a7457639bc1e | cirros            | active |
+--------------------------------------+-------------------+--------+

以上是关于Openstack之路创建云主机镜像的主要内容,如果未能解决你的问题,请参考以下文章

OpenStack快照分析:从镜像启动的云主机离线快照分析

Openstack liberty 创建实例快照源码分析1

openstack——使用命令行发放云主机

Redhat OpenStack使用命令行发放云主机

Redhat OpenStack使用命令行发放云主机

Redhat OpenStack使用命令行发放云主机