一、实现功能
使用vagrant构建两台centos7的虚拟机,从而对后续研究的多台虚拟机之间的网络交互提供基础。
二、实现步骤
1.构建Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.require_version ">= 1.6.0"
boxes = [
{
:name => "docker-node1",
:eth1 => "192.168.205.10",
:mem => "1024",
:cpu => "1"
},
{
:name => "docker-node2",
:eth1 => "192.168.205.11",
:mem => "1024",
:cpu => "1"
}
]
Vagrant.configure(2) do |config|
config.vm.box = "centos/7"
boxes.each do |opts|
config.vm.define opts[:name] do |config|
config.vm.hostname = opts[:name]
config.vm.provider "vmware_fusion" do |v|
v.vmx["memsize"] = opts[:mem]
v.vmx["numvcpus"] = opts[:cpu]
end
config.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--memory", opts[:mem]]
v.customize ["modifyvm", :id, "--cpus", opts[:cpu]]
end
config.vm.network :private_network, ip: opts[:eth1]
end
end
End
2.进入Vagrantfile所在目录,启动虚拟机(如果本地没有镜像,则会去远程仓库下载)
#vagrant up
Bringing machine 'docker-node1' up with 'virtualbox' provider...
Bringing machine 'docker-node2' up with 'virtualbox' provider...
==> docker-node1: Checking if box 'centos/7' version '1902.01' is up to date...
==> docker-node1: Unpausing the VM...
==> docker-node2: Importing base box 'centos/7'...
==> docker-node2: Matching MAC address for NAT networking...
==> docker-node2: Checking if box 'centos/7' version '1902.01' is up to date...
==> docker-node2: Setting the name of the VM: chapter4_docker-node2_1561417209987_57420
==> docker-node2: Fixed port collision for 22 => 2222. Now on port 2201.
==> docker-node2: Clearing any previously set network interfaces...
==> docker-node2: Preparing network interfaces based on configuration...
docker-node2: Adapter 1: nat
docker-node2: Adapter 2: hostonly
==> docker-node2: Forwarding ports...
docker-node2: 22 (guest) => 2201 (host) (adapter 1)
==> docker-node2: Running 'pre-boot' VM customizations...
==> docker-node2: Booting VM...
==> docker-node2: Waiting for machine to boot. This may take a few minutes...
docker-node2: SSH address: 127.0.0.1:2201
docker-node2: SSH username: vagrant
docker-node2: SSH auth method: private key
docker-node2:
docker-node2: Vagrant insecure key detected. Vagrant will automatically replace
docker-node2: this with a newly generated keypair for better security.
docker-node2:
docker-node2: Inserting generated public key within guest...
docker-node2: Removing insecure key from the guest if it's present...
docker-node2: Key inserted! Disconnecting and reconnecting using new SSH key...
==> docker-node2: Machine booted and ready!
==> docker-node2: Checking for guest additions in VM...
docker-node2: No guest additions were detected on the base box for this VM! Guest
docker-node2: additions are required for forwarded ports, shared folders, host only
docker-node2: networking, and more. If SSH fails on this machine, please install
docker-node2: the guest additions and repackage the box to continue.
docker-node2:
docker-node2: This is not an error message; everything may continue to work properly,
docker-node2: in which case you may ignore this message.
==> docker-node2: Setting hostname...
==> docker-node2: Configuring and enabling network interfaces...
==> docker-node2: Rsyncing folder: /cygdrive/f/BGLearn/10docker/docker-k8s-devops-master-9287a2ca56433ca076078b564de9488df81b40be/chapter4/ => /vagrant
3.查看vagrant状态
#vagrant status
Current machine states:
docker-node1 running (virtualbox)
docker-node2 running (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`.
4.连接其中任一个虚拟机
#vagrant ssh docker-node1
[vagrant@docker-node1 ~]$
5.针对两个虚拟机分别安装docker
#/bin/sh
# install some tools
sudo yum install -y git vim gcc glibc-static telnet bridge-utils
# install docker
curl -fsSL get.docker.com -o get-docker.sh
sh get-docker.sh
# start docker service
sudo groupadd docker
sudo usermod -aG docker vagrant
sudo systemctl start docker
rm -rf get-docker.sh
6.查看docker版本
[vagrant@docker-node1 ~]$ docker version
Client:
Version: 18.09.6
API version: 1.39
Go version: go1.10.8
Git commit: 481bc77156
Built: Sat May 4 02:34:58 2019
OS/Arch: linux/amd64
Experimental: false
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.39/version: dial unix /var/run/docker.sock: connect: permission denied