Using vagrant to build environment
Vagrant is a popular tool in recent years, through which we can automatically create a running environment and realize infrastructure as code. vargrant has the following great movies.
- Convenient sharing environment By sharing the code needed to build the environment, anyone can rebuild the same environment
- Transparent creation process By looking at the code, you can easily see how and by which the local development environment is built
- Convenient maintenance As long as you modify the code, you can maintain the environment. The code is the specific operation steps of the construction environment, so there is no inconsistency between the operation steps and the environment information.
vagrant is a virtual organization building tool provided by Hashicorp. It can automatically generate the virtual machine environment you want.
- Create operating system
- Configure the system (host name, network address)
- Environment configuration after system installation (build middleware or deploy application)
Basic users of Vagrant
In the installation of vagrant, VirtualBox, Hyper-V and Docker are supported by default. In addition to these default types, we can also provide support for other platforms by installing corresponding services. In the next case, we will use VirtualBox for corresponding explanation. We can visit virtualbox official website Click Downloads to enter the download interface, and select the corresponding platform to download and install.
Vagrant can access Download Interface , to select the corresponding platform to download.
New Vagrant We can generate a project directory of Vagrant by the following command
mkdir demo cd demo vagran init
After execution, the system will return the following output
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.
A file named "Vagrantfile" will also be created under the current directory Now let's open this file and update it to the following
# -*- mode: ruby -*- # vi: set ft=ruby : # All Vagrant configuration is done below. The "2" in Vagrant.configure # configures the configuration version (we support older styles for # backwards compatibility). Please don't change it unless you know what # you're doing. Vagrant.configure("2") do |config| config.vm.box = "centos/7" config.vm.hostname = "demo" config.vm.network :private_network, ip: "192.168.100.10" config.vm.synced_folder ".", "/home/vargrant/sync" ,disabled: true end
From Vagrant.configure("2") do |config | to end, the virtual machine is set accordingly. The number 2 indicates the current version of vagrant. config.vm.box represents the images used by the current virtual machine, which are provided by Hashicorp. If we want other virtual reality, we can use Hashicorp website To choose what you want to mirror. config.vm.hostname represents the current hostname Config.vm.network: private network, IP: "192.168.33.10" specifies the current host network number
After changing the configuration file, we can run it with the following command > vagrant up
The output of this command is as follows.
>wcs[@iMac](https://my.oschina.net/u/3368814) ~/git/devops/vargrant-demo1 proxychains4 vagrant uplibproxychains4.dylib Bringing machine 'default' up with 'virtualbox' provider... ==> default: Box 'centos/7' could not be found. Attempting to find and install... default: Box Provider: virtualbox default: Box Version: >= 0 ==> default: Loading metadata for box 'centos/7' default: URL: https://vagrantcloud.com/centos/7 ==> default: Adding box 'centos/7' (v1905.1) for provider: virtualbox default: Downloading: https://vagrantcloud.com/centos/boxes/7/versions/1905.1/providers/virtualbox.box ==> default: Box download is resuming from prior download progress default: Download redirected to host: cloud.centos.org ==> default: Successfully added box 'centos/7' (v1905.1) for 'virtualbox'! ==> default: Importing base box 'centos/7'... ==> default: Matching MAC address for NAT networking... ==> default: Checking if box 'centos/7' version '1905.1' is up to date... ==> default: Setting the name of the VM: vargrant-demo1_default_1585058574248_24020 ==> default: Fixed port collision for 22 => 2222. Now on port 2250. ==> default: Clearing any previously set network interfaces... ==> default: Preparing network interfaces based on configuration... default: Adapter 1: nat default: Adapter 2: hostonly ==> default: Forwarding ports... default: 22 (guest) => 2250 (host) (adapter 1) ==> default: Booting VM... ==> default: Waiting for machine to boot. This may take a few minutes... default: SSH address: 127.0.0.1:2250 default: SSH username: vagrant default: SSH auth method: private key default: default: Vagrant insecure key detected. Vagrant will automatically replace default: this with a newly generated keypair for better security. 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... [proxychains] Strict chain ... 127.0.0.1:7890 ... 127.0.0.1:2250 ... OK ==> default: Machine booted and ready! ==> default: Checking for guest additions in VM... default: No guest additions were detected on the base box for this VM! Guest default: additions are required for forwarded ports, shared folders, host only default: networking, and more. If SSH fails on this machine, please install default: the guest additions and repackage the box to continue. default: default: This is not an error message; everything may continue to work properly, default: in which case you may ignore this message. ==> default: Setting hostname... ==> default: Configuring and enabling network interfaces... ==> default: Rsyncing folder: /Users/wcs/git/devops/vargrant-demo1/ => /vagrant //When we open virtualbox, we will find that the system has automatically created the corresponding virtual machine. !(https://i.loli.net/2020/03/25/wHNFzRr7hdYEoMg.jpg) //We can enter the virtual machine by running the following command >wcs[@iMac](https://my.oschina.net/u/3368814) ~/git/devops/vargrant-demo1 vagrant ssh 127 ↵ 5479 22:13:14 [vagrant[@demo](https://my.oschina.net/loomoo) ~]$
View the host name of the virtual machine
> [vagrant@demo ~]$ uname -n demo
View the virtual ip through the following command
> [vagrant[@demo](https://my.oschina.net/loomoo) ~]$ ip addr show dev eth1 3: eth1: <broadcast,multicast,up,lower_up> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 08:00:27:be:15:92 brd ff:ff:ff:ff:ff:ff inet **192.168.100.10/24 brd 192.168.100.255** scope global noprefixroute eth1 valid_lft forever preferred_lft forever inet6 fe80::a00:27ff:febe:1592/64 scope link valid_lft forever preferred_lft forever [vagrant[@demo](https://my.oschina.net/loomoo) ~]$
Stop the corresponding virtual machine By running vagrant halt, you can stop the virtual machine
wcs[@iMac](https://my.oschina.net/u/3368814) ~/git/devops/vargrant-demo1 vagrant halt ==> default: Attempting graceful shutdown of VM... !(https://i.loli.net/2020/03/25/xnevz8CIljwqcDa.jpg)
Delete virtual machine
Delete the newly created virtual machine by running vagrant destroy
wcs[@iMac](https://my.oschina.net/u/3368814) ~/git/devops/vargrant-demo1 vagrant destroy > default: Are you sure you want to destroy the 'default' VM? [y/N] y ==> default: Destroying VM and associated drives...
The above explains the basic operation of vagrant, but also sees its convenience. It can be developed once and used many times, and a command can create a corresponding environment with one click.
vagrant environment 2
In addition to being able to configure the environment, vagrant can execute the corresponding shell script through the configuration file to realize the configuration of the application environment.
1# -*- mode: ruby -*- 2# vi: set ft=ruby : 3 4# All Vagrant configuration is done below. The "2" in Vagrant.configure 6# configures the configuration version (we support older styles for 7# backwards compatibility). Please don't change it unless you know what 8# you're doing. 9Vagrant.configure("2") do |config| 9 config.vm.box = "centos/7" 10 config.vm.hostname = "demo" 11 config.vm.network :private_network, ip:"192.168.100.10" 12 config.vm.synced_folder ".", "/home/vagrant/sync", disabled:true 13 config.vm.provision "shell", inline: $script 14 end 15 $script = <<script 16 yum -y install epel-release 17 nginx 18 echo "hello, vagrant"> /usr/share/nginx/html/index.html 19 systemctl start nginx 20 SCRIPT
config.vm.provision on line 13. Can automatically execute 15-20 corresponding shell script, which mainly completes the following functions.
- Install EPEL (Enterprise Linux add-on package) and nginx
- Replace the contents of index.html file
- Start nginx
There are two ways to execute the provision in vagrant
- vagrant destroy delete old virtual machine
- Modify the Vagrantfile file
- Run vagrant up to build a new virtual machine
- Update old virtual machine
- Execute the vagrant provision to directly update the original virtual machine
After execution, we can verify whether the service is running normally by running curl. Through the above command, we found that vagrant did help us install nginx and modify the files while creating the virtual machine.