《實戰 Istio入門與實戰》—3.2 Vagrant常用命令
3.2 Vagrant常用命令
注意事項:由于默認情況下,Vagrant會把臨時文件和Box文件存放在用戶主目錄里。如果Box文件過大,會導致占用過大空間,可以通過設置環境變量VAGRANT_HOME來設定Vagrant的主目錄路徑。Vagrant默認設置的主目錄為用戶主目錄的.vagrant.d文件夾。本次實驗時把此環境變量設置為D:\vagrant\home,關于各個操作系統的環境變量的設置,請查閱相關文檔。
1. 基本命令
基礎命令總覽:
box add:導入box
box list:查看box
box remove:刪除box
init:初始化
up:啟動
status:查看狀態
ssh:SSH連接
reload:重載
halt:關閉
suspend:暫停
destroy:刪除
下面舉例說明這些命令的使用方法。
(1)導入Box
導入已經下載的Box命令如下:
$ vagrant box add --name centos-7.4-base /f/vagrant/box/centos-7.4-base.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'centos-7.4-base' (v0) for provider:
box: Unpacking necessary files from: file://F:/vagrant/box/centos-7.4-base.box
box:
==> box: Successfully added box 'centos-7.4-base' (v0) for 'virtualbox'!
/f/vagrant/box/centos-7.4-base.box表示Box的路徑地址,這是由于使用了Git Bash的路徑表示方法。如果使用CMD命令行,使用F:/vagrant/box/centos-7.4-base.box即可。此處使用的Box從第2章“實驗說明”中提供的百度云盤鏈接上下載。
(2)查看Box
查看可用的Box如下所示:
$ vagrant box list
centos-7.4-base? ? ? (virtualbox, 0)
(3)刪除Box
刪除不再使用的Box如下所示:
$ vagrant box remove centos-7.4-base
Removing box 'centos-7.4-base' (v0) with provider 'virtualbox'...
(4)初始化
初始化虛擬機如下所示:
$ mkdir test
$ cd test
$ vagrant init centos-7.4-base
A 'Vagrantfile' has been placed in this directory. You are now
ready to 'vagrant up' your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
'vagrantup.com' for more information on using Vagrant.
查看未被注釋配置文件內容:
$ cat Vagrantfile | tr -d '\r' | egrep -v '^#|^ +#|^$'
Vagrant.configure("2") do |config|
config.vm.box = "centos-7.4-base"
end
(5)啟動
啟動虛擬機如下所示:
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos-7.4-base'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: test_default_1543216915133_85015
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
==> default: Forwarding ports...
default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
default:
default: Vagrant insecure key detected. Vagrant will automatically replace
default: this with a newly generated keypair for better security.
default:
default: Inserting generated public key within guest...
default: Removing insecure key from the guest if it's present...
default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
default: No guest additions were detected on the base box for this VM! Guest
default: additions are required for forwarded ports, shared folders, host only
default: networking, and more. If SSH fails on this machine, please install
default: the guest additions and repackage the box to continue.
default:
default: This is not an error message; everything may continue to work properly,
default: in which case you may ignore this message.
(6)查看狀態
查看虛擬機狀態如下所示:
$ vagrant status
Current machine states:
default? ? ? ? ? ? ? ? ? ?running (virtualbox)
The VM is running. To stop this VM, you can run 'vagrant halt' to
shut it down forcefully, or you can run 'vagrant suspend' to simply
suspend the virtual machine. In either case, to restart it again,
simply run 'vagrant up'.
(7)SSH連接
注意,如果Windows下使用Git Bash時無法使用SSH連接虛擬機,可以嘗試使用系統自己帶的命令行工具CMD連接虛擬機:
$ vagrant ssh
Last login: Tue Dec 12 16:01:23 2017 from 10.0.2.2
[vagrant@localhost ~]$ hostname
localhost.localdomain
[vagrant@localhost ~]$ ip a
1: lo:
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0:
link/ether 52:54:00:ca:e4:8b brd ff:ff:ff:ff:ff:ff
inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic eth0
valid_lft 86194sec preferred_lft 86194sec
inet6 fe80::5054:ff:feca:e48b/64 scope link
valid_lft forever preferred_lft forever
[vagrant@localhost ~]$ exit;
logout
Connection to 127.0.0.1 closed.
(8)重新
當我們編輯當前文件夾下的虛擬機配置文件Vagrantfile后,可以使用reload命令重載虛擬機,使配置生效。比如添加設置主機名的配置:
...
config.vm.box = "centos-7.4-base"
config.vm.hostname = "istio"
...
重載使配置文件生效:
$ vagrant reload
==> default: Attempting graceful shutdown of VM...
Connection to 127.0.0.1 closed by remote host.
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
==> default: Forwarding ports...
default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
default: No guest additions were detected on the base box for this VM! Guest
default: additions are required for forwarded ports, shared folders, host only
default: networking, and more. If SSH fails on this machine, please install
default: the guest additions and repackage the box to continue.
default:
default: This is not an error message; everything may continue to work properly,
default: in which case you may ignore this message.
==> default: Setting hostname...
==> default: Machine already provisioned. Run 'vagrant provision' or use the '--provision'
==> default: flag to force provisioning. Provisioners marked to run always will still run.
$ vagrant ssh
Last login: Tue Dec 12 16:11:12 2017 from 10.0.2.2
[vagrant@istio ~]$ hostname
istio
[vagrant@istio ~]$ exit;
logout
Connection to 127.0.0.1 closed.
(9)關閉
關閉虛擬機如下所示:
$ vagrant halt
==> default: Attempting graceful shutdown of VM...
(10)暫停
由于上一步驟關閉虛擬機,執行本步驟時需要先啟動虛擬機,然后再暫停虛擬機:
$ vagrant suspend
==> default: Saving VM state and suspending execution...
(11)刪除
刪除虛擬機的命令如下所示:
$ vagrant destroy
default: Are you sure you want to destroy the 'default' VM? [y/N] y
==> default: Discarding saved state of VM...
==> default: Destroying VM and associated drives...
2. 使用虛擬機快照命令
虛擬機快照命令如下:
save:保存虛擬機快照。
list:查看虛擬機快照。
restore:用快照恢復虛擬機。
delete:刪除虛擬機快照。
進行如下快照的相關操作時,需要先創建虛擬機并啟動虛擬機。
保存虛擬機快照示例:
$ vagrant snapshot save istio
==> default: Snapshotting the machine as 'istio'...
==> default: Snapshot saved! You can restore the snapshot at any time by
==> default: using 'vagrant snapshot restore'. You can delete it using
==> default: 'vagrant snapshot delete'.
查看虛擬機快照示例:
$ vagrant snapshot list
istio
用快照恢復虛擬機示例:
$ vagrant snapshot restore istio
==> default: Forcing shutdown of VM...
==> default: Restoring the snapshot 'istio'...
==> default: Resuming suspended VM...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
==> default: Machine booted and ready!
刪除虛擬機快照示例:
$ vagrant snapshot delete istio
==> default: Deleting the snapshot 'istio'...
==> default: Snapshot deleted!
虛擬化 Istio
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。