服务器虚拟化
0x01 准备工作
虚拟机开启虚拟化
确定是否已经支持KVM
[root@localhost ~]# egrep -o '(vmx | svm)' /proc/cpuinfo
命令执行后输出vmx
说明开启虚拟化成功
0x02 安装KVM以及相关组件依赖
换源
在安装之前先换成国内源,把速度搞起来
CentOS 5
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo
CentOS 6
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
CentOS 7
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
安装VNC
yum install vnc
yum install vnc-server
安装之后重启虚拟机
安装QEMU、libvirt
yum install -y qemu-kvm qemu-img virt-manager virt-install virt-viewer libguest* libvirt*
yum groupinstall -y virtualization-client virtualization-platform virtualization-tools
确认安装完成
命令行运行以下命令
[root@localhost ~]# virsh
Welcome to virsh, the virtualization interactive terminal.
Type: 'help' for help with commands
'quit' to quit
virsh #
出现以上内容说明安装成功了
0x03 创建虚拟机虚拟磁盘
创建一个10G大小qcow2格式的虚拟磁盘
在系统根目录运行
qemu-img create -f qcow2 -o preallocation=falloc Cloud_Centos6.4_64bit.qcow2 10G
修改vnc_listen
vi /etc/libvirt/qemu.conf
# 把 vnc_listen = "0.0.0.0" 前面的#去掉
重启libvirtd
service libvirtd restart
0x04 创建KVM虚拟机(命令行)
需要准备你想安装CentOS版本镜像,上传到根目录
[root@localhost /]# ls
CentOS-6.4-x86_64-bin-DVD1.iso
在系统根目录运行
virt-install --name Cloud_Centos6_64bit --ram 1024 --vcpus=1 --disk path=/Cloud_Centos6.4_64bit.qcow2 --network network:default,model=virtio --arch=x86_64 --os-type=linux --os-variant=rhel6 --graphics vnc,port=5910 --cdrom /CentOS-6.4-x86_64-bin-DVD1.iso --boot cdrom
--name 虚拟机的名字
--disk path=PATH 创建的虚拟磁盘的路径
--cdrom PATH CentOS iso镜像路径
--graphics vnc,port=PORT 开启VNC 端口
运行该命令之后,图形化界面的系统应该会弹安装界面,不是图形化界面的需要物理机使用VNC连接虚拟机IP,端口为你刚刚设置的
如果虚拟机需要调用VNC远程访问KVM虚拟机, 那么本机至少安装gnome或者kde其中的一种图形显示界面 ,然后在左上角
Applications -> Internet -> TigerVNC Viewer
打开VNC,直接填虚拟机的IP就可以连接到KVM虚拟机了
其实图形化界面的虚拟机就不需要使用VNC连接KVM虚拟机了
然后跟正常安装虚拟机一样安装就行了
最小化安装的如果出现无IP,无法联网,需要运行以下命令
[root@localhost ~]# dhclient
# 获取IP
0x05 创建KVM虚拟机(Virtual Machine Manager)
......
通过Virtual Machine Manager 安装KVM虚拟机有两种方法
- 直接在Manager里面新建,最后找到新建的img镜像,通过
qemu-img
相关命令把img 转换为qcow2 - 通过命令创建虚拟磁盘,通过Virtual Machine Manager 配置虚拟机的配置
0x06 搭建网络服务
搭建LAMP
使用一键脚本搭建LAMP
这里给出一个
wget http://soft.vpser.net/lnmp/lnmp1.6.tar.gz -cO lnmp1.6.tar.gz && tar zxf lnmp1.6.tar.gz && cd lnmp1.6 && LNMP_Auto="y" DBSelect="2" DB_Root_Password="root" InstallInnodb="y" PHPSelect="5" SelectMalloc="1" ApacheSelect="2" ServerAdmin="webmaster@example.com" ./install.sh lamp
MySQL 5.5
数据库Root密码 :root
PHP 5.6
Apache 2.4
漫长的等待开始了...
安装成功之后,浏览器打开127.0.0.1
测试Apache是否正常运行
安装vsftp
# 安装
yum -y install vsftpd
# 设置开机自启
chkconfig vsftpd on
相关命令
# 启动/查看状态/重启/关闭
service vsftpd [start/status/restart/stop]
# 创建用户
sudo useradd -d /home/ftp 用户名
# 设置用户到文件夹
chown -R 用户名 /home/ftp
# 禁止用户登录系统
usermod -s /sbin/nologin 用户名
# 设置该文件的权限
chown -R 777 /home/ftp
# 添加密码
password 用户名
0x07 导入镜像
因为我们创建的是raw格式的镜像,非稀疏文件,整个磁盘大小就是镜像大小,嫌弃镜像太大的话,可以把他转化为qcow2格式,压缩,然后使用转换后的虚拟磁盘传到eve-NG
在虚拟磁盘所处目录运行
qemu-img convert -O qcow2 Cloud_Centos6.4_64bit.qcow2 Cloud_Centos6.4_64bit1.qcow2
上传镜像到EVE-NG
安装网络服务之后,在虚拟机测试好,确保Apache和FTP服务能正常运行,关闭虚拟机里面的虚拟机
在eve-NG的/opt/unetlab/addons/qemu/
目录下创建一个格式为linux-centos*
的目录
eve-NG的目录以及文件命名必要按照格式,否则eve识别不出
文件夹 linux-centos* 可以改为你自己的centos版本号,比如 linux-centos7
使用scp
命令把虚拟磁盘(qcow2镜像)上传到eve-NG刚刚创建的目录下
[root@localhost /]# scp Cloud_Centos6.4_64bit.qcow2 root@192.168.136.156:/opt/unetlab/addons/qemu/linux-centos6.4/hda.qcow2
我的eve-NG的IP是192.168.136.156,具体IP看自己的
文件命名必须为 hda.qcow2
EVE-NG修正权限
/opt/unetlab/wrappers/unl_wrapper -a fixpermissions
添加节点
搞完这些之后,可以在eve管理页面添加这个
0x08 测试
添加路由和Net,具体用Cloud几就要看你的
Linux服务器设置IP
IP --> 110.110.110.113
子网掩码 --> 255.255.255.248
网关 --> 110.110.110.118
Router设置接口IP
F0/1 --> 110.110.110.118
F0/0 --> 192.168.137.50
Router#conf t
Enter configuration commands, one per line. End with CNTL/Z.
Router(config)#int f0/1
Router(config-if)#ip address 110.110.110.118 255.255.255.248
Router(config-if)#no shut
Router(config)#int f0/0
Router(config-if)#ip address 192.168.137.50 255.255.255.0
Router(config-if)#no shut
Router(config-if)#end
Router#wr
Building configuration...
[OK]
Router#
Router设置路由
Router(config)#ip route 110.110.110.112 255.255.255.248 110.110.110.113
Router(config)#end
Router#wr
*Mar 1 00:04:25.695: %SYS-5-CONFIG_I: Configured from console by console
Building configuration...
[OK]
Router#show ip route
Codes: C - connected, S - static, R - RIP, M - mobile, B - BGP
D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area
N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2
E1 - OSPF external type 1, E2 - OSPF external type 2
i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2
ia - IS-IS inter area, * - candidate default, U - per-user static route
o - ODR, P - periodic downloaded static route
Gateway of last resort is not set
110.0.0.0/29 is subnetted, 1 subnets
C 110.110.110.112 is directly connected, FastEthernet0/1
C 192.168.137.0/24 is directly connected, FastEthernet0/0
Router#
物理机设置静态路由
Win+X 使用Windwos PowerShell (管理员)
C:\Windows\system32> route add -p 110.110.110.112 mask 255.255.255.248 192.168.136.50
# route add -p 目的网络 mask 目的网络子网掩码 下一跳
# 这里下一跳即为路由器连接Cloud的接口IP
-p 永久路由 持久化处理,重启系统路由仍有效
# 删除路由
C:\Windows\system32> route delete 110.110.110.112
# 打印当前路由表
C:\Windows\system32> route print
尝试在物理机用浏览器打开虚拟服务器的IP110.110.110.113
还有一个Seafile,看文档就行了
下班!!!