跳转至

基础-T版-01-简单运维

验证扩容

source demo-openrc
openstack network list

openstack server create --flavor m1.nano --image cirros \
  --nic net-id=8547b120-8083-432f-a5f9-8ea7beeda429 --security-group default \
  --key-name mykey selfservice-instance01

source admin-openrc
openstack server list --all --long

source demo-openrc 
openstack console url show selfservice-instance01

openstack floating ip create provider
openstack server add floating ip selfservice-instance 203.0.113.104


source demo-openrc 
openstack volume create --size 10 selfservice-instance01-volume
openstack server add volume selfservice-instance01 selfservice-instance01-volume

openstack server remove volume selfservice-instance01 selfservice-instance01-volume

节点迁移

冷迁移

冷迁移(cold migration),也叫静态迁移。 在迁移到另外的计算节点时,这段时间虚拟机实例是处于宕机状态的,即关闭电源的虚拟机进行迁移。 通过冷迁移,可以选择将关联的磁盘从一个数据存储移动到另一个数据存储,实例需要重启才能工作。 适用于对象系统可用性要求不是很高的时候,迁移过程中会关机, 拷贝 instance 的镜像文件和重建 libvirt.xml (这个配置文件中会涉及网络环境, 存储设备等配置), 并重新在目标主机上启动。

优点:虚拟机不需要位于共享存储器上,数据丢失率小。
缺点:需要关闭电源,业务中断。
  1. 关闭虚拟机
  2. 找到虚拟机位于 /var/lib/nova/instances 下文件
  3. 将虚拟机的文件全部 copy 到目标主机的相同位置下
  4. 修改用户组
  5. 更新数据库中 host,node 字段为目标主机的名字
  6. 重启目标主机的 nova-compute服务

创建一个虚拟机

source demo-openrc
openstack network list

openstack server create --flavor m1.nano --image cirros \
  --nic net-id=667c3eb8-3162-4794-966d-0b9d591df566 --security-group default \
  --key-name mykey selfservice-instance01

显示运行的虚机

nova list

关闭虚机

nova stop selfservice-instance01

将文件copy到目标主机的对应位置下

scp -rP 10000 355c2264-a2bd-4485-962b-4a231175d39d 192.168.1.223:/var/lib/nova/instances/

chown -R nova.nova /var/lib/nova/instances/355c2264-a2bd-4485-962b-4a231175d39d

修改数据库中的字段

mysql
use nova;
desc instances;
select id,hostname,host from instances;

update instances set host='compute2', node='compute2' where uuid='355c2264-a2bd-4485-962b-4a231175d39d';

查询虚机所在的主机

nova show selfservice-instance01

启动虚拟机

nova start selfservice-instance01

最后检查虚拟机所在的主机

source admin-openrc 
openstack server list --all --long


[root@compute1 instances]# virsh list
 Id    Name                           State
----------------------------------------------------


[root@compute2 instances]# virsh list
 Id    Name                           State
----------------------------------------------------

最后不要忘记清理计算节点上的虚拟机

virsh undefine instance-00000004
rm -fr /var/lib/nova/instances/ff5e871d-14ab-4367-a8ce-35a777df9142

热迁移

https://blog.csdn.net/tony_vip/article/details/123733056

编辑 /etc/libvirt/libvirtd.conf,
去掉 listen_tls = 0、listen_tcp = 1、tcp_port = "16509" 三行前面的注释
修改 auth_tcp = "none"

编辑 /etc/sysconfig/libvirtd,去掉 LIBVIRTD_ARGS="--listen" 前面的注释

执行服务重启命令:
systemctl daemon-reload
systemctl restart libvirtd
systemctl status libvirtd


ps -ef|grep libvirtd
ss -ltnp|grep 16509
virsh -c qemu+tcp://192.168.1.219/system
virsh -c qemu+tcp://192.168.1.220/system


vi /etc/nova/nova.conf
live_migration_flag=VIR_MIGRATE_UNDEFINE_SOURCE,VIR_MIGRATE_PEER2PEER,VIR_MIGRATE_LIVE

systemctl restart openstack-nova-compute.service


openstack server list --long --all
nova live-migration eb77e2dc-1516-424a-81fe-e5a1c78cc6e2 compute1

下线节点

下线Nova-compute

https://www.cnblogs.com/kevingrace/p/6024251.html

nova service-list
nova  service-disable 154ad59a-d986-446e-91ac-fab6dbf47f0b

不推荐删除
nova  service-delete 154ad59a-d986-446e-91ac-fab6dbf47f0b

systemctl disable libvirtd.service openstack-nova-compute.service
systemctl stop libvirtd.service openstack-nova-compute.service

下线Neutron-compute

systemctl disable neutron-linuxbridge-agent.service
systemctl stop neutron-linuxbridge-agent.service

neutron agent-list
neutron agent-delete 154ad59a-d986-446e-91ac-fab6dbf47f0b

下线Cinder-compute

systemctl disable openstack-cinder-volume.service target.service
systemctl stop openstack-cinder-volume.service target.service

systemctl disable openstack-cinder-backup
systemctl stop openstack-cinder-backup

cinder service-list
cinder service-disable compute1 cinder-volume

cinder service-disable compute2@nfs cinder-volume 
cinder service-disable compute2@lvm cinder-volume 
cinder service-disable compute2@rbd cinder-volume 


cinder service-disable compute1@nfs cinder-volume 
cinder service-disable compute1@lvm cinder-volume 
cinder service-disable compute1@rbd cinder-volume 

cinder service-disable compute3@nfs cinder-volume 
cinder service-disable compute3@lvm cinder-volume 
cinder service-disable compute3@rbd cinder-volume 

不推荐删除
cinder-manage service remove  cinder-volume compute3
openstack volume service list

恢复节点

查看服务状态

source /root/admin-openrc 
nova service-list
neutron agent-list
cinder service-list

恢复Nova-compute

systemctl enable libvirtd.service openstack-nova-compute.service
systemctl start libvirtd.service openstack-nova-compute.service

source /root/admin-openrc 
su -s /bin/sh -c "nova-manage cell_v2 discover_hosts --verbose" nova

nova service-list 
nova  service-enable 86d37f11-26b6-4c56-9496-383b4dd750ca
nova service-list 

恢复Neutron-compute

systemctl enable neutron-linuxbridge-agent.service
systemctl start neutron-linuxbridge-agent.service

source admin-openrc 
neutron agent-list

恢复Cinder-compute

systemctl enable openstack-cinder-volume.service target.service
systemctl start openstack-cinder-volume.service target.service

systemctl enable openstack-cinder-backup
systemctl start openstack-cinder-backup

source admin-openrc
cinder service-enable compute3 cinder-volume


cinder service-enable compute2@nfs cinder-volume 
cinder service-enable compute2@lvm cinder-volume 
cinder service-enable compute2@rbd cinder-volume 
cinder service-list

验证恢复节点

source demo-openrc
openstack network list

openstack server create --flavor m1.nano --image cirros \
  --nic net-id=8547b120-8083-432f-a5f9-8ea7beeda429 --security-group default \
  --key-name mykey selfservice-instance02

source admin-openrc
openstack server list --all --long

source demo-openrc 
openstack console url show selfservice-instance02

openstack floating ip create provider
openstack server add floating ip selfservice-instance02 203.0.113.124


source demo-openrc 
openstack volume create --size 10 selfservice-instance02-volume
openstack server add volume selfservice-instance02 selfservice-instance02-volume

openstack server remove volume selfservice-instance02 selfservice-instance02-volume

控制节点服务

恢复glance-controller

systemctl restart openstack-glance-api

恢复nova-controller

systemctl restart \
    openstack-nova-api.service \
    openstack-nova-scheduler.service \
    openstack-nova-conductor.service \
    openstack-nova-novncproxy.service

恢复neutron-controller

systemctl restart neutron-server.service \
  neutron-linuxbridge-agent.service neutron-dhcp-agent.service \
  neutron-metadata-agent.service \
  neutron-l3-agent.service

恢复cinder-controller

systemctl restart openstack-cinder-api.service openstack-cinder-scheduler.service

快速克隆节点

计算节点一定是虚拟机,那么就满足快速克隆计算节点的要求

首先需要将openstack集群中的虚拟机和卷都清理干净

1.修改主机名称

2.修改IP地址

3.修改nova配置文件
vim /etc/nova/nova.conf 
server_listen = 192.168.1.219
server_proxyclient_address = 192.168.1.219

systemctl restart libvirtd.service openstack-nova-compute.service

