Chinaunix首页 | 论坛 | 博客
  • 博客访问: 46501
  • 博文数量: 30
  • 博客积分: 510
  • 博客等级: 下士
  • 技术积分: 260
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-06 08:56
文章存档

2012年(30)

我的朋友

分类: 虚拟化

2012-05-06 09:01:07

Build Base Box Vagrant Ubuntu Oneiric 11.10 Server

Oct 26th, 2011

Virtual Machine (VM) is good solution to handle several different environment of applications. All my server working on Ubuntu Server with different version. At this point, virtualenv and RVM still doesn’t enough for me.

So I use Virtualbox and install several Ubuntu Server version. Then I hear about Vagrant which a great tools for managing Virtualbox VM. Using Vagrant, we can run VM without execute Virtualbox because it running on background. This is what i want!

I will explain my steps bundling Ubuntu 11.10 into Vagrant box and run it.

1. Create Ubuntu 11.10 VM on Virtualbox

Create new VM with name “vagrant-oneiric” and start install Ubuntu Server 11.10. In this steps, I disable USB and Audio which not used in Server. Also, I set Video size to 4MB only which server doesn’t run GUI.

2. Installing Ubuntu 11.10

Because we will use Vagrant, then we should follow standard convention here. Make sure you setup this :

hostname : vagrant username : vagrant password : vagrant

Also, you must install OpenSSH-Server packages.

3. Configure Ubuntu 11.10

After installation, boot and login into your new server. We need to setup several things here :

Edit sudoers by :sudo visudo %admin ALL=NOPASSWD: ALL sudo service sudo restart

Save and exit the editor. Now we run this command:

echo 'echo $PATH' | sudo sh

Copy it result and run “sudo visudo” again. Change Defaults env_reset into Defaults env_reset, secure_path="

Defaults env_reset, secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/X11R6/bin:/var/lib/gems/1.8/bin"

This is mine if you not sure what to do :

# # This file MUST be edited with the 'visudo' command as root. # # Please consider adding local content in /etc/sudoers.d/ instead of # directly modifying this file. # # See the man page for details on how to write a sudoers file. # Defaults env_reset, secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/X11R6/bin" # Host alias specification # User alias specification # Cmnd alias specification # User privilege specification root ALL=(ALL:ALL) ALL # Members of the admin group may gain root privileges #%admin ALL=(ALL) ALL %admin ALL=NOPASSWD: ALL # Allow members of group sudo to execute any command %sudo ALL=(ALL:ALL) ALL Using Vagrant Public Key :mkdir ~/.ssh/ cd ~/.ssh wget wget .pub mv vagrant.pub authorized_keys

In case you want to use your private key. This simply requires copying a public key into ~/.ssh/authorized_keys.

Installing Virtualbox Guest Addition for shared folder integrationsudo apt-get install linux-headers-$(uname -r) build-essential sudo mkdir /tmp/cdrom sudo mount /dev/cdrom /tmp/cdrom sudo su cd /tmp/cdrom ./VBoxLinuxAdditions.run

If all things done, now you can turn off your VM.

4. Build Vagrant base box Ubuntu Oneiric 11.10

I use Vagrant folder in my home path for deploy and manage my VM.

mkdir ~/Vagrant && cd ~/Vagrant

Now we are build base box by :

vagrant package --base vagrant-oneiric package.box

And you will got result like this :

[vagrant] Creating home directory since it doesn't exist: /home/ubuntu/.vagrant.d [vagrant] Creating home directory since it doesn't exist: /home/ubuntu/.vagrant.d/tmp [vagrant] Creating home directory since it doesn't exist: /home/ubuntu/.vagrant.d/boxes [vagrant] Creating home directory since it doesn't exist: /home/ubuntu/.vagrant.d/logs [vagrant] Clearing any previously set forwarded ports... [vagrant] Creating temporary directory for export... [vagrant] Exporting VM... Progress: 0%Progress: 1%Progress: 2%Progress: 3%Progress: 4%Progress: 5%Progress:

Then we add our base box by :

vagrant box add vagrant-oneiric package.box

You will get result like this :

[vagrant] Downloading with Vagrant::Downloaders::File... [vagrant] Copying box to temporary location... [vagrant] Extracting box... [vagrant] Verifying box... [vagrant] Cleaning up downloaded box... 5. Running your Vagrant base box

We should define our vagrant project by “vagrant init”.

vagrant init

Edit Vagrantfile and change config.vm.box :

config.vm.box = "vagrant-oneiric"

