《实战 Istio入门与实战》—3.3 模拟实验时的场景

举报
华章计算机 发表于 2019/06/20 16:08:54 2019/06/20
【摘要】 本节书摘来自华章计算机《实战 Istio入门与实战》一文中的第3章,第3.3节,作者是毛广献。

3.3 模拟实验时的场景

       由于后续实验时使用三台虚拟机进行实验,在实验时,不可避免地会使用Vagrant管理虚拟机环境,包括初始化创建虚拟机,启动虚拟机,连接并登录到虚拟机环境,保存实验环境,以及快速恢复实验环境等。本节会模拟后续实验场景用到的步骤,方便你熟悉整个实验环境管理的流程。

1. 初始化虚拟机集群

(1)创建虚拟机配置文件

创建名为istio的目录,并把如下的配置文件写入istio目录的Vagrantfile文件:

 1 # -*- mode: ruby -*-

 2 # vi: set ft=ruby :

 3 

 4 Vagrant.configure("2") do |config|

 5     (1..3).each do |i|

 6         config.vm.define "lab#{i}" do |node|

 7             node.vm.box = "centos-7.4-base"

 8     

 9             node.ssh.insert_key = false

10             node.vm.hostname = "lab#{i}"

11     

12             node.vm.network "private_network", ip: "11.11.11.11#{i}"

13     

14             node.vm.provision "shell", run: "always",

15                 inline: "ntpdate ntp.api.bz"

16       

17             node.vm.provision "shell", run: "always",

18                 inline: "echo hello from lab#{i}"

19     

20             node.vm.provider "virtualbox" do |v|

21                 v.cpus = 2

22                 v.customize ["modifyvm", :id, "--name", "lab#{i}", "--memory", 

                              "2048"]

23             end

24         end

25     end

26 end

       第5行表示创建三台虚拟机。

       第7行表示使用名为"centos-7.4-base"的box。

       第9行表示不自动生成新的ssh key,使用Vagrant默认的ssh key注入到虚拟机中,这么做主要是为了方便登录。不用为每台虚拟机设置ssh key登录。

       第10行设置3台虚拟机主机名分别为lab1、lab2、lab3。

       第12行设置3台虚拟机的私有网络为11.11.11.111、11.11.11.112、11.11.11.113。

       第14~15行表示当虚拟机启动完成之后,执行inline中配置的shell命令,此命令用于开机之后的时间同步。

       第17~18行表示当虚拟机启动完成之后,执行inline中配置的shell命令,此命令用于输出测试字符串。

       第21~22行设置虚拟机的CPU核心数和内存大小,本次实验设置为每台虚拟机2核2G内存,可以根据自己电脑的实际情况适当加大。

       上面的虚拟机集群配置文件,也是我们后续实验时所使用的虚拟机环境配置文件。

(2)启动虚拟机集群

       如果启动不成功,请调整上一步骤中配置文件时CPU和Memery相关的配置。还需要确保虚拟机目录D:\virtualbox中没有和此次实验中创建的同名的虚拟机目录(lab1、lab2、lab3)存在。代码如下:

$ vagrant up

Bringing machine 'lab1' up with 'virtualbox' provider...

Bringing machine 'lab2' up with 'virtualbox' provider...

Bringing machine 'lab3' up with 'virtualbox' provider...

==> lab1: Importing base box 'centos-7.4-base'...

==> lab1: Matching MAC address for NAT networking...

==> lab1: Setting the name of the VM: istio_lab1_1543225240637_26922

==> lab1: Clearing any previously set network interfaces...

==> lab1: Preparing network interfaces based on configuration...

    lab1: Adapter 1: nat

    lab1: Adapter 2: hostonly

==> lab1: Forwarding ports...

    lab1: 22 (guest) => 2222 (host) (adapter 1)

==> lab1: Running 'pre-boot' VM customizations...

==> lab1: Booting VM...

...

==> lab1: Running provisioner: shell...

    lab1: Running: inline script

    lab1: hello from lab1

==> lab2: Importing base box 'centos-7.4-base'...

...

==> lab2: Running provisioner: shell...

    lab2: Running: inline script

    lab2: hello from lab2

==> lab3: Importing base box 'centos-7.4-base'...

==> lab3: Matching MAC address for NAT networking...

...

==> lab3: Setting hostname...

==> lab3: Configuring and enabling network interfaces...

    lab3: SSH address: 127.0.0.1:2201

    lab3: SSH username: vagrant

    lab3: SSH auth method: private key

==> lab3: Running provisioner: shell...

    lab3: Running: inline script

    lab3: hello from lab3

(3)查看虚拟机状态

代码如下:

$ vagrant status

Current machine states:


lab1                      running (virtualbox)

lab2                      running (virtualbox)

lab3                      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'.

2. 连接虚拟机集群

1)使用Xshell导入vagrant的密钥

       密钥存储在VAGRANT_HOME环境变量里指定目录的insecure_private_key文件中,添加该密钥到Xshell中。选择顶部菜单中“工具”菜单的“用户密钥管理者”,在弹出的对话框中点击“导入”按钮,选择insecure_private_key文件即可完成密钥的导入,如图3-1所示。

2)使用Xshell创建新的会话。

       选择顶部菜单中“文件”菜单的“新建”,创建连接到三台虚拟机的会话。实验中配置的三台虚拟机IP地址分别为11.11.11.111、11.11.11.112、11.11.11.113,ssh端口为22,如图3-2所示。


图3-1 导入vagrant的密钥


图3-2 创建新的会话

       认证方式选择Public Key,用户名为vagrant,用户密钥选择insecure_private_key,如图3-3所示。


图3-3 认证方式选择

3)Xshell连接测试。

       测试结果如下所示,表明实验环境创建正确,可以在Xshell继续添加lab2、lab3虚拟机的SSH连接。



3. 暂停实验环境虚拟机

       当我们的实验进行到一定步骤后,可能需要暂停,换个时间再次进行实验。这个时候我们可以直接暂停整个实验环境中的所有虚拟机,等下次再进行实验时,直接恢复之前的环境即可,非常方便。

       暂停集群中所有虚拟机如下所示:

$ vagrant suspend

==> lab1: Saving VM state and suspending execution...

==> lab2: Saving VM state and suspending execution...

==> lab3: Saving VM state and suspending execution...

        暂停集群中单个虚拟机如下所示:

$ vagrant suspend lab1

==> lab1: Saving VM state and suspending execution...

        恢复集群中所有虚拟机如下所示:

$ vagrant resume

==> lab1: Resuming suspended VM...

==> lab1: Booting VM...

...

==> lab2: Resuming suspended VM...

==> lab2: Booting VM...

...

==> lab3: Resuming suspended VM...

==> lab3: Booting VM...

==> lab3: Waiting for machine to boot. This may take a few minutes...

    lab3: SSH address: 127.0.0.1:2201

    lab3: SSH username: vagrant

    lab3: SSH auth method: private key

==> lab3: Machine booted and ready!

==> lab3: Machine already provisioned. Run 'vagrant provision' or use the '--provision'

==> lab3: flag to force provisioning. Provisioners marked to run always will still run.

==> lab3: Running provisioner: shell...

    lab3: Running: inline script

    lab3: 16 Dec 13:30:58 ntpdate[2983]: step time server 114.118.7.161 offset 25.563654 sec

==> lab3: Running provisioner: shell...

    lab3: Running: inline script

    lab3: hello from lab3

         恢复集群中单个虚拟机,使用resume和up都能恢复暂停的虚拟机:

$ vagrant resume lab1

==> lab1: Resuming suspended VM...

==> lab1: Booting VM...

==> lab1: Waiting for machine to boot. This may take a few minutes...

    lab1: SSH address: 127.0.0.1:2222

    lab1: SSH username: vagrant

    lab1: SSH auth method: private key

==> lab1: Machine booted and ready!

==> lab1: Machine already provisioned. Run 'vagrant provision' or use the '--provision'

==> lab1: flag to force provisioning. Provisioners marked to run always will still run.

==> lab1: Running provisioner: shell...

    lab1: Running: inline script

    lab1: 16 Dec 13:40:35 ntpdate[2983]: step time server 114.118.7.161 offset 25.563654 sec

==> lab1: Running provisioner: shell...

    lab1: Running: inline script

lab1: hello from lab1


$ vagrant up lab1

Bringing machine 'lab1' up with 'virtualbox' provider...

==> lab1: Resuming suspended VM...

==> lab1: Booting VM...

==> lab1: Waiting for machine to boot. This may take a few minutes...

    lab1: SSH address: 127.0.0.1:2222

    lab1: SSH username: vagrant

    lab1: SSH auth method: private key

==> lab1: Machine booted and ready!

==> lab1: Machine already provisioned. Run 'vagrant provision' or use the '--provision'

==> lab1: flag to force provisioning. Provisioners marked to run always will still run.

