Zum Inhalt

Vagrant

generieren

Umfassende HashiCorp Vagrant-Befehle und Workflows für Entwicklungsumgebungsmanagement und virtuelle maschinelle Bereitstellung.

Installation und Inbetriebnahme

| | Command | Description | | | --- | --- | | | vagrant version | Show Vagrant version | | | | vagrant -h | Show help information | | | | vagrant init | Initialize new Vagrantfile | | | | vagrant init ubuntu/jammy64 | Initialize with specific box | |

Box Management

Box Operationen

| | Command | Description | | | --- | --- | | | vagrant box list | List installed boxes | | | | vagrant box add ubuntu/jammy64 | Add box | | | | vagrant box add --name mybox /path/to/box.box | Add local box | | | | vagrant box remove ubuntu/jammy64 | Remove box | | | | vagrant box update | Update all boxes | | | | vagrant box update --box ubuntu/jammy64 | Update specific box | |

Box Information

| | Command | Description | | | --- | --- | | | vagrant box outdated | Check for outdated boxes | | | | vagrant box prune | Remove old box versions | | | | vagrant box repackage ubuntu/jammy64 | Repackage box | |

VM Lebenszyklusmanagement

Grundgeschäfte

| | Command | Description | | | --- | --- | | | vagrant up | Start and provision VM | | | | vagrant halt | Shutdown VM | | | | vagrant reload | Restart VM | | | | vagrant destroy | Destroy VM | | | | vagrant suspend | Suspend VM | | | | vagrant resume | Resume suspended VM | |

VM Status und Informationen

| | Command | Description | | | --- | --- | | | vagrant status | Show VM status | | | | vagrant global-status | Show all VMs status | | | | vagrant global-status --prune | Clean up invalid entries | |

SSH und Zugang

SSH Operationen

| | Command | Description | | | --- | --- | | | vagrant ssh | SSH into VM | | | | vagrant ssh-config | Show SSH configuration | | | | vagrant ssh -- -L 8080:localhost:80 | SSH with port forwarding | |

Bereitstellung

Vorläufige Befehle

| | Command | Description | | | --- | --- | | | vagrant provision | Run provisioners | | | | vagrant provision --provision-with shell | Run specific provisioner | | | | vagrant up --provision | Start and provision | | | | vagrant reload --provision | Restart and provision | |

Multi-Machine Umgebungen

Multi-Machine Befehle

| | Command | Description | | | --- | --- | | | vagrant up web | Start specific machine | | | | vagrant ssh web | SSH to specific machine | | | | vagrant halt db | Halt specific machine | | | | vagrant destroy --force | Destroy all without confirmation | |

Fingern

Snapshot Management

| | Command | Description | | | --- | --- | | | vagrant snapshot save snapshot_name | Save snapshot | | | | vagrant snapshot list | List snapshots | | | | vagrant snapshot restore snapshot_name | Restore snapshot | | | | vagrant snapshot delete snapshot_name | Delete snapshot | |

Plugin Management

Plugins

| | Command | Description | | | --- | --- | | | vagrant plugin list | List installed plugins | | | | vagrant plugin install vagrant-vbguest | Install plugin | | | | vagrant plugin uninstall vagrant-vbguest | Uninstall plugin | | | | vagrant plugin update | Update all plugins | |

Vagrantfile Beispiele

Basic Vagrantfile

```ruby | Vagrant.configure("2") do | config | | config.vm.box = "ubuntu/jammy64" config.vm.hostname = "myserver"

# Network configuration config.vm.network "private_network", ip: "192.168.56.10" config.vm.network "forwarded_port", guest: 80, host: 8080

# Provider configuration | config.vm.provider "virtualbox" do | vb | | vb.memory = "2048" vb.cpus = 2 vb.name = "MyVM" end

# Provisioning config.vm.provision "shell", inline: <<-SHELL apt-get update apt-get install -y nginx systemctl enable nginx systemctl start nginx SHELL end ```_

Multi-Machine Setup

