當(dāng)我在搭建好OpenStack的環(huán)境后,迫不及待的想創(chuàng)建利用它來(lái)創(chuàng)建一個(gè)自己常用的虛擬機(jī)來(lái)實(shí)驗(yàn),我自己干的很蠢的事情,就是在開(kāi)源的網(wǎng)站上下了鏡像直接傳到OpenStack,然后發(fā)現(xiàn)不能用,蠢不蠢啊.這里把我在做鏡像的過(guò)程分享一下.在創(chuàng)建鏡像的時(shí)候,需要用到KVM的一些知識(shí),需要了解的可以自己百度,我自己之前也在別的博客里寫(xiě)了一篇KVM的安裝使用過(guò)程,這里分享一下:
http://www.cnblogs.com/marvin-ma/p/7407509.html
在管理節(jié)點(diǎn)創(chuàng)建一個(gè)虛擬機(jī)鏡像
1. 安裝virt-install,qemu-kvm, libvirt,并啟動(dòng)libvrit
[root@Marvin-Node1 ~]# yum install -y qemu-kvm libvirt
[root@Marvin-Node1 ~]# yum install -y virt-install
[root@Marvin-Node1 ~]# systemctl start libvirtd && systemctl enable libvirtd
[root@Marvin-Node1 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.56 netmask 255.255.255.0 broadcast 10.0.0.255
inet6 fe80::250:56ff:fe91:df22 prefixlen 64 scopeid 0x20<link>
ether 00:50:56:91:df:22 txqueuelen 1000 (Ethernet)
RX packets 6449007 bytes 8443638629 (7.8 GiB)
RX errors 0 dropped 508 overruns 0 frame 0
TX packets 3246349 bytes 274871849 (262.1 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)
RX packets 4957466 bytes 1360755790 (1.2 GiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4957466 bytes 1360755790 (1.2 GiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:cc:71:8c 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
這里如果沒(méi)有問(wèn)題,使用ifconfig命令,可以看到多出一個(gè)virbr0的網(wǎng)卡
2. 創(chuàng)建一個(gè)目錄,用于存放鏡像,并上傳CentOS7的鏡像到該目錄下
創(chuàng)建目錄/opt/image
[root@Marvin-Node1 ~]# mkdir -p /opt/image
可以通過(guò)xftp等軟件來(lái)上傳鏡像,上傳完成后開(kāi)始創(chuàng)建虛擬機(jī)
3.創(chuàng)建虛擬機(jī)
3.1 創(chuàng)建一個(gè)10G的虛擬機(jī)硬盤(pán)
[root@Marvin-Node1 ~]# qemu-img create -f qcow2 /tmp/centos.qcow2 10G
3.2 創(chuàng)建虛擬機(jī)
[root@Marvin-Node1 ~]# virt-install --virt-type kvm --name centos --ram 1024 \
> --disk /tmp/centos.qcow2,format=qcow2 \
> --network network=default \
> --graphics vnc,listen=0.0.0.0 --noautoconsole \
> --os-type=linux --os-variant=centos7.0 \
> --location=/opt/image/CentOS-7-x86_64-Everything-1511.iso
Starting install...
Retrieving file .treeinfo... | 1.1 kB 00:00:00
Retrieving file vmlinuz... | 4.9 MB 00:00:00
Retrieving file initrd.img... | 37 MB 00:00:00
Creating domain... | 0 B 00:00:00
Domain installation still in progress. You can reconnect to
the console to complete the installation process.
通過(guò)VNC工具連接到管理界面進(jìn)行系統(tǒng)安裝
時(shí)區(qū)選擇上海
選擇支持中文
分區(qū),將所有容量都給根分區(qū),且只分一個(gè)根分區(qū)
完成安裝,設(shè)置root密碼
安裝完成后點(diǎn)擊reboot,系統(tǒng)會(huì)關(guān)機(jī),并不會(huì)重啟,這時(shí)候在宿主機(jī)上可以通過(guò)命令查看剛才已經(jīng)建立的虛擬機(jī)
[root@Marvin-Node1 ~]# virsh list --all
Id Name State
----------------------------------------------------
- centos shut off
啟動(dòng)虛擬機(jī)
[root@Marvin-Node1 ~]# virsh start centos
Domain centos started
啟動(dòng)后再次通過(guò)VNC工具連接上去,進(jìn)行IP配置,系統(tǒng)優(yōu)化,安裝常用軟件,配置yum源等工作,ip的如果不需要IPv6,只要留下以下4行就行
新建的虛擬機(jī)上操作:
編輯網(wǎng)絡(luò)
vi /etc/sysconfig/network-scripts/ifcfg-eth0
按照?qǐng)DIP配置來(lái)修改
修改完成后重啟network
systemctl restart network
測(cè)試外網(wǎng)是否聯(lián)通,ping 百度等網(wǎng)站測(cè)試
安裝常用軟件
yum install vim lrzsz net-tools ntp tree wget -y
關(guān)閉防火墻,關(guān)閉selinux
sed -i 's@=enforcing@=disabled@' /etc/sysconfig/selinux
systemctl stop firewalld && systemctl disable firewalld
配置yum源
[root@localhost ~]# rpm -Uvh http://mirrors.zju.edu.cn/epel/7Server/x86_64/e/epel-release-7-10.noarch.rpm
[root@localhost ~]# yum install yum-plugin-fastestmirror -y
設(shè)置完成后關(guān)閉虛擬機(jī)
4. 在OpenStack的管理節(jié)點(diǎn)創(chuàng)建虛擬機(jī)鏡像
[root@Marvin-Node1 ~]# source admin-openstack.sh
[root@Marvin-Node1 ~]# openstack image create "centos7-x86_64" --file /tmp/centos.qcow2 --disk-format qcow2 --container-format bare --public
+------------------+------------------------------------------------------+
| Field | Value |
+------------------+------------------------------------------------------+
| checksum | ec4343723602ab6df95ffeecee3d96ac |
| container_format | bare |
| created_at | 2017-09-11T17:19:01Z |
| disk_format | qcow2 |
| file | /v2/images/66d34783-d85c-479f-ad89-9fbdc3da38f7/file |
| id | 66d34783-d85c-479f-ad89-9fbdc3da38f7 |
| min_disk | 0 |
| min_ram | 0 |
| name | centos7-x86_64 |
| owner | 22cd7c4eb794462a94ef4907971effb9 |
| protected | False |
| schema | /v2/schemas/image |
| size | 1243676672 |
| status | active |
| tags | |
| updated_at | 2017-09-11T17:19:08Z |
| virtual_size | None |
| visibility | public |
+------------------+------------------------------------------------------+
[root@Marvin-Node1 ~]# cd /var/lib/glance/images/
[root@Marvin-Node1 images]# ll -h
total 1.2G
-rw-r----- 1 glance glance 1.2G Sep 12 01:19 66d34783-d85c-479f-ad89-9fbdc3da38f7 ## 這個(gè)1.2G的就是創(chuàng)建的鏡像
-rw-r----- 1 glance glance 13M Sep 9 01:31 91355df9-dd45-45d6-9b95-67b61aedafbd
[root@Marvin-Node1 images]#
5. 在web界面創(chuàng)建一臺(tái)centos7的虛擬機(jī)
5.1 使用admin登錄到web界面
5.2 創(chuàng)建云主機(jī)類型
創(chuàng)建完成后注銷登錄,使用demo用戶登錄到web
5.3 創(chuàng)建centos7的云主機(jī)
創(chuàng)建完成后在這里就可以看到虛擬機(jī)了
點(diǎn)擊右邊的管理欄,打開(kāi)控制臺(tái)
看到控制臺(tái)后就可以使用前面root帳號(hào)的密碼登錄了
注意: 登錄后修改主機(jī)名及IP,避免生產(chǎn)中重復(fù),另外可以直接在創(chuàng)建鏡像的時(shí)候,寫(xiě)一個(gè)腳本,在新建云主機(jī)完成后,直接更改主機(jī)名和IP地址.
另外,后面幾張圖不是當(dāng)前環(huán)境的IP,是自己用來(lái)測(cè)試的另外一套環(huán)境截的圖.
筆者QQ: 779734791 昵稱: Marvin 歡迎大家一起討論.