Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VAGRANT 使用 #46

Open
pfan123 opened this issue Aug 21, 2019 · 0 comments
Open

VAGRANT 使用 #46

pfan123 opened this issue Aug 21, 2019 · 0 comments

Comments

@pfan123
Copy link
Owner

pfan123 commented Aug 21, 2019

Vagrant 是基于Ruby的工具,依赖 VirtualBox,VMware,AWS 或其他虚拟机管理软件 (provider) 的接口来创建虚拟机, 用于创建和部署虚拟化开发环境。标准的 配置管理工具(provisioning tools (例如 shell 脚本, Chef, 或 Puppet)都可以在 Vagrant 创建的虚拟机上使用,用来进行自动安装及配置管理。我们可以使用它来干如下这些事:

  • 建立和删除虚拟机
  • 配置虚拟机运行参数
  • 管理虚拟机运行状态
  • 自动配置和安装开发环境
  • 打包和分发虚拟机运行环境

注意版本兼容问题目前 2.0 版本不兼容前面的版本。

Install VirtualBox

Download VirtualBox: https://www.virtualbox.org/wiki/Downloads

Install Vagrant

Download Vagrant: https://www.vagrantup.com/downloads.html

Uninstall Vagrant

On Windows

Uninstall using the add/remove programs section of the control panel

On Mac OS X

rm -rf /opt/vagrant
rm -f /usr/local/bin/vagrant
sudo pkgutil --forget com.vagrant.vagrant

On Linux

rm -rf /opt/vagrant
rm -f /usr/bin/vagrant

Vagrantfile 配置

创建使用 Vagrant 的第一步就是对 Vagrantfile 配置文件进行配置。Vagrantfile 配置文件的作用有两个方面:

  1. 设置项目的根目录,很多 Vagrant 的配置都是与根目录有紧密关系。
  2. 定义项目中所需的虚拟机类型、资源、插件。
# Setting the Language using environment variable LC_ALL and LANG in UNIX OS in EngageOne Generate
ENV["LC_ALL"] = "en_US.UTF-8"
$Script = <<-SHELL
    apt-get update
    apt-get install -y nginx
  SHELL
# Vagrantfile 配置 基本格式
Vagrant.configure("2") do |config|
	# 指定镜像
	config.vm.box = "centos/7"  
	# 分配磁盘大小
	config.disksize.size = '50GB'
	# 定义转发端口
	config.vm.network "forwarded_port", guest: 80, host: 8080
	config.vm.boot_timeout = 5000
	config.ssh.username = 'Vagrant'	
	
	# 定义依赖虚拟机内存、CPU等
	config.vm.provider "virtualbox" do |vb|
  	vb.name = "my_vm"
  	vb.memory = "1024"
  	vb.cpus = 2
	end
	
	# Vagrant 中 Provisioner 允许你自动安装软件、更改配置
	# 通常在 vagrant up、vagrant reload执行,可以--provision(强制执行)、--no-provision (取消执行)
	# 变量换行模式
	config.vm.provision "shell" do |s|
    s.inline = "echo hello"
  end
  # inline 模式
	config.vm.provision "shell", inline: $Script
  # ...
end

初始化 Vagrant 项目

$ mkdir vagrant_getting_started  # 建立 Vagrant 项目目录
$ cd vagrant_getting_started
$ vagrant init # 初始化 vagrant 项目,生成 Vagrantfile, 也可直接 vagrant init centos/7 生成对应 Vagrantfile

BOXES 镜像

基础镜像包在 Vagrant 中被称为 boxes ,类似于docker体系中的image(镜像)。

查找更多 Vagrant Boxes https://app.vagrantup.com/boxes/search

$ vagrant box list  # 列出本地环境中所有的box

$ vagrant box add centos/7 # 添加 box 到本地vagrant环境

# 选择 provider,依赖虚拟技术提供方,如我本地安装 virtualbox 则选择 virtualbox
1) hyperv
2) libvirt
3) virtualbox
4) vmware_desktop

$ vagrant box update box-name  # 更新本地指定 box
$ vagrant box remove box-name  # 删除本地指定 box
$ vagrant box repackage box-name	# 重新打包本地指定 box

Vagrant 基本命令

# 在空文件夹初始化虚拟机
$ vagrant init [box-name]

# 在初始化完的文件夹内启动虚拟机
$ vagrant up

# ssh登录启动的虚拟机
$ vagrant ssh

# 挂起本地启动的虚拟机
$ vagrant suspend

# 恢复本地启动的虚拟机
$ vagrant suspend

# 重启虚拟机
$ vagrant reload

# 关闭虚拟机
$ vagrant halt

# 查找虚拟机的运行状态
$ vagrant status

# 销毁当前虚拟机
$ vagrant destroy

Vagrant 使用

1.vagrant centos7 创建 root用户并使用ssh连接

# 先使用ragrant 用户登录
config.ssh.username = "vagrant"

# 按照提示设置 root 密码
$ sudo passwd root

# 切换至root用户
$ su root  

2.vagrant centos7 安装 node.js

# 切换到 root, epel 是社区强烈打造的免费开源发行软件包版本库
$ yum install epel-release

# 新版的nodejs已集成了npm
$ yum install nodejs

Other Resources

Vagrant Documentation

VAGRANT 中文文档

征服诱人的Vagrant!

vagrant ubuntu 创建 root用户并使用ssh连接

Vagrant (二) - 日常操作

@pfan123 pfan123 changed the title 使用 VAGRANT VAGRANT Aug 21, 2019
@pfan123 pfan123 changed the title VAGRANT VAGRANT 使用 Aug 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant