使用Virtual Box和Vagrant虚拟环境搭建vpp测试场景
目的:在虚拟环境中,运行多个vpp实例并将它们相互连接以形成拓扑
- 安装Virtual Box
for centos
https://wiki.centos.org/HowTos/Virtualization/VirtualBox
cd /etc/yum.repos.d
wget http://download.virtualbox.org/virtualbox/rpm/rhel/virtualbox.repo
yum --enablerepo=epel install dkms
yum install VirtualBox-5.2
usermod -a -G vboxusers ych
for ubuntu
sudo apt-get install virtualbox
- 安装vagrant
for centos
yum -y install https://releases.hashicorp.com/vagrant/2.1.2/vagrant_2.1.2_x86_64.rpm
for ubuntu
sudo apt-get install https://releases.hashicorp.com/vagrant/2.1.2/vagrant_2.1.2_x86_64.deb
- 创建Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
config.vm.box = "puppetlabs/ubuntu-16.04-64-nocm"
config.vm.box_check_update = false
vmcpu=(ENV['VPP_VAGRANT_VMCPU'] || 2)
vmram=(ENV['VPP_VAGRANT_VMRAM'] || 4096)
config.ssh.forward_agent = true
config.vm.provider "virtualbox" do |vb|
vb.customize ["modifyvm", :id, "--ioapic", "on"]
vb.memory = "#{vmram}"
vb.cpus = "#{vmcpu}"
#support for the SSE4.x instruction is required in some versions of VB.
vb.customize ["setextradata", :id, "VBoxInternal/CPUM/SSE4.1", "1"]
vb.customize ["setextradata", :id, "VBoxInternal/CPUM/SSE4.2", "1"]
end
end
-
下载box
默认vagrant up的时候,vagrant检测到本地没有box的话,会自动去官网下载,
但是下载太慢了,可以自己用下载软件下载:
https://vagrantcloud.com/puppetlabs/boxes/ubuntu-16.04-64-nocm/versions/1.0.0/providers/virtualbox.box -
添加box
vagrant box add puppetlabs/ubuntu-16.04-64-nocm virtualbox.box
- 运行vagrant
vagrant up
[ych@localhost vpp-tutorial]$ vagrant up
/opt/vagrant/embedded/gems/2.2.4/gems/vagrant-2.2.4/lib/vagrant/util/which.rb:37: warning: Insecure world writable dir /home/ych in PATH, mode 040777
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'puppetlabs/ubuntu-16.04-64-nocm'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: vpp-tutorial_default_1553474751917_85362
Vagrant is currently configured to create VirtualBox synced folders with
the `SharedFoldersEnableSymlinksCreate` option enabled. If the Vagrant
guest is not trusted, you may want to disable this option. For more
information on this option, please refer to the VirtualBox manual:
https://www.virtualbox.org/manual/ch04.html#sharedfolders
This option can be disabled globally with an environment variable:
VAGRANT_DISABLE_VBOXSYMLINKCREATE=1
or on a per folder basis within the Vagrantfile:
config.vm.synced_folder '/host/path', '/guest/path', SharedFoldersEnableSymlinksCreate: false
==> 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: Running 'pre-boot' VM customizations...
==> 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:
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: 5.0.20
default: VirtualBox Version: 5.2
==> default: Mounting shared folders...
default: /vagrant => /home/ych/vpp-tutorial
- 访问ssh
vagrant ssh
[ych@localhost vpp-tutorial]$ vagrant ssh
/opt/vagrant/embedded/gems/2.2.4/gems/vagrant-2.2.4/lib/vagrant/util/which.rb:37: warning: Insecure world writable dir /home/ych in PATH, mode 040777
Welcome to Ubuntu 16.04 LTS (GNU/Linux 4.4.0-21-generic x86_64)
* Documentation: https://help.ubuntu.com/
New release '18.04.2 LTS' available.
Run 'do-release-upgrade' to upgrade to it.
- 用Vagrant设置VPP环境
$ vagrant up
$ vagrant ssh
$ sudo apt-get update
$ sudo reboot -n
$ # Wait for the VM to reboot
$ vagrant ssh
- 在vm里面安装vpp
增加vpp的apt源并安装vpp
sudo bash
echo "deb [trusted=yes] https://nexus.fd.io/content/repositories/fd.io.ubuntu.xenial.main/ ./" > /etc/apt/sources.list.d/99fd.io.list
apt-get update
apt-get install vpp-lib vpp vpp-plugins
停止vpp默认安装的vpp
service vpp stop
- 在vm里面创建一对veth接口,作为vpp的测试接口,互ping测试
sudo ip link add name vpp1out type veth peer name vpp1host
vpp1out是需要加到vpp的虚拟网口,vpp1host是在vm里面的虚拟网口,他们两个是一对背靠背的网口.
sudo ip link set dev vpp1out up
sudo ip link set dev vpp1host up
sudo ip addr add 10.10.1.1/24 dev vpp1host
进入vpp命令行
sudo vppctl -s /run/vpp/cli-vpp1.sock
vpp# create host-interface name vpp1out
host-vpp1out
vpp# show hardware
Name Idx Link Hardware
host-vpp1out 1 up host-vpp1out
Ethernet address 02:fe:d9:75:d5:b4
Linux PACKET socket interface
local0 0 down local0
local
vpp# set int state host-vpp1out up
vpp# set int ip address host-vpp1out 10.10.1.2/24
vpp# show int addr
host-vpp1out (up):
L3 10.10.1.2/24
local0 (dn):
使用vpp1out拼vpp1host
vpp# ping 10.10.1.1
116 bytes from 10.10.1.1: icmp_seq=1 ttl=64 time=13.9565 ms
116 bytes from 10.10.1.1: icmp_seq=2 ttl=64 time=113.2802 ms