Vagrant安装和使用

摘要:简介在一次偶然的机会我碰到了vagrant,Vagrant是一个基于Ruby的工具,用于创建和部署虚拟化开发环境,总之简单的说vagrant就是一款管理虚拟机的工具官网
https://www.vagrantup.com/ 一些文档还有镜像可以在官网里找到安装安装很简单,只要下载对应的安装包,安装起来就好了,比如我用的是fedora那就下载rpm包,上面写着centos的就是了下载好后安装sudodnfinstall./vagrant_1.9.7_x86_64.rpm接着安装vi

简介
之前使用的是phpstudy、wamp等Windows系统集成环境,现在使用Mac系统还没有找到适合的集成环境。在一次偶然的机会我碰到了vagrant,Vagrant是一个基于Ruby的工具,用于创建和部署虚拟化开发环境,总之简单的说vagrant就是一款管理虚拟机的工具

官网

https://www.vagrantup.com/
一些文档还有镜像可以在官网里找到

安装

安装很简单,只要下载对应的安装包,安装起来就好了,比如我用的是fedora那就下载rpm包,上面写着centos的就是了
下载好后安装
sudo dnf install ./vagrant_1.9.7_x86_64.rpm
接着安装virtualbox
virtualbox的安装和vagrant一样,先下载安装包之后安装就好了

使用

首先添加一个镜像
vagrant box add ubuntu/xenial64
我添加的是ubuntu16.04 64位的虚拟机镜像,具体你想下载什么镜像你可以在
https://app.vagrantup.com/boxes/search?_ga=2.204284501.492827600.1503221431-11326269.1503221431
这个网站里找
出现下面的提示说明镜像添加成功

➜ ~ vagrant box add ubuntu/xenial64
==> box: Loading metadata for box ‘ubuntu/xenial64’
box: URL: https://vagrantcloud.com/ubuntu/xenial64
==> box: Adding box ‘ubuntu/xenial64’ (v20170815.1.0) for provider: virtualbox
box: Downloading: https://vagrantcloud.com/ubuntu/boxes/xenial64/versions/20170815.1.0/providers/virtualbox.box
==> box: Box download is resuming from prior download progress
==> box: Successfully added box ‘ubuntu/xenial64’ (v20170815.1.0) for ‘virtualbox’!

之后你可以确认一下镜像是否添加成功

➜ ~ vagrant box list
ubuntu/xenial64 (virtualbox, 20170815.1.0)

接着创建一个目录来存放Vagrantfile
mkdir bboysoul
cd bboysoul/
初始化,此操作会在当前目录下生成一个Vagrantfile

➜ bboysoul vagrant init ubuntu/xenial64
A Vagrantfile has been placed in this directory. You are now
ready to vagrant up your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
vagrantup.com for more information on using Vagrant.
➜ bboysoul ls
Vagrantfile
➜ bboysoul

接着启动一个虚拟机

➜ vagrant up
Bringing machine ‘default’ up with ‘virtualbox’ provider…
==> default: Importing base box ‘ubuntu/xenial64’…
==> default: Matching MAC address for NAT networking…
==> default: Checking if box ‘ubuntu/xenial64’ is up to date…
==> default: Setting the name of the VM: bboysoul_default_1503234312551_82817
==> default: Clearing any previously set network interfaces…
==> default: Preparing network interfaces based on configuration…
default: Adapter 1: nat
==> default: Forwarding ports…
default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running ‘pre-boot’ VM customizations…
==> default: Booting VM…
==> default: Waiting for machine to boot. This may take a few minutes…
default: SSH address: 127.0.0.1:2222
default: SSH username: ubuntu
default: SSH auth method: password
default: Warning: Connection reset. Retrying…
default: Warning: Remote connection disconnect. Retrying…
default:
default: Inserting generated public key within guest…
default: Removing insecure key from the guest if it’s present…
default: Key inserted! Disconnecting and reconnecting using new SSH key…
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM…
default: The guest additions on this VM do not match the installed version of
default: VirtualBox! In most cases this is fine, but in rare cases it can
default: prevent things such as shared folders from working properly. If you see
default: shared folder errors, please make sure the guest additions within the
default: virtual machine match the version of VirtualBox you have installed on
default: your host and reload your VM.
default:
default: Guest Additions Version: 5.0.40
default: VirtualBox Version: 5.1
==> default: Mounting shared folders…
default: /vagrant => /home/bboysoul/bboysoul

