microstackを使ってAWS上のubuntu20.04上にopenstackを構築する

# やること
microstackを使ってAWS上のubuntu20.04上にopenstackを構築する

# 手順

## AWSでubuntu20.04のVMを作成する
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/76498/01361e86-7085-7922-5b26-104d819e7c77.png)

## microstackのrequirementsを見る
memory 16G, disk 50Gが必要
Ref: https://ubuntu.com/openstack/install

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/76498/29bff37b-0722-9f45-cd25-b8d35729ff92.png)

## リソースを割り当ててVMを起動
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/76498/c2935e13-acba-0f81-22d2-47e7365f3fde.png)

## 注意
自分のlocalマシンのsshの公開鍵を登録してVMを起動することを忘れずに。これを忘れると作ったVMにsshアクセスできない。画面省略。

## 作られたVMへsshアクセス
```bash:bash
# xxx.xxx.xxx.xxxはVMに自動的に割り当てられたパブリックIPv4アドレスを指0900定
$ ssh xxx.xxx.xxx.xxx -l ubuntu
```

## snapでmicrostackをinstall
Ref: https://ubuntu.com/openstack/install の2

```bash:bash
$ sudo snap install microstack --beta --devmode
microstack (beta) ussuri from Canonical✓ installed
```

## microstackを初期化
Ref: https://ubuntu.com/openstack/install の3

```bash:bash
$ sudo microstack init --auto --control
...
2021-05-11 04:47:34,227 - microstack_init - INFO - Complete. Marked microstack as initialized!
```

## VMのsecurity groupにhttpアクセスをinboundで追加
これで自分のlocalマシンからVM(OpenStackのhorizon)へhttpアクセスできるようになる。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/76498/b89b93e9-54b4-8690-1dd8-0ce987d77971.png)

## httpでhorizonにアクセスしてみる
horizonにloginする時のpasswordは以下のコマンドで取得できる

```bash:bash
$ sudo snap get microstack config.credentials.keystone-password
```

その上でhttp://[VMのパブリックIPv4アドレス]でhorizonにアクセスできる。

## sshの鍵をopenstackに登録
```bash:bash
# openstackをinstallしたubuntu20.04マシンにて
$ ssh-keygen
```

## このあたりからkeyを登録
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/76498/7be35827-d687-bc75-81e8-e95d77befa0b.png)

## OpenStack上からVMを作成してみる
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/76498/426d3536-db69-a355-b73d-3238f4058318.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/76498/7abce1d2-ba35-1b84-5c75-e74e70b704cb.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/76498/8265122f-e5dd-e533-a73e-b7f4c6e8eab7.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/76498/faab3bb7-0f65-b324-c56e-93e44780e50e.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/76498/9559e180-43ca-5115-2027-a903b9dfbf4a.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/76498/1c0b80e6-94bb-d23a-8a3d-0e7444f7ba3f.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/76498/c371620f-4197-ef1d-188a-17d223c4ef8d.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/76498/c63a156d-0616-74fd-7fb0-26ad3b0f534c.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/76498/b263815d-afef-44e0-471f-e077b6c0d38d.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/76498/89f7c5af-1d88-ae7d-9b43-c9907aa155a0.png)

## oepnstackコマンドからVMを作ってみる

### microstack.openstackをopenstackで実行できるようにする
Ref: https://tech.virtualtech.jp/entry/2019/12/17/113221

```bash:bash
$ sudo snap alias microstack.openstack openstack
```

### VMを作成

