openstack.md 3.04 KB
Newer Older
Bo-Chun Chen's avatar
Bo-Chun Chen committed
1
2
3
4
5
6
7
8
9
## OpenStack note

### Prerequisite

You need to set a bunch of thing to access OpenStack. Fortunately, you can just download OpenStack RC File from openstack dashboard by clicking your user dropdown on top right. Once you have your `openrc.sh` file downloaded, simply source it and you are good to go.

```shell
source openrc.sh
```
Bo-Chun Chen's avatar
Bo-Chun Chen committed
10
11
12
13
14
15
16

Alternatively, you can create an application credential from web UI, and download the credential.

```shell
source app-cred.sh
```

Bo-Chun Chen's avatar
Bo-Chun Chen committed
17
18
19
- Virtual environment (Optional but recommended)

```shell
20
# Use Virtualenv
Bo-Chun Chen's avatar
Bo-Chun Chen committed
21
virtualenv osc
22
23
24
# Or use python3
python3 -m venv osc

Bo-Chun Chen's avatar
Bo-Chun Chen committed
25
26
27
28
29
source osc/bin/activate
```

- OpenStack CLI

30
Everything starts with the CLI unless you have web interface. So, let's install it via pip. For more details, please see [here](https://docs.openstack.org/newton/user-guide/common/cli-install-openstack-command-line-clients.html) at OpenStack official documentation.
Bo-Chun Chen's avatar
Bo-Chun Chen committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

```shell
pip install python-openstackclient
```

- Auto-completion (Optional)

OpenStack CLI provides lots of command to interactive with OpenStack. It might be a little to much to memorize all of the command. You can setup auto-completion to make it a little bit easier.

```shell
# On most Linux Environment
openstack complete | sudo tee /etc/bash_completion.d/osc.bash_completion > /dev/null

# On MAC with bash-completion installed by homebrew
openstack complete | sudo tee /usr/local/etc/bash_completion.d/osc.bash_completion > /dev/null
```

## Launch first instance from CLI

50
Following instruction from manual provided for using the openstack command line:
Bo-Chun Chen's avatar
Bo-Chun Chen committed
51
52
53
54
55
56
57
58
59
60

- Add keypair

```shell
openstack keypair create --public-key ~/.ssh/id_rsa.pub key_name
```

- Create Public Network and subnet

```shell
61
62
openstack network create mynet
openstack subnet create --dns-nameserver 8.8.8.8 --subnet-range 192.168.1.0/24 --network mynet mysubnet
Bo-Chun Chen's avatar
Bo-Chun Chen committed
63
64
65
66
67
68
69
70
71
72
73
74
```

- Create Private Network and subnet

```shell
openstack network create compute
openstack subnet create --no-dhcp --subnet-range 10.1.1.0/24 --network compute computesubnet
```

- Create router attach to external network

```shell
75
openstack router create myrouter
Bo-Chun Chen's avatar
Bo-Chun Chen committed
76

77
78
79
networkid=$(openstack network list -c ID --name mynet -f value)
openstack router set myrouter --external-gateway uab-campus
openstack router add subnet myrouter mysubnet
Bo-Chun Chen's avatar
Bo-Chun Chen committed
80
81
```

Bo-Chun Chen's avatar
Bo-Chun Chen committed
82
83
84
- Create VM with Centos image

```shell
85
86
networkid=$(openstack network list -c ID --name mynet -f value)
openstack server create --image CentOS-7-x86_64-GenericCloud-1905 --flavor m1.medium --key-name louis-key --nic net-id=$networkid myvm
Bo-Chun Chen's avatar
Bo-Chun Chen committed
87
88
```

Bo-Chun Chen's avatar
Bo-Chun Chen committed
89
90
91
- Create floating IP and attach to the VM

```shell
92
openstack floating ip create uab-campus
Bo-Chun Chen's avatar
Bo-Chun Chen committed
93

94
95
floatingipnet=$(openstack floating ip list -c "Floating IP Address" -f value)
openstack server add floating ip myvm $floatingipnet
Bo-Chun Chen's avatar
Bo-Chun Chen committed
96
97
98
99
100
```

- Enable port 22 in default security group

```shell
101
openstack security group rule create --protocol tcp --dst-port 22 default
Bo-Chun Chen's avatar
Bo-Chun Chen committed
102
```
Bo-Chun Chen's avatar
Bo-Chun Chen committed
103
104
105

- ssh into VM

106
When ssh into VMs, you will to use your floating IP.
Bo-Chun Chen's avatar
Bo-Chun Chen committed
107
108
109
110
111

```shell
# Default user for login is different depends on distribution.

# CentOS
112
ssh centos@<floatingip>
Bo-Chun Chen's avatar
Bo-Chun Chen committed
113
114

# Ubuntu
115
ssh ubuntu@<floatingip>
Bo-Chun Chen's avatar
Bo-Chun Chen committed
116
```