Openstack之路(八)创建云主机镜像
[*]准备CentOS-7.x镜像,可以从网上直接下载,我这里准备的镜像是CentOS-7.2-x86_64
# ls -l /tmp/CentOS-7-x86_64-DVD-1511.iso
-rw-r--r-- 1 root root 4329570304 Jan72016 /tmp/CentOS-7-x86_64-DVD-1511.iso
[*]安装相关软件
# yum -y install qemu-kvm libvirt virt-install
# 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
# systemctl start libvirtd
# systemctl status libvirtd
# ifconfig virbr0
virbr0: flags=4099<UP,BROADCAST,MULTICAST>mtu 1500
inet 192.168.122.1netmask 255.255.255.0broadcast 192.168.122.255
ether 00:00:00:00:00:00txqueuelen 0(Ethernet)
RX packets 0bytes 0 (0.0 B)
RX errors 0dropped 0overruns 0frame 0
TX packets 0bytes 0 (0.0 B)
TX errors 0dropped 0 overruns 0carrier 0collisions 0
[*]创建虚拟磁盘,”-f”指定磁盘格式qcow2,存放路径/opt/CentOS-7.2_x86_64.qcow2,磁盘大小10G
# 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
# 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
[*]创建云主机
# 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),接下来的步骤和我们平时安装系统没有什么区别,注意,只分根分区,不需要分交换分区。
[*]启动云主机,并查看状态
# virsh start CentOS-7.2
Domain CentOS-7.2 started
# virsh list --all
Id Name State
----------------------------------------------------
2 CentOS-7.2 running
[*]通过TightVNS工具连接192.168.56.11:5900,配置云主机网络
DNS配置
# echo -e "nameserver 114.114.114.114\nnameserver 202.96.128.86" > /etc/resolv.conf
# cat /etc/resolv.conf
nameserver 114.114.114.114
nameserver 202.96.128.86
网卡配置
# vi /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=dhcp
IPV4_FAILURE_FATAL=no
NAME=eth0
DEVICE=eth0
ONBOOT=yes
# systemctl stop NetworkManager
# systemctl status NetworkManager
# systemctl disable NetworkManager
# systemctl restart netwrok
# systemctl status netwrok
[*]关闭firewalld,selinux,postfix等服务
# systemctl stop firewalld
# systemctl status firewalld
# systemctl disable firewalld
# setenforce 0
# getenforce
Permissive
# sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
# systemctl stop postfix
# systemctl status postfix
# systemctl disable postfix
[*]设置系统文件描述符数
# ulimit -SHn 65535
# ulimit -n
65535
# echo "* - nofile 65535" >> /etc/security/limits.conf
[*]设置系统字符集
# vi /etc/locale.conf
LANG="en_US.UTF-8"
[*]更新国内yum源
aliyun
# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
epel
# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[*]安装必要的软件包
# yum -y install lrzsz vim dos2unix telnet nmap nc net-tools ntpdate wget screen tree
[*]安装ACPI服务,支持关闭和重启云主机实例
# yum -y install acpid
# systemctl enable acpid
[*]同步网络时间服务器
# ntpdate 0.pool.ntp.org
# hwclock
# crontab -e
####Synchronization Network Time Server####
*/5 * * * * /usr/sbin/ntpdate 0.pool.ntp.org &>/dev/null
[*]编写镜像初始化脚本
# 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:linbin@keywa.com
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文件的执行权限,并添加镜像初始化脚本
# ls -l /etc/rc.local
lrwxrwxrwx 1 root root 13 Jan 20 13:24 /etc/rc.local -> rc.d/rc.local
# ls -l /etc/rc.d/rc.local
-rw-r--r-- 1 root root 473 Oct 20 11:07 /etc/rc.d/rc.local
# chmod +x /etc/rc.d/rc.local
# ls -l /etc/rc.d/rc.local
-rwxr-xr-x 1 root root 473 Oct 20 11:07 /etc/rc.d/rc.local
# echo "/bin/bash /tmp/system_init.sh" >> /etc/rc.local
# tail -1 /etc/rc.local
/bin/bash /tmp/system_init.sh
[*]关闭云主机,并查看状态
# virsh shutdown CentOS-7.2
Domain CentOS-7.2 is being shutdown
# virsh list --all
Id Name State
----------------------------------------------------
- CentOS-7.2 shut off
[*]获得admin凭证来获取只有管理员能执行的命令的访问权限
# source admin-openrc
[*]上传镜像到镜像服务并设置公共可见,这样所有的项目都可以访问它
# 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 |
+------------------+------------------------------------------------------+
[*]确认镜像的上传并验证属性
# openstack image list
+--------------------------------------+-------------------+--------+
| ID | Name | Status |
+--------------------------------------+-------------------+--------+
| 18587404-efaa-4c9d-bd88-682a835933db | CentOS-7.2-x86_64 | active |
| cd96090c-87ca-4eb3-b964-a7457639bc1e | cirros | active |
+--------------------------------------+-------------------+--------+
页:
[1]