Istio入門與實(shí)戰(zhàn)》 ——3 使用Vagrant管理虛擬機(jī)">《Istio入門與實(shí)戰(zhàn)》 ——3 使用Vagrant管理虛擬機(jī)
686
2025-04-06
3.2 Vagrant常用命令
注意事項:由于默認(rèn)情況下,Vagrant會把臨時文件和Box文件存放在用戶主目錄里。如果Box文件過大,會導(dǎo)致占用過大空間,可以通過設(shè)置環(huán)境變量VAGRANT_HOME來設(shè)定Vagrant的主目錄路徑。Vagrant默認(rèn)設(shè)置的主目錄為用戶主目錄的.vagrant.d文件夾。本次實(shí)驗(yàn)時把此環(huán)境變量設(shè)置為D:\vagrant\home,關(guān)于各個操作系統(tǒng)的環(huán)境變量的設(shè)置,請查閱相關(guān)文檔。
1. 基本命令
基礎(chǔ)命令總覽:
box add:導(dǎo)入box
box list:查看box
box remove:刪除box
init:初始化
up:啟動
status:查看狀態(tài)
ssh:SSH連接
reload:重載
halt:關(guān)閉
suspend:暫停
destroy:刪除
下面舉例說明這些命令的使用方法。
(1)導(dǎo)入Box
導(dǎo)入已經(jīng)下載的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章“實(shí)驗(yàn)說明”中提供的百度云盤鏈接上下載。
(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)初始化
初始化虛擬機(jī)如下所示:
$ 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.
查看未被注釋配置文件內(nèi)容:
$ cat Vagrantfile | tr -d '\r' | egrep -v '^#|^ +#|^$'
Vagrant.configure("2") do |config|
config.vm.box = "centos-7.4-base"
end
(5)啟動
啟動虛擬機(jī)如下所示:
$ 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)查看狀態(tài)
查看虛擬機(jī)狀態(tài)如下所示:
$ 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連接虛擬機(jī),可以嘗試使用系統(tǒng)自己帶的命令行工具CMD連接虛擬機(jī):
$ 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)重新
當(dāng)我們編輯當(dāng)前文件夾下的虛擬機(jī)配置文件Vagrantfile后,可以使用reload命令重載虛擬機(jī),使配置生效。比如添加設(shè)置主機(jī)名的配置:
...
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)關(guān)閉
關(guān)閉虛擬機(jī)如下所示:
$ vagrant halt
==> default: Attempting graceful shutdown of VM...
(10)暫停
由于上一步驟關(guān)閉虛擬機(jī),執(zhí)行本步驟時需要先啟動虛擬機(jī),然后再暫停虛擬機(jī):
$ vagrant suspend
==> default: Saving VM state and suspending execution...
(11)刪除
刪除虛擬機(jī)的命令如下所示:
$ 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. 使用虛擬機(jī)快照命令
虛擬機(jī)快照命令如下:
save:保存虛擬機(jī)快照。
list:查看虛擬機(jī)快照。
restore:用快照恢復(fù)虛擬機(jī)。
delete:刪除虛擬機(jī)快照。
進(jìn)行如下快照的相關(guān)操作時,需要先創(chuàng)建虛擬機(jī)并啟動虛擬機(jī)。
保存虛擬機(jī)快照示例:
$ 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'.
查看虛擬機(jī)快照示例:
$ vagrant snapshot list
istio
用快照恢復(fù)虛擬機(jī)示例:
$ 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!
刪除虛擬機(jī)快照示例:
$ vagrant snapshot delete istio
==> default: Deleting the snapshot 'istio'...
==> default: Snapshot deleted!
虛擬化 Istio
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。