Tuesday, 2 February 2016

2- Launching an Instance with the Nova CLI client

In this Lab, we are going to learn to use the nova compute CLI in order to launch an instance instead of from the web Dashboard.

We are first going to connect to our instance in the Dashboard in order to learn how to download from any OpenStack environment what is called the OpenStack RC file.

An OpenStack RC file is used to set environment variables in our Linux terminal session in order to set project-specific credentials for the OpenStack CLI services such as Compute, Image, and Identity services.

1. Login to Openstack Dashboard as "demo"  and "openstack" credentials:






2. Switch from "invisable_to_admin" project to "demo" one:


3. Download OpenStack RC file:




4. Copy"demo-openrc.sh" contents as the following:

root@openstack02022016-090302:~# hostname
openstack02022016-090302
root@openstack02022016-090302:~# cat /etc/os-release 
NAME="Ubuntu"
VERSION="14.04.2 LTS, Trusty Tahr"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 14.04.2 LTS"
VERSION_ID="14.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
root@openstack02022016-090302:~# pwd
/root
root@openstack02022016-090302:~# vim demo-openrc.sh
root@openstack02022016-090302:~# cat /root/demo-openrc.sh 
#!/bin/bash

# To use an OpenStack cloud you need to authenticate against the Identity
# service named keystone, which returns a **Token** and **Service Catalog**.
# The catalog contains the endpoints for all services the user/tenant has
# access to - such as Compute, Image Service, Identity, Object Storage, Block
# Storage, and Networking (code-named nova, glance, keystone, swift,
# cinder, and neutron).
#
# *NOTE*: Using the 2.0 *Identity API* does not necessarily mean any other
# OpenStack API is version 2.0. For example, your cloud provider may implement
# Image API v1.1, Block Storage API v2, and Compute API v2.0. OS_AUTH_URL is
# only for the Identity API served through keystone.
export OS_AUTH_URL=http://104.130.30.116:5000/v2.0

# With the addition of Keystone we have standardized on the term **tenant**
# as the entity that owns the resources.
export OS_TENANT_ID=a67c3ddf7efc44e099a49b487548ccde
export OS_TENANT_NAME="demo"
export OS_PROJECT_NAME="demo"

# In addition to the owning entity (tenant), OpenStack stores the entity
# performing the action as the **user**.
export OS_USERNAME="demo"

# With Keystone you pass the keystone password.
echo "Please enter your OpenStack Password: "
read -sr OS_PASSWORD_INPUT
export OS_PASSWORD=$OS_PASSWORD_INPUT

# If your configuration has multiple regions, we set that information here.
# OS_REGION_NAME is optional and only valid in certain environments.
export OS_REGION_NAME="RegionOne"
# Don't leave a blank variable, unset it if it was empty
if [ -z "$OS_REGION_NAME" ]; then unset OS_REGION_NAME; fi

5. Source the new created file:
root@openstack02022016-090302:~# source /root/demo-openrc.sh 
Please enter your OpenStack Password: 
root@openstack02022016-090302:~# echo $?
0
root@openstack02022016-090302:~# export | grep OS
declare -x LESSCLOSE="/usr/bin/lesspipe %s %s"
declare -x OS_AUTH_URL="http://104.130.30.116:5000/v2.0"
declare -x OS_PASSWORD="openstack"
declare -x OS_PROJECT_NAME="demo"
declare -x OS_REGION_NAME="RegionOne"
declare -x OS_TENANT_ID="a67c3ddf7efc44e099a49b487548ccde"
declare -x OS_TENANT_NAME="demo"
declare -x OS_USERNAME="demo"

6. Create new SSH-key to insert it into the new created instance:
root@openstack02022016-090302:~# ssh-keygen -f ssh-key
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in ssh-key.
Your public key has been saved in ssh-key.pub.
The key fingerprint is:
b8:f8:0a:5f:ce:12:4f:a8:ca:a0:8d:ef:fc:fc:fa:17 root@openstack02022016-090302
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|                 |
|       .         |
|     .. S        |
|    o...E        |
|. ...+o  .       |
|++.+.=. .        |
|+==.***.         |
+-----------------+
# ls -lh /root/
total 12K
-rw-r--r-- 1 root root 1.6K Feb  2 14:03 demo-openrc.sh
-rw------- 1 root root 1.7K Feb  2 14:16 ssh-key
-rw-r--r-- 1 root root  411 Feb  2 14:16 ssh-key.pub
root@openstack02022016-090302:~# nova help | grep key
            [--os-key ] [--timeout ] [--os-auth-url OS_AUTH_URL]
    flavor-key                  Set or unset extra_spec for a flavor.
    keypair-add                 Create a new key pair for use with servers.
    keypair-delete              Delete keypair given by its name.
    keypair-list                Print a list of keypairs for a user
    keypair-show                Show details about the given keypair.
  --os-key                 Defaults to env[OS_KEY].
