User Tools

Site Tools


технология_vagrant

Технология Vagrant

Getting Start

λ C:\HashiCorp\Vagrant\bin\vagrant.exe box add debian/buster64
  или
λ C:\HashiCorp\Vagrant\bin\vagrant.exe box add debian/bullseye64

λ bash

$ alias vagrant=C:\HashiCorp\Vagrant\bin\vagrant.exe $*
$ vagrant box list

$ mkdir node3

$ cd node3

student@.... ~/node3
$ vagrant init debian/buster64
  или
$ vagrant init debian/bullseye64

$ vagrant up

$ vagrant ssh

$ vagrant global-status

$ vagrant halt

$ vagrant destroy

Provision

Внутри Vagrantfile

Демонстрирует преподаватель на примере node9 ??
λ npp Vagrantfile
...
Vagrant.configure("2") do |config|

  config.vm.network "private_network", ip: "192.168.X.3", virtualbox__intnet: true
  config.vm.hostname = "node3.corpX.un"
  
  config.vm.provision "provision_once", type: "shell", inline: <<-SHELL

    sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config
#    sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config
    systemctl restart sshd.service
    echo 'vagrant:123' | chpasswd

    apt-get update     
    apt-get install -y net-tools

  SHELL

  config.vm.provision "provision_onstart_1", 
    type: "shell",
    run: "always",
    inline: "route add default gw 192.168.13.254"

  config.vm.provision "provision_onstart_2", run: "always", type: "shell", inline: <<-SHELL

    eval `route -n | awk '{ if ($8 ==\"eth0\" && $2 != \"0.0.0.0\") print \"route del default gw \" $2; }'`

    cat <<EOF >/etc/resolv.conf
search corp13.un
nameserver 192.168.13.254
EOF

  SHELL

...
λ vagrant reload --provision

С использованием внешних скриптов

λ npp provision_once.sh
sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config  

apt-get update
apt-get install -y net-tools
λ npp provision_onstart.sh
route add default gw 192.168.X.254

eval `route -n | awk '{ if ($8 =="eth0" && $2 != "0.0.0.0") print "route del default gw " $2; }'`

cat <<EOF >/etc/resolv.conf
search corpX.un
nameserver 192.168.X.254
EOF
λ npp Vagrantfile
...
  config.vm.provision "provision_once", type: "shell", run: "once", path: "provision_once.sh"
  config.vm.provision "provision_onstart", type: "shell", run: "always", path: "provision_onstart.sh"
...
λ vagrant provision --provision-with provision_once,provision_onstart

С использованием ansible

λ cat Vagrantfile
...
  config.vm.provision "provision_docker", type: "ansible_local", run: "always" do |ansible|
    ansible.playbook = "provision_docker.yml"
  end
$ vagrant provision --provision-with provision_docker

Multi-Machine Vagrant Environments

addnodes → nodes ?

$ mkdir addnodes

$ cd addnodes

$ vagrant init debian/buster64
  или
$ vagrant init debian/bullseye64

Внутри Vagrantfile

λ npp Vagrantfile
...
Vagrant.configure("2") do |config|

  config.vm.define "node3" do |node3|
    node1.vm.network "private_network", ip: "192.168.X.3"
  end

  config.vm.define "node4" do |node4|
    node2.vm.network "private_network", ip: "192.168.X.4"
  end

  config.vm.define "node5" do |node5|
    node3.vm.network "private_network", ip: "192.168.X.5"
  end
...

С использованием структур данных ruby