Now we ready to start and login into our Vagrant box by :

vagrant up vagrant ssh Running Multiple VM

This is the real power of Vagrant. We can defined multiple VM on single box. In this case, i need 3 server called dev, stagging and production.

So i create Vagrantfile :

Vagrant::Config.run do |config| config.vm.define :dev do |dev_config| dev_config.vm.network("33.33.33.10") dev_config.vm.box = "vagrant-oneiric" config.ssh.forwarded_port_key = "ssh" config.vm.forward_port("ssh", 22, 2222) end config.vm.define :stagging do |stagging_config| stagging_config.vm.network("33.33.33.50") stagging_config.vm.box = "vagrant-oneiric" config.ssh.forwarded_port_key = "ssh" config.vm.forward_port("ssh", 22, 2223) end config.vm.define :production do |production_config| production_config.vm.network("33.33.33.100") production_config.vm.box = "vagrant-oneiric" config.ssh.forwarded_port_key = "ssh" config.vm.forward_port("ssh", 22, 2224) end end

Do vagrant up and it will process 3 server :

[dev] Importing base box 'vagrant-oneiric'... [dev] Preparing host only network... [dev] Creating new host only network for environment... [dev] Matching MAC address for NAT networking... [dev] Clearing any previously set forwarded ports... [dev] Forwarding ports... [dev] -- ssh: 22 => 2222 (adapter 1) [dev] Creating shared folders metadata... [dev] Running any VM customizations... [dev] Booting VM... [dev] Waiting for VM to boot. This can take a few minutes. [dev] VM booted and ready for use! [dev] Enabling host only network... [dev] Mounting shared folders... [dev] -- v-root: /vagrant [stagging] Fixed port collision 'ssh'. Now on port 2200. [stagging] Importing base box 'vagrant-oneiric'... [stagging] Preparing host only network... [stagging] Matching MAC address for NAT networking... [stagging] Clearing any previously set forwarded ports... [stagging] Forwarding ports... [stagging] -- ssh: 22 => 2200 (adapter 1) [stagging] Creating shared folders metadata... [stagging] Running any VM customizations... [stagging] Booting VM... [stagging] Waiting for VM to boot. This can take a few minutes. [stagging] VM booted and ready for use! [stagging] Enabling host only network... [stagging] Mounting shared folders... [stagging] -- v-root: /vagrant [production] Fixed port collision 'ssh'. Now on port 2201. [production] Importing base box 'vagrant-oneiric'... [production] Preparing host only network... [production] Matching MAC address for NAT networking... [production] Clearing any previously set forwarded ports... [production] Forwarding ports... [production] -- ssh: 22 => 2201 (adapter 1) [production] Creating shared folders metadata... [production] Running any VM customizations... [production] Booting VM... [production] Waiting for VM to boot. This can take a few minutes. [production] VM booted and ready for use! [production] Enabling host only network... [production] Mounting shared folders... [production] -- v-root: /vagrant

Now you can ssh into several server with vagrant ssh dev or by port.

I can’t ping into my VM even configured properly through Vagrantfile

This is because bug in Virtualbox. Login into your VM. Delete your current network rules and reboot :

sudo rm -rf /etc/udev/rules.d/70-persistent-net.rules sudo reboot How if i have several problem and need to reset all my Vagrant configuration ?

It simple by doing this :

cd sudo rm -rf ~/.vagrant.d sudo rm -rf .vagrant sudo rm -rf Vagrantfile You want both NAT & host-only adapter independent but not from Vagrantfile ?

Do this if you failed enabling network from Vagrantfile.

So, we need to shutdown our vagrant by :

vagrant suspend vagrant halt

Go to your Virtualbox & you should see new VM called “Vagrant_138923…” or some else like that. Enable 2 network card with NAT and host-only adapter.

Boot your Vagrant by vagrant up and login with vagrant ssh.

Now, delete your current network rules by :

sudo rm -rf /etc/udev/rules.d/70-persistent-net.rules

Because we enable 2 network cards, then we should wake it up on boot. Edit /etc/network/interfaces :

# This file describes the network interfaces available on your system # and how to activate them. For more information, see interfaces(5). # The loopback network interface auto lo iface lo inet loopback # The primary network interface auto eth1 iface eth1 inet dhcp # The primary network interface auto eth0 iface eth0 inet dhcp

Now reboot. Then you will got 2 network card up and can communicate with Host.

Several good reference :

Oct 26th, 2011

阅读(1338) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:su和sudo命令的区别与使用技巧

给主人留下些什么吧!~~