==> lab1: Running provisioner: shell...

    lab1: Running: inline script

    lab1: 16 Dec 13:45:55 ntpdate[2983]: step time server 114.118.7.161 offset 25.563654 sec

==> lab1: Running provisioner: shell...

    lab1: Running: inline script

        lab1: hello from lab1

4. 保存与恢复实验环境

       完成前面的步骤后,我们的实验虚拟机环境就已经基本搭建完成了。之后我们可以安装Docker、Git、Wget等基础软件。安装之后保存实验环境,之后实验不成功或者实验环境被污染,可以快速恢复到当前的实验环境。

       保存集群中所有虚拟机快照:

$ vagrant snapshot save base

==> lab1: Snapshotting the machine as 'base'...

==> lab1: Snapshot saved! You can restore the snapshot at any time by

==> lab1: using 'vagrant snapshot restore'. You can delete it using

==> lab1: 'vagrant snapshot delete'.

==> lab2: Snapshotting the machine as 'base'...

==> lab2: Snapshot saved! You can restore the snapshot at any time by

==> lab2: using 'vagrant snapshot restore'. You can delete it using

==> lab2: 'vagrant snapshot delete'.

==> lab3: Snapshotting the machine as 'base'...

==> lab3: Snapshot saved! You can restore the snapshot at any time by

==> lab3: using 'vagrant snapshot restore'. You can delete it using

==> lab3: 'vagrant snapshot delete'.

       保存集群中单个虚拟机快照:

$ vagrant snapshot save lab1 base

==> lab1: Snapshotting the machine as 'base'...

==> lab1: Snapshot saved! You can restore the snapshot at any time by

==> lab1: using 'vagrant snapshot restore'. You can delete it using

==> lab1: 'vagrant snapshot delete'.

       删除集群中所有虚拟机快照:

$ vagrant snapshot delete base

==> lab1: Deleting the snapshot 'base'...

==> lab1: Snapshot deleted!

==> lab2: Deleting the snapshot 'base'...

==> lab2: Snapshot deleted!

==> lab3: Deleting the snapshot 'base'...

==> lab3: Snapshot deleted!

       删除集群中单个虚拟机快照:

$ vagrant snapshot delete lab1 base

==> lab1: Deleting the snapshot 'base'...

==> lab1: Snapshot deleted!

       查看集群中所有虚拟机快照:

$ vagrant snapshot list

base

base

base

       查看集群中单个虚拟机快照:

$ vagrant snapshot list lab1

base

       恢复集群中所有虚拟机快照:

$ vagrant snapshot restore base

==> lab1: Forcing shutdown of VM...

==> lab1: Restoring the snapshot 'base'...

==> lab1: Resuming suspended VM...

==> lab1: Booting VM...

...

==> lab2: Forcing shutdown of VM...

==> lab2: Restoring the snapshot 'base'...

==> lab2: Resuming suspended VM...

==> lab2: Booting VM...

==> lab2: Waiting for machine to boot. This may take a few minutes...

...

==> lab3: Forcing shutdown of VM...

==> lab3: Restoring the snapshot 'base'...

==> lab3: Resuming suspended VM...

==> lab3: Booting VM...

...

==> lab3: Machine booted and ready!

==> lab3: Running provisioner: shell...

    lab3: Running: inline script

    lab3: 16 Dec 13:50:38 ntpdate[2983]: step time server 114.118.7.161 offset 55.563654 sec

==> lab3: Running provisioner: shell...

    lab3: Running: inline script

    lab3: hello from lab3

       恢复集群中单个虚拟机快照:

$ vagrant snapshot restore lab1 base

==> lab1: Forcing shutdown of VM...

==> lab1: Restoring the snapshot 'base'...

==> lab1: Resuming suspended VM...

==> lab1: Booting VM...

==> lab1: Waiting for machine to boot. This may take a few minutes...

    lab1: SSH address: 127.0.0.1:2222

    lab1: SSH username: vagrant

    lab1: SSH auth method: private key

==> lab1: Machine booted and ready!

==> lab1: Running provisioner: shell...

    lab1: Running: inline script

    lab1: 16 Dec 14:10:29 ntpdate[2983]: step time server 114.118.7.161 offset 25.563654 sec

==> lab1: Running provisioner: shell...

    lab1: Running: inline script

    lab1: hello from lab1

         在使用Vagrant时,可能会出现偶发的异常错误,大部分情况下,重启电脑即可解决。


【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。