• PVE Cloud-INIT 模板配置


    PVE Cloud-INIT 模板配置

    Cloud-init是什么

    Cloud-init是开源的云初始化程序,能够对新创建弹性云服务器中指定的自定义信息(主机名、密钥和用户数据等)进行初始化配置。通过Cloud-init进行弹性云服务器的初始化配置,将对您使用弹性云服务器、镜像服务和弹性伸缩产生影响。简单地讲,cloud-init是一个Linux虚拟机的初始化工具,被广泛应用在AWS和OpenStack等云平台中,用于在新建的虚拟机中进行时间设置、密码设置、扩展分区、安装软件包等初始化设置。

    对镜像服务的影响

    为了保证使用私有镜像新创建的弹性云服务器可以自定义配置,您需要在创建私有镜像前先安装Cloud-init/Cloudbase-init。

    如果是Windows操作系统,需下载并安装Cloudbase-init。

    如果是Linux操作系统,需下载并安装Cloud-init。

    在镜像上安装Cloud-init/Cloudbase-init后,即可在创建弹性云服务器时,按照用户的需要自动设置弹性云服务器的初始属性。

    对弹性云服务器的影响

    在创建弹性云服务器时,如果选择的镜像支持Cloud-init特性,此时,您可以通过系统提供的“用户数据注入”功能,注入初始化自定义信息(例如为弹性云服务器设置登录密码),完成弹性云服务器的初始化配置。

    支持Cloud-init特性后ZQ,弹性云服务器的登录方式会产生影响。

    对于运行中的的弹性云服务器,支持Cloud-init特性后,用户可以通过查询、使用元数据,对正在运行的弹性云服务器进行配置和管理。

    对弹性伸缩的影响

    创建伸缩配置时,您可以使用“用户数据注入”功能,指定弹性云服务器的初始化自定义信息。如果伸缩组使用了该伸缩配置,则伸缩组新创建的弹性云服务器会自动完成初始化配置。

    对于已有的伸缩配置,如果其私有镜像没有安装Cloud-init/Cloudbase-init,则使用该伸缩配置的伸缩组创建的弹性云服务器在登录时会受到影响。

    官方镜像下载

    # cloud images下载地址
    # centos:
    http://cloud.centos.org/centos/
    # ubuntu:
    http://cloud-images.ubuntu.com/releases/
    # debian:
    https://cloud.debian.org/images/cloud/OpenStack/
    # fedora:
    https://alt.fedoraproject.org/cloud/
    # rehat7:
    https://access.redhat.com/downloads/content/69/ver=/rhel---7/x86_64/product-downloads
    # opensuse:
    https://software.opensuse.org/distributions/leap#JeOS-ports
    
    

    下载镜像

    # 下载Ubuntu官方CloudINIT镜像
    root@cby:~# wget https://mirrors.ustc.edu.cn/ubuntu-cloud-images/jammy/20230405/jammy-server-cloudimg-amd64.img
    --2023-04-06 19:00:50--  https://mirrors.ustc.edu.cn/ubuntu-cloud-images/jammy/20230405/jammy-server-cloudimg-amd64.img
    Resolving mirrors.ustc.edu.cn (mirrors.ustc.edu.cn)... 2001:da8:d800:95::110, 202.141.176.110, 202.141.160.110
    Connecting to mirrors.ustc.edu.cn (mirrors.ustc.edu.cn)|2001:da8:d800:95::110|:443... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 684654592 (653M) [application/octet-stream]
    Saving to: ‘jammy-server-cloudimg-amd64.img’
    
    jammy-server-cloudimg-amd64.img                             100%[=========================================================================================================================================>] 652.94M  64.7MB/s    in 9.7s    
    
    2023-04-06 19:01:00 (67.3 MB/s) - ‘jammy-server-cloudimg-amd64.img’ saved [684654592/684654592]
    
    
    # 下载CentOS官方CloudINIT镜像
    root@cby:~# wget https://mirrors.ustc.edu.cn/centos-cloud/centos/7/images/CentOS-7-x86_64-GenericCloud.qcow2
    --2023-04-06 19:01:48--  https://mirrors.ustc.edu.cn/centos-cloud/centos/7/images/CentOS-7-x86_64-GenericCloud.qcow2
    Resolving mirrors.ustc.edu.cn (mirrors.ustc.edu.cn)... 2001:da8:d800:95::110, 202.141.176.110, 202.141.160.110
    Connecting to mirrors.ustc.edu.cn (mirrors.ustc.edu.cn)|2001:da8:d800:95::110|:443... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 902889472 (861M) [application/octet-stream]
    Saving to: ‘CentOS-7-x86_64-GenericCloud.qcow2’
    
    CentOS-7-x86_64-GenericCloud.qcow2                          100%[=========================================================================================================================================>] 861.06M  60.5MB/s    in 15s     
    
    2023-04-06 19:02:03 (59.1 MB/s) - ‘CentOS-7-x86_64-GenericCloud.qcow2’ saved [902889472/902889472]
    root@cby:~# 
    
    

    使用命令配置

    VM_ID=101
    # 创建虚拟机
    qm create $VM_ID --cores 4 --memory 4096 --name ubuntu --net0 virtio,bridge=vmbr0
    # 给虚拟机导入镜像
    qm importdisk $VM_ID jammy-server-cloudimg-amd64.img local-lvm
    # 创建磁盘到local-lvm
    qm set $VM_ID --sata0 local-lvm:vm-$VM_ID-disk-0
    # 创建cloudinit
    qm set $VM_ID --sata1 local-lvm:cloudinit
    # 设置默认启动项
    qm set $VM_ID --boot c --bootdisk sata0
    # 设置vga
    qm set $VM_ID --serial0 socket --vga serial0
    # 设置root密码
    qm set $VM_ID --ciuser root --cipassword 123123
    # 配置静态网络
    #qm set $VM_ID  --ipconfig0 ip=10.0.10.123/24,gw=10.0.10.1,ip6=dhcp
    # 配置DHCP网络
    qm set $VM_ID --ipconfig0 ip=dhcp,ip6=dhcp
    # 配置DNS
    qm set $VM_ID --nameserver 223.5.5.5
    qm set $VM_ID --searchdomain 223.5.5.5
    # 转换为模板
    qm template $VM_ID
    # 克隆
    qm clone 101 103 --name cby
    

    简化整理命令

    # Ubuntu简化
    VM_ID=101
    qm create $VM_ID --cores 4 --memory 4096 --name ubuntu --net0 virtio,bridge=vmbr0
    qm importdisk $VM_ID jammy-server-cloudimg-amd64.img local-lvm
    qm set $VM_ID --sata0 local-lvm:vm-$VM_ID-disk-0 --sata1 local-lvm:cloudinit --boot c --bootdisk sata0 --serial0 socket --vga serial0 --ciuser root --cipassword 123123 --ipconfig0 ip=dhcp,ip6=dhcp --nameserver 8.8.8.8 --searchdomain 8.8.8.8
    
    
    # CentOS简化
    VM_ID=102
    qm create $VM_ID --cores 4 --memory 4096 --name centos --net0 virtio,bridge=vmbr0
    qm importdisk $VM_ID CentOS-7-x86_64-GenericCloud.qcow2 local-lvm
    qm set $VM_ID --sata0 local-lvm:vm-$VM_ID-disk-0 --sata1 local-lvm:cloudinit --boot c --bootdisk sata0 --serial0 socket --vga serial0 --ciuser root --cipassword 123123 --ipconfig0 ip=dhcp,ip6=dhcp --nameserver 8.8.8.8 --searchdomain 8.8.8.8
    

    配置模板系统

    CentOS

    # 配置yum源
    sudo sed -e 's|^mirrorlist=|#mirrorlist=|g' -e 's|^#baseurl=http://mirror.centos.org/centos|baseurl=https://mirrors.ustc.edu.cn/centos|g' -i.bak /etc/yum.repos.d/CentOS-Base.repo
    
    # 设置时区
    timedatectl set-timezone Asia/Shanghai
    
    # 安装vim
    yum install vim bash-completion
    
    # 修改ssh远程
    vim /etc/ssh/sshd_config
    # 开启root登录(如果需要的话;这里开启了之后Cloud-Init用户就可以设置root)
    PermitRootLogin yes
    # 开启密钥登录
    PubkeyAuthentication yes
    # 密钥路径
    AuthorizedKeysFile
    # 开启密码登录(默认只允许密钥登录)
    PasswordAuthentication yes
    # 不允许空密码登录
    PermitEmptyPasswords no
    # 关闭连接的DNS解析
    UseDNS no
    

    Ubuntu

    # 设置时区
    timedatectl set-timezone Asia/Shanghai
    
    # 设置APT仓库源
    sed -i 's/archive.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list
    
    # 更新源和安装常用软件
    apt update && sudo apt install vim bash-completion -y
    
    # 更新系统
    sudo apt upgrade
    
    # 修改SSH配置
    vim /etc/ssh/sshd_config
    # 开启root登录(如果需要的话;这里开启了之后Cloud-Init用户就可以设置root)
    PermitRootLogin yes
    # 开启密钥登录
    PubkeyAuthentication yes
    # 密钥路径
    AuthorizedKeysFile
    # 开启密码登录(默认只允许密钥登录)
    PasswordAuthentication yes
    # 不允许空密码登录
    PermitEmptyPasswords no
    # 关闭连接的DNS解析
    UseDNS no
    

    附录

    qm monitor          # 连接到虚拟机控制监视器
    qm clone   [OPTIONS]  # 克隆虚拟机
    qm start                # 启动实例
    qm shutdown       # 优雅停止实例 发送关机命令
    qm wait  [time]   wait until vm is stopped
    qm stop                # 停止实例 强制停止
    qm reset               # 重启实例 相当于stop然后再start
    qm suspend         # 暂停实例
    qm resume          # 恢复实例
    qm cad                #发送按键 ctrl-alt-delete
    qm destroy         # 销毁实例(删除所有已使用/拥有的卷)
    qm unlock          # 清除迁移/备份锁
    qm status           # 显示实例状态
    qm cdrom  []   set cdrom path. 
    qm cdrom  [] eject   eject cdrom
    qm unlink    delete unused disk images
    qm vncproxy    open vnc proxy
    qm vnc            start (X11) vncviewer (experimental)
    qm showcmd      # 显示命令行(调试信息)
    qm list                             # 列出所有虚拟机
    qm startall                       # 启动所有虚拟机 当onboot=1时
    qm stopall [timeout]       # 停止所有虚拟机(默认超时为3分钟)
    
    
    qm [create|set]   # 创建虚拟机
        --memory      memory in MB (64 - 8192)
        --sockets          set number of CPU sockets 
        --cores            set cores per socket to 
        --ostype NAME         specify OS type
        --onboot [yes|no]     start at boot
        --keyboard XX         set vnc keyboard layout
        --cpuunits       CPU weight for a VM
        --name          set a name for the VM
        --description   set VM description
        --boot [a|c|d|n]      specify boot order
        --bootdisk      enable booting from 
        --acpi (yes|no)       enable/disable ACPI
        --kvm (yes|no)        enable/disable KVM
        --tdf (yes|no)        enable/disable time drift fix
        --localtime (yes|no)  set the RTC to local time
        --vga (gd5446|vesa)   specify VGA type
        --vlan[0-9u]          MODEL=XX:XX:XX:XX:XX:XX[,MODEL=YY:YY:YY:YY:YY:YY]
        --ide              [volume=]volume,[,media=cdrom|disk]
                              [,cyls=c,heads=h,secs=s[,trans=t]]
                              [,cache=none|writethrough|writeback]
                              [,snapshot=on|off][,cache=on|off][,format=f]
                              [,werror=enospc|ignore|report|stop]
                              [,rerror=ignore|report|stop]
                              [,backup=no|yes]
        --ide      create new disk
        --ide delete       remove drive - destroy image
        --ide undef        remove drive - keep image
        --cdrom         is an alias for --ide2 ,media=cdrom
        --scsi             [volume=]volume,[,media=cdrom|disk]
                              [,cyls=c,heads=h,secs=s[,trans=t]]
                              [,snapshot=on|off][,format=f]
                              [,cache=none|writethrough|writeback]
                              [,werror=enospc|ignore|report|stop]
                              [,backup=no|yes]
        --scsi     create new disk
        --scsi delete      remove drive - destroy image
        --scsi undef       remove drive - keep image
        --virtio           [volume=]volume,[,media=cdrom|disk]
                              [,cyls=c,heads=h,secs=s[,trans=t]]
                              [,snapshot=on|off][,format=f]
                              [,cache=none|writethrough|writeback]
                              [,werror=enospc|ignore|report|stop]
                              [,rerror=ignore|report|stop]
                              [,backup=no|yes]
        --virtio   create new disk
        --virtio delete    remove drive - destroy image
        --virtio undef     remove drive - keep image
        
    
    
    
    
    pveperf    # 基准脚本
    pvesr list    # 列出存储复制作业
    ha-manager status   # 查看HA状态
    pvecm nodes    # 查看集群节点
    pvecm status    # 查看集群状态
    pve-firewall compile  # 查看防火墙规则
    pve-firewall localnet  # 输出本地网络信息
    pve-firewall restart    # 重启防火墙
    pve-firewall stop       # 停止防火墙
    pve-firewall start      # 启动防火墙
    
    
    
    pvesh get /version   # 查看集群
    pvesh get /cluster/resources  #查看集群资源状况
    pvesh get /nodes   # 显示所有node
    pvesh get /nodes//qemu   # 显示某节点中的虚拟机
    pvesh get /nodes//qemu//status/current    # 显示某虚拟机状态
    pvesh create /nodes//qemu//status/start    # 开启一个虚拟机 [注意是create哦,不是get了]
    pvesh get /nodes//lxc//snapshot    # 显示某节点下容器快照
    pvesh get /nodes//disks/zfs    # 显示某节点的ZFS存储区
    pvesh get /nodes//disks/list  # 显示某节点的磁盘列表
    

    关于

    https://www.oiox.cn/

    https://www.oiox.cn/index.php/start-page.html

    CSDN、GitHub、51CTO、知乎、开源中国、思否、博客园、掘金、简书、华为云、阿里云、腾讯云、哔哩哔哩、今日头条、新浪微博、个人博客

    全网可搜《小陈运维》

    文章主要发布于微信公众号

  • 相关阅读:
    计算即时订单比例-首单使用开窗函数row_number()
    7 个不容忽视的开源安全工具
    时序数据库InfluxDB了解
    【springboot】1、快速入门
    AB实验:科学归因与增长的利器
    Apache解析漏洞实验
    概率论与数理统计学习:随机事件(一)——知识总结与C语言实现案例
    Vue3——Suspense组件
    径向基神经网络RBF:Matlab实现RBF神经网络(含例子及代码)
    接口测试和性能测试的区别
  • 原文地址:https://www.cnblogs.com/chenby/p/17294392.html