Windows仮想環境の設定


目次

  • Introduction

  • Install and setup Vagrant
  • Creating a new project
  • Your first vagrant up
  • Customizing your VM
  • Provisioning
  • Conclusion
  • 導入と理由

    Recently we've been trying to implement a Continuous Delivery pipeline for our in-house .NET applications suite - you can't imagine how long and painful it is to install and configure a multi-tier application by hand 😓 ! Being a demanding task (a looot of scripting required!), we needed to be able to quickly provision and destroy several test environments (where 'environment', containers not being supported yet, is a whole Windows Virtual Machine).

    The main idea was to create an easy, repeatable and trackable procedure. The best practices mentioned by Google's DevOps capabilities 私たちをコードとしてインフラストラクチャを使用するように説得しました.IACは基本的にコードですgit - 適切な仮想化エンジンに供給される作成と構成スクリプトでできています.
    Virtualbox , VMWare and Hyper-V すべて自分のクリスを提供しますが、我々はプロバイダー中立ツールを使用したいと思います.だから我々はHashicorp's Vagrant , 安open-source CLIベースのVMマネージャは、メインのハイパーバイザーと統合されたプロバイダを既に装備.

    この記事はあなたの最初のテスト環境を作成するクイックスタートガイドです.

    インストールとセットアップ

    You can download Vagrant from the download page .
    インストールが完了すると、お好みのシェルを開くことで確認できますWindows Terminal ), 実行中:
    vagrant --version
    

    新しいプロジェクトの作成

    Vagrant depends on a specific location on your workstation to store all the configuration files of your project and the virtualized disks: therefore, you need to create your own MyVagrant\ folder and cd into it.

    Once you're in it, run

    vagrant init --minimal
    
    to initialize ローカルフォルダ内のあなたの迷宮プロジェクト.これは最小設定ファイルVagrantfile .
    GrarantFileは、説明されたインフラストラクチャを作成するために様々な仮想化プロバイダーと対話するのに使用する命令を含んでいるRubyベースのスクリプトです.
    で作成された最小のファイルランブinit コマンドはとても簡単です.
    Vagrant.configure("2") do |config|
      config.vm.box = "base"
    end
    
    The Vagrant.configure ブロック特定の"configurator" version : すべての次の手順は、このブロックに含まれる必要があります.
    中央コマンドconfig.vm.box ) 仮想マシンを作成するのに使われる「BOX」を指定します.エー"box" ) は、イメージの概念とかなり似ているDocker images - すでに作成されたVMSからダウンロード可能またはコミュニティからのダウンロードVagrant Cloud repository .

    あなたの最初の浮浪者

    If you try to spin up your VM with the initial Vagrantfile, Vagrant will return an error because it doesn't know any "base" box. You may replace that value with any box name available: in our example, we will try to boot a Windows Server machine, therefore we'll use the publicly available Stefan Scherer's Windows Server 2019 box デスクトップを使用してWindows Server 2019 evalの180日間の試用版を持つこと.
    場合は、ボックスのローカルコピーを持っていない場合は、違反者は自動的にVMを作成する前に、流浪雲からそれをダウンロードします.
    VMをブートするには、次のコマンドを実行します.
    vagrant up
    
    我々は仮想化プロバイダを指定しなかったのでVirtualbox デフォルトとして:もしあなたが異なるハイパーバイザーを持っているなら、appropriate settings . 次に、--provider=PROVIDER_NAME (この場合、vmware_fusion , hyperv , vmware_desktop , docker , などにvagrant up コマンド.
    放浪は、イメージをダウンロードして、選択されたOSに応じてVMを起動するには数分かかります.出力が含まれている場合
    Timed out while waiting for the machine to boot. This means that
    Vagrant was unable to communicate with the guest machine within
    the configured ("config.vm.boot_timeout" value) time period.
    
    VMは正しくVirtualBox上で起動しました、しかし、放浪者はそれと通信することができなくて、以下のような指示を進めますprovisioning ソフトウェアしたがって、次のプロパティについては、GrarantFileの最初の部分に追加する必要がありますcommunicator (i.e. WinRM ) 私たちのWindowsオペレーティングシステムに接続するには、その違反が使用されます.
      # Additional parameters to communicate with Windows
      config.vm.boot_timeout = 60
      config.vm.communicator = "winrm"
      config.winrm.port = 55985
    
    VirtualBox GUIを開くことによって、あなたは新しいVMをリストで見つけるかもしれません:我々が提供したものとして最小のGrarantfileで、仮想マシンはプロバイダーのものとちりのデフォルトの特性の組合せを持っています
  • Grarantによって選択されたデフォルトのVM名はPROJECT-FOLDER_default_TIMESTAMP ; より詳細なホスト名などでカスタマイズすることができますweb or db .
  • ==> default: Setting the name of the VM: MyVagrant_default_1629122422277_92221
    
  • VMはホストにNAT経由で接続されたメインネットワークアダプタ(「アダプタ1」)を持ちます:
  • ==> default: Preparing network interfaces based on configuration...
        default: Adapter 1: nat
    
  • ホスト上のネットワークTCPポート22は、ゲストのTCPポート2222にマップされます.
  • ==> default: Forwarding ports...
        default: 22 (guest) => 2222 (host) (adapter 1)
    
    次のセクションで上記のプロパティの変更方法を参照してください.

    VMのカスタマイズ

    Customizing our newborn Virtual Machine requires a little bit of additional configuration いくつかの設定を追加する必要がありますconfig.vm 不正なファイルの名前空間.
    以下で説明されたすべてのコードをVagrant.configure("2") do |config| セクション.
  • いくつかのプロパティは、provider , このようなVM's name またはhypervisor's GUI . いくつかの設定で特定のショートカットがないことに注意してください customize プロパティー
  • # Customization
    config.vm.provider "virtualbox" do |v|
      v.name = "my_vm"    # Sets the new VM's name
      v.gui = true        # Enables the hypervisor's GUI
      v.memory = 2048     # Sets the VM's RAM
      v.customize ["modifyvm", :id, "--draganddrop", "hosttoguest"] # Enables drag-and-drop between host and guest
      v.customize ["modifyvm", :id, "--clipboard", "bidirectional"] # Enables a bidirectional clipboard between host and guest
    end
    
  • 追加のポートをホストとゲストの間でマッピングすることができますconfig.vm.network 設定(ネットワーク上の追加の詳細を見つけることができます)here )
  •   # Customization
      config.vm.network :forwarded_port, guest: 80, host: 8080, id: "http"  # Map host's port 8080 to guest's port 80
    
  • デフォルトでは"sync" ) あなたのプロジェクトディレクトリ--すなわち、Grarantfileを持つディレクトリ/vagrant ゲスト.追加の同期フォルダを設定できます.
  •   # Customization
      config.vm.synced_folder "app/wwwroot", "src/"  # Map the host's "src/" folder to the guest's "/app/wwwroot"
    
    そして、より多く!
    各変更後、実行を覚えてvagrant validate 文法エラーをチェックするには、任意の構文エラーを返します.
    検証が成功した場合は、vagrant reload VMを再起動し、すべての変更を適用します.

    プロビジョニング

    So now we have a ready VM, but what if we need to also install software or change the configuration during the boot process? This practice is called provisioning そして、それはvagrant up コマンド.
    いくつかのプロビジョナーのような放浪と統合されてAnsible , Puppet , Docker そして、単にアップロードfiles を返します.
    この場合、パッケージマネージャをインストールして開発環境を設定してみましょうChocolatey ) それから、IDEVisual Studio Code ?), そしてそれを試してみましょうshell プロビジョナー、すなわちシェルスクリプトのアップロードと実行.
    クリエイトアscripts/ プロジェクトのフォルダとInstallChocolatey.ps1 PowerShellスクリプト(オフィシャルシェルスクリプト)Chocolatey install page , してくださいインターネットからスクリプトをダウンロードするには❗) その中で:
    あなたのGrabRanfileの設定ブロックで
      # Provisioning
      config.vm.provision "shell", path: "scripts/InstallChocolatey.ps1"  # Run the external script to install Chocolatey
      config.vm.provision "shell", inline: "choco install vscode --yes"   # Run the inline script to install VSCode via Chocolatey
    
    さて、もしvagrant reload あなたのVM、プロビジョニングはブートの終わりに実行されます.最初のシェルスクリプト(外部ファイルから呼び出される)path をダウンロードしてインストールしますinline スクリプトはダウンロードとインストールを強制しますVisual Studio Code's choco package Communateyコミュニティリポジトリから.

    結論

    In just a few minutes, we were able to spin up a Virtual Machine with our desired operating system and with some software installed (a package manager and an IDE) and now we're ready to write code and start some tests! 🎉 🎊


    さらに、我々のIACファイル(すなわち、Grarantfileと対応するPowerShellスクリプト)は、現在Agit リポジトリは、このようにすべてのソースコントロール機能を利用します.基本的に、バージョン管理されたインフラストラクチャを持ちます.
    Here この投稿に記載されているコードを見つけることができます.
    また会いましょう!👋