4.修改neutron配置文件
vim /etc/neutron/plugins/ml2/linuxbridge_agent.ini
local_ip = 192.168.1.219

systemctl enable neutron-linuxbridge-agent.service
systemctl restart neutron-linuxbridge-agent.service


5.启动cinder服务
systemctl enable openstack-cinder-volume.service target.service
systemctl start openstack-cinder-volume.service target.service
systemctl enable openstack-cinder-backup
systemctl start openstack-cinder-backup


解决pymysql.err.OperationalError: (1040, ‘Too many connections‘)
https://blog.csdn.net/LelemamaAnne/article/details/113585805

获取管理员凭据以启用仅限管理员的 CLI 命令,然后确认数据库中有计算主机:

source admin-openrc
openstack compute service list --service nova-compute

发现计算主机:

su -s /bin/sh -c "nova-manage cell_v2 discover_hosts --verbose" nova

添加新计算节点时,必须在控制器节点上运行以注册这些新计算节点。或者,您可以在 中设置适当的间隔 :nova-manage cell_v2 discover_hosts``/etc/nova/nova.conf

[scheduler]
discover_hosts_in_cells_interval = 300

创建虚拟机测试

source /root/demo-openrc
openstack network list

openstack server create --flavor m1.nano --image cirros \
  --nic net-id=8547b120-8083-432f-a5f9-8ea7beeda429 --security-group default \
  --key-name mykey selfservice-instance

虚拟化类型

解决openstack虚拟机性能慢的方法: 1.计算节点直接使用物理机 virt_type = kvm 2.将vcenter虚拟机的cpu透传到openstack虚拟机上

检查服务器是否支持虚拟化

egrep -c '(vmx|svm)' /proc/cpuinfo

我们可以修改虚拟化类型直接使用物理机CPU

virsh capabilities

[root@compute1 ~]# vim /etc/nova/nova.conf 
[libvirt]
注释此行
virt_type = qemu

添加如下行
virt_type = kvm
# 更改虚拟化类型
hw_machine_type = x86_64=pc-i440fx-rhel7.2.0
# 直接使用宿主机cpu
cpu_mode = host-passthrough

virt_type = kvm
hw_machine_type = x86_64=pc-i440fx-rhel7.2.0
cpu_mode = host-passthrough

systemctl restart libvirtd.service openstack-nova-compute.service

nova delete centos79001
sleep 5

openstack server create --flavor 2C2G --image linux-node-moban   --nic net-id=8547b120-8083-432f-a5f9-8ea7beeda429 --security-group default   --key-name mykey centos79001

source demo-openrc 
openstack console url show centos79001

openstack floating ip create provider
openstack server add floating ip centos79001 203.0.113.109

> /root/.ssh/known_hosts 
ssh 203.0.113.109

web界面问题

openstack错误-dashboard-not found

https://www.cnblogs.com/learn2teach/p/12443185.html

出现如下报错的解决方法

Not Found
The requested URL /auth/login/ was not found on this server.

解决方法如下步骤:

vim /etc/openstack-dashboard/local_settings

WEBROOT="/dashboard"


vim /etc/httpd/conf.d/openstack-dashboard.conf
WSGIApplicationGroup %{GLOBAL}

WSGIScriptAlias /dashboard /usr/share/openstack-dashboard/openstack_dashboard/wsgi/django.wsgi
Alias /dashboard/static /usr/share/openstack-dashboard/static

systemctl restart httpd.service memcached.service

访问web界面

http://controller/dashboard

image-20220516113748148

提供商网络添加子网

source /root/admin-openrc 
openstack subnet  list


openstack subnet create --network provider \
  --allocation-pool start=10.10.11.10,end=10.10.11.100 \
  --dns-nameserver 114.114.114.114 --gateway 10.10.11.1 \
  --subnet-range 10.10.11.0/24 provider-public


source /root/demo-openrc 
openstack floating ip  create --subnet provider-public provider
openstack floating ip list

ip addr add 10.10.11.251/24 dev eth0

虚拟机开启嵌套虚拟化

openstack开启kvm虚拟机嵌套 https://blog.csdn.net/dylloveyou/article/details/81267690

嵌套式虚拟nested是一个可通过内核参数来启用的功能。它能够使一台虚拟机具有物理机CPU特性,支持vmx或者svm(AMD)硬件虚拟化。关于nested的具体介绍,可以看这里 。该特性需要内核升级到Linux 3.X版本 ,所以在centos6下是需要先升级内核的,而在centos7下已默认支持该特性,不过默认是不开启的,需要通过修改参数支持。

启用Nested

echo 'options kvm_intel nested=1' >/etc/modprobe.d/kvm-nested.conf

卸载模块

modprobe -r kvm_intel

重新加载模块

modprobe kvm_intel

查看Nested是否启用成功

cat /sys/module/kvm_intel/parameters/nested
Y

在nova的配置文件修改cpu mode

vim /etc/nova/nova.conf

[libvirt]
cpu_mode=host-passthrough

systemctl restart libvirtd.service openstack-nova-compute.service

验证是否开启嵌套虚拟化

source /root/admin-openrc
openstack role add --project myproject --user myuser admin
openstack flavor list
nova service-list

source /root/demo-openrc
openstack image list
openstack network list


openstack server create --flavor 4C8G --image ceph-centos79200920220510 \
  --availability-zone nova:compute2 \
  --nic net-id=667c3eb8-3162-4794-966d-0b9d591df566 --security-group    342104a8-b7a9-47a9-910e-11cf4d1d0e26 \
  --key-name mykey centos79003

lsmod|grep kvm
yum install qemu-kvm qemu-kvm-tools virt-manager libvirt virt-install wget -y 
systemctl enable libvirtd.service
systemctl start libvirtd.service

ssh 192.168.57.167 
mkdir -p /data/kvm-data/linux-node-moban/
qemu-img create -f qcow2 /data/kvm-data/linux-node-moban/linux-node-moban.qcow2 100G
ssh 192.168.57.167 

virsh destroy linux-node-moban
virsh undefine linux-node-moban

virt-install --name linux-node-moban --virt-type kvm --vcpus 2 --ram 2048 \
--disk path=/data/kvm-data/linux-node-moban/linux-node-moban.qcow2  \
--cdrom=/tmp/CentOS-7-x86_64-Minimal-2009.iso  \
--channel  unix,path=/var/lib/libvirt/qemu/linux-node-moban.agent,mode=bind,target_type=virtio,name=org.qemu.guest_agent.0   \
--network network=default  \
--graphics vnc,listen=0.0.0.0  \
--noautoconsole

虚拟机开启内核转发

source /root/demo-openrc 
openstack floating ip  create --subnet provider provider
openstack floating ip list

openstack server create --flavor 4C8G --image ceph-centos79200920220510 \
  --availability-zone nova:compute2 \
  --nic net-id=667c3eb8-3162-4794-966d-0b9d591df566 --security-group    342104a8-b7a9-47a9-910e-11cf4d1d0e26 \
  --key-name mykey centos79002

nova list

方式一:放开allow_address_pair 权限
查询port id
openstack port list
nova interface-list vm_uuid

放开当前机器allow_address_pair 权限
neutron port-show PORT_ID
neutron port-update --allowed-address-pair ip_address=0.0.0.0/0 PORT_ID

yum -y install iptables-services
echo net.ipv4.ip_forward = 1  >>/etc/sysctl.conf;sysctl -p
iptables -t nat -I POSTROUTING -s 0.0.0.0/0 -j MASQUERADE ; iptables-save >/etc/sysconfig/iptables ; iptables -t nat -S ; systemctl enable iptables

计算节点超配

内存和磁盘超配虽然能提供更多数量的虚拟机,当该宿主机上大量虚拟机的负载都很高时,轻着影响虚拟机性能,重则引起 qemu-kvm 相关进程被杀,即虚拟机被关机。因此对于线上稳定性要求高的业务,建议不要超配 RAM 和 DISK,但可适当超配 CPU。建议这几个参数设置为:

CPU: CONF.cpu_allocation_ratio = 4
RAM: CONF.ram_allocation_ratio = 1.0
DISK: CONF.disk_allocation_ratio = 1.0
RAM-Reserve: CONF.reserved_host_memory_mb = 2048
DISK-Reserve: CONF.reserved_host_disk_mb = 20480

在OpenStack中,默认的CPU超配比例是1:16,内存超配比例是1:1.5。当宿主机使用swap交换分区来为虚拟机分配内存的时候,则虚拟机的性能将急速下降。生产环境上不建议开启内存超售(建议配置比例1:1)。另外,建议设置nova.conf文件中的reserved_host_memory_mb 参数,即内存预留量(建议至少预留4GB),保证该部分内存不能被虚拟机使用。

