设为首页 收藏本站
查看: 662|回复: 0

[经验分享] Openstack之路(八)创建云主机镜像

[复制链接]

尚未签到

发表于 2018-5-30 11:31:40 | 显示全部楼层 |阅读模式

  • 准备CentOS-7.x镜像,可以从网上直接下载,我这里准备的镜像是CentOS-7.2-x86_64

[root@linux-node1 ~]# 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

  • 安装相关软件

[root@linux-node1 ~]# yum -y install qemu-kvm libvirt virt-install
[root@linux-node1 ~]# 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

[root@linux-node1 ~]# systemctl start libvirtd
[root@linux-node1 ~]# systemctl status libvirtd
[root@linux-node1 ~]# 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

[root@linux-node1 ~]# 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
[root@linux-node1 ~]# 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

  • 创建云主机

[root@linux-node1 ~]# 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),接下来的步骤和我们平时安装系统没有什么区别,注意,只分根分区,不需要分交换分区。
DSC0000.jpg



  • 启动云主机,并查看状态

[root@linux-node1 ~]# virsh start CentOS-7.2
Domain CentOS-7.2 started
[root@linux-node1 ~]# virsh list --all
Id    Name                           State
----------------------------------------------------
2     CentOS-7.2                     running

  • 通过TightVNS工具连接192.168.56.11:5900,配置云主机网络
  DNS配置

[root@localhost ~]# echo -e "nameserver 114.114.114.114\nnameserver 202.96.128.86" > /etc/resolv.conf
[root@localhost ~]# cat /etc/resolv.conf
nameserver 114.114.114.114
nameserver 202.96.128.86
  网卡配置

[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=dhcp
IPV4_FAILURE_FATAL=no
NAME=eth0
DEVICE=eth0
ONBOOT=yes
[root@localhost ~]# systemctl stop NetworkManager
[root@localhost ~]# systemctl status NetworkManager
[root@localhost ~]# systemctl disable NetworkManager
[root@localhost ~]# systemctl restart netwrok
[root@localhost ~]# systemctl status netwrok

  • 关闭firewalld,selinux,postfix等服务

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl status firewalld
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce
Permissive
[root@localhost ~]# sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
[root@localhost ~]# systemctl stop postfix
[root@localhost ~]# systemctl status postfix
[root@localhost ~]# systemctl disable postfix

  • 设置系统文件描述符数

[root@localhost ~]# ulimit -SHn 65535
[root@localhost ~]# ulimit -n
65535
[root@localhost ~]# echo "*               -       nofile          65535" >> /etc/security/limits.conf

  • 设置系统字符集

[root@localhost ~]# vi /etc/locale.conf
LANG="en_US.UTF-8"

  • 更新国内yum源
  aliyun

[root@localhost ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
  epel

[root@localhost ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

  • 安装必要的软件包

[root@localhost ~]# yum -y install lrzsz vim dos2unix telnet nmap nc net-tools ntpdate wget screen tree

  • 安装ACPI服务,支持关闭和重启云主机实例

[root@localhost ~]# yum -y install acpid
[root@localhost ~]# systemctl enable acpid

  • 同步网络时间服务器

[root@localhost ~]# ntpdate 0.pool.ntp.org
[root@localhost ~]# hwclock
[root@localhost ~]# crontab -e
####Synchronization Network Time Server####
*/5 * * * * /usr/sbin/ntpdate 0.pool.ntp.org &>/dev/null

  • 编写镜像初始化脚本

[root@localhost ~]# 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文件的执行权限,并添加镜像初始化脚本

[root@localhost ~]# ls -l /etc/rc.local
lrwxrwxrwx 1 root root 13 Jan 20 13:24 /etc/rc.local -> rc.d/rc.local
[root@localhost ~]# ls -l /etc/rc.d/rc.local
-rw-r--r-- 1 root root 473 Oct 20 11:07 /etc/rc.d/rc.local
[root@localhost ~]# chmod +x /etc/rc.d/rc.local
[root@localhost ~]# ls -l /etc/rc.d/rc.local
-rwxr-xr-x 1 root root 473 Oct 20 11:07 /etc/rc.d/rc.local
[root@localhost ~]# echo "/bin/bash /tmp/system_init.sh" >> /etc/rc.local
[root@localhost ~]# tail -1 /etc/rc.local
/bin/bash /tmp/system_init.sh

  • 关闭云主机,并查看状态

[root@linux-node1 ~]# virsh shutdown CentOS-7.2
Domain CentOS-7.2 is being shutdown
[root@linux-node1 ~]# virsh list --all
Id    Name                           State
----------------------------------------------------
-     CentOS-7.2                     shut off

  • 获得admin凭证来获取只有管理员能执行的命令的访问权限

[root@linux-node1 ~]# source admin-openrc

  • 上传镜像到镜像服务并设置公共可见,这样所有的项目都可以访问它

[root@linux-node1 ~]# 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                                               |
+------------------+------------------------------------------------------+

  • 确认镜像的上传并验证属性

[root@linux-node1 ~]# 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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-483046-1-1.html 上篇帖子: Openstack之路(七)可视化服务Horizon 下篇帖子: Openstack中虚拟机磁盘扩容
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表