在使用springcloud在使用虚拟机时,经常需要安装redis,mysq,zookeeper等等,我们可以直接导入已经配置好的springcloud镜像,这样就十分方便了。 我直接安装的是集群,可以只启动一个。 链接:链接:pan.baidu.com/s/1u1p8JBtM… 提取码:47oi
准备工作:安装GitBash (这个应该都有吧) 步骤1:下载好链接,安装 VirtualBox 、vagrant(一路点击,自己修改目录) 步骤2:第二步,添加镜像到 Vagrant:【我的目录是/e/virtual/workcluster】 将springcloud-dev-10.box 和 vagrant-centos-7.2.box 镜像添加到vagrant目录下:
vagrant box add springcloud-dev /e/virtualbox/workcluster/springcloud-dev-10.box
springcloud-dev 表示指定名称,如果使用base,之后可以直接使用
/springcloud-dev-10.box 是box的文件名,这里是本地保存box的路径。也可以是可以下载box的网址,如果是网址的话,Vagrant会自动启动下载。
我使用git 添加失败:
只能以管理员身份打开命令提示
`
vagrant box add centos E:\virtualbox\workcluster\vagrant-centos-7.2.box
vagrant box add springcloud-dev E:\virtualbox\workcluster\springcloud-dev-10.box
`
步骤3:查看镜像 vagrant box list
步骤4: 初始化镜像
vagrant init springcloud-dev
初始化成功显示:
【不用管vagrant-centos-7.2.box 镜像】
步骤5: 修改Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
(1..3).each do |i|
config.vm.define vm_name = "cdh#{i}" do |config|
config.vm.provider "virtualbox" do |v|
# v.customize ["modifyvm", :id, "--name", vm_name, "--memory", "4096",'--cpus', 1]
v.customize ["modifyvm", :id, "--name", vm_name]
# v.customize ["modifyvm", :id, "--memory", "4096"]
v.customize ["modifyvm", :id, "--memory", "8192"]
v.customize ["modifyvm", :id, "--cpus", "2"]
end
config.vm.box = "springcloud-dev"
config.vm.hostname =vm_name
config.ssh.username = "root"
config.ssh.password = "vagrant"
# config.ssh.shell = "powershell"
#config.ssh.shell = "bash -l"
config.vm.network :private_network, ip: "192.168.56.12#{i}"
config.vm.provision :shell, :path => "bootstrap.sh"
end
end
end
上面的文件中定义了三个虚拟机,三个虚拟机的名字和 hostname 分别为cdh一、cdh二、cdh3,网络使用的是 host-only 网络。
步骤6:编写bootstrap.sh 可以先创建一个text文本,然后重命名
#!/usr/bin/env bash
# The output of all these installation steps is noisy. With this utility
# the progress report is nice and concise.
echo "Update /etc/hosts"
cat > /etc/hosts <<EOF
127.0.0.1 localhost
192.168.56.121 cdh1
192.168.56.122 cdh2
192.168.56.123 cdh3
EOF
echo "Disable iptables"
setenforce 0 >/dev/null 2>&1 && iptables -F
### Set env ###
echo "export LC_ALL=en_US.UTF-8" >> /etc/profile
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
步骤7:启动虚拟机
$ vagrant up # 启动全部虚拟机节点
$ vagrant up cdh1 # 启动名字为cdh1的虚拟机节点
Windows 用户注意:Windows 终端并不支持 ssh,所以需要安装第三方 SSH 客户端,比如:Putty、Cygwin 、Git Bash等。
有关初始账户和密码:
| 账户 | 密码 |
|---|---|
| root | vagrant |
推倒重来
在使用的过程中,如果遇到问题,不好解决。 可以直接推倒重来,做好镜像的及时备份即可。
首先使用 vagrant status 查看一下状态
$ vagrant status
Current machine states:
cdh1 running (virtualbox)
cdh2 not created (virtualbox)
cdh3 not created (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`.
然后使用vagrant halt 关机
$ vagrant halt cdh1
==> cdh1: Attempting graceful shutdown of VM...
The configured shell (config.ssh.shell) is invalid and unable
to properly execute commands. The most common cause for this is
using a shell that is unavailable on the system. Please verify
you're using the full path to the shell and that the shell is
executable by the SSH user.
vagrant destroy 销毁虚拟机
$ vagrant destroy
==> cdh3: VM not created. Moving on...
==> cdh2: VM not created. Moving on...
cdh1: Are you sure you want to destroy the 'cdh1' VM? [y/N] y
==> cdh1: Destroying VM and associated drives...
vagrant up cdh1 重新启动
$ vagrant up cdh1
Bringing machine 'cdh1' up with 'virtualbox' provider...
==> cdh1: Importing base box 'springcloud-dev'...
==> cdh1: Matching MAC address for NAT networking...
。。。。。
cdh1: Update /etc/hosts
cdh1: Disable iptables
vagrant up (启动虚拟机)
vagrant halt (关闭虚拟机——对应就是关机)
vagrant suspend (暂停虚拟机——只是暂停,虚拟机内存等信息将以状态文件的方式保存在本地,可以执行恢复操作后继续使用)
vagrant resume (恢复虚拟机 —— 与前面的暂停相对应)
vagrant destroy (删除虚拟机,删除后在当前虚拟机所做进行的除开Vagrantfile中的配置都不会保留)
vagrant reload (重启)