root@openstack02022016-090302:~# nova keypair-list
+------+------+-------------+
| Name | Type | Fingerprint |
+------+------+-------------+
+------+------+-------------+
root@openstack02022016-090302:~# nova help keypair-add
usage: nova keypair-add [--pub-key ] [--key-type ] 

Create a new key pair for use with servers.

Positional arguments:
                   Name of key.

Optional arguments:
  --pub-key     Path to a public ssh key.
  --key-type   Keypair type. Can be ssh or x509. (Supported by API
                         versions '2.2' - '2.latest')
root@openstack02022016-090302:~# nova keypair-add novakey --pub-key /root/ssh-key.pub 
root@openstack02022016-090302:~# nova keypair-list
+---------+------+-------------------------------------------------+
| Name    | Type | Fingerprint                                     |
+---------+------+-------------------------------------------------+
| novakey | ssh  | b8:f8:0a:5f:ce:12:4f:a8:ca:a0:8d:ef:fc:fc:fa:17 |
+---------+------+-------------------------------------------------+

8. Add new rule to the "default" security group where "104.130.30.116/24" is the public network:
First of all, determine the public network by login as admin:
root@openstack02022016-090302:~# nova help | grep security
    scrub                       Delete networks and security groups associated
                                added to the 'default' security group for new
    secgroup-add-group-rule     Add a source group rule to a security group.
    secgroup-add-rule           Add a rule to a security group.
    secgroup-create             Create a security group.
    secgroup-delete             Delete a security group.
                                be added to the 'default' security group for
    secgroup-delete-group-rule  Delete a source group rule from a security
    secgroup-delete-rule        Delete a rule from a security group.
    secgroup-list               List security groups for the current tenant.
                                security group for new tenants.
    secgroup-list-rules         List rules for a security group.
    secgroup-update             Update a security group.
root@openstack02022016-090302:~# nova secgroup-list
+--------------------------------------+---------+------------------------+
| Id                                   | Name    | Description            |
+--------------------------------------+---------+------------------------+
| ae233df5-fd24-412f-9985-f7a7d9290a92 | default | Default security group |
+--------------------------------------+---------+------------------------+
root@openstack02022016-090302:~# nova help secgroup-add-rule
usage: nova secgroup-add-rule    
                              

Add a rule to a security group.

Positional arguments:
     ID or name of security group.
     IP protocol (icmp, tcp, udp).
    Port at start of range.
      Port at end of range.
         CIDR for address range.
root@openstack02022016-090302:~# nova secgroup-add-rule default tcp 22 22 172.24.4.0/24 
+-------------+-----------+---------+---------------+--------------+
| IP Protocol | From Port | To Port | IP Range      | Source Group |
+-------------+-----------+---------+---------------+--------------+
| tcp         | 22        | 22      | 172.24.4.0/24 |              |
+-------------+-----------+---------+---------------+--------------+
root@openstack02022016-090302:~# nova secgroup-list-rules default
+-------------+-----------+---------+---------------+--------------+
| IP Protocol | From Port | To Port | IP Range      | Source Group |
+-------------+-----------+---------+---------------+--------------+
|             |           |         |               | default      |
| tcp         | 22        | 22      | 172.24.4.0/24 |              |
|             |           |         |               | default      |
+-------------+-----------+---------+---------------+--------------+

9. Boot the new instance:
root@openstack02022016-090302:~# nova help | grep boot
    boot                        Boot a new server.
    image-list                  Print a list of available images to boot from.
    reboot                      Reboot a server.
    rebuild                     Shutdown, re-image, and re-boot a server.
    rescue                      Reboots a server into rescue mode, which
                                current boot disk as secondary.
    unrescue                    Restart the server from normal boot disk
root@openstack02022016-090302:~# nova help boot
usage: nova boot [--flavor ] [--image ]
                 [--image-with ] [--boot-volume ]
                 [--snapshot ] [--min-count ]
                 [--max-count ] [--meta ]
                 [--file ] [--key-name ]
                 [--user-data ]
                 [--availability-zone ]
                 [--security-groups ]
                 [--block-device-mapping ]
                 [--block-device key1=value1[,key2=value2...]]
                 [--swap ]
                 [--ephemeral size=[,format=]]
                 [--hint ]
                 [--nic ]
                 [--config-drive ] [--poll] [--admin-pass ]
                 