启动完成之后你可以打开virtualbox,你可以看到一个已经启动完成的虚拟机
接着ssh进去

➜ bboysoul vagrant ssh
Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-92-generic x86_64)


WARNING! Your environment specifies an invalid locale.
The unknown environment variables are:
LC_CTYPE=zh_CN.UTF-8 LC_ALL=
This can affect your user experience significantly, including the
ability to manage packages. You may install the locales by running:
sudo apt-get install language-pack-zh
or
sudo locale-gen zh_CN.UTF-8
To see all available language packs, run:
apt-cache search “^language-pack-[a-z][a-z]$”
To disable this message for all users, run:
sudo touch /var/lib/cloud/instance/locale-check.skip


从启动虚拟机的时候的输出信息我们可以看到还挂载了一个共享目录

==> default: Mounting shared folders…
default: /vagrant => /home/bboysoul/bboysoul

在虚拟机里的路径是/vagrant,实体机的路径是/home/bboysoul/bboysoul
也就是Vagrantfile的所在路径

xxx@xxx.com:~$ ls /vagrant/
Vagrantfile ubuntu-xenial-16.04-cloudimg-console.log
xxx@xxx.com:~$

接着你可以退出来重启虚拟机
vagrant reload

当你想要退出Vagrant时
在命令行敲exit,回车即可

➜ bboysoul vagrant reload
==> default: Attempting graceful shutdown of VM…
==> default: Checking if box ‘ubuntu/xenial64’ is up to date…
==> default: Clearing any previously set forwarded ports…
==> default: Clearing any previously set network interfaces…
==> default: Preparing network interfaces based on configuration…
default: Adapter 1: nat
==> default: Forwarding ports…
default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running ‘pre-boot’ VM customizations…
==> default: Booting VM…
==> default: Waiting for machine to boot. This may take a few minutes…
default: SSH address: 127.0.0.1:2222
default: SSH username: ubuntu
default: SSH auth method: password
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM…
default: The guest additions on this VM do not match the installed version of
default: VirtualBox! In most cases this is fine, but in rare cases it can
default: prevent things such as shared folders from working properly. If you see
default: shared folder errors, please make sure the guest additions within the
default: virtual machine match the version of VirtualBox you have installed on
default: your host and reload your VM.
default:
default: Guest Additions Version: 5.0.40
default: VirtualBox Version: 5.1
==> default: Mounting shared folders…
default: /vagrant => /home/bboysoul/bboysoul
==> default: Machine already provisioned. Run vagrant provision or use the --provision
==> default: flag to force provisioning. Provisioners marked to run always will still run.

你可以使用
vagrant halt
来关闭虚拟机

➜ bboysoul vagrant halt
==> default: Attempting graceful shutdown of VM…

vagrant up
来启动虚拟机

➜ bboysoul vagrant up
Bringing machine ‘default’ up with ‘virtualbox’ provider…
==> default: Checking if box ‘ubuntu/xenial64’ is up to date…
==> default: Clearing any previously set forwarded ports…
==> default: Clearing any previously set network interfaces…
==> default: Preparing network interfaces based on configuration…
default: Adapter 1: nat
==> default: Forwarding ports…
default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running ‘pre-boot’ VM customizations…
==> default: Booting VM…
==> default: Waiting for machine to boot. This may take a few minutes…
default: SSH address: 127.0.0.1:2222
default: SSH username: ubuntu
default: SSH auth method: password
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM…
default: The guest additions on this VM do not match the installed version of
default: VirtualBox! In most cases this is fine, but in rare cases it can
default: prevent things such as shared folders from working properly. If you see
default: shared folder errors, please make sure the guest additions within the
default: virtual machine match the version of VirtualBox you have installed on
default: your host and reload your VM.
default:
default: Guest Additions Version: 5.0.40
default: VirtualBox Version: 5.1
==> default: Mounting shared folders…
default: /vagrant => /home/bboysoul/bboysoul
==> default: Machine already provisioned. Run vagrant provision or use the --provision
==> default: flag to force provisioning. Provisioners marked to run always will still run.

