谷粒商城-全栈-01 使用 Vagrant 快速搭建 Linux 虚拟机
使用SpringBoot+Vue+element-ui+逆向工程搭建全套后台管理系统,基于Docker环境,通过前后分离方式,以商品系统为例,进行微服务全栈开发。另,本博文是尚硅谷开发的惊天大作谷粒商城实战教程的学习笔记。
一、下载VirtualBox
先下载 VirtualBox 虚拟机到本地安装。
二、下载 Vagrant
到vagrant官网vagrant官网下载vagrant软件 ,根据自己的操作系统选择。
安装成功后,用vagrant打印以下是否安装OK:
➜ ~ vagrant
Usage: vagrant [options] <command> [<args>]
-v, --version Print the version and exit.
-h, --help Print this help.
可以看到该命令已经有了。
三、Vagrant安装Linux镜像
vagrant init centos/7
可以使用该命令给 virtualbox 中安装 centos7 系统。
在当前安装路径下,修改 Vagrantfile
配置
# -*- mode: ruby -*-
# vi: set ft=ruby :
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.
# Every Vagrant development environment requires a box. You can search for
# boxes at https://vagrantcloud.com/search.
config.vm.box = "centos/7"
# Disable automatic box update checking. If you disable this, then
# boxes will only be checked for updates when the user runs
# `vagrant box outdated`. This is not recommended.
# config.vm.box_check_update = false
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
# NOTE: This will enable public access to the opened port
# config.vm.network "forwarded_port", guest: 80, host: 8080
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine and only allow access
# via 127.0.0.1 to disable public access
# config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"
# Create a private network, which allows host-only access to the machine
# using a specific IP.
# config.vm.network "private_network", ip: "192.168.33.10"
# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
config.vm.network "public_network"
# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
# config.vm.synced_folder "../data", "/vagrant_data"
# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
# Example for VirtualBox:
#
# config.vm.provider "virtualbox" do |vb|
# # Display the VirtualBox GUI when booting the machine
# vb.gui = true
#
# # Customize the amount of memory on the VM:
# vb.memory = "1024"
# end
config.vm.provider "virtualbox" do |vb|
vb.memory = "2048"
vb.cpus=2
vb.name="docker-centos7"
end
#
# View the documentation for the provider you are using for more
# information on available options.
# Enable provisioning with a shell script. Additional provisioners such as
# Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
# documentation for more information about their specific syntax and use.
# config.vm.provision "shell", inline: <<-SHELL
# apt-get update
# apt-get install -y apache2
# SHELL
end
启动虚拟机,启动过程会下载 virtualbox.box
,为避免长时间等待,可以使用迅雷下载或者直接从百度网盘下载到本地,然后再安装。
virtualbox.box百度网盘:https://pan.baidu.com/s/1EBrysc13fd0qf9EbT5rKdw 提取码:1dk4
添加本地centos/7镜像
1) 、找到下载好的 virtualbox.box
文件目录
使用 vagrant box add centos/7 本地镜像地址
添加vagrant镜像
➜ ~ vagrant box add centos/7 /Users/kaiyiwang/javaweb/guli/enviroment/virtualbox.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'centos/7' (v0) for provider:
box: Unpacking necessary files from: file:///Users/kaiyiwang/javaweb/guli/enviroment/virtualbox.box
==> box: Successfully added box 'centos/7' (v0) for 'virtualbox'!
2)、使用vagrant box list查询镜像
> vagrant box list
centos/7 (virtualbox, 0)
laravel/homestead (virtualbox, 0.6.0)
3)、启动虚拟机,这时候会在 virtual box 中会自动添加一个虚拟机
> vagrant up
启动打印:
➜ ~ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos/7'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: kaiyiwang_default_1596874530186_26782
==> 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: in which case you may ignore this message.
==> default: Rsyncing folder: /Users/kaiyiwang/ => /vagrant
四、访问CentOS
创建好了CentOS之后,我们如何访问呢 ?
# 默认的用户名为:vagrant
vagrant ssh
以后访问只需要使用命令即可启动该虚拟机:
> vagrant up
重启虚拟机:
> vagrant reload
五、虚拟机网络设置
默认虚拟机的 ip 地址不是固定 ip,开发非常不方便:
修改 Vagrantfile 文件,在修改之前,我们先看下物理机的网卡信息:
# ipconfig # windows系统
ifconfig
打印:
vboxnet0: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
ether 0a:00:27:00:00:00
inet 192.168.10.1 netmask 0xffffff00 broadcast 192.168.10.255
我们可以看到物理机虚拟网卡的IP为:192.168.10.1
, 那么我们 Vagrantfile 文件中的私有网路的IP就应该为 192.168.10.x
,需要在同一个局域网内,这里我直接写为:192.168.10.10
Vagrantfile文件修改:
# 去掉私有网络的注释,ip改为192.168.10.10
config.vm.network "private_network", ip: "192.168.10.10"
重启虚拟机:
vagrant reload
验证:
我们进入到虚拟机,然后看下网络地址:
[vagrant@localhost ~]$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
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: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 52:54:00:8a:fe:e6 brd ff:ff:ff:ff:ff:ff
inet 10.0.2.15/24 brd 10.0.2.255 scope global noprefixroute dynamic eth0
valid_lft 86260sec preferred_lft 86260sec
inet6 fe80::5054:ff:fe8a:fee6/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:05:87:39 brd ff:ff:ff:ff:ff:ff
inet 192.168.10.10/24 brd 192.168.10.255 scope global eth1
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fe05:8739/64 scope link
valid_lft forever preferred_lft forever
可以看到已经起作用了,地址为:192.168.10.10
然后在物理机ping 虚拟机的地址,看是否ping的通:
➜ ~ ping 192.168.10.10
PING 192.168.10.10 (192.168.10.10): 56 data bytes
64 bytes from 192.168.10.10: icmp_seq=0 ttl=64 time=0.491 ms
可以看到ping的通。
然后可以在虚拟机内部ping物理机的IP
[vagrant@localhost ~]$ ping 192.168.1.101
PING 192.168.1.101 (192.168.1.101) 56(84) bytes of data.
64 bytes from 192.168.1.101: icmp_seq=1 ttl=63 time=0.377 ms
64 bytes from 192.168.1.101: icmp_seq=2 ttl=63 time=0.346 ms
64 bytes from 192.168.1.101: icmp_seq=3 ttl=63 time=0.792 ms
虚拟机和物理机可以互相ping的通,说明我们的网络设置OK了。
六、设置密码登录
默认只允许 ssh 登录方式,为了后来方便操作,文件上传等,我们可以配置允许账号密码登录虚拟机。
Vagrant ssh 进去系统之后
vi /etc/ssh/sshd_config
修改 PasswordAuthentication no/yes # 将 no改为yes
重启服务 service sshd restart
以后可以使用提供的 ssh 连接工具来连接了。
七、设置其他配置
1、设置虚拟机网关和DNS
> cd /etc/sysconfig/network-scripts
> ip addr # 查看虚拟机连接的网卡,我们的网卡使用的是 eth1
> vi ifcfg-eth1
设置网关和DNS1,DNS2
#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
NM_CONTROLLED=no
BOOTPROTO=none
ONBOOT=yes
IPADDR=192.168.10.10
NETMASK=255.255.255.0
#set gateway and DNS(ADD)
#GATEWAY=192.168.10.1 #暂时不用
#DNS1=114.114.114.114 #暂时不用
#DNS2=8.8.8.8 #暂时不用
DEVICE=eth1
PEERDNS=no
#VAGRANT-END
重启网卡:
service network restart
2、修改yum源
由于默认的官方源在国外,国外访问比较慢,所以需要把镜像源切换为国内的,这里使用网易的源。
1)、备份原 yum 源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
2)、使用新 yum 源
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
3)、生成缓存
yum makecache
相关文章:
环境篇:Virtualbox+Vagrant安装Centos7
virtualbox+vagrant环境安装docker
为者常成,行者常至
自由转载-非商用-非衍生-保持署名(创意共享3.0许可证)