Virtual Machines on IBM Bluemix

Hey I am back..and I have just moved to new team focusing on Bluemix which is IBM’s PaaS . Bluemix enables organizations and developers to quickly and easily create, deploy, and manage applications on the cloud.

In this blog we explore VMs in Bluemix . IBM Bluemix provides the IBM Virtual Machines infrastructure as a beta feature that is currently available in US-South region . You can use this infrastructure to create virtual machines that are running in public and private (on-premise) OpenStack clouds. To know more  one can go through the following document

I have been working with customers who are evaluating this feature , any many of them have very similar questions and difficulties getting around. Hence I am putting up this blog which details some of the common scenarios which most folks were playing around with.. and to help others get started.

  1. Creation of VM Image
  2. Accessing the image created through ssh
  3. Adding new user to that image and login is as new user
  4. Pushing artifacts to new VM using WinSCP
  5. UI access to VM images

1.  Creation of VM Image

(i) From Bluemix Dashboard , click on Virtual Machines and then Create VM Image

Create-VM

(ii) On the Create Virtual Machine Bluemix screen , there are multiple flavors to choose from , I decided to create Ubuntu VM So I select Ubuntu from Image to launch , I add a VM group name and then the Security Key.

Ubuntu-Image

To access this VM instance after it has been created one needs a SSH key , there are multiple ways one can create SSH key and you could follow steps is the BluemixVM Image Documentation . I did not use puttygen and used the windows systems  SSH command line alternative found here.

On my windows command prompt I ran command as below and it prompted me for passphase. Enter any passphase and remember that passphase as it would be used while logging in later.

ssh-keygen -t rsa -f cloud.key

This command generates a pair of keys:

  • a key that you keep private :  cloud.key
  • a public key : cloud.key.pub

So I imported the cloud.key.pub by clicking on Add New Key , gave it name  and copied the contents of cloud.key.pub into Public Key to Import section and I am now ready to click on the Create Button.

(iii) The image is created in a minute or so and a Public as well as Private IP is assigned as shown below

IP

2. Accessing the image created through ssh

(i)  On the command prompt (in location where the private ssh key is kept) , I type:  ssh -i    ibmcloud@    ( keyname is private key generated in step1 , ibmcloud is the default user on all IBM VM images on Bluemix , public IP is IP starting with 129 as shown in screenshot above)

ssh -i cloud.key ibmcloud @129.xx.xxx.xx

It would ask for passphase ,enter the passphase supplied in Section1 and volia we are in now.

3. Adding new user to that image and login is as new user

Let’s add a new user now..

(i) To get root access

$sudo bash

(ii) To add a new user , run   adduser , it would ask to enter password as well and couple of other information

adduser smith

(iii) Edit the sshd_config file to allow newly added user to login  via ssh . Edit the last  line in sshd_config which says AllowUser , add the user created above and save the file

vi /etc/ssh/sshd_config

AllowUsers

(iv) Lets restart the service

service ssh restart

(v) Now we are ready to Login with new user , open a new command prompt and enter ssh username@, it would prompt for password , supply password entered while creating the user.

ssh smith@129.xx.xx.xx

We are now logged is as the new user .

4.Pushing artifacts to new VM using WinSCP

We may need to transfer files to VM and you can use WinSCP here

(i) The newly added user smith can easily Login through WinSCP entering userid/password.

WinSCP

(ii) ibmcloud can also login through WinSCP. Enter username as ibmcloud and leave password blank and instead click on Advanced Button and on SSH tab , under Authentication enter the ppk key.

WinSCP-ibmcloud

If you created key using openssh , the key needs to be converted into ppk format , one can use puttygen to do the key conversion .( On puttygen click on  Conversions -> Import key and import the private  cloud.key generated in step1 and then click on generate key to convert to ppk format ).

5. UI access to VM Image

Finally , our last scenario in this Blog .. UI acccess, there are multiple ways one could do this , in this I am exploring xfce4

(i) Login into the VM using the ssh key

ssh -i cloud.key ibmcloud@129.x.x.x

(ii) Login as superuser

sudo bash

(iii) Install the required packages : By default, most Linux server installations will not come with a graphical desktop environment.In this case you need to install one that you can work with. In this example, we will install XFCE4

apt-get install gnome-core xfce4 firefox
apt-get install vnc4server
apt-get install xfonts-base

(iv) To make sure that vnc server is installed, type this command:

dpkg -l | grep vnc

Output is as follows:

VNC

(v) Now my goal is to give VNC access to user smith I had created in Section3

su - smith
vncserver

The output would be something like this , one needs to set password which would be used to access VNC Server

vncserver-smith

(vi) I backup  the xstartup file before I make changes in case I need it later and edit it as below:

cp ~/.vnc/xstartup ~/.vnc/xstartup.bak
vi ~/.vnc/xstartup

And I edit the file as below :

xstartup

(vii) Next I stop the VNC server instance that is running on port 5901

vncserver -kill :1

(viii) Now to easily control our the VNC server, let us set it up as an Ubuntu service. Create a file as below as paste contents in that file.

su 
vi /etc/init.d/vncserver
#!/bin/bash
 
