利用vagrant来构建环境
Vagrant是近年来比较火的一款工具,通过它可以实现自动化创建运行环境,实 现 基 础 设 施 即 代 码 。 vargrant有以下好片。
- 方便共享环境 通过共享构建环境所需要的代码,任何人都可以重建相同的环境
- 创建过程透明 通过查看代码,可以很方便查看本地开发环境是如何构建、由什么构建等信息
- 维护方便 只要修改代码就可以实现对环境的维护。代码就是代表构建环境的具体操作步骤,所以操作步骤和环境信息之间不会产生不一致总是。
vagrant是由Hashicorp公司提供的一个虚拟机构建工具。通过它可以自动化生成你想要的虚拟机环境。
- 创建操作系统
- 对系统进行配置(主机名、网络地址)
- 对系统安装完成后的环境配置(构建中间件或部署应用程序)
Vagrant基本用户
在安装vagrant默认支持VirtualBox、Hyper-V和Docker的支持,除了这些默认的类型外,我们还可以通过安装相应的服务来提供其他平台的支持,接下来的案例,我们采用virtualbox来进行相应的讲解。 我们可以访问virtualbox官网点击Downloads进入下载界面,选择相应的平台进行下载安装。
Vagrant可以通过访问下载界面,来选择对应的平台进行下载就可以。
新建Vagrant 我们可以通过下面的命令来生成一个Vagrant的工程目录
mkdir demo
cd demo
vagran init
执行完成之后系统会返回下面的输出
> A 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.
同时会在当前的目录下面创建一个名为“Vagrantfile"文件 现在我们打开这个文件,并且更新成下面的内容
# -*- 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
从Vagrant.configure("2") do |config|到end,是对虚拟机进行相应的设置。其中的数字2表示当前vagrant的版本。 config.vm.box 代表当前虚拟机所用的镜像,这些镜像由Hashicorp公司提供。 如果想要其他的虚拟我们可以通过Hashicorp网站来选择自己想要镜像。
config.vm.hostname 代表当前的主机名称 config.vm.network :private_network, ip: "192.168.33.10" 指定当前的主机网络号改完配置文件之后,我们可以通过下面的命令来运行 > vagrant up
该命令执行后的输出结果如下所示。
> wcs@iMac ~/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
我们打开virtualbox,会发现系统已经自动创建了相应的虚拟机。
我们可以通过运行下面的命令来进入虚拟机 >wcs@iMac ~/git/devops/vargrant-demo1 vagrant ssh 127 ↵ 5479 22:13:14 [vagrant@demo ~]$
查看虚拟机的主机名
> [vagrant@demo ~]$ uname -n demo
通过下面的命令查看虚拟的ip > [vagrant@demo ~]$ 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 ~]$
停止相应的虚拟机
通过运行vagrant halt,可以停止虚拟机
> wcs@iMac ~/git/devops/vargrant-demo1 vagrant halt
==> default: Attempting graceful shutdown of VM...
删除虚拟机
通过运行vagrant destroy来删除新创建的虚拟机
> wcs@iMac ~/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...
上面讲解了vagrant基本的操作,但是也见窥见其便利之处,可以开发一次,多次使用,一条命令就可以一键创建相应的环境。
vagrant 环境2
vagrant除了能够配置环境之外,可以通过配置文件来执行相应 的shell脚本,来实现应用环境的配置。
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
13行的config.vm.provision。能够自动执行15-20相应的shell脚本 ,该脚本主要完成以下功能。
- 安装 EPEL(企业版 Linux 附加软件包)和 nginx
- 替换 index.html 文件内容
- 启动 nginx
在vagrant里面由两种方法来执行provision
- 重建
- vagrant destroy删除旧的虚拟机
- 修改Vagrantfile文件
- 运行vagrant up来构建新的虚拟机
- 更新旧的虚拟机
- 执行 vagrant provision,直接更新原有的虚拟机
执行完成之后,我们可以通过运行curl,来印证服务是否正常运行。
通过上面的命令,我们发现vagrant确实在创建虚拟机的同时帮助我们安装了nginx,以及修改了文件。</script></broadcast,multicast,up,lower_up>
来源:oschina
链接:https://my.oschina.net/u/215677/blog/3211527