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

Vagrant operation and maintenance to build a unified development environment

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Features: through the vagrant packaging environment, it can be used across platforms. It means an environment that can be configured by the ubuntu system under windows.

Platform used: windows+ 64-bit

Tools that need to be prepared:

Virtualbox: virtual machine https://www.virtualbox.org/wiki/Downloads

Vagrant: download address http://downloads.vagrantup.com/

Download the box you need to use

Download via http://www.vagrantbox.es/

* General operation command

Vagrant box add NAME URL # add a box

Vagrant box list # View locally added box

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

* the function of vagrantfile file:

Configure this virtual host network connection, port forwarding, synchronized folders, and a configuration file on how to combine with puppet,chef. 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.

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

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

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

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 manifest

Class 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.

Recommend a shell integrated installation environment, lnmp/lamp, etc.

Https://oneinstack.com/install/

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

Servers

Wechat

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

12
Report