Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction to vagrant usage

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

1 > introduction:

Vagrant provides an easy to configure, repeatable, portable work environment, which is very useful to developers. It allows developers to create simple and reusable VirtualBox-based virtual machines (now also support VMware and AWS, etc.), these virtual machines can be quickly created and destroyed. Vagrant can also be combined with puppet,chef to realize the automation of virtual machine management. Vagrant's official website: http://www.vagrantup.com this article is from cclo's blog, please be sure to indicate the original source of the article in the form of hyperlink when reprinting: http://xuclv.blog.51cto.com/5503169/1239250

2 > install: (OS:ubuntu12.04 vagrant:1.2.2)

$sudo apt-get install virtualbox

$sudo dpkg-I vagrant_1.2.2_x86_64.deb

Version 1.0.x can also be installed this way (OS:ubuntu12.04)

Sudo apt-get install vagrant

Or

1:sudo apt-get install virtualbox

2:sudo apt-get install ruby1.9.1 rubygems

3:gem install vagrant

NOTE: install virtualbox on top of the physical machine. If you install virtualbox and vagrant in the virtual machine created with vm, then vagrant will not work. Here http://downloads.vagrantup.com/ downloads the corresponding vagrant version. Note that the configurations of version 1.0.x and version 1.1 + are quite different, which will be described later.

3 > A simple project

Version 1.1 +

$vagrant init precise32 http://files.vagrantup.com/precise32.box

$vagrant up

Version 1.0.x and 1.1+

$vagrant box add precise32 http://files.vagrantup.com/precise32.box

$vagrant init precise32

$vagrant up

After the above command runs, there will be a virtual machine VirtualBox running Ubuntu 12.04 LTS 32-bit system. Use the command $vagrant ssh to enter the system.

NOTE: "precise32" is the name of the virtual machine, which can be changed to the name you want.

Box is a zip package that contains configuration information for vagrant and virtual machine image files for VirtualBox.

"http://files.vagrantup.com/precise32.box" is the path where the image is located, and it can be a local path, so it is recommended to download the box and specify it as the local path. The speed will be faster.

There are many public base boxes available for download and use here in http://www.vagrantbox.es/, and I will show you how to create a base box later.

4 > commonly used vagrant commands:

$vagrant box add NAME URL # add a box

$vagrant box list # View the box that has been added locally

$vagrant box remove NAME virtualbox # delete the box that has been added locally, if version 1.0.x, execute $vagrant box remove NAME

$vagrant init NAME # initialization, which should essentially create a Vagrantfile file

$vagrant up # start the virtual machine

$vagrant halt # shut down the virtual machine

$vagrant destroy # destroy a virtual machine

$vagrant reload # restart the virtual machine

$vagrant package # the currently running VirtualBox virtual environment is packaged into a reusable box

$vagrant ssh # enter the virtual environment

5 > Vagrantfile

The official explanation goes like this: The primary function of the Vagrantfile is to describe the type of machine required for a project, and how to configure and provision these machines. The translation is too raw, simply to configure the virtual host network connection, port forwarding, synchronization folder, and how to combine with the puppet,chef of a configuration file. After executing $vagrant init, you will find this file in the working directory.

NOTE: configuration version notes:

Vagrant.configure ("2") do | config | #... end

Two currently supported versions: "1" and "2". "1": describes the configuration of Vagrant 1.0.x (see Vagrant::Config.run do | config | this is also the configuration of Vagrant 1.0.x); "2": describes the configuration of 1.1 + leading up to 2.0.x. The Vagrantfiles of vagrant 1.1+ maintains backward compatibility with the Vagrantfiles of vagrant 1.0.x, and significantly introduces new features and configuration options.

6 > configure the network (this article will provide 2 versions of common configurations, of which the configuration of version 1 has been verified in practice)

(1) Port forwarding: (assume that port 80 of the virtual machine provides web service. Here, web access will be realized by accessing port 8080 of the physical machine to port 80 of the virtual machine.)

Version "2":

Vagrant.configure ("2") do | config | config.vm.network: forwarded_port, guest: 80, host: 8080end

Version "1"

Vagrant::Config.run do | config | # Forward guest port 80 to host port 8080 config.vm.forward_port 80, 8080end

(2) bridged network (public network, LAN DHCP server automatically allocates IP)

Version "2"

Vagrant.configure ("2") do | config | config.vm.network: public_networkend

Version "1"

Vagrant::Config.run do | config | config.vm.network: bridgedend