```ruby | Vagrant.configure("2") do | config | | # Web server | config.vm.define "web" do | web | | web.vm.box = "ubuntu/jammy64" web.vm.hostname = "web" web.vm.network "private_network", ip: "192.168.56.10"

| web.vm.provider "virtualbox" do | vb | | vb.memory = "1024" vb.cpus = 1 end

web.vm.provision "shell", inline: <<-SHELL
  apt-get update
  apt-get install -y nginx
SHELL

end

# Database server | config.vm.define "db" do | db | | db.vm.box = "ubuntu/jammy64" db.vm.hostname = "database" db.vm.network "private_network", ip: "192.168.56.11"

| db.vm.provider "virtualbox" do | vb | | vb.memory = "2048" vb.cpus = 2 end

db.vm.provision "shell", inline: <<-SHELL
  apt-get update
  apt-get install -y mysql-server
SHELL

end end ```_

Erweiterte Konfiguration

```ruby | Vagrant.configure("2") do | config | | config.vm.box = "ubuntu/jammy64" config.vm.box_version = "20230607.0.0"

# Synced folders config.vm.synced_folder ".", "/vagrant", disabled: true config.vm.synced_folder "./app", "/var/www/html", owner: "www-data", group: "www-data"

# Network configuration config.vm.network "private_network", ip: "192.168.56.10", netmask: "255.255.255.0"

config.vm.network "public_network", bridge: "en0: Wi-Fi (AirPort)"

# Port forwarding config.vm.network "forwarded_port", guest: 80, host: 8080 config.vm.network "forwarded_port", guest: 443, host: 8443

# Provider-specific configuration | config.vm.provider "virtualbox" do | vb | | vb.gui = false vb.memory = "4096" vb.cpus = 4 vb.name = "Development Server"

# VirtualBox specific settings
vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
vb.customize ["modifyvm", :id, "--natdnsproxy1", "on"]

end

# Multiple provisioners config.vm.provision "shell", inline: <<-SHELL apt-get update apt-get upgrade -y SHELL

config.vm.provision "file", source: "./configs/nginx.conf", destination: "/tmp/nginx.conf"

config.vm.provision "shell", path: "scripts/setup.sh"

| config.vm.provision "ansible" do | ansible | | ansible.playbook = "playbook.yml" ansible.inventory_path = "inventory" end end ```_

Vorläufige Methoden

Shell Provisioning

```ruby

Inline shell commands

config.vm.provision "shell", inline: <<-SHELL apt-get update apt-get install -y docker.io usermod -aG docker vagrant SHELL

External script

config.vm.provision "shell", path: "scripts/bootstrap.sh"

Privileged and non-privileged

config.vm.provision "shell", privileged: false, inline: <<-SHELL echo "Running as vagrant user" SHELL

Environment variables

config.vm.provision "shell", env: \\{"DEBIAN_FRONTEND" => "noninteractive"\\}, inline: <<-SHELL apt-get install -y mysql-server SHELL ```_

Dateivorgabe

```ruby config.vm.provision "file", source: "~/.ssh/id_rsa.pub", destination: "~/.ssh/authorized_keys"

config.vm.provision "file", source: "./configs/", destination: "/tmp/configs" ```_

Mögliche Bereitstellung

```ruby | config.vm.provision "ansible" do | ansible | | ansible.playbook = "site.yml" ansible.inventory_path = "inventory/vagrant" ansible.limit = "all" ansible.verbose = "v"

ansible.extra_vars = \\{ nginx_port: 80, database_name: "myapp" \\} end ```_

Docker Provisioning

```ruby | config.vm.provision "docker" do | d | | d.pull_images "nginx" d.pull_images "mysql:8.0"

d.run "nginx", args: "-p 80:80 -v /vagrant/html:/usr/share/nginx/html"

d.run "mysql", args: "-e MYSQL_ROOT_PASSWORD=secret -p 3306:3306" end ```_

Netzwerkkonfiguration

Private Netzwerke

```ruby

Static IP

config.vm.network "private_network", ip: "192.168.56.10"

DHCP

config.vm.network "private_network", type: "dhcp"

Multiple interfaces

config.vm.network "private_network", ip: "192.168.56.10" config.vm.network "private_network", ip: "10.0.0.10" ```_

