OpenStack client example
This example will show how to create an instance with a jupyter notebook exposed on the 8888
port, it is used an ubuntu
image with a 2GB as additional volume for persistant storage.
These are the commands used with the openstack
client, if you do not have this client installed on your computer, please refer to login section.
Step 1: Download API file (OpenStack RC File)
Download the API file from the API section (see login section). This file will allow us to login onto https://colossus.cesar.unizar.es:
source <file>-openrc.sh
Step 2: Create ssh keypair
If you already have a keypair skip to step 3.
openstack keypair list
openstack keypair create --public-key ~/.ssh/id_key.pub id-key-openstack
Step 3: Create security group
Create an isolated security group only for your new instance, we call the security group security-group-jupyter
.
openstack security group create security-group-jupyter
Step 4: Open ports in security group
Once the security group is created, we open the ports neeeded:
openstack security group rule create --protocol tcp --dst-port 22 --ingress security-group-jupyter
openstack security group rule create --protocol tcp --dst-port 22 --egress security-group-jupyter
openstack security group rule create --protocol tcp --dst-port 8888 --ingress security-group-jupyter
openstack security group rule create --protocol icmp --ingress security-group-jupyter # incoming ICMP
openstack security group rule create --protocol icmp --egress security-group-jupyter # outgoing ICMP
Step 5: Create network and subnetwork
The instance needs a subnetwork to route the traffic, this subnetwork will be linked to a external router, here we set the range for the private subnetwork and the DNS server to resolve the outgoing traffic.
openstack network create network-jupyter
openstack subnet create --network network-jupyter --subnet-range 192.168.0.0/24 --gateway 192.168.0.1 --dns-nameserver 155.210.12.9 network-jupyter-subnet
Step 6: Create a router
Now, a new router es created, we link the previous subnetwork created to the router. Note that you will have a virual network to point the internet on yout Network
-> Networks
name as vlanXXXX
, this is the trunk network to intenet.
openstack router create router-jupyter
openstack router set --external-gateway vlanXXX router-jupyter
openstack router add subnet router-jupyter network-jupyter-subnet
Step 7: Instance creation
We can now create am instance, a network is linked to the instance, we choose the flavour 2
(m1.small
) which a tiny instance and the image id of the ubuntu
image, lastly, we all set the keypair
for login. You can list the image IDs with the command: openstack image list
openstack flavor list
openstack image list
openstack server create --flavor 2 --image <IMAGE_ID> --key-name id-key-openstack --security-group security-group-jupyter --network network-jupyter ubuntu-jupyter
Step 8: Data volume creation
Now, we create a 2GB data volume for persistent storage.
openstack volume list
openstack volume create --size 2 --description "This is data volume for jupyter notebooks" jupyter-data-volume
Step 9: Attach volume to instance
The volume created in step 8 (jupyter-data-volume) is attached to the instance we created, called ubuntu-jupyter
. We reboot the instance.
openstack server list
openstack server add volume ubuntu-jupyter jupyter-data-volume
openstack server show ubuntu-jupyter
openstack server reboot ubuntu-jupyter
Step 10: Set floating IP to instance
In order to access the instance, we set an external floating IP available in the Network
-> Floating IPs
section, we atach the available floating IP to the instance:
openstack floating ip list
openstack server add floating ip ubuntu-jupyter FLOATING_IP
Step 11: SSH login into the instance
We can now mount and give a filesystem format to the atached volume, in this case we have it on the /dev/vdb
device:
ssh -i ~/.ssh/id_key ubuntu@FLOATING_IP
sudo lsblk
sudo mkfs.ext4 /dev/vdb
sudo mkdir -p /mnt/data-jupyter
sudo chown -R ubuntu:ubuntu /mnt/data-jupyter
sudo mount /dev/vdb /mnt/data-jupyter/
Step 12: Install jupyter notebook
As a normal ubuntu
installation, we can use apt-get
to install jupyter notebook
, we also make use of venv
(virtual environment):
sudo apt update
sudo apt install python3-venv
python3 -m venv jupyter-env
source jupyter-env/bin/activate
pip install notebook
jupyter notebook --no-browser --ip=0.0.0.0 --port=8888
deactivate
Rolling back changes (optional)
It is also possible to detroy all the previous configuration, but we must be aware that we must delete from the lowest level to top, therefore, we should first release the floating IP, the remove the subnetwork, the router, the network, the volume, security gorup and the instance.
# shutdown instance
openstack server list
openstack server stop ubuntu-jupyter
# release floating IP
openstack floating ip list
openstack floating ip delete FLOATING_IP
# remove router
openstack router list
openstack router remove subnet router-jupyter network-jupyter-subnet # remove subnet
openstack router delete router-jupyter
# remove instance
openstack server list
openstack server delete ubuntu-jupyter
# remove data volume
openstack volume list
openstack volume delete jupyter-data-volume
# remove security group
openstack security group list
openstack security group delete security-group-jupyter
# remove subnetworks
openstack subnet list
openstack subnet delete network-jupyter-subnet
# remove network
openstack network list
openstack network delete network-jupyter