vim /etc/nova/nova.conf

[DEFAULT]
reserved_host_memory_mb=4096

#磁盘预留空间,这部分空间不能被虚拟机使用
reserved_host_disk_mb= 10240 

默认OpenStack的CPU超配比例是1:16,内存超配比例是1:1.5。下面配置的就是这个比例,你可以自己算一下,cat /proc/cpuinfo里面的逻辑核数,再x16就是你能够分配给虚拟机的。内存也是类似。

物理cpu数

cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l

单块cpu核数

cat /proc/cpuinfo| grep "cpu cores"|wc -l

总核数 = 物理CPU个数 × 每颗物理CPU的核数

cpu和内存的超售目的就是为了资源利用的最大化

[root@compute2 ~]# vim /etc/nova/nova.conf
disk_allocation_ratio = 10.0
cpu_allocation_ratio = 10.0
ram_allocation_ratio = 10.0

systemctl restart openstack-nova-compute.service

需要重启控制节点服务

systemctl restart openstack-nova-api.service openstack-nova-scheduler.service openstack-nova-conductor.service

虚拟机开救援模式

https://blog.csdn.net/weixin_33810006/article/details/93227979

在控制节点执行如下命令nova rescue vm_uuid

nova rescue 473fbedd-73e3-4163-a859-4010b0e8c4ae --password passw0rd

Note:

这里最好要指定密码,并且作为一个通用救援密码rescue_password,该虚拟机有第一次rescue之后rescue密码就不能改变了,以后再对该虚拟机进行救援,只能使用这个密码了,下次再设置是无效的。

Cause: rescue虚拟机会在ceph里生成一个用指定镜像(如果没有指定则是使用虚拟机的源镜像)创建的卷,rebuild一个newVM,并将原虚拟机的卷作为vdb挂载给newVM,如果已经存在rescue的卷则不会再次生成,所以第二次rescue的密码并不能修改,除非手动删除该rescue卷.

[root@node-1 ~]# rbd ls compute|grep 473fbedd-73e3-4163-a859-4010b0e8c4ae
473fbedd-73e3-4163-a859-4010b0e8c4ae_disk
473fbedd-73e3-4163-a859-4010b0e8c4ae_disk.rescue

在删除该虚拟机的时候会将rescue的卷一并删除.

此时虚拟机会进行重启,Horizon界面的虚拟机状态变为"救援",nova list命令看到虚拟机也会变成RESCUE状态.

rescue的过程,会用创建虚拟机的image作为引导镜像来引导虚拟机,并把虚拟机作为一个vdb盘,挂载到虚拟机上,进入虚拟机后可以通过lsblk可以看到

[root@wl-test2 ~]# lsblk
NAME   MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda    253:0    0  10G  0 disk 
└─vda1 253:1    0  10G  0 part /
vdb    253:16   0  60G  0 disk 
└─vdb1 253:17   0  60G  0 part 

Note:

如何进入rescue模式的虚拟机,此时可以通过ssh连接并使用root/rescue_password 进入虚拟机。比例如:

nova ssh vm_uuid (此方式需要虚拟机有一个物理节点能通信的ip,或者从命名空间ssh连接)

或者通过vnc的方式,页面上不显示vnc标签页,只能通过命令获取vnc地址:

nova get-vnc-console 473fbedd-73e3-4163-a859-4010b0e8c4ae novnc

将vdb即虚拟机的磁盘挂载给/mnt目录(目录可以任意指定),挂载命令如下:

mount -o nouuid /dev/vdb1 /mnt

此时cd /mnt后,可以看原虚拟机的所有目录

[root@wl-test2 ~]# ls /mnt
bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
boot  etc  lib   media  opt  root  sbin  sys  usr

修复场景:忘记了root密码

[root@wl-test2 ~]# chroot /mnt
[root@wl-test2 /]# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@wl-test2 /]# passwd
Changing password for user root.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.

此时root密码已经修改完成

退出rescue状态后,即可使用新密码登录虚拟机root用户

2. xfs文件系统损坏

xfs_repair /dev/vdb1

如果此时有报错 ERROR: The filesystem has valuable metadata changes in a log which needs to be replayed. ....

执行

xfs_repair -L /dev/vdb1 清理掉xfs的日志信息(可能会丢失一些尚未完成的日志数据),再修复

