2019 年 最簡單最通俗的 vagrant 安裝使用說明,附帶示例Vagrantfile

2019 年 最簡單最通俗的 vagrant 安裝使用說明,附帶示例vagrantfile

本章內容須要關閉 Hyper-V 啊,但願你們提早關閉,避免其餘問題

附帶說明,本文有示例demo,在文章下方,請耐心查看,使用demo時,但願您已經安裝好了vagrant + VirtualBox。python

Vagrant是一個基於Ruby的工具,用於建立和部署虛擬化開發環境。它 使用Oracle的開源VirtualBox虛擬化系統,使用 Chef建立自動化虛擬環境。git

老套路,更新留坑

VirtualBox 安裝 【官網下載】

具體安裝過程無非就是下一步下一步,沒啥好說的github

vagrant 安裝

vagrant windows 下載

vagrant mac 下載

vagrant box 下載

vagrant box 添加到本地鏡像

  • 下載好的鏡像添加
vagrant box add {鏡像名稱} {鏡像地址}

例如:shell

vagrant box add C:/box/centos7.box --name centos/7.5
  • 使用遠程鏡像
vagrant box add https://mirrors.tuna.tsinghua.edu.cn/ubuntu-cloud-images/bionic/current/bionic-server-cloudimg-amd64-vagrant.box --name ubuntu/bionic

vagrant 命令說明

vagrant destroy 刪除虛擬機

vagrant destroy -f lincms

vagrant up 啓動/建立虛擬機

vagrant up  lincms

vagrant ssh 進行虛擬機交互命令行

vagrant ssh lincms

vagrant reload 從新啓動虛擬機

vagrant reload lincms

vagrant up/reload --provision 從新建立或從新執行腳本

vagrant up --provision
vagrant reload --provision

vagrant status 查看當前目錄下的虛擬機狀態

vagrant status

例如:json

G:\project
λ vagrant status
Current machine states:

lincms                    running (virtualbox)

This environment represents multiple VMs. The VMs are all listed
above with their current state. For more information about a specific
VM, run `vagrant status NAME`.

vagrant 使用樣例

咱們舉例以lin-cms-tp的全家桶進行舉例,其餘目錄同樣哈,別太注重目錄ubuntu

  • 進入 lin-cms-tp 全家桶 平級目錄
λ ls

lin-cms-tp
  • 初始化Vagrantfile
vagrant init

查看目錄變化,查看是否生成Vagrantfilevim

ls

lin-cms-tp  Vagrantfile
  • 添加 box 鏡像
vagrant box add https://mirrors.tuna.tsinghua.edu.cn/ubuntu-cloud-images/bionic/current/bionic-server-cloudimg-amd64-vagrant.box --name ubuntu/bionic
  • 而後替換Vagrantfile 內容
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.require_version ">= 1.6.0"

boxes = [
    {
        :name => "lincms",
        :eth1 => "10.10.1.10",
        :mem => "1024",
        :cpu => "1"
    }
]

Vagrant.configure(2) do |config|

  config.vm.box = "ubuntu/bionic"

  boxes.each do |opts|
      config.vm.define opts[:name] do |config|
        config.vm.hostname = opts[:name]
        config.vm.provider "vmware_fusion" do |v|
          v.vmx["memsize"] = opts[:mem]
          v.vmx["numvcpus"] = opts[:cpu]
        end

        config.vm.provider "virtualbox" do |v|
          v.customize ["modifyvm", :id, "--memory", opts[:mem]]
          v.customize ["modifyvm", :id, "--cpus", opts[:cpu]]
        end

        config.vm.network :private_network, ip: opts[:eth1]
      end
  end

  config.vm.synced_folder "./lin-cms-tp", "/home/vagrant/lin-cms-tp"
  config.vm.provision "shell", privileged: true, path: "./setup.sh"

end
  • 新建腳本文件:setup.sh , 內容以下:
# Timezone
sudo /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && echo 'Asia/Shanghai' > /etc/timezone

# 設置國內源
sudo mv /etc/apt/sources.list /etc/apt/sources.list.back && \
     echo '# 默認註釋了源碼鏡像以提升 apt update 速度,若有須要可自行取消註釋 \n \
     deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse \n \
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse \n \
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse \n \
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse \n \
    deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse \n \
    deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse \n \
    deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse \n \
    deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse \n \
    deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse \n \
    deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse' >> /etc/apt/sources.list
     
# Libs
sudo apt-get update && sudo apt-get install -y wget curl git vim gcc glibc-static telnet bridge-utils

# install docker

sudo curl -fsSL https://get.docker.com | sudo bash -s docker --mirror Aliyun

sudo groupadd docker
sudo gpasswd -a vagrant docker
sudo systemctl start docker

rm -rf get-docker.sh

#  配置鏡像加速器
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://dt77flbr.mirror.aliyuncs.com"]
}
EOF

sudo systemctl enable docker
sudo systemctl daemon-reload
sudo systemctl restart docker

# #下載docker-compose
# sudo curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# #給docker-compose執行權限
# sudo chmod +x /usr/local/bin/docker-compose 
# #測試安裝是否成功,成功的話打印出docker-compose的版本信息
# sudo docker-compose --version

# 使用pip安裝docker-compose 
sudo apt install python-pip -y
sudo pip install docker-compose

# 打印IP地址信息
ip a
  • 最後一步,建立並啓動虛擬器

建立windows

vagrant up lincms

查看狀態centos

vagrant status

進入

vagrant ssh lincms
若是在以上過程出現錯誤,請到搜索引擎搜索解決,或者提交評論和留言

錯誤收集文檔【點我】

相關文章
相關標籤/搜索