$VBoxManage list bridgedifs | grep ^ Name # you can view the Nic of this machine through this command

Name: eth0

Specify the network card, and the configuration can be written as follows:

Vagrant::Config.run do | config | config.vm.network: bridged,: bridge = > "eth0" end

(3) VPC: allows multiple virtual machines to communicate with each other through the network through hosts, and vagrant allows users to assign a static IP, and then use VPC settings.

Version "2"

Vagrant.configure ("2") do | config | config.vm.network: private_network, ip: "192.168.50.4" end

Version "1"

Vagrant::Config.run do | config | config.vm.network: hostonly, "192.168.50.4" end

7 > synchronize folders

By default, vagrant will share your working directory (that is, the directory where Vagrantfile is located) to the / vagrant in the virtual machine, so it generally does not need to be configured, if you need to:

Version "2"

Vagrant.configure ("2") do | config | # other config here config.vm.synced_folder "src/", "/ srv/website" end

"src/": physical machine directory; "/ srv/website" virtual machine directory

8 > vagrant and shell (to self-run required shell commands or scripts when the virtual machine starts)

Version "2"

Embedded script:

Vagrant.configure ("2") do | config | config.vm.provision: shell,: inline = > "echo Hello, World" end

The call for complexity is as follows:

$script = $scriptend

External script:

Vagrant.configure ("2") do | config | config.vm.provision: shell,: path = > "script.sh" # the path of the script is relative to the project root, or you can use the absolute path end

The script can pass parameters:

Vagrant.configure ("2") do | config | config.vm.provision: shell do | s | s.inline = "echo $1" s.args = "'hello, worldview'" Endend

Version "1":

Internal script:

Vagrant::Config.run do | config | config.vm.provision: shell,: inline = > "echo abc > / tmp/test" end

External script:

Vagrant::Config.run do | config | config.vm.provision: shell,: path = > "test.sh" end

Script parameters:

Vagrant::Config.run do | config | config.vm.provision: shell do | shell | shell.inline = "echo $1 > / tmp/test" shell.args = "'this is test'" endend

9 > vagrant and puppet (if you don't know puppet, please see http://xuclv.blog.51cto.com/blog/5503169/1154261) here

(1) vagrant calls puppet for separate use

Vagrant.configure ("2") do | config | config.vm.provision: puppet do | puppet | puppet.manifests_path = "my_manifests" # the path is relative to the project root. If this item is not configured, the default is manifests puppet.manifest_file = "default.pp" # if this item is not configured, the default is default.pp puppet.module_path = "modules" # path relative to the root puppet.options = "- verbose-- debug" endend

Directory structure of the default configuration:

$tree

.

|-- Vagrantfile

|-- manifests

| |-- default.pp |

(2) vagrant uses puppet as a proxy to connect to Puppet master

Vagrant.configure ("2") do | config | config.vm.provision: puppet_server do | puppet | puppet.puppet_server = "puppet.example.com" # master domain name puppet.puppet_node = "node.example.com" # the name passed to the puppet server node. Default is "puppet" puppet.options = "--verbose-- debug" # option endend

NOTE:

Version 1 configuration is not much different and will not be described in detail. Difference: Vagrant.configure ("2") do | config | change to Vagrant::Config.run do | config |

After the above Vagrantfile is configured, you can restart the virtual machine with $vagrant reload to make the configuration effective.

The official gave an example (you can try it):

1. Enter the working directory

two。 Modify Vagrantfile

$vim Vagrantfile # enable or add the following lines:

Vagrant.configure ("2") do | config | config.vm.provision: puppet # there is no path to configure pp files, etc., all using the default endend

3. Create a home directory for puppet

$mkdir manifests

4. Configure the pp file

$vim manifests/default.pp

# Basic Puppet Apache manifestclass apache {exec {'apt-get update': command = >' / usr/bin/apt-get update'} package {"apache2": ensure = > present,} service {"apache2": ensure = > running, require = > Package ["apache2"],} file {'/ var/www': ensure = > link, target = > "/ vagrant", notify = > Service ['apache2'], force = > true}} include apache

5. Restart the virtual machine

$vagrant reload # after restarting, you can see that apache has been installed in the virtual machine

Postscript:

Generally speaking, vagrant is a simple and easy-to-use software, which is often used in combination with puppet or chef to realize the automatic deployment of the test environment, ensuring the rapid creation, convenient deployment, consistency and destruction of the test environment. In addition, chef is not often used here, so this article does not introduce it, those who are interested can study it by themselves.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report