退出rescue模式:

修复完成后,退出rescue模式,在控制节点执行nova unrescue vm_uuid

[root@node-1 ~]# nova unrescue 5ec5fed5-90ac-464e-a584-6b303c29ff0a

之后,虚拟机会再次重启,进入正常的运行状态,此时界面上的虚拟机的状态变回运行中。

计算节点疏散

https://docs.openstack.org/nova/queens/admin/evacuate.html

1)在疏散之前,需要把故障机器管理的卷迁移走

如果要疏散虚机挂载的卷正好被故障机器管理,则疏散过程中会出现错误。

使用命令:

在控制节点执行

cinder --os-volume-api-version 3.33 list --filters host=compute3 --all

查看是否有管理的卷,如果有,使用如下命令迁移卷,目标节点是正常节点。

cinder service-list

cinder-manage --config-file /etc/cinder/cinder.conf volume update_host --currenthost compute3@rbd --newhost compute1@rbd

cinder-manage --config-file /etc/cinder/cinder.conf backup update_host --currenthost compute3@rbd --newhost compute1@rbd

注意:上述命令中尤其是主机名参数一定不能错,因为该命令参数出错也不会报错。

2) 再次验证故障机器上是否有管理的卷

cinder --os-volume-api-version 3.33 list --filters host=compute1 --all
cinder --os-volume-api-version 3.33 list --filters host=compute3 --all

输出空,则说明成功迁移走了卷。

主机查询

source /root/admin-openrc
openstack host list

一台计算节点上的一台虚拟机疏散到另外一台计算节点

nova evacuate 9ea34ae5-fcc7-4110-ba01-390ccfc458ae compute1

nova hypervisor-list
nova hypervisor-show 334261c9-b850-42e9-879b-37782a18c23a   


如果疏散失败可以重置虚拟机状态
nova reset-state --active  23a90bdd-747c-4851-b33e-c69bbed2bb15


openstack server set --state active 21e3b795-4ed6-4ca3-b549-e9cd3ca4a4e2

一台计算节点的所有虚拟机疏散到另外一台计算节点

nova host-evacuate --target_host compute1 compute3

虚拟机热迁移

热迁移单个虚拟机

source /root/admin-openrc
openstack server list --long --all
nova live-migration eb77e2dc-1516-424a-81fe-e5a1c78cc6e2 compute1
openstack server list --long --all

热迁移批量虚拟机

source /root/admin-openrc
openstack server list --long --all
nova host-evacuate-live --target-host compute3 compute1
openstack server list --long --all

cinder卷清理

openstack 后期维护(四)--- 删除僵尸卷 http://www.manongjc.com/detail/25-gwvjcrjjpykydby.html

在长时间使用openstack之后,删除虚机后,经常会有因这样那样的问题,导致卷处于僵尸状态,无法删除!

情况

虚机已近删除,然而卷却挂在到了 None上无法删除

source /root/admin-openrc 
cinder reset-state --attach-status detached 1b3f852c-cb2f-4b8e-ac38-134e37a08c8a
source /root/demo-openrc 
cinder delete 1b3f852c-cb2f-4b8e-ac38-134e37a08c8a


source /root/admin-openrc 
cinder reset-state --attach-status attached eef5fb60-3ca7-4bdf-9a32-75f800ee10cf

情况2:

卷状态为可用状态,但却删除失败。解决办法:

1、首先排查此卷是否有卷快照,需要先删除快照,才可以删除卷。
2、如果删除了快照还是不可以删除卷,那就只能暴力一些,从数据库上动手了

# use cinder;
# select * from volumes where id = 'ID';
# update volumes set deleted=1 ,status = 'deleted' where id ='ID';

efe7784c-6720-48e7-9110-cb2753b4d70b

#解释下:deleted 字段:0 表示正常, 1表示已删除(在web中不可见)
            attach_status: detached   表示不连接
            status:  deleting表示已删除     in-use表示使用中    available 可用状态

虚拟机自动启动

1.1 参数描述与默认值

# Whether to start guests that were running before the host rebooted (boolean
# value)
#resume_guests_state_on_host_boot=false

1.2 修改配置为状态恢复

[root@compute3 ~]# vim /etc/nova/nova.conf
[DEFAULT]
resume_guests_state_on_host_boot=true

强制删除镜像

openstack中镜像的删除是会将后端存储内的镜像文件实体删除掉,同时将数据库内镜像记录的状态改为删除状态。当需要重复上传相同ID镜像的时候就会影响下一次镜像的上传。我是因为第一次上传写错了镜像类型,所以需要重新上传

直接删除数据库内的相关记录 假设镜像的uuid为‘5ed778f9-13a9-4e60-9f29-4ea06c88a81d’

use glance;
select * from images;


delete from image_locations where image_id = '19fc500b-635d-4375-ac93-e09831129d0d';
delete from image_properties where image_id = '19fc500b-635d-4375-ac93-e09831129d0d';
delete from images where id = '19fc500b-635d-4375-ac93-e09831129d0d';

批量删除镜像的脚本

#!/bin/bash

IMAGE_ID_LIST="27684dee-d83a-46be-a2c9-379c0347b340 3f3ca205-f2f2-412e-93e7-e8c0b3f2f780 0ab6491f-8aa3-4cd9-905c-6348773a6d8d f93bceca-aded-4fa5-b47c-b5ca6fedefec 50285133-afc8-47dd-b435-83dce6e15b1f fa9857ee-1c55-42cc-adcd-8fc822ed27fa f481834d-37ee-415c-97a8-d0cf66dec5b4 060f6112-d68d-4bf6-b0e8-edbd0cee16b2 3472748e-609f-44c7-915e-9c5edfce8f23"

    echo 'use glance;'
for n in $IMAGE_ID_LIST
do
    IMAGE_ID=$n
    echo 'delete from image_locations where image_id = '\'${IMAGE_ID}\'';'
    echo 'delete from image_properties where image_id = '\'${IMAGE_ID}\'';'
    echo 'delete from images where id = '\'${IMAGE_ID}\'';'
done

修改镜像名称

glance image-update --name centos79200920220510 faa80086-962d-4dee-9925-ba8be0319c23

初始化虚拟机密码

#!/bin/bash
echo "123"|passwd --stdin root

文件和元数据注入

在创建虚拟机的时候,用户往往需要对虚拟机进行一些配置,比如:开启一些服务、安装某些包、添加 SSH 秘钥、配置 hostname 等等。在 OpenStack 中,这些配置信息被分成两类:metadata 和 user data。Metadata 主要包括虚拟机自身的一些常用属性,如 hostname、网络配置信息、SSH 登陆秘钥等,主要的形式为键值对。而 user data 主要包括一些命令脚本等。User data 通过文件传递,并支持多种文件格式,包括 gzip 压缩文件、shell 脚本、cloud-init 配置文件等。虽然 metadata 和 user data 并不相同,但是 OpenStack 向虚拟机提供这两种信息的机制是一致的,只是虚拟机在获取到信息后,对两者的处理方式不同罢了。所以下文统一用 matadata 来描述。

https://www.pubyun.com/blog/openstack/%E4%BB%80%E4%B9%88%E6%98%AFopenstack%E7%9A%84-metadata/

https://www.csdn.net/tags/MtzaUg0sMDYzNTYtYmxvZwO0O0OO0O0O.html

curl -sL 169.254.169.254/openstack/2018-08-27/meta_data.json|python -m json.tool

openstack在创建实例的时候,可以对实例进行文件注入,注入的内容可为: key ssh公钥; network 网络参数; admin_password 注入超级用户root 的密码; metadata 注入元数据meta.js;

file 注入文件

首先来看各种文件注入的命令行实现: 注入ssh密钥 创建密钥(ssh-keygen),添加密钥对; nova keypair-add --pub_key.ssh/id_rsa.pub mykey 创建实例的时候注入密钥; nova boot --key_name mykey --imageid_of_image --flavor 1 name_of_instance

注入文件 假如我要注入.vimrc这个文件到新创建的实例中,可以: nova boot --file /root/.vimrc=/root/.vimrc--image id_of_image --flavor 1 name_of_instance 注:可以注入多个文件(最多5个),只要写多个--file;

注入元数据 可以通过--meta给实例中传入键值对,注入后会写在/meta.js文件里,以类似python字典的方式存储在虚拟机的/meta.js文件中; nova boot --meta key2=value2 --metakey1=value1 --image id_of_image --flavor 1 name_of_instance cat /meta.js {”key2〃: “value2〃,“key1〃: “value1〃}