在windows平台上使用Hyper-V搭建虚拟机集群环境
开启windows服务
首先需要启动hyper-v的windows服务
![image-20220114173507925](https://img-blog.csdnimg.cn/img_convert/daa51b4aedb6b36d96075d01521978db.png)
打开windows更新
![image-20220114173610883](https://img-blog.csdnimg.cn/img_convert/f5c764f5a2327eabee41586aa54ea0a2.png)
选择开发人员模式
![image-20220114173646380](https://img-blog.csdnimg.cn/img_convert/d4d4d12396d38bcfa285653f139a87e5.png)
然后选择应用
![image-20220114173715973](https://img-blog.csdnimg.cn/img_convert/268fc71a3e8fcae44fb0caa6d33cf772.png)
然后选择【程序和功能】
![image-20220114173748839](https://img-blog.csdnimg.cn/img_convert/923d8ceec5f3db21467ed702fe152014.png)
然后选择【启用或关闭windows功能】
![image-20220114173832545](https://img-blog.csdnimg.cn/img_convert/bda8fdab026db41f0278c80abc324743.png)
把Hyper-V的√选中
![image-20220114173903636](https://img-blog.csdnimg.cn/img_convert/23a1cdba97638f86006e4bedbf24c7bb.png)
重启电脑生效。
如果上述步骤中哪一步没有,请更新windows至最新。
启动服务
打开【服务】管理
![image-20220114174034646](https://img-blog.csdnimg.cn/img_convert/e55ce7a91113e4a624ec1a7ca900f8e0.png)
手动启动Hyper-V的服务
![image-20220114174117700](https://img-blog.csdnimg.cn/img_convert/28bdd5c50c9147c08dfe86c2eb36054a.png)
然后打开Hyper-V的虚拟机创建向导
![image-20220114174152916](https://img-blog.csdnimg.cn/img_convert/d9fb1686fb14d4432cd52cea43565d76.png)
当出现如下窗口,表示Hyper-V启动成功
![image-20220114174322406](https://img-blog.csdnimg.cn/img_convert/1a409fe3c4a1f03735b3ade647266309.png)
下载镜像
下载自己想安装的操作系统,我选择的是centos,The CentOS Project
![image-20220114174448535](https://img-blog.csdnimg.cn/img_convert/78ca152f4ccf5f40195dc34a968e4af2.png)
创建虚拟机
在创建虚拟机向导中,选择本地安装源
![image-20220114174521620](https://img-blog.csdnimg.cn/img_convert/5e57c0f2e8254247f1721e36d5a2e60c.png)
然后更改安装源
![image-20220114174551754](https://img-blog.csdnimg.cn/img_convert/e8f5c16339f81367b5f3a973097c30d1.png)
选择想要安装的操作系统的镜像文件,如果你安装的操作系统不是windows,请去掉windows的√
![image-20220114174744997](https://img-blog.csdnimg.cn/img_convert/e845ce6173abd1402d946090622b3ed5.png)
接着配置虚拟机的物理资源
![image-20220114174820531](https://img-blog.csdnimg.cn/img_convert/f4531c5b219098439066018cff9c3ada.png)
内存
![image-20220114175107670](https://img-blog.csdnimg.cn/img_convert/be600e78f438c2b23eb97cacaa0a5e54.png)
处理器
![image-20220114175140290](https://img-blog.csdnimg.cn/img_convert/c97821eccb3834c6a9b1f2496857a11f.png)
硬盘
默认是虚拟机使用C盘的磁盘空间
![image-20220114175241903](https://img-blog.csdnimg.cn/img_convert/474647d699946e6f82aae59ef1ea0c8e.png)
使用虚拟硬盘新建向导,自定义位置
![image-20220114175311941](https://img-blog.csdnimg.cn/img_convert/873983a01ff74681ed7e574f206868dc.png)
选择动态扩展
![image-20220114175327660](https://img-blog.csdnimg.cn/img_convert/d97e30ef649c681c333ffe4101e6184b.png)
自己指定存储名字和位置
![image-20220114175411002](https://img-blog.csdnimg.cn/img_convert/00a2125e095472e09d5aa0b39d4a3618.png)
指定磁盘大小
![image-20220114175432626](https://img-blog.csdnimg.cn/img_convert/870b1e98566b9711a98690363f97e22e.png)
最后完成
![image-20220114175444361](https://img-blog.csdnimg.cn/img_convert/5c9fed84edbd8f9b473d2f44d94861d6.png)
虚拟机主机名
![image-20220114175519609](https://img-blog.csdnimg.cn/img_convert/ba8332120d6cc913463bad4065d4f184.png)
集成服务
![image-20220114175541704](https://img-blog.csdnimg.cn/img_convert/df063f452c859d989d0f80cb281ad7f9.png)
检查点
关闭检查点可以节省资源
![image-20220114175619501](https://img-blog.csdnimg.cn/img_convert/9ea2ef5ea1c7deb79daeeaef632b278b.png)
智能分页
![image-20220114175725094](https://img-blog.csdnimg.cn/img_convert/106790b9006a687ac7bfde285966c6b6.png)
自动启动
自动启动是指是否物理主机启动时,自动启动虚拟机
![image-20220114175808426](https://img-blog.csdnimg.cn/img_convert/c576461ae34748610d114a7359202036.png)
自动停止
自动停止是指物理主机关闭时,保存虚拟机
![image-20220114175855518](https://img-blog.csdnimg.cn/img_convert/35d5757c3a393bb3431a9b1a211bfdf2.png)
到了这里就配置完了物理资源了,点击确定
然后接着点击连接,进入操作系统安装
![image-20220114175942148](https://img-blog.csdnimg.cn/img_convert/74c13369de117a1bc55ea35c99fdf065.png)
需要点击启动,来启动虚拟机
![image-20220114180002325](https://img-blog.csdnimg.cn/img_convert/308300379a11ac563e77b4e4a6af92da.png)
你可以在Hyper-V管理中修改物理资源配置。
安装linux系统
选择安装操作系统
![image-20220114180030598](https://img-blog.csdnimg.cn/img_convert/35858f5d3d8a5eac68262f31cadfb7ff.png)
然后会进行一系列的检查等操作,检查通过后,就会展示可视化界面选择语言
![image-20220114180126714](https://img-blog.csdnimg.cn/img_convert/5691680f63be555fdea1d1b9b7771d5b.png)
然后选择安装位置
![image-20220114180216182](https://img-blog.csdnimg.cn/img_convert/cb2d5866433db6b4738770b4ce35b320.png)
点进去什么都不需要操作,点击完成即可
![image-20220114180353230](https://img-blog.csdnimg.cn/img_convert/5ef062baeab1f562d65cce573d14d96c.png)
然后选择开始安装,这里因为下载的就是最小的包,安装也是默认最小安装即可
![image-20220114180445704](https://img-blog.csdnimg.cn/img_convert/ca00c9e60be8732eb8a5ff5a52e7e7de.png)
在安装的过程中,设置root密码
![image-20220114180524331](https://img-blog.csdnimg.cn/img_convert/31fa704d80fbbe4b7bb483537a8b4614.png)
为了简单,设置为123456,简单密码需要点击两次完成
![image-20220114180559237](https://img-blog.csdnimg.cn/img_convert/6c6ce15bd2857b77ddc6cea71aff1e97.png)
![image-20220114180626698](https://img-blog.csdnimg.cn/img_convert/b07fdf3c0f3d12f1a088a3b2e2d8b44d.png)
然后等待安装完成即可
![image-20220114180655759](https://img-blog.csdnimg.cn/img_convert/babb7215769a16d0e1888d3f47fe18e7.png)
安装完成重启就好了
![image-20220114182415914](https://img-blog.csdnimg.cn/img_convert/1b717145fab696a2784f548e851874ed.png)
重启后就进入系统了
![image-20220114182509219](https://img-blog.csdnimg.cn/img_convert/6b501f7035216eda1b661032380d35c0.png)
登录root,即可进入
![image-20220114182544644](https://img-blog.csdnimg.cn/img_convert/f041ea91fdaba2058b5a409e5331a0cb.png)
别忘记安装完操作系统后,把挂载的操作系统镜像弹出,要不然每次启动虚拟机,都会挂载,也会影响启动速度。
取消挂载是在设置中,移除硬件资源即可(需要在虚拟机关机的状态下修改配置)
工具连接
首先新增虚拟交换机
![image-20220114235650681](https://img-blog.csdnimg.cn/img_convert/a53aa5dfef74e6debc5b1641ad812928.png)
记得是创建内部类型的交换机
![image-20220114204405361](https://img-blog.csdnimg.cn/img_convert/4b7939e0e62195b319bc34613ecfc19c.png)
设置名字后确定
![image-20220114204502379](https://img-blog.csdnimg.cn/img_convert/1285400bb04a21fa052f880692d15370.png)
此时打开物理机的网络适配器
![image-20220114204607041](https://img-blog.csdnimg.cn/img_convert/5cd7746028af1b6b84bba24dd213758c.png)
![image-20220114204619443](https://img-blog.csdnimg.cn/img_convert/7c193655b849c4983fff73d34e964a86.png)
找到物理机使用的网卡
![image-20220114235757778](https://img-blog.csdnimg.cn/img_convert/da5c6ac5f373187dfe20b9645619edc8.png)
打开【属性】-【共享】,选择刚才创建的网卡共享网络
![image-20220114235840889](https://img-blog.csdnimg.cn/img_convert/7fe63652d12ed24a1eeec417c47adc0d.png)
中间会提示一些信息,不管他确定。
确定之后,被共享的网卡的ip为192.168.137.1,这里千万不要修改。网上很多说这里可以随便写,你随便写了,就会导致虚拟机无法链接外网。这是windows定的一个nat转换地址,当然修改注册表可以修改这个ip,不过我没有尝试。
![image-20220115000058241](https://img-blog.csdnimg.cn/img_convert/7317aad196946559e2b8692693705b4c.png)
除此之外,还需要关闭硬件网卡的一些校验和检测,用于增加网卡性能【属性】-【配置】-【高级】下的IPv4校验和关闭,TCP硬件校验和关闭,UDP硬件校验和关闭。
![image-20220115000301035](https://img-blog.csdnimg.cn/img_convert/48d05a336d39b5a59071b8167132958a.png)
物理机设置完了,还需要设置虚拟机
使用root登录虚拟机,然后切换到/etc/sysconfig/network-scrpts/
目录下,然后使用vi ifcfg-eth0
编辑网络配置。
主要修改这些内容
BOOTPROTO=static
ONBOOT=yes
IPADDR=192.168.137.101 # 网址可以随便写,但是需要是在192.168.137.1网段下的
NETMASK=255.255.255.0
GATEWAY=192.168.137.1
DNS1=114.114.114.114
DNS2=8.8.8.8
修改完保存退出
![image-20220115000720035](https://img-blog.csdnimg.cn/img_convert/2c8cbf15b031f7fb82da6d83ae66487b.png)
然后使用systemctl restart network
重启网络服务
然后使用ip addr
查看网络信息
![image-20220115000805177](https://img-blog.csdnimg.cn/img_convert/c2bf33f9808ed4c06b67543c2233b825.png)
接着使用ping www.baidu.com
验证外网是否可用
![image-20220115000856760](https://img-blog.csdnimg.cn/img_convert/142560f46714a958f84670be410e1a7c.png)
然后在物理机上使用cmd的命令ping 192.168.137.101
ping虚拟机
![image-20220115000938581](https://img-blog.csdnimg.cn/img_convert/da2a8bbd7814adbed6fabc41c823020c.png)
需要注意的是,这里一个单向的交换机,物理机可以ping虚拟机,但是虚拟机不能ping物理机。
最后我们使用xshell工具连接虚拟机(虚拟机连接是可以关闭的,关闭虚拟机连接后,虚拟机还是启动的)
![image-20220115001111089](https://img-blog.csdnimg.cn/img_convert/b6ae5d374c6f0f3af814d8516973ed7c.png)
xshell可以在XSHELL - NetSarang Website下载,我们选择家庭和学校的免费版。填写你的邮箱后,会给你的邮箱发送下载链接,下载即可。(下载比较慢,科学上网能快点)
下载安装后打开,连接虚拟机
![image-20220115001242114](https://img-blog.csdnimg.cn/img_convert/792022b1826175ea288c1789ef5849f0.png)
多个虚拟机
我自己的笔记本是16G内存的,而且初始分配的硬盘比较小(因为随时可以调整)
所以我打算创建3个虚拟机,一起启动,做到虚拟机互联,访问外网,固定ip,物理机使用xshell访问三个虚拟机。
首先创建3个虚拟机,配置都相同分别为hadoop01,hadoop02,hadoop03
这些虚拟机使用同一个网络交换机hadoop
安装操作系统的时候,设置root密码也相同123456
(我之前创建的时候ip错位了,101给了hadoop02,可以重新设置ip,给hadoop02设置102,hadoop01设置101,hadoop03设置103)
在设置网络的使用,同时设置主机名vi /etc/hostname
,设置完成后将主机名进行映射,不仅仅映射自己,还需要映射其他的虚拟机。
vi /etc/hosts
![image-20220115005830124](https://img-blog.csdnimg.cn/img_convert/0cb05faf2e13f27654fbef9a249dabd5.png)
全部虚拟机这样配置后,就可以相互访问了
![image-20220115010053710](https://img-blog.csdnimg.cn/img_convert/38d1a5413a2530418b936b3c15e740b2.png)
![image-20220115010116396](https://img-blog.csdnimg.cn/img_convert/8e6288a0832c6c8a71e122b12ef5011a.png)
![image-20220115010142714](https://img-blog.csdnimg.cn/img_convert/b88e925c5cb6cf0e5325921afe61756e.png)
总结
总的来说还是很不错的,除了网络设置有点难度(其实也不算难,主要是网上的资料不正确),其他的都比较容易。
而且占用的资源也算还行,并不是很大。
![image-20220115010318970](https://img-blog.csdnimg.cn/img_convert/82301b4848071bddc8930596106cdc20.png)
自己学习啥的,也够折腾了。
== 2022-01-22 更新==
直接使用物理机的网卡–解决虚拟机网络慢的问题
之前使用内网的网络交换设备,然后将物理机外网网卡共享给内网的网络交换设备,虽然也能使用,但是存在一个非常大的问题:网络慢。
这个问题真的好恶心,物理机使用xshell连接虚拟机,都需要将近2分钟才能连上。
这也太慢了吧。
想到物理网卡可以共享网络给内网网络交换机,那么为什么不让虚拟机直接使用物理网卡呢?
说干就干。
首先在hyper-v的网络交换设置中增加物理网卡的虚拟网卡,其实也是虚拟网卡,只是对外的
![在这里插入图片描述](https://img-blog.csdnimg.cn/c739bb37ded64ee0b6492eda7a8e04f9.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAYTE4NzkyNzIxODMx,size_20,color_FFFFFF,t_70,g_se,x_16)
然后给虚拟机更换为这个网络交换机
![在这里插入图片描述](https://img-blog.csdnimg.cn/ed86bfcec92d4430bd4f4a995e6dfa4c.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAYTE4NzkyNzIxODMx,size_20,color_FFFFFF,t_70,g_se,x_16)
然后重新启动虚拟机,使用hyper-v自己的连接窗口连接虚拟机,此时之前可用的xshell的连接可能已经无效了,无法连接了,所以需要使用hyper-v自己的连接工具。
连接上后需要修改ip地址,修改为和物理机相同网段的地址
![在这里插入图片描述](https://img-blog.csdnimg.cn/20412623674d41c1b2747ebf03a799ca.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAYTE4NzkyNzIxODMx,size_10,color_FFFFFF,t_70,g_se,x_16)
然后修改/etc/hosts
文件,以及物理机的hosts文件。
理论上物理机的hosts不用修改,虚拟机的也不用修改,因为都是同一个网段,使用同一个网卡,连接的同一个路由器。
如果无法互通,那么配置/etc/hsots
可能是一个解决方案。
刚配置上,网速还是挺快的
不知道会不会和虚拟内网一样,用一段时间就变慢了。
![在这里插入图片描述](https://img-blog.csdnimg.cn/47a0921e9a98469f895239a5ff2d78b7.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAYTE4NzkyNzIxODMx,size_15,color_FFFFFF,t_70,g_se,x_16)