openstack 常用命令回顾及总结

1. 概述

命令实际执行基于OpenStack Queens版本,更高版本亦可,长时间未使用openstack有些遗忘,整理后方便自己回顾学习,仅供各位参考,详细命令及参数可以参考openstack官方网址


2. 认证管理

2.1 查看服务目录

openstack endpoint list
openstack catalog list

2.2 查看项目列表

openstack project list

2.3 查看用户列表

openstack user list

2.4 查看角色列表

openstack role list

2.5 创建项目

openstack project create PROJECT_NAME

2.6 创建用户

openstack user create --project PROJECT_NAME USER_NAME

2.7 用户分配角色

openstack role add --project PROJECT_NAME --user USER_NAME _member_

2.8 查看角色分配列表

openstack role assignment list

2.9 删除项目

openstack project delete PROJECT_NAME

2.10 删除用户

openstack user delete USER_NAME

3. 镜像管理

3.1 列出镜像

openstack image list

3.2 查看镜像的详细信息

openstack image show IMAGE_NAME

3.3 设置镜像属性

openstack image set --public IMAGE_NAME 

3.4 上传镜像

3.4.1 上传完整的qcow2镜像

openstack image create --file IMAGE_NAME.qcow2  --disk-format qcow2 --container-format bare --property hw_scsi_model=virtio-scsi --property hw_disk_bus=virtio  --public  IMAGE_NAME

3.4.2 上传含kernel、initramfs和rootfs三者的镜像

openstack image create "cirros-threepart-kernel" --disk-format aki --container-format aki --public --file ~/images/cirros-0.3.5-x86_64-kernel
openstack image create "cirros-threepart-ramdisk" --disk-format ari --container-format ari --public --file ~/images/cirros-0.3.5-x86_64-initramfs
openstack image create "cirros-threepart" --disk-format ami --container-format ami --public --property kernel_id=$KID-property ramdisk_id=$RID --file ~/images/cirros-0.3.5-x86_64-rootfs.img

3.5 下载镜像

openstack image save IMAGE_NAME --file IMAGE_NAME.qcow2

3.6 查看镜像元数据

glance md-namespace-list
glance md-property-show NAMESPACE
glance md-namespace-show OS::Compute::LibvirtImage

结果如下:

+----------------------------+----------------------------------------------------------------------------------+
| Property                   | Value                                                                            |
+----------------------------+----------------------------------------------------------------------------------+
| created_at                 | 2023-01-11T16:58:18Z                                                             |
| description                | Libvirt驱动相关配置                                                              |
| display_name               | libvirt Driver Options for Images                                                |
| namespace                  | OS::Compute::LibvirtImage                                                        |
| owner                      | admin                                                                            |
| properties                 | ["hw_machine_type", "hw_qemu_guest_agent", "hw_vif_model", "os_command_line",    |
|                            | "hw_scsi_model", "hw_video_ram", "hw_video_model", "hw_rng_model",               |
|                            | "img_hide_hypervisor_id", "hw_disk_bus", "hw_pointer_model"]                     |
| protected                  | True                                                                             |
| resource_type_associations | ["OS::Glance::Image"]                                                            |
| schema                     | /v2/schemas/metadefs/namespace                                                   |
| visibility                 | public                                                                           |
+----------------------------+----------------------------------------------------------------------------------+

3.7 设置镜像元数据

openstack image set --property  IMAGE_NAME

3.8 取消镜像元数据

openstack image unset --property  IMAGE_NAME

3.8 删除镜像

openstack image delete IMAGE_NAME

4. 网路管理

4.1 查看网络服务状态

openstack network agent list

4.2 查看网络列表

openstack network list

4.3 查看子网列表

openstack subnet list

4.4 创建网络

4.4.1 创建网络

根据配置文件tenant_network_types顺序选择第一个

openstack network create NETWORK_NAME

4.4.2 创建一个指定的provider类型的网络,比如vxlan网络创建,自动分配segmentation_id

openstack network create NETWORK_NAME --provider-network-type vxlan
openstack network create NETWORK_NAME --provider-network-type vxlan --provider-segment 1000 --shared

4.4.3 创建一个指定的provider类型的网络,比如vlan网络创建,可选择指定physical_network,segmentation_id

openstack network create NETWORK_NAME --provider-network-type vlan
openstack network create NETWORK_NAME --provider-network-type vlan --provider-physical-network physnet1 --provider-segment 101 --shared

4.4.4 创建一个指定的provider类型的网络,比如flat网络创建,并设置为外部网络