Boot a new server.

Positional arguments:
                          Name for the new server.
root@openstack02022016-090302:~# nova help | grep flavor
    flavor-access-add           Add flavor access for the given tenant.
    flavor-access-list          Print access information about the given
                                flavor.
    flavor-access-remove        Remove flavor access for the given tenant.
    flavor-create               Create a new flavor.
    flavor-delete               Delete a specific flavor
    flavor-key                  Set or unset extra_spec for a flavor.
    flavor-list                 Print a list of available 'flavors' (sizes of
    flavor-show                 Show details about the given flavor.
root@openstack02022016-090302:~# nova flavor-list
+----+-----------+-----------+------+-----------+------+-------+-------------+-----------+
| ID | Name      | Memory_MB | Disk | Ephemeral | Swap | VCPUs | RXTX_Factor | Is_Public |
+----+-----------+-----------+------+-----------+------+-------+-------------+-----------+
| 1  | m1.tiny   | 512       | 1    | 0         |      | 1     | 1.0         | True      |
| 2  | m1.small  | 2048      | 20   | 0         |      | 1     | 1.0         | True      |
| 3  | m1.medium | 4096      | 40   | 0         |      | 2     | 1.0         | True      |
| 4  | m1.large  | 8192      | 80   | 0         |      | 4     | 1.0         | True      |
| 5  | m1.xlarge | 16384     | 160  | 0         |      | 8     | 1.0         | True      |
+----+-----------+-----------+------+-----------+------+-------+-------------+-----------+
root@openstack02022016-090302:~# nova help | grep image
    image-create                Create a new image by taking a snapshot of a
    image-delete                Delete specified image(s).
    image-list                  Print a list of available images to boot from.
    image-meta                  Set or Delete metadata on an image.
    image-show                  Show details about the given image.
    rebuild                     Shutdown, re-image, and re-boot a server.
                                image or a specified image, attaching the
root@openstack02022016-090302:~# nova image-list
+--------------------------------------+---------------------------------+--------+--------+
| ID                                   | Name                            | Status | Server |
+--------------------------------------+---------------------------------+--------+--------+
| dfa3a913-e229-4530-a15a-d77aa0cabad3 | cirros-0.3.4-x86_64-uec         | ACTIVE |        |
| b2567272-4a9f-499f-8893-ade063228a9c | cirros-0.3.4-x86_64-uec-kernel  | ACTIVE |        |
| b3ed9ad3-a881-46e4-b9e8-7bec1bd7b1f4 | cirros-0.3.4-x86_64-uec-ramdisk | ACTIVE |        |
+--------------------------------------+---------------------------------+--------+--------+
root@openstack02022016-090302:~# nova boot instance2 --flavor m1.tiny --image cirros-0.3.4-x86_64-uec --key-name novakey
+--------------------------------------+----------------------------------------------------------------+
| Property                             | Value                                                          |
+--------------------------------------+----------------------------------------------------------------+
| OS-DCF:diskConfig                    | MANUAL                                                         |
| OS-EXT-AZ:availability_zone          |                                                                |
| OS-EXT-STS:power_state               | 0                                                              |
| OS-EXT-STS:task_state                | scheduling                                                     |
| OS-EXT-STS:vm_state                  | building                                                       |
| OS-SRV-USG:launched_at               | -                                                              |
| OS-SRV-USG:terminated_at             | -                                                              |
| accessIPv4                           |                                                                |
| accessIPv6                           |                                                                |
| adminPass                            | ab62JNARTYyP                                                   |
| config_drive                         |                                                                |
| created                              | 2016-02-02T14:40:34Z                                           |
| flavor                               | m1.tiny (1)                                                    |
| hostId                               |                                                                |
| id                                   | ba28eac7-733b-4c7b-a236-7be7cff76321                           |
| image                                | cirros-0.3.4-x86_64-uec (dfa3a913-e229-4530-a15a-d77aa0cabad3) |
| key_name                             | novakey                                                        |
| metadata                             | {}                                                             |
| name                                 | instance2                                                      |
| os-extended-volumes:volumes_attached | []                                                             |
| progress                             | 0                                                              |
| security_groups                      | default                                                        |
| status                               | BUILD                                                          |
| tenant_id                            | a67c3ddf7efc44e099a49b487548ccde                               |
| updated                              | 2016-02-02T14:40:34Z                                           |
| user_id                              | 29b94926b3974d92a754e108e775f0be                               |
+--------------------------------------+----------------------------------------------------------------+
root@openstack02022016-090302:~# nova list
+--------------------------------------+-----------+--------+------------+-------------+----------+
| ID                                   | Name      | Status | Task State | Power State | Networks |
+--------------------------------------+-----------+--------+------------+-------------+----------+
| ba28eac7-733b-4c7b-a236-7be7cff76321 | instance2 | BUILD  | spawning   | NOSTATE     |          |
+--------------------------------------+-----------+--------+------------+-------------+----------+
root@openstack02022016-090302:~# nova list
+--------------------------------------+-----------+--------+------------+-------------+--------------------------------------------------------+
| ID                                   | Name      | Status | Task State | Power State | Networks                                               |
+--------------------------------------+-----------+--------+------------+-------------+--------------------------------------------------------+
| ba28eac7-733b-4c7b-a236-7be7cff76321 | instance2 | ACTIVE | -          | Running     | private=fd01:6936:fefc:0:f816:3eff:fe9e:422a, 10.0.0.6 |
+--------------------------------------+-----------+--------+------------+-------------+--------------------------------------------------------+

10. Login to "instance2" VM:
root@openstack02022016-090302:~# ssh -i ssh-key cirros@10.0.0.6
$ hostname
instance2
$ exit
Connection to 10.0.0.6 closed.

11. Delete 'instance2' VM:
root@openstack02022016-090302:~# nova help | grep delete
    agent-delete                Delete existing agent build.
    aggregate-delete            Delete the aggregate.
    delete                      Immediately shut down and delete specified
    dns-delete                  Delete the specified DNS entry.
    dns-delete-domain           Delete the specified DNS domain.
    flavor-delete               Delete a specific flavor
    floating-ip-bulk-delete     Bulk delete floating IPs by range (nova-
    floating-ip-delete          De-allocate a floating IP.
    image-delete                Delete specified image(s).
    keypair-delete              Delete keypair given by its name.
    network-delete              Delete network by label or id.
    quota-delete                Delete quota for a tenant/user so their quota
    secgroup-delete             Delete a security group.
    secgroup-delete-default-rule
    secgroup-delete-group-rule  Delete a source group rule from a security
    secgroup-delete-rule        Delete a rule from a security group.
    server-group-delete         Delete specific server group(s).
    service-delete              Delete the service.
    volume-delete               DEPRECATED: Remove volume(s).
    volume-snapshot-delete      DEPRECATED: Remove a snapshot.
    volume-type-delete          DEPRECATED: Delete a specific volume type.
    net-delete                  DEPRECATED, use tenant-network-delete instead.
    tenant-network-delete       Delete a tenant network.
    baremetal-node-delete       Remove a baremetal node and any associated
    force-delete                Force delete a server.
    restore                     Restore a soft-deleted server.
root@openstack02022016-090302:~# nova help delete
usage: nova delete [--all-tenants]  [ ...]

Immediately shut down and delete specified server(s).

Positional arguments:
         Name or ID of server(s).

Optional arguments:
  --all-tenants  Delete server(s) in another tenant by name (Admin only).
root@openstack02022016-090302:~# nova list
+--------------------------------------+-----------+--------+------------+-------------+--------------------------------------------------------+
| ID                                   | Name      | Status | Task State | Power State | Networks                                               |
+--------------------------------------+-----------+--------+------------+-------------+--------------------------------------------------------+
| db2cc0a6-ed2c-4ee0-b0bf-38c41ed08aa3 | instance2 | ACTIVE | -          | Running     | private=fd01:6936:fefc:0:f816:3eff:fe13:d6bd, 10.0.0.6 |
+--------------------------------------+-----------+--------+------------+-------------+--------------------------------------------------------+
root@openstack02022016-090302:~# nova delete instance2
Request to delete server instance2 has been accepted
root@openstack02022016-090302:~# echo $?
0
root@openstack02022016-090302:~# nova list
+----+------+--------+------------+-------------+----------+
| ID | Name | Status | Task State | Power State | Networks |
+----+------+--------+------------+-------------+----------+
+----+------+--------+------------+-------------+----------+

1- Launching an Instance with the Dashboard

In this Lab, we are going to use the OpenStack Dashboard to create our first instance.

1. Login to OpenStack Dashboard using "demo" and "openstack" credentials:



2. Create New Instance:




















3. Connect to "Instance1" one console: