创建vagrant 镜像_vagrant快速搭建centos7

一、安装Vagrant

  1. 访问vagrant官网https://www.vagrantup.com/
  2. 下载对应系统的Vagrant
  3. 安装
  4. 命令行输入vagrant,测试是否安装成功

82531bf2247c0b627071f82c7ad38528.png

二、安装Virtual Box

  1. 访问virtualbox官网https://www.virtualbox.org/
  2. 选择左侧的downloads
  3. 选择下载对应的操作系统版本
  4. 安装
  5. [win10中若出现]安装virtualbox快完成时立即回滚,并提示安装出现严重错误

​ (1)打开服务

​ (2)找到Device Install Service和Device Setup Manager,然后启动

​ (3)再次尝试安装

三、安装Centos7

  1. 创建centos7.2文件夹,并进入其中[目录路径中不要有中文字符]
  2. 在此目录下打开cmd,运行vagrant init centos/7 此时会在当前目录下生成Vagrantfile
PS D:vagrantcentos7.2> vagrant init
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.
  1. 运行vagrant up [注意不要运行,拉取远端的centos7太慢] 此时会找centos7的镜像,本地有就用本地的,本地没有就会拉取远端的
  2. 准备centos7的box
    a. 下载centos box https://github.com/CommanderK5/packer-centos-template/releases/download/0.7.2/vagrant-centos-7.2.box
    b. 将下载好的文件放到指定目录
    c. 运行vagrant box add centos7.2 D:downloadvagrant-centos-7.2.box
PS D:vagrantcentos7.2> vagrant box add centos7.2 D:downloadvagrant-centos-7.2.box --force
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'centos7.2' (v0) for provider:
    box: Unpacking necessary files from: file:///D:/download/vagrant-centos-7.2.box
    box:
==> box: Successfully added box 'centos7.2' (v0) for 'virtualbox'!

d. vagrant box list 查看本地的box[这时候可以看到centos7.2

PS D:vagrantcentos7.2> vagrant box list
centos7.2 (virtualbox, 0)
PS D:vagrantcentos7.2>
  1. 在 centos7.2 文件夹下创建Vagrantfile文件,文件内容如下:
    修改文件中的box名称 ,添加私有IP地址
# -*- 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 = "centos7.2"
  config.disksize.size = '50GB'
  # 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.10.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 = "2048"
   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
  # Ansible, Chef, Docker, Puppet and Salt 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
​
  1. 根据本地的centos7 box创建虚拟机:

​ vagrant up [打开virtual box,可以发现centos7创建成功]

PS D:vagrantcentos7.2> vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos7.2'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: centos72_default_1607175131857_64238
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Resized disk: old 10000 MB, req 51200 MB, new 51200 MB
==> default: You may need to resize the filesystem from within the guest.
==> 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: Warning: Connection aborted. Retrying...
    default: Warning: Connection reset. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
    default: Warning: Connection aborted. Retrying...
    default: Warning: Connection reset. Retrying...
    default: Warning: Remote connection disconnect. Retrying...
    default: Warning: Connection aborted. Retrying...
    default: Warning: Connection reset. Retrying...
    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: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default:
    default: Guest Additions Version: 4.3.30
    default: VirtualBox Version: 6.0
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => D:/vagrant/centos7.2

7.通过vagrant ssh可以进入虚拟机,我们会发现默认情况下,由它生成的虚拟机系统磁盘容量只有8G,这是设计者出于对这个工具的应用场景而设定的。但对于想将其作为开发环境的人员,8G的容量如同鸡肋。

ps:vagrant的centos镜像的初始用户密码 vagrant/vagrant、root/vagrant

PS D:vagrantcentos7.2> vagrant ssh
[vagrant@localhost ~]$ df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root  8.3G  1.5G  6.9G  18% /
devtmpfs                 299M     0  299M   0% /dev
tmpfs                    308M     0  308M   0% /dev/shm
tmpfs                    308M  4.3M  304M   2% /run
tmpfs                    308M     0  308M   0% /sys/fs/cgroup
/dev/sda1                497M  164M  334M  33% /boot
none                     376G   34G  342G  10% /vagrant
tmpfs                     62M     0   62M   0% /run/user/1000

8.安装vagrant-disksize

PS D:vagrantcentos7.2> vagrant plugin install vagrant-disksize
Installing the 'vagrant-disksize' plugin. This can take a few minutes...
Fetching vagrant-disksize-0.1.3.gem
Installed the plugin 'vagrant-disksize (0.1.3)'!

9.修改vagrantfile配置文件

Vagrant.configure('2') do |config|
  config.vm.box = 'centos7.2'
  config.disksize.size = '50GB'
end

10.重启虚拟机,进入虚拟机进行分区扩容

[root@localhost vagrant]# fdisk -l
​
Disk /dev/sda: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0000ca5e
​
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    20479999     9726976   8e  Linux LVM
​
Disk /dev/mapper/centos-root: 8866 MB, 8866758656 bytes, 17317888 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
​
​
Disk /dev/mapper/centos-swap: 1048 MB, 1048576000 bytes, 2048000 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
​
[root@localhost vagrant]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).
​
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
​
​
Command (m for help): p
​
Disk /dev/sda: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0000ca5e
​
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    20479999     9726976   8e  Linux LVM
​
Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p
Partition number (3,4, default 3): 3
First sector (20480000-104857599, default 20480000):
Using default value 20480000
Last sector, +sectors or +size{K,M,G} (20480000-104857599, default 104857599):
Using default value 104857599
Partition 3 of type Linux and of size 40.2 GiB is set
​
Command (m for help): p
​
Disk /dev/sda: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0000ca5e
​
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
/dev/sda2         1026048    20479999     9726976   8e  Linux LVM
/dev/sda3        20480000   104857599    42188800   83  Linux
​
Command (m for help): w
The partition table has been altered!
​
Calling ioctl() to re-read partition table.
​
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

11.刷新分区表

[root@localhost vagrant]# partprobe
[root@localhost vagrant]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   50G  0 disk
├─sda1            8:1    0  500M  0 part /boot
├─sda2            8:2    0  9.3G  0 part
│ ├─centos-root 253:0    0  8.3G  0 lvm  /
│ └─centos-swap 253:1    0 1000M  0 lvm  [SWAP]
└─sda3            8:3    0 40.2G  0 part

12.创建物理卷(pvcreate),添加新的物理卷到卷组centos,增加卷组centos的大小

[root@localhost vagrant]# pvcreate /dev/sda3
  Physical volume "/dev/sda3" successfully created
[root@localhost vagrant]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               9.28 GiB / not usable 3.00 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              2374
  Free PE               10
  Allocated PE          2364
  PV UUID               grTv3L-mzhC-TS8a-z5ln-y6rS-3kJi-qX82hr
​
  "/dev/sda3" is a new physical volume of "40.23 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sda3
  VG Name
  PV Size               40.23 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               ysH18d-R0ML-XDBA-xM7f-rZ1i-qqN8-Pu8X0V
[root@localhost vagrant]# vgextend  centos  /dev/sda3  //添加新的物理卷到卷组centos
  Volume group "centos" successfully extended
[root@localhost vagrant]# lvresize -L +40G /dev/mapper/centos-root //增加卷组centos的大小
  Size of logical volume centos/root changed from 8.26 GiB (2114 extents) to 48.26 GiB (12354 extents).
  Logical volume root successfully resized.

13.刷新逻辑卷centos-root的大小

注意:(1)如果此逻辑卷上的文件系统是EXT3/EXT4类型,需要使用resize2fs工具;(2)如果此逻辑卷上的文件系统是XFS类型,需要使用 xfs_growfs

[root@localhost vagrant]# xfs_growfs  /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=256    agcount=4, agsize=541184 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=2164736, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 2164736 to 12650496
//查看扩容是否成功
[root@localhost vagrant]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   49G  1.5G   47G   3% /
devtmpfs                 911M     0  911M   0% /dev
tmpfs                    921M     0  921M   0% /dev/shm
tmpfs                    921M  8.3M  912M   1% /run
tmpfs                    921M     0  921M   0% /sys/fs/cgroup
/dev/sda1                497M  164M  334M  33% /boot
tmpfs                    185M     0  185M   0% /run/user/1000

四、vagrant基本操作

(1) vagrant ssh 进入刚才创建的centos7中

(2) vagrant status 查看centos7的状态

(3) vagrant halt 停止centos7

(4) vagrant destroy 删除centos7

(5) vagrant status 查看当前vagrant创建的虚拟机

(6) Vagrantfile中也可以写脚本命令,使得centos7更加丰富 但是要注意,修改了Vagrantfile,要想使正常运行的centos7生效,必须使用vagrant reload

五、box的打包分发

  1. 退出虚拟机 vagrant halt
  2. 打包 vagrant package --output first-docker-centos7.box
  3. 得到first-docker-centos7.box
  4. 将first-docker-centos7.box添加到其他的vagrant环境中 vagrant box add first-docker-centos7 first-docker-centos7.box
  5. 得到Vagrantfile vagrant init first-docker-centos7
  6. 根据Vagrantfile启动虚拟机 vagrant up [此时可以得到和之前一模一样的环境,但是网络要重新配置]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值