openstack network create NETWORK_EXTRERNAL --provider-network-type flat --provider-physical-network external --external --shared

4.5 为网络创建子网

openstack subnet create SUBNET_NAME --network NETWORK_NAME --subnet-range 192.0.2.0/24

4.6 创建路由器

openstack router create ROUTER_NAME

4.7 将路由器连接到外部的提供商网络

openstack router set ROUTER_NAME --external-gateway NETWORK_NAME

4.8 将路由器连接到子网

openstack router add subnet ROUTER_NAME SUBNET_NAME

4.9 在不指定IP地址的情况下创建一个端口

openstack port create PORT_NAME --network NETWORK_NAME

4.10 创建指定IP地址的端口

openstack port create --network NETWORK_NAME --fixed-ip subnet=SUBNET_NAME,ip-address=192.0.2.40 PORT_NAME

4.11 指定的固定IP地址的查询端口

neutron port-list --fixed-ips ip_address=192.0.2.2 ip_address=192.0.2.40

4.12 浮动IP

4.12.1 列出浮动IP池中提供的所有浮动IP地址

openstack network list --external

4.12.2 列出分配给当前项目的所有浮动的IP地址

openstack floating ip list

4.12.3 分配浮动IP

openstack floating ip create NETWORK_EXTRERNAL

4.12.3 释放浮动IP

openstack floating ip delete FLOATING_IP_ADDRESS

4.13 删除子网

openstack subnet delete SUBNET_NAME 

4.14 删除网络

openstack network delete NETWORK_NAME

5. 计算管理

5.1 服务状态管理

5.1.1 查看计算服务状态

openstack compute service list

5.1.2 设置计算服务可用与不可用

openstack compute service set --enable/--disable HOSTNAME nova-compute

5.2 查看虚拟化节点列表

openstack hypervisor list

5.3 查看虚拟化节点详细信息

openstack hypervisor show HYPERVISOR_ID

查看所有虚拟化节点统计信息如下:

openstack hypervisor stats show

5.4 查看云主机/虚拟机列表

openstack server list --all

5.5 显示云主机详细信息

openstack server show INSTANCE_NAME

5.6 查看云主机的控制台日志

openstack console log show INSTANCE_NAME

5.7 获得一个可访问的云主机的URL

openstack console url show INSTANCE_NAME/INSTANCE_ID --novnc

5.8 创建云主机

openstack server create --flavor FLAVOR_ID --image IMAGE_ID --network NETWORK_ID --wait INSTANCE_NAME 

5.9 挂起和恢复云主机

openstack server suspend INSTANCE_NAME
openstack server resume INSTANCE_NAME

5.10 暂停和取消暂停云主机

openstack server pause INSTANCE_NAME
openstack server unpause INSTANCE_NAME

5.11 关闭云主机

openstack server stop INSTANCE_NAME

5.12 启动云主机

openstack server start INSTANCE_NAME

5.13 重启云主机

软重启

openstack server reboot --soft INSTANCE_NAME

硬重启

openstack server reboot --hard INSTANCE_NAME

5.14 重置云主机状态

openstack server set --state active INSTANCE_NAME

5.15 调整云主机

openstack server resize --flavor FLAVOR_ID INSTANCE_NAME

手动确认调整大小

openstack server resize --confirm INSTANCE_ID

如果调整失败或未按预期运行,可以恢复调整大小

openstack server resize --revert INSTANCE_ID

5.16 重建虚拟机

openstack server rebuild --image IMAGE_ID INSTANCE_NAME

5.17 云主机挂载云硬盘

openstack server add volume INSTANCE_ID VOLUME_ID
openstack server add volume --device /dev/vdb INSTANCE_ID VOLUME_ID

5.18 云主机卸载云硬盘

openstack server remove volume INSTANCE_ID VOLUME_ID

5.19 云主机添加浮动IP

openstack server add floating ip INSTANCE_NAME/INSTANCE_ID FLOATING_IP_ADDRESS

5.20 云主机移除浮动IP

openstack server remove floating ip INSTANCE_NAME/INSTANCE_ID FLOATING_IP_ADDRESS

5.21 云主机添加固定IP地址

openstack server add fixed ip  --fixed-ip-address FIXED_IP_ADDRESS  INSTANCE_NAME/INSTANCE_ID NETWORK_NAME

5.22 云主机移除固定IP地址

openstack server remove fixed ip  INSTANCE_NAME/INSTANCE_ID FIXED_IP_ADDRESS

5.23 云主机添加网络

