#镜像制作官网参考文档
https://docs.openstack.org/image-guide/
#这里仅演示centos镜像制作,ubuntu配置一样。也可以参考官方文档制作。
#cloud-init:
#cloud-init 是 linux 的一个工具,需要在制作镜像时安装。当系统启动时,cloud-init 可从 nova metadata 服务或者 config drive 中获取 metadata,完成包括但不限于下面的定制化工作:
设置默认语言环境
设置实例主机名
添加 ssh keys到 .ssh/authorized_keys
设置用户密码
配置网络安装软件包
#neutron-controller安装--配置元数据代理中,/etc/neutron/metadata_agent.ini文件和/etc/nova/nova.conf文件中的metadata_proxy_shared_secret = tan20220920必须配置密码一样。/etc/neutron/metadata_agent.ini文件中的nova_metadata_host = openstack-vip.tan.local定义的时域名,因此必须在haproxy中添加nova-api监听的8775端口转发到openstack-vip.magedu.local域名,cloud-init才能获取得到metadata数据。
#在ha1节点添加一个nova-api端口8775的转发
[root@openstack-ha1 ~]# vim /etc/haproxy/haproxy.cfg
listen openstack-nova-api-8775
bind 10.0.0.188:8775
mode tcp
server 10.0.0.101 10.0.0.101:8775 check inter 3s fall 3 rise 5
[root@openstack-ha1 ~]# systemctl restart haproxy
[root@openstack-ha1 ~]# ss -tnl |grep 8775
LISTEN 0 128 10.0.0.188:8775 *:*
#通过KVM安装ubuntu和Centos操作系统步骤并将磁盘文件作为镜像上传到openstack glance
##因镜像虚拟机需要连接外网安装 rpm 包,需要网络桥接,因此将 bondX 桥接到 brX:
#单独准备一台10.0.0.162的主机,安装kvm做镜像
[root@images ~]# yum install -y bridge-utils
[root@images ~]# cd /etc/sysconfig/network-scripts/
[root@images network-scripts]# vim ifcfg-bond0
BOOTPROTO=static
NAME=bond0
DEVICE=bond0
ONBOOT=yes
BONDING_MASTER=yes
BONDING_OPTS="mode=1 miimon=100"
BRIDGE=br0
[root@images network-scripts]# cat ifcfg-br0
TYPE=Bridge
BOOTPROTO=static
NAME=br0
DEVICE=br0
ONBOOT=yes
IPADDR=10.0.0.10
NETMASK=255.255.255.0
GATEWAY=10.0.0.2
DNS1=10.0.0.2
[root@images network-scripts]# systemctl restart network
#安装图形桌面支持:后期会调用图形配置镜像网卡等
[root@images ~]# yum groupinstall "GNOME Desktop" –y
#重启系统:重启之后确保以上各网卡通信正常,然后才进行下一步镜像制作
[root@images ~]# reboot
#centos7.9镜像制作
#做镜像就是在宿主机最小化安装系统并配置优化,做完配置之后将虚拟机关机,然后将虚拟机磁盘文件上传至 glance 即可启动虚拟机。
#安装kvm环境
[root@images ~]# yum install -y qemu-kvm qemu-kvm-tools libvirt virt-manager virt-install
#创建磁盘
[root@images ~]# qemu-img create -f qcow2 /var/lib/libvirt/images/CentOS7.9.qcow2 30G
Formatting '/var/lib/libvirt/images/CentOS7.9.qcow2', fmt=qcow2 size=10737418240 encryption=off cluster_size=65536 lazy_refcounts=off
#下载iso镜像并安装
[root@images ~]# ll CentOS-7.9-x86_64-DVD-2009.iso
-rw-r--r-- 1 root root 4712300544 Sep 21 15:39 CentOS-7.9-x86_64-DVD-2009.iso
[root@images opt]# virt-install --virt-type kvm --name CentOS7-x86_64 --ram 2024 --cdrom=/opt/CentOS-7.9-x86_64-DVD-2009.iso --disk path=/var/lib/libvirt/images/CentOS7.9.qcow2 --network bridge=br0 --graphics vnc,listen=0.0.0.0 --noautoconsole
WARNING No operating system detected, VM performance may suffer. Specify an OS with --os-variant for optimal results.
Starting install...
Domain installation still in progress. You can reconnect to
the console to complete the installation process.
#如果报错不支持kvm,请关闭虚拟机,在虚拟机设置--处理器--勾选“虚拟化intel VT-x/EPT或AMD-V/RVI(V)”,再开机。
#验证端口
[root@images opt]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 1 *:5900 *:*
#打开控制台--打开虚拟机的控制台
[root@images opt]# virt-manager
#传递内核参数重命名网卡为eth0.在centos7安装界面,选择install centos7 按tab键,在内核参数后添加net.ifnames=0 biosdevname=0,回车
#选择语言,磁盘分区,配置时区等等,安装系统,安装完成重启系统,第一次起不来,需要手动开机。
#控制台登录配置好网卡后,10.0.0.164的ip地址。通过ssh远程连接配置安装基础包,安装httpd,安装cloud-init,配置ansible等的公钥等等。
[root@images opt]# ssh 10.0.0.164
[root@10 ~]# hostnamectl set-hostname openstack-centos7
[root@openstack-centos7 ~]#yum install cloud-init -y
[root@openstack-centos7 ~]# systemctl enable cloud-init
[root@openstack-centos7 ~]# yum install cloud-utils-growpart -y
[root@openstack-centos7 ~]#yum install -y httpd
[root@openstack-centos7 ~]# echo test 11111 >/var/www/html/index.html
[root@openstack-centos7 ~]# systemctl enable httpd --now
[root@openstack-centos7 ~]# curl localhost
test 11111
[root@openstack-centos7 ~]#exit
[root@images opt]# scp /var/lib/libvirt/images/CentOS7.9.qcow2 10.0.0.101:/root
#上传镜像。注意存放镜像的nfs目录空间不够可以挂一块新盘上去,先把文件拷贝出来,挂盘后再把文件拷贝进去。注意挂盘后nfs的/data/glance目录的权限会变,注意chown更改成glance的用户id
[root@openstack-controller1 ~]# openstack image create "centos7-image" --file /root/centOS7.qcow2 --disk-format qcow2 --container-format bare --public
+------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Field | Value |
+------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| checksum | 6c936392d20ae2ba361d18f36a8fe78b |
| container_format | bare |
| created_at | 2022-09-21T11:46:11Z |
| disk_format | qcow2 |
| file | /v2/images/4c3c5d96-67c0-4b05-a776-860a0427b0af/file |
| id | 4c3c5d96-67c0-4b05-a776-860a0427b0af |
| min_disk | 0 |
| min_ram | 0 |
| name | centos77 |
| owner | f7c80417780a4bd59beaa38a9d36271e |
| properties | os_hash_algo='sha512', os_hash_value='fbac06400d5df6998d9bb8096927004681dcf90ea263ec9085d0f6b26a41df8e2796075acd7d298f0f7d19123bd9294c33acf71c2f10d6c0b0dbececfd67f43e', os_hidden='False' |
| protected | False |
| schema | /v2/schemas/image |
| size | 1821638656 |
| status | active |
| tags | |
| updated_at | 2022-09-21T11:46:34Z |
| virtual_size | None |
| visibility | public |
+------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
[root@openstack-ha1 ~]# iotop
Total DISK READ : 0.00 B/s | Total DISK WRITE : 63.87 M/s
Actual DISK READ: 0.00 B/s | Actual DISK WRITE: 241.15 M/s
#重新创建虚拟机类型
[root@openstack-controller1 ~]# openstack flavor create --vcpus 1 --ram 2048 --disk 30 centos-template
+----------------------------+--------------------------------------+
| Field | Value |
+----------------------------+--------------------------------------+
| OS-FLV-DISABLED:disabled | False |
| OS-FLV-EXT-DATA:ephemeral | 0 |
| disk | 30 |
| id | 558d4bbf-c137-4d83-8978-55fa8040ab62 |
| name | centos-template |
| os-flavor-access:is_public | True |
| properties | |
| ram | 2048 |
| rxtx_factor | 1.0 |
| swap | |
| vcpus | 1 |
+----------------------------+--------------------------------------+
#扩容compute1计算节点根分区,先在VMwareworkstation上给compute1节点添加一块100G的scsci磁盘。
[root@openstack-compute1 ~]#echo "- - -" > /sys/class/scsi_host/host0/scan
[root@openstack-compute1 ~]#echo "- - -" > /sys/class/scsi_host/host1/scan
[root@openstack-compute1 ~]#echo "- - -" > /sys/class/scsi_host/host2/scan
[root@openstack-compute1 ~]#fdisk -l
[root@openstack-compute1 ~]#mkfs.xfs /dev/sdb
[root@openstack-compute1 ~]#pvcreate /dev/sdb
[root@openstack-compute1 ~]#vgextend centos /dev/sdb
[root@openstack-compute1 ~]#lvdisplay
[root@openstack-compute1 ~]#lvextend -l +100%FREE /dev/centos/root
[root@openstack-compute1 ~]#df -h
[root@openstack-compute1 ~]#xfs_growfs /dev/centos/root
#创建实例
[root@openstack-controller1 ~]# openstack server create --flavor centos-template --image centos77 --nic net-id=86a33d1d-a373-4c7d-90fe-31c4d3018546 --security-group e2a17048-c290-443d-bdb9-7ad34cb43e5d --key-name mykey centos7.9
+-------------------------------------+--------------------------------------------------------+
| Field | Value |
+-------------------------------------+--------------------------------------------------------+
| OS-DCF:diskConfig | MANUAL |
| OS-EXT-AZ:availability_zone | |
| OS-EXT-SRV-ATTR:host | None |
| OS-EXT-SRV-ATTR:hypervisor_hostname | None |
| OS-EXT-SRV-ATTR:instance_name | |
| OS-EXT-STS:power_state | NOSTATE |
| OS-EXT-STS:task_state | scheduling |
| OS-EXT-STS:vm_state | building |
| OS-SRV-USG:launched_at | None |
| OS-SRV-USG:terminated_at | None |
| accessIPv4 | |
| accessIPv6 | |
| addresses | |
| adminPass | 6GQgjS2pmSAe |
| config_drive | |
| created | 2022-09-21T11:50:45Z |
| flavor | centos-template (558d4bbf-c137-4d83-8978-55fa8040ab62) |
| hostId | |
| id | af9ef6f1-6eb9-4a8b-9c65-995f383a9b76 |
| image | centos77 (4c3c5d96-67c0-4b05-a776-860a0427b0af) |
| key_name | mykey |
| name | centos7.9 |
| progress | 0 |
| project_id | f7c80417780a4bd59beaa38a9d36271e |
| properties | |
| security_groups | name='e2a17048-c290-443d-bdb9-7ad34cb43e5d' |
| status | BUILD |
| updated | 2022-09-21T11:50:45Z |
| user_id | 317a4ea405d745bbb9e5d76fc87a0751 |
| volumes_attached | |
+-------------------------------------+--------------------------------------------------------+
[root@openstack-controller1 ~]# openstack server list
+--------------------------------------+-----------+--------+------------------------+----------+-----------------+
| ID | Name | Status | Networks | Image | Flavor |
+--------------------------------------+-----------+--------+------------------------+----------+-----------------+
| af9ef6f1-6eb9-4a8b-9c65-995f383a9b76 | centos7.9 | ACTIVE | external-net=10.0.0.92 | centos77 | centos-template |
+--------------------------------------+-----------+--------+------------------------+----------+-----------------+
[root@openstack-controller1 ~]# ssh 10.0.0.92
Last login: Wed Sep 21 20:03:18 2022 from 10.0.0.101
[root@centos7 ~]#检查haproxy的nova_metadata的8775端口是否代理
[root@centos7 ~]# curl -f http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC9if6zVTnO+Rlrb5qrVVihVw8LydfO+ToJKnk+PB8N+dXIBytlEoJmIuXxw0QaHnmyRc6QODiTnPVe8ZAnp5Pfe85xPzMDhwi6syrIiRGXZ+qITyMhSpDZNlaKqpX7hBgn1dekOgbxh5aTisxopTPONV6CsHPB2OFCXSTCcaMkpK6dofJZfiTkq3FiIRXQKGU3/a842nFWyDbilcZZZ20xPcFmIlNFf6+rqSnKyOxjlQFfmw0mhtYTszkDNMuS6ApDCBGNHRnDnxGa9+4ndfDFl+R2L+K3r5ux6/Ydkl1K0wVEtjf24+Tuv+gh+8VGxA1RZGHh15SRuVsCycYqjuv3 [email protected]
#开启了cloud-init会使虚拟机开机贼慢,一般第一次修改主机名,获取controller公钥,等等功能才开启一次即可。后续可关闭
![](/i/l/?n=22&i=blog/1914165/202209/1914165-20220926142959167-1411046953.png)
![](/i/l/?n=22&i=blog/1914165/202209/1914165-20220926143013080-1432291946.png)
标签:10.0,自定义,centos,--,centos7,images,openstack,root From: https://www.cnblogs.com/tanll/p/17746591.html