Practice of building development environment by Vagrant

Keywords: vagrant

introduce

Development Environments Made Easy
- Official Website Title

vagrant is a command line hypervisor. It is used to simplify the construction of development environment.

vagrant is developed based on Chef using ruby language. Work with other virtual machine programs, such as vmare, virualbox, hyper-v, etc. And manage all functions of the virtual machine through CLI and VagrantFile (configuration file). vagrant uses its own image format for environment deployment, that is, box. It is similar to the image in iso or docker of virtual machine.

Choose the combination of vagrant+virualbox because they are open source and cross platform. Vagrant is simple and powerful to build a development environment.

install

Download and install from the official website. virualbox can be installed optionally
VirtualBox Extension Pack. Using the virtual machine requires the BIOS to turn on virtualization.

VirtualBox official website

https://www.virtualbox.org/

Vagrant official website

http://www.vagrantup.com/

BOX search

https://app.vagrantup.com/boxes/search

Several scenes

Here are several usage scenarios of vagrant. All steps have been verified under WIN11 system.

1. One line command to build a basic development environment

Start a virtual machine containing centos system under CMD command line. Where "centos/7" is the box name, and the default password of root user is vagrant. That's easy.

mkdir demo && cd demo && vagrant init centos/7 && vagrant up

2. Stand alone development environment

The goal is to build a small amount of customized stand-alone development environment. You need to use CLI commands and configuration files to do this.

Manually download BOX

The network speed is slow. You can download the box file yourself and add it to the local box library.

Download address:

https://app.vagrantup.com/centos/boxes/7/versions/2004.01/providers/virtualbox.box

Command to add box to local library

vagrant box add centos/7 CentOS-7-x86_64-Vagrant-2004_01.VirtualBox.box

**Initialization * * enter the command line, create a directory, and initialize with centos/7 box.

md demo
cd demo
vagrant init centos/7

After the basic configuration file is initialized, a file named Vagrantfile will be generated in the current directory. In Ruby language format.

Vagrant.configure("2") do |config|
  config.vm.box = "centos/7" #box name
  config.vm.hostname="base" #hostname in the virtual machine operating system
  config.vm.network "private_network", type: "dhcp" #Use private networks to automatically assign IP addresses
  #config.vm.network "private_network", ip: "172.28.128.3"    #Static IP can also be configured
  config.vm.provider "virtualbox" do |vb|
    vb.gui = false #Do not open GUI on startup
    vb.memory = "4096" #Memory limit 4096MB
  end
end

Common commands

vagrant up #Start the virtual machine
vagrant status #View virtual machine status
vagrant ssh #Linked virtual machine
vagrant halt #Shut down the virtual machine
vagrant destroy #Delete virtual machine
vagrant reload #Restart the virtual machine to make the modified configuration effective.

3. Cluster development environment

The goal is to build a cluster environment running in the LAN, including multiple physical machines and virtual machines.

The idea is to customize the public box after it is installed. Package it into a custom box and distribute it to different physical machines for deployment.

Using vagrant's public network mode, each virtual machine is assigned a fixed IP in the LAN, and any physical machine and virtual machine in the cluster environment can communicate directly.

Initialize a centos environment

md test
cd test
vagrant init centos/7

Custom environment

Allow user name password link SSH

Linking virtual machines using vagrant ssh

vagrant ssh

su #Switch the root user with the password vagrant
vagrant

vi /etc/ssh/sshd_config #Edit ssh profile

Modify configuration items, allow password login, and save.

PasswordAuthentication yes

service sshd restart #Restart ssh service

Operating system settings

#Modify time zone
timedatectl set-timezone Asia/Shanghai
#Turn off firewall
systemctl stop firewalld
systemctl disable firewalld
#Turn off Linux
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0
#Close swap 
swapoff -a #Temporarily Closed
sed -ri 's/.*swap.*/#&/' /etc/fstab #Permanent shutdown
free -g #Verify that swap must be 0

Set alicloud Centos image source

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum clean all
yum makecache
yum update -y

Install Docker

#rely on
yum install -y yum-utils device-mapper-persistent-data lvm2
#source
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
#edition
yum list docker-ce --showduplicates | sort -r
#install
yum install docker-ce-18.03.1.ce-1.el7.centos  -y
#start-up
systemctl start docker
systemctl enable docker
#Set alicloud docker image source

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://mt1tth70.mirror.aliyuncs.com"]
}
EOF

systemctl daemon-reload
systemctl restart docker

Custom BOX

Package the current environment. The default name is package.box. Join the local library and name it base.

Box default path of local warehouse C:\Users\Administrator.vagrant.d\box

vagrant package
vagrant box add **base **package.box

configuration file

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

  #Virtual machine 1 
  config.vm.define "node201" do |node1|
    node1.vm.box = "base" #Use a custom box
    node1.vm.hostname = "node201"
    node1.vm.network "public_network", ip: "192.168.31.201"
  end

  #Virtual machine 2
  config.vm.define "node202" do |node2|
    node2.vm.box = "base"
    node2.vm.hostname = "node202"
    node2.vm.network "public_network", ip: "192.168.31.202"
  end

  # config.vm.define "node203" do |node3|
  #   node3.vm.box = "base"
  #   node3.vm.hostname = "node203"
  #   node3.vm.network "public_network", ip: "192.168.31.203"
  # end

  config.vm.provider "virtualbox" do |vb|
    vb.gui = false
    vb.memory = "4096"
    vb.cpus = 4
  end

end

Deploy cluster environment

Copy the packaged custom box and configuration file to each physical machine to be deployed, and install the vagrant environment.

Add the box to the local library of the physical machine through vagrant box add.

Adjust the vagrant file and deploy it through vagrant init.

epilogue

Extended reading vagrant CLI, VagrantFile, plug-in, network configuration, etc. can be found on the official website.

Documentation | Vagrant by HashiCorp (vagrantup.com)

Posted by mikep on Sat, 30 Oct 2021 01:09:26 -0700