openstack server add network INSTANCE_NAME/INSTANCE_ID NETWORK_NAME

5.24 云主机移除网络

openstack server remove network INSTANCE_NAME/INSTANCE_ID NETWORK_NAME

5.25 云主机添加端口

openstack server add port INSTANCE_NAME/INSTANCE_ID PORT_NAME

5.26 云主机移除端口

openstack server remove port INSTANCE_NAME/INSTANCE_ID PORT_NAME

5.27 云主机添加安全组

openstack server add security group INSTANCE_NAME/INSTANCE_ID SECURITY_GROUP_ID

5.28 云主机移除安全组

openstack server remove security group INSTANCE_NAME/INSTANCE_ID SECURITY_GROUP_ID

5.29 创建云主机快照

openstack server image create INSTANCE_NAME --name INSTANCE_NAME_SNAPSHOT

5.30 撤离云主机

从失败的计算节点撤离该云主机,若为共享存储则保留原系统数据,若非共享存储,则基于原镜像和原端口重建虚拟机,挂载的共享盘保留。

openstack server evacuate INSTANCE_NAME/INSTANCE_ID 

5.31 云主机迁移

openstack server migrate  --live-migration  INSTANCE_ID

5.32 删除云主机

openstack server delete INSTANCE_NAME

6. 存储管理

6.1 查看存储服务状态

openstack volume service list

6.2 查看卷列表

openstack volume list

6.3 创建新的空卷

openstack volume create --size SIZE_IN_GB VOLUME_NAME

6.4 创建基于镜像的的卷

openstack volume create --image IMAGE_ID --size SIZE_IN_GB VOLUME_NAME

6.5 查看卷详细信息

openstack volume show VOLUME_ID

6.6 调整卷大小

卸载卷后再调整

openstack volume set VOLUME_ID --size NEW_SIZE_IN_GB

6.7 重置卷的状态

openstack volume set  --state available VOLUME_ID

6.8 设置为启动卷

openstack volume set --bootable VOLUME_ID

6.9 删除卷

openstack volume delete VOLUME_ID

7. 安全组管理

7.1 查看安全组列表

openstack security group list

7.2 查看安全组规则列表

openstack security group rule list

7.3 查看安全组详细信息

openstack security group show SECURITY_GROUP_ID

7.4 创建安全组

openstack security group create SECURITY_GROUP_NAME
openstack security group create SECURITY_GROUP_NAME --project PROJECT_ID

7.5 在安全组中,添加ssh规则

openstack security group rule create default --dst-port 22 --description ssh SECURITY_GROUP_ID

7.6 在安全组中,添加允许所有icmp、tcp、udp

openstack security group rule create default --protocol icmp --description icmp SECURITY_GROUP_ID
openstack security group rule create default --protocol tcp --description tcp SECURITY_GROUP_ID
openstack security group rule create default --protocol udp --description udp SECURITY_GROUP_ID

7.7 查看规则详细信息

openstack security group rule show SECURITY_GROUP_RULE_ID

7.8 删除规则

openstack security group rule delete SECURITY_GROUP_RULE_ID

7.9 删除安全组

openstack security group delete SECURITY_GROUP_ID

8. 秘钥管理

8.1 查看秘钥列表

openstack keypair list

8.2 创建秘钥

openstack keypair create KEY_PAIR_NAME > KEY_PAIR_NAME.pem

8.3 使用秘钥

openstack server create --flavor FLAVOR_ID --image IMAGE_ID  --network NETWORK_ID --key-name KEY_PAIR_NAME INSTANCE_NAME
chmod 600 KEY_PAIR_NAME.pem
ip netns exec qdhcp-xxxxxx ssh -i KEY_PAIR_NAME.pem [email protected]

8.4 删除秘钥

openstack keypair delete KEY_PAIR_NAME

9. FLAVOR管理

9.1 查看FLAVOR列表

openstack flavor list

9.2 创建FLAVOR

openstack flavor create --ram 8192 --disk 200 --vcpus 4 FLAVOR_NAME

9.3 删除FLAVOR

openstack flavor delete FLAVOR_NAME

10. 资源统计

10.1 查看资源使用情况

openstack usage list

10.2 查看单个项目的资源使用情况

openstack usage show --project 

11. 参考文献:

https://docs.openstack.org/zh_CN/user-guide/index.html
https://docs.openstack.org/python-openstackclient/latest/cli/command-objects/hypervisor.html
https://blog.csdn.net/sun2333/article/details/78559182

你可能感兴趣的:(云计算,openstack)