boxes = [
    {
        :name => "node3",
        :ip => "192.168.X.3",
        :vbox_config => [
            { "--cpus" => "1" },
            { "--memory" => "1024" }
        ],
    },
    {
        :name => "node4",
        :ip => "192.168.X.4",
        :vbox_config => [
            { "--cpus" => "1" },
            { "--memory" => "1024" }
        ],
    },
    {
        :name => "node5",
        :ip => "192.168.X.5",
        :vbox_config => [
            { "--cpus" => "1" },
            { "--memory" => "1024" }
        ],
    }
]

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

  boxes.each do |opts|
    config.vm.define opts[:name] do |config|
      config.vm.network "private_network", ip: opts[:ip]
      config.vm.hostname = opts[:name]

      config.vm.provider "virtualbox" do | vb |
        file_to_disk = opts[:name] + '_2.vdi'
        unless File.exist?(file_to_disk)
          vb.customize ['createhd', '--filename', file_to_disk, '--size', 5 * 1024]
        end
        vb.customize ['storageattach', :id, '--storagectl', 'SATA Controller', '--port', 2, '--device', 0, '--type', 'hdd', '--medium', file_to_disk]
      end

      opts[:vbox_config].each do |hash|
        hash.each do |key, value|
          config.vm.provider :virtualbox do |vb|
            vb.customize ["modifyvm", :id, key, value]
          end
        end
      end
    end
  end
  ...
end
λ vagrant up

λ vagrant ssh node2

λ vagrant destroy node3

λ vagrant destroy -f

С использованием json

addnodes -> nodes ??
node1 .. 3 ??
$ cat addnodes.json
[
    {
        "name": "node3",
        "ip": "192.168.X.3",
        "vbox_config": [
            { "--cpus": "1" },
            { "--memory": "1024" }
        ]
    },
    {
        "name": "node4",
        "ip": "192.168.X.4",
        "vbox_config": [
            { "--cpus": "1" },
            { "--memory": "1024" }
        ]
    },
    {
        "name": "node5",
        "ip": "192.168.X.5",
        "vbox_config": [
            { "--cpus": "1" },
            { "--memory": "1024" }
        ]
    }
]
$ cat Vagrantfile
require 'json'
boxes = JSON.parse(File.read('./addnodes.json'))

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

  boxes.each do |opts|
    config.vm.define opts["name"] do |config|

      config.vm.network "private_network", ip: opts["ip"],
	    virtualbox__intnet: true
      config.vm.hostname = opts["name"]

#      config.vm.provider "virtualbox" do | vb |
#        file_to_disk = opts["name"] + '_2.vdi'
#        unless File.exist?(file_to_disk)
#          vb.customize ['createhd', '--filename', file_to_disk, '--size', 4 * 1024]
#        end
#        vb.customize ['storageattach', :id, '--storagectl', 'SATA Controller', '--port', 2, '--device', 0, '--type', 'hdd', '--medium', file_to_disk]
#      end

      opts["vbox_config"].each do |hash|
        hash.each do |key, value|
          config.vm.provider :virtualbox do |vb|
            vb.customize ["modifyvm", :id, key, value]
          end
        end
      end

    end
  end
#  config.vm.box = "debian/buster64"
  config.vm.box = "debian/bullseye64"

  config.vm.provision "shell", inline: <<-SHELL

    sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config
#    sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config
    systemctl restart sshd.service
    echo 'vagrant:123' | chpasswd

    apt-get update     
    apt-get install -y net-tools

    cat <<EOF >/etc/resolv.conf
search corpX.un
nameserver 192.168.X.254
EOF

  SHELL

  config.vm.provision "shell", run: "always",
    inline: "route add default gw 192.168.X.254"

  config.vm.provision "shell", run: "always",
    inline: "eval `route -n | awk '{ if ($8 ==\"eth0\" && $2 != \"0.0.0.0\") print \"route del default gw \" $2; }'`"

end

Дополнительные возможности

Использование синхронизируемых каталогов

λ cat Vagrantfile
...
#  config.vm.synced_folder ".", "/vagrant", type: "rsync", rsync__exclude: ".git/"
...
λ npp SomeFile
...

λ vagrant rsync

λ vagrant ssh

vagrant@node3:~$ ls /vagrant/
...
Vagrantfile
...
SomeFile
...
технология_vagrant.txt · Last modified: 2021/11/06 15:13 by val