unset VNCSERVERARGS
VNCSERVERS=""
[ -f /etc/vncserver/vncservers.conf ] && . /etc/vncserver/vncservers.conf
prog=$"VNC server"
start() {
 . /lib/lsb/init-functions
 REQ_USER=$2
 echo -n $"Starting $prog: "
 ulimit -S -c 0 >/dev/null 2>&1
 RETVAL=0
 for display in ${VNCSERVERS}
 do
 export USER="${display##*:}"
 if test -z "${REQ_USER}" -o "${REQ_USER}" == ${USER} ; then
 echo -n "${display} "
 unset BASH_ENV ENV
 DISP="${display%%:*}"
 export VNCUSERARGS="${VNCSERVERARGS[${DISP}]}"
 su ${USER} -c "cd ~${USER} && [ -f .vnc/passwd ] && vncserver :${DISP} ${VNCUSERARGS}"
 fi
 done
}
stop() {
 . /lib/lsb/init-functions
 REQ_USER=$2
 echo -n $"Shutting down VNCServer: "
 for display in ${VNCSERVERS}
 do
 export USER="${display##*:}"
 if test -z "${REQ_USER}" -o "${REQ_USER}" == ${USER} ; then
 echo -n "${display} "
 unset BASH_ENV ENV
 export USER="${display##*:}"
 su ${USER} -c "vncserver -kill :${display%%:*}" >/dev/null 2>&1
 fi
 done
 echo -e "\n"
 echo "VNCServer Stopped"
}
case "$1" in
start)
start $@
;;
stop)
stop $@
;;
restart|reload)
stop $@
sleep 3
start $@
;;
condrestart)
if [ -f /var/lock/subsys/vncserver ]; then
stop $@
sleep 3
start $@
fi
;;
status)
status Xvnc
;;
*)
echo $"Usage: $0 {start|stop|restart|condrestart|status}"
exit 1
esac

(ix) To ensure that the VNC server will be able to use this new startup file properly, we’ll need to grant executable privileges to it:

chmod +x /etc/init.d/vncserver

(x) Now its time to start this service  to start a new VNC server instance

service vncserver start

(xi) In VNC you can define a separate access session for each particular user. For this we will use VNC configuration file:

mkdir -p /etc/vncserver
vi /etc/vncserver/vncservers.conf

(xii) To configure VNC for a user “smith” /etc/vncservers/vncservers.conf file insert the following lines:

VNCSERVERS="1:smith"
VNCSERVERARGS[1]="-geometry 1024x768"

Here your port comes to be 5901 & 1024×768 resolution for the VNC client, you can choose resolution of your own choice.

(xiii) Next I will add it into boot startups:

update-rc.d vncserver defaults 99

(xiv) Finally reboot your server.

reboot

(xv) To test your VNC server, since I am on Windows I downloaded  VNCViwer ,

Now I am going to connect with the VNC server through my VNC-client and I am now on my last step.. ( I added the entry for the of the VM in my hosts file in Local machine mapping it to its IP) and ..

Viewer

and there you go… we finally have logged into the image !

UI

Ow yes , I disabled the firewall in between ( not sure though if that was needed)

sudo ufw disable


9 Comments

  1. dtoczala

    Smith, great article. I plan on walking through this sometime this week.

    • Thanks Tox, I hope you find it useful.

  2. Thanks, I’ve now got access to my VM!
    In my case I had already generated the key file during VM creation. However I had to update security permissions on the *.pem key file before it would work. Essentially I had to explicitly set full control permission to my windows logon account, then removed permissions from all users.

  3. Thanks for this article. I have a question on step3. I changed to bash, and tried to create a user but I can’t only root can do it. I am logged in as the default ibmcloud.

    $sudo bash
    ibmcloud@abc-01-98765433:~$
    ibmcloud@abc-01-98765433:~$ adduser ignacio
    adduser: Only root may add a user or group to the system.

    • Hi , I tried again today . I was able to sudo to root . I am using Ubuntu 14.04, on running sudo bash.. if you see prompt changed to root@ and then you should be able to add user.
      I suspect in your case ‘sudo bash’ has not run correctly. Could you retry ?

      $ sudo bash
      sudo: unable to resolve host delete-50425703
      root@delete-50425703:~# adduser smith
      Adding user `smith’ …
      Adding new group `smith’ (1001) …
      Adding new user `smith’ (1001) with group `smith’ …
      Creating home directory `/home/smith’ …
      Copying files from `/etc/skel’ …
      New password:

  4. Akshay

    Hi,
    Nice explaination.
    I am looking for some Bluemix APIs for managing VMs (creation, deletion).Can you let me know if there are any APIs available?

  5. Divya

    Hi,
    Nice article. I have a question on 1.ii step – Can we add our own flavor and choose during VM creation? I have a custom image that I’d like to boot up the VM with. If that is possible, can you elaborate steps to upload custom images and provision VM using it? Thanks in advance.

    • The way to go about doing that would be to upload your images using Openstack CLI

      In Bluemix, you can upload virtual server images that are supported by OpenStack and have qcow2 format only.
      If you have a virtual server image with a format that is not qcow2, you can use the qemu-img convert command to convert between formats.
      (http://docs.openstack.org/image-guide/convert-images.html)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Bluemix HUB

The community for bluemix developers

Smith's Blog

Continuous delivery of software-driven innovation

Sanjeev Sharma's Site

My thoughts on #Cloud, #DevOps, #Architecture, #Agile, #Development, #Innovation, and Life...

Takehiko Amano's Blog on Emerging Technologies.

My thought on DevOps and Cloud technologies

Tim Feeney's Blog on Jazz

close encounters of the Jazz kind

Dan Toczala's Blog

THINK - About what's possible....

%d bloggers like this: