准备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),接下来的步骤和我们平时安装系统没有什么区别,注意,只分根分区,不需要分交换分区。
[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"
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
[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