温馨提示×

如何通过Cobbler实现Ubuntu系统的自动化运维

小樊
45
2025-12-25 02:02:22
栏目: 智能运维

通过 Cobbler 实现 Ubuntu 自动化运维

一、架构与前置条件

  • 组件与角色:Cobbler 负责 PXE 引导编排,配合 DHCP/TFTP/HTTP 完成网络安装;Ubuntu 使用 preseed(Debian 系自动应答)cloud-init(22.04/24.04 推荐) 完成无人值守配置。
  • 网络与服务:在同一二层网段部署;确保 DHCP next-server 指向 Cobbler 服务器 IPTFTP 提供引导文件HTTP 提供安装源与模板;防火墙放行 69/UDP(TFTP)80/TCP(HTTP),以及 Cobbler API 端口 25150–25151/TCP(如使用)。
  • 工具与源:安装 debmirror(Ubuntu 镜像同步)、按需安装 cobbler-web;导入发行版前执行 cobbler signature update 提升识别率。

二、部署步骤

  • 安装软件包(Ubuntu 控制节点示例)
    • 执行:sudo apt-get update && sudo apt-get install -y cobbler cobbler-web dhcp3-server tftpd-hpa xinetd debmirror
  • 配置 DHCP(两种模式)
    • Cobbler 托管 DHCP:编辑 /etc/cobbler/dhcp.template,在子网段中加入
      • allow booting; allow bootp; filename "/pxelinux.0"; next-server $next_server;
      • $next_server 设为 Cobbler 服务器 IP;随后 sudo cobbler sync 会自动渲染到 /etc/dhcp/dhcpd.conf
    • 外部 DHCP:在现有 dhcpd.conf 的 subnet 段添加
      • filename "/pxelinux.0"; next-server <Cobbler_IP>;,并重启 DHCP 服务。
  • 配置 TFTP
    • 编辑 /etc/xinetd.d/tftp,将 disable = yes 改为 disable = no;启用并重启 xinetd/tftp。
  • 初始化 Cobbler
    • 启动服务:sudo systemctl enable --now cobblerd apache2 tftp xinetd
    • 获取引导加载器:sudo cobbler get-loaders
    • 检查并修正:sudo cobbler check(按提示修复,例如设置 server/next_server、管理 DHCP 与否等)。
  • 防火墙(firewalld 示例)
    • 执行:sudo firewall-cmd --permanent --add-service=tftp --add-service=http --add-port=25150-25151/tcp && sudo firewall-cmd --reload

三、导入镜像与创建系统

  • 方式 A(ISO 导入,快速上手)
    • 挂载 ISO:sudo mount -o loop /path/ubuntu-22.04.iso /mnt/ubuntu
    • 导入镜像:sudo cobbler import --path=/mnt/ubuntu --name ubuntu-22.04 --breed ubuntu --os-version=jammy
  • 方式 B(镜像站点同步,适合内网离线)
    • 同步镜像:sudo debmirror -a amd64 --method http --dist jammy --section main,restricted,universe,multiverse --host archive.ubuntu.com /var/www/html/ubuntu-mirror
    • 导入镜像:sudo cobbler import --path=/var/www/html/ubuntu-mirror --name ubuntu-22.04 --breed ubuntu --os-version=jammy
  • 查看与校验
    • 列出发行版/配置:cobbler distro listcobbler profile listcobbler report
    • 如导入失败,先 cobbler signature update 再重试。

四、无人值守配置与自动化扩展

  • Ubuntu 22.04/24.04 推荐 cloud-init 自动安装
    • 生成 root 密码哈希:mkpasswd -m sha-512,填入 /var/lib/cobbler/templates/cloud-init_user-datapassword: $default_password_crypted
    • 关键片段(示例):
      • autoinstall: version: 1
      • apt: primary: [{arches: [amd64], uri: "http://$http_server/cblr/links/$distro"}]
      • identity: username: ubuntu; password: $default_password_crypted
      • ssh: allow-pw: true; install-server: true
      • storage: layout: {name: lvm, sizing-policy: all}
      • late-commands: - wget -O /target/tmp/autoinstall-user-data.yaml http://$http_server/cblr/svc/op/autoinstall/system/<name>; - chroot /target /bin/bash -s ssh-keygen -t rsa -b 2048 -N "" -f /root/.ssh/id_rsa
    • 创建必要的 snippets(如 cloud-init_hostname、cloud-init_network),用于渲染主机名与网络。
  • Ubuntu 20.04/18.04 使用 preseed
    • 准备 preseed(示例要点):d-i partman-auto/method string lvmd-i pkgsel/include string openssh-server vim htop
    • 创建 Profile:sudo cobbler profile add --name ubuntu-20.04-preseed --distro ubuntu-20.04-x86_64 --kickstart /var/lib/cobbler/kickstarts/ubuntu-20.04.seed
  • 定义主机与网络
    • 添加系统:sudo cobbler system add --name host01 --profile ubuntu-22.04-cloudinit --mac 00:16:3E:xx:xx:xx --interface eth0 --static 1 --ip 192.168.1.101 --netmask 255.255.255.0 --gateway 192.168.1.1 --dns-nameservers 8.8.8.8
    • 启用网络引导:sudo cobbler system edit --name host01 --netboot-enabled true
  • 使配置生效
    • 同步:sudo cobbler sync(每次变更后执行)。

五、上线与运维实践

  • 批量上线:按 MAC 创建多个 system,或按业务线维护多个 profile;新主机 PXE 启动后自动按绑定关系安装。
  • 内网加速:搭建 debmirror 内网镜像源,Profile 的 apt 源指向内网地址,显著提升安装与后续包管理速度。
  • 安全加固:
    • 使用 cobbler-webdigest 认证;限制管理网段访问;为 root 设置强密码或禁用 root 登录、改用 sudo 与 SSH Key。
    • 防火墙仅开放必要端口;对 cobbler sync 与 API 调用进行审计与最小权限控制。
  • 持续交付:将镜像导入、profile 与 system 定义纳入 Git 管理;变更通过 CI 执行 cobbler sync 并做 dry-run 校验。

0