User Tools

Site Tools


технология_vagrant

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
технология_vagrant [2022/09/22 11:50]
val [Provision с использованием ansible]
технология_vagrant [2024/03/11 19:00] (current)
val [Multi-Machine Vagrant Environments]
Line 1: Line 1:
 ====== Технология Vagrant ====== ====== Технология Vagrant ======
 +
 +  * [[https://​ru.wikipedia.org/​wiki/​Vagrant|HashiCorp Vagrant]]
  
   * [[https://​www.vagrantup.com/​|Vagrant - Development Environments Made Easy]]   * [[https://​www.vagrantup.com/​|Vagrant - Development Environments Made Easy]]
Line 7: Line 9:
   * [[https://​oracle-base.com/​articles/​vm/​create-a-vagrant-base-box-virtualbox|Create a Vagrant Base Box (VirtualBox)]]   * [[https://​oracle-base.com/​articles/​vm/​create-a-vagrant-base-box-virtualbox|Create a Vagrant Base Box (VirtualBox)]]
  
-  * [[https://​www.vultr.com/​docs/​stop-dhcp-from-changing-resolve-conf/​|Stop DHCP From Changing resolv.conf]] 
  
 ===== Установка ===== ===== Установка =====
 +
 +  * [[https://​hashicorp-releases.yandexcloud.net/​vagrant/​]]
  
   * [[https://​val.bmstu.ru/​unix/​HashiCorp/​vagrant_2.2.10_x86_64.msi]] ​   * [[https://​val.bmstu.ru/​unix/​HashiCorp/​vagrant_2.2.10_x86_64.msi]] ​
 +  * [[http://​gate.isp.un/​unix/​HashiCorp/​vagrant_2.2.10_x86_64.msi]] ​
   * Можно не перезагружать систему,​ но нужно перезапустить Cmder   * Можно не перезагружать систему,​ но нужно перезапустить Cmder
  
Line 36: Line 40:
  
   * [[https://​val.bmstu.ru/​unix/​HashiCorp/​ubuntu_20.04.box]]   * [[https://​val.bmstu.ru/​unix/​HashiCorp/​ubuntu_20.04.box]]
 +  * [[http://​gate.isp.un/​unix/​HashiCorp/​ubuntu_20.04.box]]
  
   * [[Переменные окружения]]   * [[Переменные окружения]]
Line 47: Line 52:
 $ vagrant box list $ vagrant box list
  
-$ vagrant package --base ubuntu_20.04_03 --output /​c/​distrs/​ubuntu_20.04.box+### vagrant package --base ubuntu_20.04_03 --output /​c/​distrs/​ubuntu_20.04.box
  
 $ vagrant box add /​c/​distrs/​ubuntu_20.04.box --name specialist/​ubuntu20 $ vagrant box add /​c/​distrs/​ubuntu_20.04.box --name specialist/​ubuntu20
Line 71: Line 76:
  
 $ vagrant up $ vagrant up
 +
 +$ ssh -p 2222 student@localhost
  
 $ vagrant ssh $ vagrant ssh
Line 78: Line 85:
 $ vagrant global-status --prune $ vagrant global-status --prune
  
-### vagrant halt+$ vagrant halt
  
-### vagrant destroy+$ vagrant destroy
 </​code>​ </​code>​
  
Line 93: Line 100:
   * [[https://​stackoverflow.com/​questions/​30820949/​print-message-after-booting-vagrant-machine-with-vagrant-up|Print message after booting vagrant machine with "​vagrant up"]]   * [[https://​stackoverflow.com/​questions/​30820949/​print-message-after-booting-vagrant-machine-with-vagrant-up|Print message after booting vagrant machine with "​vagrant up"]]
   * [[https://​stackoverflow.com/​questions/​19648088/​pass-environment-variables-to-vagrant-shell-provisioner|Pass environment variables to vagrant shell provisioner]]   * [[https://​stackoverflow.com/​questions/​19648088/​pass-environment-variables-to-vagrant-shell-provisioner|Pass environment variables to vagrant shell provisioner]]
 +  * !!! Зрелищнее - по очереди,​ config и provision ​
 <​code>​ <​code>​
-λ npp Vagrantfile ​&+λ cat Vagrantfile
 </​code><​code>​ </​code><​code>​
 ... ...
Line 107: Line 114:
  
   config.vm.hostname = "​node1.corp"​ + X + "​.un"​   config.vm.hostname = "​node1.corp"​ + X + "​.un"​
-  config.vm.network "​private_network",​ ip: "​192.168."​ + X + "​.201"​ +  config.vm.network "​private_network", ​ 
 +    adapter: 2,  
 +    name: "​VirtualBox Host-Only Ethernet Adapter",​ 
 +    ​ip: "​192.168."​ + X + "​.201"​
   config.vm.provider "​virtualbox"​ do |vb|   config.vm.provider "​virtualbox"​ do |vb|
     vb.memory = "​2048"​     vb.memory = "​2048"​
Line 153: Line 162:
  
 end end
-</​code>​ +</​code><​code>​ 
-<​code>​+λ vagrant validate 
 + 
 +λ vagrant up 
 +... 
 +==> default: Machine already provisioned ... 
 +... 
 +λ vagrant ssh 
 +$ date 
 λ vagrant reload --provision λ vagrant reload --provision
 +
 +λ vagrant ssh
 +$ date
 </​code>​ </​code>​
  
Line 165: Line 185:
 λ touch provision_once.sh λ touch provision_once.sh
 </​code><​code>​ </​code><​code>​
 +#!/bin/sh
 +
 #echo '​root:​strongpassword'​ | chpasswd #echo '​root:​strongpassword'​ | chpasswd
 #sed -i '​s/​PasswordAuthentication no/​PasswordAuthentication yes/g' /​etc/​ssh/​sshd_config #sed -i '​s/​PasswordAuthentication no/​PasswordAuthentication yes/g' /​etc/​ssh/​sshd_config
Line 180: Line 202:
 λ touch provision_onstart.sh λ touch provision_onstart.sh
 </​code><​code>​ </​code><​code>​
 +#!/bin/bash
 +
 X=$1 X=$1
  
-echo $X+echo "$X"
  
-route add default gw 192.168.$X.1+route add default gw 192.168."$X".1
  
-eval `route -n | awk '{ if ($8 =="​eth0"​ && $2 != "​0.0.0.0"​) print "route del default gw " $2; }'`+eval "$(route -n | awk '{ if ($8 =="​eth0"​ && $2 != "​0.0.0.0"​) print "route del default gw " $2; }')"
  
 chattr -i /​etc/​resolv.conf chattr -i /​etc/​resolv.conf
Line 208: Line 232:
 ... ...
 </​code><​code>​ </​code><​code>​
 +vagrant@node1:​~$ ls /vagrant/
 +
 +vagrant@node1:​~$ shellcheck /​vagrant/​provision_once.sh
 +vagrant@node1:​~$ shellcheck /​vagrant/​provision_onstart.sh
 +
 λ vagrant provision --provision-with provision_once,​provision_onstart λ vagrant provision --provision-with provision_once,​provision_onstart
  
-vagrant@node1:~$ ls /vagrant/+server# ssh vagrant@node1
 </​code>​ </​code>​
  
Line 257: Line 286:
   nodes.each do |opts| ​   nodes.each do |opts| ​
     config.vm.define opts["​name"​] do |config|     config.vm.define opts["​name"​] do |config|
-      config.vm.network "​private_network",​ ip: "​192.168."​ + X + "​."​ + opts["​ip"​]+      config.vm.network "​private_network", ​ 
 +        adapter: 2,  
 +        name: "​VirtualBox Host-Only Ethernet Adapter",​ 
 +        ​ip: "​192.168."​ + X + "​."​ + opts["​ip"​]
       config.vm.hostname = opts["​name"​]       config.vm.hostname = opts["​name"​]
    ​    ​
Line 277: Line 309:
 ... ...
 </​code><​code>​ </​code><​code>​
-λ ### cd ~/​conf/​vagrant/​nodes ​ #!!! set X in Vagrantfile 
- 
 λ vagrant up node1 λ vagrant up node1
  
 λ vagrant up λ vagrant up
  
-λ vagrant ​up --parallel+λ vagrant ​status
  
 λ vagrant ssh node2 λ vagrant ssh node2
  
-λ ### vagrant ​destroy node1+λ vagrant ​halt node2
  
-λ ### vagrant destroy ​-f +λ vagrant destroy ​node1
-</​code>​+
  
-===== Дополнительные возможности =====+λ vagrant destroy -f
  
-==== Использование синхронизируемых каталогов ====+λ cd ~/​conf/​vagrant/​nodes
  
-<​code>​ +λ nano Vagrantfile ​  ​#!!! set X
-λ cat Vagrantfile +
-</​code><​code>​ +
-... +
- ​config.vm.synced_folder "​.",​ "/​vagrant",​ type: "​rsync",​ rsync__exclude:​ "​.git/"​ +
-... +
-</​code><​code>​ +
-λ npp SomeFile +
-...+
  
-λ vagrant ​rsync+λ cat provision_once.sh ​  # look passwords 
 + 
 +λ time vagrant ​up --parallel 
 +</​code>​ 
 + 
 +===== Дополнительные возможности =====
  
-λ vagrant ssh 
  
-vagrant@node3:​~$ ls /vagrant/ 
-</​code><​code>​ 
-... 
-Vagrantfile 
-... 
-SomeFile 
-... 
-</​code>​ 
  
 ==== Еще варианты Multi-Machine Vagrant Environments ==== ==== Еще варианты Multi-Machine Vagrant Environments ====
технология_vagrant.1663836604.txt.gz · Last modified: 2022/09/22 11:50 by val