France-Grilles | Université Toulouse 3
Now that you've gone through the process of a simple contextualised VM,
in this section, we'll show you the process that will gives you the ability to launch a VM with external code executed as root during startup.
No network contextualisation In the CloudMIP platform, the contextualisation of the network has been disabled on all images. This stems from the fact that our DHCP server will give you all of the required network parameters enabling your VM Internet & Local access without hassle about NIC configuration.
Therefore, we'll now step through the following items:
*yes, contextualisation is an initd service launched at startup … you may have a look at the /etc/init.d/vmcontext script within your VM.
Prior to registering CONTEXT files within OpenNebula, you first need to create a startup script.
To ease this step, we provide you the following script that is versatile enough to handle most case of archived* CONTEXT files:
#!/bin/bash # # Custom init script for contextualized VMs # # François 2014,Apr. # # # Start network # # Note: vm-context service is supposed to contains network # setup ... except in CloudMIP because the DHCP server gives # all of the needed parameters. service network restart # Install software yum --skip-broken --nogpgcheck -y install httpd php # Retrieve web application from context # Note the WWW_SRC variable that belong to the CONTEXT image file src="/mnt/${WWW_SRC}" dst="/var/www/html" cd ${dst} if [ -d ${src} ]; then # DIR type cp -af ${src}/* . else # FILE type (archive/gzip or bz2 or ...) _type=$(file -b ${src}) if [ $(echo -e ${_type} | grep -iqE "^gzip"; echo $?) -eq 0 ]; then _compress_flag="-z" elif [ $(echo -e ${_type} | grep -iqE "^bzip2"; echo $?) -eq 0 ]; then _compress_flag="-j" elif [ $(echo -e ${_type} | grep -iqE "^xz"; echo $?) -eq 0 ]; then _compress_flag="-J" fi # untar with/without uncompress tar ${_compress_flag} -xvf ${src} fi chown -R apache:apache ${dst} # Start application service httpd start
Ok, now you downloaded this httpd_init.sh
file within your CloudMIP $HOME
account, let's register it as an OpenNebula CONTEXT file.
To do so, we first need to create the CONTEXT file itself:
NAME="httpd_init.sh" DESCRIPTION="httpd init script" TYPE="CONTEXT" PATH="/cloudmip/home/<user>/httpd_init.sh"
Remember to change the <user> item with yours e.g /cloudmip/home/francois/httpd_init.sh
… then we'll register it in ONE:
oneimage create -d 2 httpd_init.one
*Note: the -d 2
flag tells ONE to use the files
datastore.
Normally, you should end up with something similar to:
francois@cloudmip[~] oneimage list ID USER GROUP NAME DATASTORE SIZE TYPE PER STAT RVMS 34 francois users httpd_init.sh files 1M CX No rdy 0
At this point, you should have an already registered CONTEXT file handling the initialisation of your VM.
We'll now register an archive that contains all of the HTTP sources as a CONTEXT file.
First, you need either to download the HTTP source archive or create your own one.
We'll now create a CONTEXT file we'll then register within ONE:
NAME="httpd_sample" DESCRIPTION="PHP5 httpd sample website" TYPE="CONTEXT" PATH="/cloudmip/home/<user>/httpd_sample.tar.gz" WWW_SRC="httpd_sample.tar.gz"
Remember to change the <user> item with yours e.g /cloudmip/home/francois/httpd_sample.tar.gz
… and please note the $WWW_SRC
variable used by our httpd_init.sh script.
… then we'll register it in ONE:
oneimage create -d 2 httpd_sample.one
*Note: the -d 2
flag tells ONE to use the files
datastore.
*Be aware that it seems there's a strange OpenNebula behaviour when uploading archived CONTEXT files: it seems there's an uncompress step leading to either the bare directory or, for example, a .tar file instead of a .tar.gz … :|
Normally, you should end up with something similar to:
francois@cloudmip[~] oneimage list ID USER GROUP NAME DATASTORE SIZE TYPE PER STAT RVMS 34 francois users httpd_init.sh files 1M CX No rdy 0 35 francois users httpd_sample files 1M CX No rdy 0
At this point, you have CONTEXT files http_init.sh and httpd_sample already registered within OpenNebula.
Next step is to create a custom template that will link together the following items:
Below is a sample OpenNebula template file:
NAME = <your template name> CPU="1" MEMORY="2048" VCPU="1" DISK=[ BUS="virtio", DRIVER="qcow2", IMAGE="SL65", IMAGE_UNAME = "admin" ] NIC=[ MODEL="virtio", NETWORK="production", NETWORK_UNAME="admin" ] OS=[ ARCH="x86_64", BOOT="hd", MACHINE="rhel6.5.0" ] # Contextualization CONTEXT=[ SSH_PUBLIC_KEY="$USER[SSH_PUBLIC_KEY]", FILES_DS="$FILE[IMAGE=\"httpd_init.sh\"] $FILE[IMAGE=\"httpd_sample\"]", INIT_SCRIPTS="httpd_init.sh", WWW_SRC="httpd_sample" ] # Console (don't forget to pass the option 'console=ttyS0' to the kernel args) RAW = [ type = "kvm", data = "<devices> <serial type='pty'> <source path='/dev/pts/1'/> <target port='0'/> <alias name='serial0'/> </serial> <console type='pty' tty='/dev/pts/1'> <source path='/dev/pts/1'/> <target type='serial' port='0'/> <alias name='serial0'/> </console> </devices>" ]
Remember to set a NAME for your template ;)
… then we'll register this template in ONE:
onetemplate create httpd_template.one
Normally, you should end up with something similar to:
francois@cloudmip[~] onetemplate list ID USER GROUP NAME REGTIME 51 francois users SL65_httpd 04/30 15:15:13
The easiest step of this tutorial … launching the VM!
<user>@cloudmip[~] onetemplate instantiate <your httpd template [name|ID]> VM ID: 92
*you can identify your http template either by name or its ID.
this latter command means that you've just launched a new VM whose status can be checked with the onevm top command.
Details about an already deployed VM are obtained through the onevm show <VM ID> command.
<user>@cloudmip[~] onevm list ID USER GROUP NAME STAT UCPU UMEM HOST TIME 92 francois users SL65_httpd-92 runn 0 2G wn1 4d 16h49
… then to connect as root to your newly created VM, just retrieve its IP through the onevmgetip command.
<user>@cloudmip[~] onevmgetip ID USER GROUP NAME STAT UCPU UMEM HOST TIME IPv4 92 francois users SL65_httpd-92 runn 0 2G wn1 4d 16h49 172.28.112.101 (vm-112-101)
<user>@cloudmip[~] ssh root@vm-112-101 Warning: Permanently added 'vm-112-101.cloudmip.univ-tlse3.fr,195.220.53.54' (RSA) to the list of known hosts. Warning: No xauth data; using fake authentication data for X11 forwarding. root@vm-112-101[~]
Once deployment of your VM has been fully achieved, you could start a life check of your HTTP server through the curl
or wget
commands.
This optional part will teach you how to gain access to your HTTP server running in a VM featuring a private IP from the Internet.
To do this, we'll make use of the SSH tunneling functionality. Thus from a shell within your Linux computer, you should type:
ssh -p 2220 -L 8080:vm-112-101:80 <user>@cloudmip.univ-tlse3.fr
This will create a local port 8080 on your computer that is linked to the port 80 on your VM through your account on the CloudMIP platform.
Now you just have to open a browser and type http://localhost:8080