Vagrant是一款用於構建及配置虛擬開發環境的軟體,基於Ruby,主要以命令行的方式運行。
主要使用Oracle的開源VirtualBox虛擬化系統,與Chef,Salt,Puppet等環境配置管理軟體搭配使用, 可以實行快速虛擬開發環境的構建。
# 安裝 virtualbox
brew cask install virtualbox
# 安裝 vagrant
brew cask install vagrant
# 安裝 vagrant-manager
brew cask install vagrant-manager
[32-bit][64-bit]
VirtualBox Extension Park
# 創建資料夾
mkdir vagrant_centos_7
# 切換目錄
cd vagrant_centos_7
# 初始化 Vargant 並指定作業系統為 centos7,會建立一個 Vagrantfile
vagrant init centos/7
vagrant up
vagrant ssh
# 創建vagrant file
vagrant init
# 啟動虛擬機
vagrant up
# 登入虛擬機
vagrant ssh
# 關閉虛擬機
vagrant halt
# 刪除虛擬機
vagrant destroy
# 查看虛擬機狀態
vagrant status
Vagrant.configure("2") do |config|
config.vm.box = "bento/centos-7.1"
end
config.vm.network "public_network"
Provision 時機:
config.vm.provision "shell", inline: <<-SHELL
sudo yum update
sudo rpm -Uvh https://mirror.webtatic.com/yum/el6/latest.rpm
sudo yum -y install nginx18
sudo yum -y install http://yum.postgresql.org/9.5/redhat/rhel-6-x86_64/pgdg-centos95-9.5-2.noarch.rpm
sudo yum -y install postgresql95-server postgresql95-contrib postgresql95-devel
sudo service postgresql-9.5 initdb
SHELL
SCRIPTS = ["install-base.sh","project.sh"]
for scripts in SCRIPTS
config.vm.provision "shell", path: scripts
end
透過 port mapping/forwarding 機制
config.vm.network "forwarded_port", guest: 80, host: 8080
透過 private network 機制
config.vm.network "private_network", ip: “192.168.33.10"
config.vm.provision "file", source: "project.sh", destination: "/home/project/"
config.vm.synced_folder "./app", "/var/www/html/app"
同步資料夾,並設定擁有者權限
config.vm.synced_folder "./app", "/var/www/html/app",
owner: "www-data"
boxcutter/ubuntu1404-desktop
VBoxManage list vms
VBoxManage unregister xxxx --delete
###實作
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
# 指定系統OS
config.vm.box = "centos/7"
# 設定快取yum
config.cache.enable :yum
# 設定VirtualBox網路,使用Bridge橋接網路
config.vm.network "public_network"
# 使用系統權限執行下面指令
config.vm.provision "shell", inline: <<-SHELL
# 安裝必要Library和軟體
yum -y update
yum -y install epel-release htop vim iptraf-ng.x86_64 net-tools gcc gcc-c++ make git patch openssl-devel zlib-devel readline-devel sqlite-devel bzip2-devel zip unzip
yum -y install libjpeg zlib libtiff libfreetype littlecms libwebp tcl tk openjpeg libtiff-devel libjpeg-devel libzip-devel freetype-devel lcms2-devel libwebp-devel tcl-devel tk-devel python-devel
yum -y install curl-devel expat-devel gettext-devel perl-ExtUtils-MakeMaker wget postgresql-libs postgresql-devel libffi-devel open-vm-tools
# 設定時區服務ntp
yum -y install ntp
timedatectl set-timezone Asia/Taipei
rm /etc/localtime
ln -s /usr/share/zoneinfo/Asia/Taipei /etc/localtime
systemctl enable ntpd
systemctl start ntpd
SHELL
# 系統基本建置
SCRIPTS = ["base.sh"]
for scripts in SCRIPTS
config.vm.provision "shell", path: scripts
end
# 複製檔案到VM內部,、Source Code、Project設定檔
config.vm.provision "file", source: "./", destination: "~/"
# 複製過去後的處理過程
config.vm.provision "shell", inline: <<-SHELL
mkdir /home/project/.ssh
cp /home/vagrant/id_rsa /home/project/.ssh/id_rsa
ssh-keyscan -T 60 bitbucket.org > /home/project/.ssh/known_hosts
chmod 600 /home/project/.ssh/id_rsa
chmod 700 /home/project/.ssh
chown -R project.project /home/project/.ssh
chown -R project.project /home/project/.ssh/*
# nginx
cp /home/vagrant/nginx/nginx.conf /etc/nginx/nginx.conf
cp -r /home/vagrant/nginx/conf.d/* /etc/nginx/conf.d/
cp -r /home/vagrant/nginx/ssl /etc/nginx/ssl
# systemd
cp -r /home/vagrant/systemd/* /etc/systemd/system/
# 使用者
cp /home/vagrant/project.sh /home/project/project.sh
# 複製 token 專案
unzip /home/vagrant/source_code/ithome_pellok_2018.zip -d /home/project/
mv /home/project/ithome_pellok_2018 /home/project/ithome_pellok_2018
chown -R project.project /home/project/
su - project -c "sh project.sh"
chmod +x /home/project
# 重新載入systemctl 設定檔
systemctl daemon-reload
# 依順序來啟動服務
systemctl start ithome_pellok_2018;sleep 5s
# 設定開機自動啟動服務
systemctl enable ithome_pellok_2018
# 開機後自動重新啟動服務,依據服務順序
echo "systemctl restart ithome_pellok_2018;sleep 5s" >> /etc/rc.d/rc.local
SHELL
end
使用 Vagrant File 可以讓系統環境轉換成程式碼,並且加入版控,達到 Service As Code 的概念,有時候因應專案需求,需要把我們的軟體部署在 VMware 上面的時候,會使用 Vagrant File 把專案搭包成 OVA 檔案,客戶只要在 VMware 上面匯入 OVA 在設定一下網路就可以直些使用,還蠻方便的.