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/21 15:37]
val [Multi-Machine Vagrant Environments]
технология_vagrant [2025/02/11 13:00] (current)
val [Provision с использованием внешних скриптов]
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://​val.bmstu.ru/​unix/​HashiCorp/​vagrant_2.2.10_x86_64.msi]] ​+  ​* [[https://​hashicorp-releases.yandexcloud.net/​vagrant/​]] 
 + 
 +  ​* [[https://​val.bmstu.ru/​unix/​HashiCorp/​vagrant_2.4.3_windows_amd64.msi]] ​
   * Можно не перезагружать систему,​ но нужно перезапустить Cmder   * Можно не перезагружать систему,​ но нужно перезапустить Cmder
  
Line 36: Line 39:
  
   * [[https://​val.bmstu.ru/​unix/​HashiCorp/​ubuntu_20.04.box]]   * [[https://​val.bmstu.ru/​unix/​HashiCorp/​ubuntu_20.04.box]]
 +  * [[https://​val.bmstu.ru/​unix/​HashiCorp/​debian12-generic.box]]
  
   * [[Переменные окружения]]   * [[Переменные окружения]]
Line 43: Line 47:
 $ vagrant box list $ vagrant box list
  
-$ vagrant box add debian/bullseye64+### vagrant box add /c/​distrs/​ubuntu_20.04.box --name specialist/​ubuntu20
  
-$ vagrant box list +### vagrant box add generic/debian12
- +
-$ 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/debian12-generic.box --name ​generic/debian12
  
 $ vagrant box list $ vagrant box list
- 
-$ ### vagrant box remove specialist/​ubuntu20 
 </​code>​ </​code>​
  
Line 63: Line 63:
 $ cd ~/nodes $ cd ~/nodes
  
-$ vagrant init specialist/​ubuntu20+###vagrant init specialist/​ubuntu20 
 +$ vagrant init generic/​debian12
  
 $ less Vagrantfile $ less Vagrantfile
 ... ...
-    config.vm.box = "specialist/​ubuntu20"+    config.vm.box = "........"
 ... ...
  
 $ vagrant up $ vagrant up
 +
 +$ ssh -p 2NNN vagrant@localhost
  
 $ vagrant ssh $ vagrant ssh
Line 76: Line 79:
 $ vagrant global-status $ vagrant global-status
  
-$ ### vagrant halt+$ ###vagrant global-status --prune 
 + 
 +vagrant halt
  
-### vagrant destroy+$ vagrant destroy
 </​code>​ </​code>​
  
Line 91: Line 96:
   * [[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 110:
  
   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 115: Line 122:
 #      vb.customize ['​createhd',​ '​--filename',​ file_to_disk,​ '​--size',​ 4 * 1024] #      vb.customize ['​createhd',​ '​--filename',​ file_to_disk,​ '​--size',​ 4 * 1024]
 #    end #    end
-#    vb.customize ['​storageattach',​ :id, '​--storagectl',​ '​SATA',​ '​--port',​ 2, '​--device',​ 0, '​--type',​ '​hdd',​ '​--medium',​ file_to_disk]+#    vb.customize ['​storageattach',​ :id, '​--storagectl',​ '​SATA ​или SATA Controller', '​--port',​ 2, '​--device',​ 0, '​--type',​ '​hdd',​ '​--medium',​ file_to_disk]
  
   end   end
Line 151: Line 158:
  
 end end
-</​code>​ +</​code><​code>​ 
-<​code>​ +λ vagrant validate 
-λ vagrant reload --provision+ 
 +λ vagrant up 
 +... 
 +==> default: Machine already provisioned ... 
 +... 
 +λ vagrant ssh 
 +$ date 
 + 
 +λ vagrant up --provision 
 +λ ###vagrant provision --provision-with provision_once 
 +λ ###vagrant reload --provision 
 + 
 +λ vagrant ssh 
 + 
 +$ date 
 +$ ip a
 </​code>​ </​code>​
  
Line 159: Line 181:
  
   * [[https://​stackoverflow.com/​questions/​15461898/​passing-variable-to-a-shell-script-provisioner-in-vagrant|Passing variable to a shell script provisioner in vagrant]]   * [[https://​stackoverflow.com/​questions/​15461898/​passing-variable-to-a-shell-script-provisioner-in-vagrant|Passing variable to a shell script provisioner in vagrant]]
 +
 +<​code>​
 +vagrant.exe ssh node2 -c 'sudo apt install open-iscsi -y'
 +
 +vagrant.exe ssh node3 -c 'sudo apt install open-iscsi -y'
 +</​code>​
  
 <​code>​ <​code>​
 λ 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 173: Line 203:
 timedatectl set-timezone Europe/​Moscow timedatectl set-timezone Europe/​Moscow
  
-apt update +apt-get update 
-#apt install -y docker.io+ 
 +apt-get purge resolvconf 
 +rm /​etc/​resolv.conf 
 +touch /​etc/​resolv.conf 
 + 
 +#apt-get install -y docker.io
 </​code><​code>​ </​code><​code>​
 λ 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 194: Line 231:
 λ cat Vagrantfile λ cat Vagrantfile
 </​code><​code>​ </​code><​code>​
 +...
 +  config.vm.synced_folder "​.",​ "/​vagrant",​ disabled: false
 ... ...
 +### My provision ###
 +### /My provision ###
 +...
 +</​code><​code>​
 +vagrant@node1:​~$ ls /vagrant/
 +
 +vagrant@node1:​~$ tr -d '​\r'​ < /​vagrant/​provision_once.sh ​ | shellcheck -
 +vagrant@node1:​~$ tr -d '​\r'​ < /​vagrant/​provision_onstart.sh ​ | shellcheck -
 +</​code><​code>​
 +λ cat Vagrantfile
 +</​code><​code>​
 ### My provision ### ### My provision ###
  
Line 208: Line 258:
 λ 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 268:
 λ 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 305:
   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 332:
 λ 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 and mem 1024
-λ 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.1663763879.txt.gz · Last modified: 2022/09/21 15:37 by val