前言
由于工作和学习的需要,在使用centos7时,也需要使用虚拟化创建主机,会经常使用vagrant管理工具来创建虚拟主机,所以写了这个总结,回顾一下。
特别说明:如果你只是想成功的快速安装好这个环境的情况下,请阅读完全文后在操作,其中有些依赖我没有同时安装,谢谢!
1、前期准备
1)vagrant
Vagrant是一个基于Ruby的工具,用于创建和部署虚拟化开发环境。它 使用Oracle的开源VirtualBox虚拟化系统,使用 Chef创建自动化虚拟环境。(引用百度百科https://baike.baidu.com/item/Vagrant/1321946?fr=aladdin)
详细的说明,请查询官网文档,软件下载地址:https://www.vagrantup.com/
2)VirtualBox
VirtualBox is a powerful x86 and AMD64/Intel64 virtualization product for enterprise as well as home use. Not only is VirtualBox an extremely feature rich, high performance product for enterprise customers, it is also the only professional solution that is freely available as Open Source Software under the terms of the GNU General Public License (GPL) version 2.(官网说明)
软件下载地址:https://www.virtualbox.org/
这里我们需要的是在官网下载rpm的安装包的安装方式,当然也可以使用yum安装VirtualBox,下边我会放上yum源(往后翻翻哦)。
3)检查电脑的bios中的虚拟化是不是打开了,这个电脑的型号比较多,请自行百度。centos中查看虚拟化的命令如下:
查看cpu是否支持 # grep -E 'svm|vmx' /proc/cpuinfo - vmx is for Intel processors - svm is for AMD processors
4)VirtualBox的yum源配置(官方地址:https://www.virtualbox.org/wiki/Linux_Downloads,在页面的最下边)
vim /etc/yum.repos.d/vbox.repo [virtualbox] name=Oracle Linux / RHEL / CentOS-$releasever / $basearch - VirtualBox baseurl=http://download.virtualbox.org/virtualbox/rpm/el/$releasever/$basearch enabled=1 gpgcheck=1 repo_gpgcheck=1 gpgkey=https://www.virtualbox.org/download/oracle_vbox.asc
2、安装部署
1、当上边配置完成或者软件下载完成后直接安装,安装命令如下:
[root@localhost ~]# rpm -ivh VirtualBox-6.0-6.0.8_130520_el7-1.x86_64.rpm 警告:VirtualBox-6.0-6.0.8_130520_el7-1.x86_64.rpm: 头V4 DSA/SHA1 Signature, 密钥 ID 98ab5139: NOKEY 错误:依赖检测失败: libGL.so.1()(64bit) 被 VirtualBox-6.0-6.0.8_130520_el7-1.x86_64 需要 libICE.so.6()(64bit) 被 VirtualBox-6.0-6.0.8_130520_el7-1.x86_64 需要 libSDL-1.2.so.0()(64bit) 被 VirtualBox-6.0-6.0.8_130520_el7-1.x86_64 需要 libSM.so.6()(64bit) 被 VirtualBox-6.0-6.0.8_130520_el7-1.x86_64 需要 libX11-xcb.so.1()(64bit) 被 VirtualBox-6.0-6.0.8_130520_el7-1.x86_64 需要 libX11.so.6()(64bit) 被 VirtualBox-6.0-6.0.8_130520_el7-1.x86_64 需要 libXcursor.so.1()(64bit) 被 VirtualBox-6.0-6.0.8_130520_el7-1.x86_64 需要 libXext.so.6()(64bit) 被 VirtualBox-6.0-6.0.8_130520_el7-1.x86_64 需要 libXmu.so.6()(64bit) 被 VirtualBox-6.0-6.0.8_130520_el7-1.x86_64 需要 libXrender.so.1()(64bit) 被 VirtualBox-6.0-6.0.8_130520_el7-1.x86_64 需要 libXt.so.6()(64bit) 被 VirtualBox-6.0-6.0.8_130520_el7-1.x86_64 需要 libfontconfig.so.1()(64bit) 被 VirtualBox-6.0-6.0.8_130520_el7-1.x86_64 需要 libopus.so.0()(64bit) 被 VirtualBox-6.0-6.0.8_130520_el7-1.x86_64 需要 libpng15.so.15()(64bit) 被 VirtualBox-6.0-6.0.8_130520_el7-1.x86_64 需要 libpng15.so.15(PNG15_0)(64bit) 被 VirtualBox-6.0-6.0.8_130520_el7-1.x86_64 需要 libvpx.so.1()(64bit) 被 VirtualBox-6.0-6.0.8_130520_el7-1.x86_64 需要 libxcb.so.1()(64bit) 被 VirtualBox-6.0-6.0.8_130520_el7-1.x86_64 需要
使用rpm包安装的时候,会出现上边的错误,缺少依赖,这是linux的特性,下边我们安装依赖:
yum -y install libGL libICE libSM libXcursor libXmu libXrender libXt libpng libvpx SDL fontconfig opus 安装依赖库
然后就可以直接安装vbox(这里简写了)了
[root@localhost ~]# rpm -ivh VirtualBox-6.0-6.0.8_130520_el7-1.x86_64.rpm 警告:VirtualBox-6.0-6.0.8_130520_el7-1.x86_64.rpm: 头V4 DSA/SHA1 Signature, 密钥 ID 98ab5139: NOKEY 准备中... ################################# [100%] 正在升级/安装... 1:VirtualBox-6.0-6.0.8_130520_el7-1################################# [100%] Creating group 'vboxusers'. VM users must be member of that group! This system is currently not set up to build kernel modules. Please install the gcc make perl packages from your distribution. Please install the Linux kernel "header" files matching the current kernel for adding new hardware support to the system. The distribution packages containing the headers are probably: kernel-devel kernel-devel-3.10.0-862.el7.x86_64 This system is currently not set up to build kernel modules. Please install the gcc make perl packages from your distribution. Please install the Linux kernel "header" files matching the current kernel for adding new hardware support to the system. The distribution packages containing the headers are probably: kernel-devel kernel-devel-3.10.0-862.el7.x86_64 There were problems setting up VirtualBox. To re-start the set-up process, run /sbin/vboxconfig as root. If your system is using EFI Secure Boot you may need to sign the kernel modules (vboxdrv, vboxnetflt, vboxnetadp, vboxpci) before you can load them. Please see your Linux system's documentation for more information.
到这里,还是缺少东西,提示我们我们linux核心上的支持相应虚拟化的模块没有添加,但是添加这些模块的也有相应的依赖,解决依赖如下:
yum -y install gcc gcc-c++ kernel-devel
安装完成后,执行如下命令,重新加载相应的内核模块
[root@localhost ~]# vboxconfig vboxdrv.sh: Stopping VirtualBox services. vboxdrv.sh: Starting VirtualBox services. vboxdrv.sh: Building VirtualBox kernel modules.
其实,在加载相应的模块的时候,我在官网的教程上也看到这样的一个命令,同样是可以的。
rcvboxdrv setup
系统核心加载相应的虚拟化模块,具体的原因参考vbox官网的使用手册。
2、安装vagrant,这个安装就没有那么多的问题了,直接执行如下命令。
[root@localhost ~]# rpm -ivh vagrant_2.2.5_x86_64.rpm 准备中... ################################# [100%] 正在升级/安装... 1:vagrant-1:2.2.5-1 ################################# [100%]
3、关于vagrant的使用
查看帮助文档
[root@localhost ~]# vagrant --help Usage: vagrant [options] <command> [<args>] -v, --version Print the version and exit. -h, --help Print this help. Common commands: box manages boxes: installation, removal, etc. cloud manages everything related to Vagrant Cloud destroy stops and deletes all traces of the vagrant machine global-status outputs status Vagrant environments for this user halt stops the vagrant machine help shows the help for a subcommand init initializes a new Vagrant environment by creating a Vagrantfile login package packages a running vagrant environment into a box plugin manages plugins: install, uninstall, update, etc. port displays information about guest port mappings powershell connects to machine via powershell remoting provision provisions the vagrant machine push deploys code in this environment to a configured destination rdp connects to machine via RDP reload restarts vagrant machine, loads new Vagrantfile configuration resume resume a suspended vagrant machine snapshot manages snapshots: saving, restoring, etc. ssh connects to machine via SSH ssh-config outputs OpenSSH valid configuration to connect to the machine status outputs status of the vagrant machine suspend suspends the machine up starts and provisions the vagrant environment upload upload to machine via communicator validate validates the Vagrantfile version prints current and latest Vagrant version winrm executes commands on a machine via WinRM winrm-config outputs WinRM configuration to connect to the machine For help on any individual command run `vagrant COMMAND -h` Additional subcommands are available, but are either more advanced or not commonly used. To see all subcommands, run the command `vagrant list-commands`.
最常用的命令顺序是:
vagrant box 【url】 下载镜像*.box
vagrant init box-name 虚拟机初始化,创建vagrantfile文件
vagrant up 启动虚拟机
vagrant ssh 远程连接虚拟机
vagrant halt 关闭虚拟机
vagrant destory 删除虚拟机
由于国外的box镜像下载太不好用,导致经常下载失败,建议使用清华的源,命令如下:
vagrant box add https://mirrors.tuna.tsinghua.edu.cn/ubuntu-cloud-images/bionic/current/bionic-server-cloudimg-amd64-vagrant.box --name ubuntu/bionic 下载box的虚拟镜像
vagrant init ubuntu/bionic 初始化镜像 vagrant up 启动虚拟机 vagrant ssh 通过ssh连接虚拟机 vagrant halt 关闭虚拟机
注:其实这里如果你没有相应的box源的话,也可以通过浏览器下载完成后,复制到centos中,在执行init初始化,也是可以的。
到这里基本的安装就结束了。