一些常用的命令

可以从vagrant的help来看

➜ bboysoul vagrant list-commands
Below is a listing of all available Vagrant commands and a brief
description of what they do.
box manages boxes: installation, removal, etc.
cap checks and executes capability
connect connect to a remotely shared Vagrant environment
destroy stops and deletes all traces of the vagrant machine
docker-exec attach to an already-running docker container
docker-logs outputs the logs from the Docker container
docker-run run a one-off command in the context of a container
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
list-commands outputs all available Vagrant subcommands, even non-primary ones
login log in to HashiCorp’s Vagrant Cloud
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
provider show provider for this environment
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
rsync syncs rsync synced folders to remote machine
rsync-auto syncs rsync synced folders automatically when files change
share share your Vagrant environment with anyone in the world
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
validate validates the Vagrantfile
version prints current and latest Vagrant version

现在列举一些常用的

vagrant box 用来管理box的

vagrant box add 添加一个box
vagrant box list 列出可用的box
vagrant box remove 删除一个box
vagrant box

vagrant halt 停止一个虚拟机
vagrant init 初始化一个虚拟机
vagrant package 打包一个虚拟机
vagrant reload 重启一个虚拟机
vagrant ssh ssh进虚拟机
vagrant up 启动一个虚拟机
vagrant status 查看虚拟机状态
打包分发虚拟机

当你把所有的环境都配置好之后,下一步要做的就是打包分发了
一个命令就可以搞定你的打包

➜ bboysoul vagrant package
==> default: Attempting graceful shutdown of VM…
==> default: Clearing any previously set forwarded ports…
==> default: Exporting VM…
==> default: Compressing package to: /home/bboysoul/bboysoul/package.box
➜ bboysoul ls
package.box ubuntu-xenial-16.04-cloudimg-console.log Vagrantfile

如显示所示vagrant会把你的虚拟机先关闭再进行打包,之后会在当前目录下生成一个package.box的文件,之后你把这个文件通过网盘或者其他文件共享的方式发给要部署的机器直接部署就好了

高级使用

你会看到我们一直没有说vagrant的配置文件Vagrantfile,这个文件主要配置了虚拟机的配置、SSH配置、Vagrant的一些基础配置。
下面我说一下常见的配置

box的名称配置
config.vm.box = “ubuntu12.04”
同步目录配置
config.vm.synced_folder “本机地址”, “虚拟机文件地址”
主机hostname设置
config.vm.hostname = “for_work”
端口转发配置
config.vm.network “forwarded_port”, guest: 80, host: 8080

其他的配置文件大家可以参考下面这个网站https://www.vagrantup.com/docs/vagrantfile/

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Vagrant安装CentOS可以参考以下步骤: 1. 首先,你需要在你的计算机上安装Vagrant和VirtualBox,这两个工具可以帮助你创建和管理虚拟机。 2. 在命令行中,创建一个新的目录来存放你的Vagrant项目:mkdir my-centos-project 3. 进入到这个新创建的目录:cd my-centos-project 4. 初始化Vagrant配置文件:vagrant init centos/7 5. 使用文本编辑器打开Vagrantfile,并根据你的需求进行必要的修改。你可以设置主机名、IP地址、端口转发等等。 6. 保存并关闭Vagrantfile后,运行vagrant up命令来启动虚拟机。 7. 当虚拟机启动完成后,你可以使用vagrant ssh命令登录到虚拟机中。 8. 现在你就可以在虚拟机中操作CentOS了,可以安装软件包、配置网络等等。 需要注意的是,这里提供的是一个基本的Vagrant安装CentOS的步骤。具体的操作可能会因为不同的环境和需求而有所不同,你可以根据自己的情况进行调整。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [m1使用VMware安装CentOS7并部署k8s高可用集群](https://blog.csdn.net/m0_66403673/article/details/127799060)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值