```bash:bash
# flavorを確認
$ openstack flavor list                                                         
+----+-----------+-------+------+-----------+-------+-----------+                      
| ID | Name      |   RAM | Disk | Ephemeral | VCPUs | Is Public |                      
+----+-----------+-------+------+-----------+-------+-----------+                      
| 1  | m1.tiny   |   512 |    1 |         0 |     1 | True      |                      
| 2  | m1.small  |  2048 |   20 |         0 |     1 | True      |                      
| 3  | m1.medium |  4096 |   20 |         0 |     2 | True      |                      
| 4  | m1.large  |  8192 |   20 |         0 |     4 | True      |                      
| 5  | m1.xlarge | 16384 |   20 |         0 |     8 | True      |                      
+----+-----------+-------+------+-----------+-------+-----------+                      

# networkを確認
$ openstack network list                                                        
op+--------------------------------------+----------+--------------------------------------+
| ID                                   | Name     | Subnets                              |
+--------------------------------------+----------+--------------------------------------+
| 75b06726-f1cc-44db-b9e1-bd1e4ca59bc3 | test     | cc026fdd-15b1-40a1-b635-ba92501d0be4 |
| ca00dd2c-022b-4315-99c9-8eee336add76 | external | 78eba1c9-18af-4958-9910-7949cd92e563 |
+--------------------------------------+----------+--------------------------------------+

# imageを確認
$ microstack.openstack image list
+--------------------------------------+--------+--------+
| ID                                   | Name   | Status |
+--------------------------------------+--------+--------+
| 388183f7-2c11-423b-9070-075372a50167 | cirros | active |
+--------------------------------------+--------+--------+

# key-pairを確認
$ microstack.openstack keypair list
+---------------+-------------------------------------------------+
| Name          | Fingerprint                                     |
+---------------+-------------------------------------------------+
| local-machine | fa:3d:46:e7:88:14:c4:52:e2:1a:39:d3:8c:d1:20:21 |
+---------------+-------------------------------------------------+

# 以上を踏まえてVMを作成
$ microstack.openstack server create --flavor m1.tiny --nic net-id=test --image cirros --key-name local-machine microstack-cirros

# 作成されたVMを確認
$ openstack server list
+--------------------------------------+-------------------+--------+------------------------------------+--------------+----------+
| ID                                   | Name              | Status | Networks                           | Image        | Flavor   |
+--------------------------------------+-------------------+--------+------------------------------------+--------------+----------+
| aa77cf28-0136-44d1-a372-cf1a961d50c9 | microstack-cirros | ACTIVE | test=192.168.222.216               | cirros       | m1.tiny  |
| 5399d01c-8dbc-4a28-a9b6-079984dc79d5 | test              | ACTIVE | test=192.168.222.165, 10.20.20.191 | cirros       | m1.tiny  |
+--------------------------------------+-------------------+--------+------------------------------------+--------------+----------+


# floating ipを生成
$ openstack floating ip create external
+---------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+     
| Field               | Value                                                                                                                                                                             |     
+---------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+     
| created_at          | 2021-05-11T05:30:10Z                                                                                                                                                              |     
| description         |                                                                                                                                                                                   |     
| dns_domain          | None                                                                                                                                                                              |     
| dns_name            | None                                                                                                                                                                              |     
| fixed_ip_address    | None                                                                                                                                                                              |     
| floating_ip_address | 10.20.20.101                                                                                                                                                                      |
| floating_network_id | ca00dd2c-022b-4315-99c9-8eee336add76                                                                                                                                              |     
| id                  | ecb68ff3-6539-4324-8d25-90a643cac34e                                                                                                                                              |
| location            | Munch({'cloud': '', 'region_name': '', 'zone': None, 'project': Munch({'id': 'b8ca3797e44b463c916dd2b3177459dd', 'name': 'admin', 'domain_id': None, 'domain_name': 'default'})}) |     
| name                | 10.20.20.101                                                                                                                                                                      |     
| port_details        | None                                                                                                                                                                              |     
| port_id             | None                                                                                                                                                                              |     
| project_id          | b8ca3797e44b463c916dd2b3177459dd                                                                                                                                                  |     
| qos_policy_id       | None                                                                                                                                                                              |     
| revision_number     | 0                                                                                                                                                                                 |     
| router_id           | None                                                                                                                                                                              |     
| status              | DOWN                                                                                                                                                                              |     
| subnet_id           | None                                                                                                                                                                              |     
| tags                | []                                                                                                                                                                                |     
| updated_at          | 2021-05-11T05:30:10Z                                                                                                                                                              |     
+---------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+     
ubuntu@ip-172-31-24-26:~$

# floating ipをVMに割り付け
$ openstack server add floating ip microstack-cirros 10.20.20.101

# sshでVMにlogin
$ ssh cirros@10.20.20.101
```

# OpenStackにubuntu18.04のimageを追加してみる
```bash:bash
$ wget https://cloud-images.ubuntu.com/releases/bionic/release/ubuntu-18.04-server-cloudimg-amd64.img

$ openstack image create --disk-format qcow2 --container-format bare \
--public --file ./ubuntu-18.04-server-cloudimg-amd64.img ubuntu-18.04

$ openstack image list                                                                                                                                                                  
+--------------------------------------+--------------+--------+                                                                                                                                                
| ID                                   | Name         | Status |                                                                                                                                                
+--------------------------------------+--------------+--------+                                                                                                                                                
| 388183f7-2c11-423b-9070-075372a50167 | cirros       | active |                                                                                                                                                
| e3547636-ca09-49b3-b06a-6b2d47a81bf1 | ubuntu-18.04 | active |                                                                                                                                                
+--------------------------------------+--------------+--------+
```