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
Next revision Both sides next revision
технология_vagrant [2022/09/21 15:37]
val [Multi-Machine Vagrant Environments]
технология_vagrant [2024/03/11 19:00]
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 76: Line 83:
 $ vagrant global-status $ vagrant global-status
  
-### vagrant ​halt+$ vagrant ​global-status --prune
  
-### vagrant destroy+vagrant halt 
 + 
 +vagrant destroy
 </​code>​ </​code>​
  
Line 91: 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 105: 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 151: 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 163: Line 185:
 λ touch provision_once.sh λ touch provision_once.sh
 </​code><​code>​ </​code><​code>​
 +#!/bin/sh
 +
 +#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
 #service ssh restart #service ssh restart
  
 echo '​vagrant:​strongpassword'​ | chpasswd echo '​vagrant:​strongpassword'​ | chpasswd
-#echo '​root:​strongpassword'​ | chpasswd 
  
 (echo '"​\e[A":​ history-search-backward';​ echo '"​\e[B":​ history-search-forward'​) >> /​etc/​inputrc (echo '"​\e[A":​ history-search-backward';​ echo '"​\e[B":​ history-search-forward'​) >> /​etc/​inputrc
Line 178: 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 206: 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 218: Line 249:
 λ cat Vagrantfile λ cat Vagrantfile
 ... ...
-  config.vm.provision "​provision_docker",​ type: "​ansible_local",​ run: "always" do |ansible|+  config.vm.provision "​provision_docker",​ type: "​ansible_local",​ run: "once" do |ansible|
     ansible.playbook = "​provision_docker.yml"​     ansible.playbook = "​provision_docker.yml"​
   end   end
 ... ...
 </​code><​code>​ </​code><​code>​
-$ vagrant provision --provision-with provision_docker+time vagrant provision --provision-with provision_docker 
 +real    11m2,127s
 </​code>​ </​code>​
  
Line 254: 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 278: Line 313:
 λ vagrant up λ vagrant up
  
-λ vagrant ​up --parallel+λ vagrant ​status
  
 λ vagrant ssh node2 λ vagrant ssh node2
 +
 +λ vagrant halt node2
  
 λ vagrant destroy node1 λ vagrant destroy node1
  
 λ vagrant destroy -f λ vagrant destroy -f
-</​code>​ 
  
-===== Дополнительные возможности =====+λ cd ~/​conf/​vagrant/​nodes
  
-==== Использование синхронизируемых каталогов ====+λ nano Vagrantfile ​  #!!! set X
  
-<​code>​ +λ cat provision_once.sh   look passwords
-λ cat Vagrantfile +
-</​code><​code>​ +
-... +
- ​config.vm.synced_folder "​.",​ "/​vagrant",​ type: "​rsync",​ rsync__exclude:​ "​.git/"​ +
-... +
-</​code><​code>​ +
-λ npp SomeFile +
-...+
  
-λ vagrant ​rsync+λ 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.txt · Last modified: 2024/04/09 16:59 by val