前言
记录一次 vagrant 虚拟机创建过程.
宿主机为 Windows 10 64位
虚拟机镜像为 ubuntu 16.04 xenial64 xenial-server-cloudimg-amd64
资料
vagrant 网址:
https://www.vagrantup.com/
离线box地址:
http://www.vagrantbox.es/
过程
虚拟套装(win7不可用):
下载安装 virtualbox :
https://download.virtualbox.org/virtualbox/6.0.10/VirtualBox-6.0.10-132072-Win.exe
下载安装 vagrant: https://releases.hashicorp.com/vagrant/2.2.5/vagrant_2.2.5_x86_64.msi
下载BOX
ubuntu 16.04: https://cloud-images.ubuntu.com/xenial/current/xenial-server-cloudimg-amd64-vagrant.box
#添加box
$ vagrant box add ubuntu_xenial64 xenial-server-cloudimg-amd64-vagrant.box
#新增配置文件 vagrantfile
```
# -*- mode: ruby -*-
# vi: set ft=ruby :
ENV["LC_ALL"] = "en_US.UTF-8"
Vagrant.configure("2") do |config|
config.vm.define "node10" do |node10|
node10.vm.box = "ubuntu_xenial64"
node10.vm.network "private_network", ip: "192.168.2.10"
end
config.vm.hostname = "node10"
config.vm.synced_folder "E:/VM/vagrant_share", "/vagrant_share"
end
```
#安装启动虚拟机
$ vagrant up
#登陆虚拟机
$ vagrant ssh
#新增账户
$ sudo useradd -m oniong -s /bin/bash
$ sudo passwd oniong
#sudo提权
$ vagrant@node10:~$ sudo chmod u+w /etc/sudoers
$ vagrant@node10:~$ sudo vim /etc/sudoers
oniong ALL=(ALL) NOPASSWD: ALL
$ sudo chmod u-w /etc/sudoers
#免密登陆
$ ssh-keygen -t rsa
将宿主机的公钥添加到 oniong home .ssh/authorized_keys 中
$ chmod 600 authorized_keys
#ubuntu 镜像加速
https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/
#docker 镜像加速
https://mirrors.tuna.tsinghua.edu.cn/help/docker-ce/
https://docs.docker.com/install/linux/docker-ce/ubuntu/
$ sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
$ sudo add-apt-repository \ "deb [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/ubuntu \ $(lsb_release -cs) \ stable"
$ sudo apt update
$ sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ gnupg-agent \ software-properties-common
$ sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose
#docker hub 镜像加速
https://www.daocloud.io/mirror
$ curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io
$ sudo systemctl restart docker.service
$ sudo docker run hello-world
$ sudo groupadd docker
$ sudo usermod -aG docker oniong
#注销再登陆
#安装openjdk8
$ sudo apt-get install openjdk-8-jdk-headless
$ java -version
#结束
后续操作
#在宿主机上重新打包配置好的虚拟机
$ vagrant package --output ubuntu_16_04_simple.box
#创建另一个文件夹配置启动三个此虚拟机
$ mkdir nodes && cd nodes
$ vagrant box add ubuntu_16_04_simple ubuntu_16_04_simple.box
$ vim vrgrantfile
```
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.define "node11" do |node11|
node11.vm.box = "ubuntu_16_04_simple"
node11.vm.network "private_network", ip: "192.168.2.11"
node11.vm.hostname = "node11"
end
config.vm.define "node12" do |node12|
node12.vm.box = "ubuntu_16_04_simple"
node12.vm.network "private_network", ip: "192.168.2.12"
node12.vm.hostname = "node12"
end
config.vm.define "node13" do |node13|
node13.vm.box = "ubuntu_16_04_simple"
node13.vm.network "private_network", ip: "192.168.2.13"
node13.vm.hostname = "node13"
end
config.vm.synced_folder "E:/VM/vagrant_share", "/vagrant_share"
end
```
$ vagrant up
#然后使用 oniong 账号登陆这三个虚拟机
完工