Öffentliche Netze

```ruby

Bridge to default interface

config.vm.network "public_network"

Bridge to specific interface

config.vm.network "public_network", bridge: "en0: Wi-Fi (AirPort)"

Static IP on public network

config.vm.network "public_network", ip: "192.168.1.100" ```_

Port Forwarding

```ruby

Basic port forwarding

config.vm.network "forwarded_port", guest: 80, host: 8080

Multiple ports

config.vm.network "forwarded_port", guest: 80, host: 8080 config.vm.network "forwarded_port", guest: 443, host: 8443 config.vm.network "forwarded_port", guest: 3306, host: 3306

Auto-correct port conflicts

config.vm.network "forwarded_port", guest: 80, host: 8080, auto_correct: true ```_

Konfiguration des Anbieters

VirtualBox Provider

```ruby | config.vm.provider "virtualbox" do | vb | | vb.gui = true vb.memory = "4096" vb.cpus = 4 vb.name = "MyVM"

# Advanced settings vb.customize ["modifyvm", :id, "--vram", "128"] vb.customize ["modifyvm", :id, "--accelerate3d", "on"] vb.customize ["modifyvm", :id, "--clipboard", "bidirectional"] end ```_

VMware Provider

ruby | config.vm.provider "vmware_desktop" do | vmware | | vmware.vmx["memsize"] = "4096" vmware.vmx["numvcpus"] = "4" vmware.vmx["displayName"] = "MyVM" end_

Hyper-V-Anbieter

ruby | config.vm.provider "hyperv" do | hv | | hv.memory = 4096 hv.cpus = 4 hv.vmname = "MyVM" end_

Synthetische Ordner

Basic Synthetische Ordner

```ruby

Default synced folder (disabled)

config.vm.synced_folder ".", "/vagrant", disabled: true

Custom synced folder

config.vm.synced_folder "./src", "/var/www/html"

With options

config.vm.synced_folder "./app", "/var/www/html", owner: "www-data", group: "www-data", mount_options: ["dmode=775,fmode=664"] ```_

NFS Synced Folders

ruby config.vm.synced_folder "./", "/vagrant", type: "nfs", nfs_udp: false, nfs_version: 4_

SMB Synced Folders (Windows)

ruby config.vm.synced_folder "./", "/vagrant", type: "smb", smb_username: "username", smb_password: "password"_

Best Practices

Leistungsoptimierung

  1. Resource Allocation: Zuordnung geeigneter CPU und Speicher
  2. *Synced Folders: Verwenden Sie NFS für bessere Leistung auf macOS/Linux
  3. Box Auswahl: Wählen Sie minimale Basisboxen
  4. Snapshot Management: Schnappschüsse für schnelle Rollbacks verwenden
  5. *Provider Optimierung: anbieterspezifische Optimierungen konfigurieren

Sicherheit

  1. *SSH Schlüssel: Verwenden Sie SSH-Tasten anstelle von Passwörtern
  2. ** Netzisolierung**: Verwenden Sie private Netzwerke, wenn möglich
  3. Firewall: Konfigurieren geeigneter Firewall-Regeln
  4. *Updates: Boxen und Vagrant aktualisieren
  5. *Secrets: Begehen Sie keine Geheimnisse zur Versionskontrolle

Entwicklungs-Workflow

  1. Version Control: Vagrantfile in Versionskontrolle enthalten
  2. ** Aussprache**: Dokumentenaufbau und -nutzung
  3. Konsistenz: Gleiche Umgebung im Team nutzen
  4. Test: Testvorbereitungsskripte
  5. Cleanup: Regelmäßig ungenutzte VMs und Boxen aufräumen

Fehlerbehebung

  1. *Logs: Vagrant und Providerlogs überprüfen
  2. SSH: Verwendung vagrant ssh-config für Debugging
  3. Networking: Testnetzwerk-Konnektivität
  4. Provisioning: Testbereitstellungsskripte getrennt
  5. ** Ressourcen**: Host-Systemressourcen überwachen