cobbler自动化安装centos7.6

核能气质少年 提交于 2021-02-20 16:47:59

一、cobbler介绍

1.前言

cobbler 是基于 python 语言开发的 pxe 二次封装的网络安装服务;可以多系统选自自动化安装配置。可以通过 Web 图形化界面配置,其安装包为 cobbler,在 epel 源上,配置过程中某些过程可能需要依赖互联网环境。

2.Cobbler可以干什么

使用 Cobbler,无需进行人工干预即可安装机器。Cobbler 设置一个 PXE 引导环境(它还可以使用 yaboot 支持 PowerPC),并控制原装相关的所有方面,比如网络引导服务(DHCP 和 TFTP)与存储库镜像。当希望安装一台新机器时,Cobbler 可以: 1)使用一个以前定义的模板来配置 DHCP 服务(如果启用了管理 DHCP) 2)讲一个存储库(yum 或 rsync)建立镜像或解压缩一个媒介,以注册一个新操作系统 3)在 DHCP 配置文件中为需要安装的机器创建一个条目,并使用指定的参数(IP 和 MAC) 4)在 TFTP 服务目录下创建适当的 PXE 文件 5)重新启动 DHCP 服务来反应新的更改 6)重新启动机器以开始安装(如果电源管理已启动

3.Cobbler 支持的系统和功能

Cobbler 支持众多的发行版:Redhat、Fedora、CentOS、Debian、Ubuntu 和 SUSE。当添加一个操作系统(通常通过使用 ISO 文件)时,Cobbler 知道如何解压缩合适的文件并调整网络服务,以正确引导机器。 Cobbler 可以使用 kickstart 模板。基于 Redhat 或 Fedora 的系统使用 kickstart 文件来自动化安装流程,通过使用模板,就会拥有基本 kickstart 模板,然后定义如何针对一种配置文件或机器配置而替换其中的变量。例如,一个模板可能包含两个变量 $domain 和 $machine_name。在 Cobbler 配置中,一个配置文件指定 domain=mydomain.com,并且每台使用该配置文件的机器在 machine_name 变量中指定其名称。该配置文件的所有机器都是用相同的 kickstart 安装且针对 domain=mydomain.com 进行配置,但每台机器拥有其自己的机器名称。仍然可以使用 kickstart 模板在不同的域中安装其他机器并使用不同的机器名称。 为了协助管理系统,Cobbler 可通过 fence_scripts 连接到各个电源管理环境。Cobbler 支持 apc_snmp、bladecenter、bullpap、drac、ether_wake、ilo、integrity、ipmilan、ipmitool、Ipar、rsa、virsh 和 wti。要重新安装一台机器,可运行 reboot system foo 命令,而且 Cobbler 会使用必要的和信息来为您运行恰当的 fence scripts(比如机器插槽数)。 除了这些特性,还可以使用一个配置管理系统(CMS)。有两种选择:该工具内的一个内部系统,或者现成的外部 CMS,比如 Chef 或 Puppet。借助内部系统,你可以指定文件模板,这些模板会依据配置参数进行处理(与 kickstart 模板的处理方式一样),然后复制到你指定的位置。如果必须自动将配置文件部署到特定机器,那么此功能很有用。 使用 koan 客户端,Cobbler 可从客户端配置虚拟机并重新安装系统。

4.Cobbler 提供的服务集成

PXE 服务支持 DHCP 服务管理 DNS 服务管理(可选bind,dnsmasq) 电源管理 Kickstart 服务支持 yum 仓库管理 TFTP (PXE 启动时需要) Apache(提供 kickstart 的安装源,并提供定制化的 kickstart 配置) 同时,它和 apache 做了深度整合

5.PXE工作流程图

因为 cobbler 基于 pxe 二次封装的,所以了解 PXE 工作流程也重要,图如下:

6.PXE + Kickstart 无人值守安装操作系统完整过程

从上图可以看出:

  • 配置服务,比如 DHCP、TFTP、(HTTP、FTP和NFS)
  • 在 DHCP 和 TFTP 配置文件中填入哥哥客户端机器的信息
  • 创建自动部署文件(比如 kickstart)
  • 将安装媒介解压缩到 HTTP/FTP/NFS 存储库中

7.cobbler 的组成

Cobbler 的配置结构基于一组注册的对象。没个对象表示一个与另一个实体相关联的实体(该对象指向另一个对象,或者另一个对象指向该对象)。当一个对象指向另一个对象时,它就集成了被指向对象的数据,并可覆盖或添加更多特定信息。 以下对象类型的定义为: 发行版:表示一个操作系统。它承载了内核和 initrd 的信息,以及内核参数等其他数据; 配置文件:包含一个发行版、一个 kickstart 文件以及可能的存储库,还包含更多特定的内核参数等其他数据; 系统:表示要配给的机器。它把汗一个配置文件或一个镜像,还包含 IP 和 MAC;地址、电源管理(地址、凭据、类型)以及更为专业的数据等信息; 存储库:保存一个 yum 或 rsync 存储库的镜像信息; 镜像:可替换一个包含不属于此类别的文件的发行版对象(例如,无法分为内核和 initrd 的对象); 基于注册的对象以及各个对象之间的关联,Cobbler 知道如何更改文件系统以反应具体配置。因为系统配置的内部是抽象的,所以可以仅关注想要执行的操作。

8.cobbler 的工作流程

二、cobbler 安装

必要服务 1)cobbler (cobbler 的核心) 2)httpd (提供 cobbler 的 web 界面) 3)dhcpd (为自动安装系统分配 IP 地址) 4)epel-release(为之提供yum源) 5)rsync (cobbler 需要同步信息) 6)cobbler-web (cobbler 的一个 web 插件) 7)xinetd (为 rsync 和 tftp 的守护进程) 8)tftp (传送安装的一些文件的 类似ftp)

1.关闭防火墙和 selinux

systemctl stop firewalld
systemctl disable firewalld
sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config

2.cobbler 安装

1.安装阿里云epel基础镜像源

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

2.cobbler 安装,fence-agents 可不安装,tftp 通过 xinetd 管理

cobbler 和 apache 有深度配合所以安装 apache,不要安装 nginx 否则生成不了相关配置

yum install -y httpd dhcpd tftp python-ctypes cobbler cobbler-web pykickstart fence-agents xinetd rsync

3.启动相关服务

systemctl start httpd xinetd cobblerd.service
systemctl enable httpd xinetd cobblerd.service

4.检查cobbler配置

cobbler 的帮助命令为 cobbler –help

cobbler check

解决方法:

  • /etc/cobbler/settings 中server配置成具体的服务器 ip 地址
  • /etc/cobbler/settings 中next_server也修改为具体的 ip 地址
  • /etc/xinetd.d/tftp 中disable从 yes 改为 no
  • 运行cobbler get-loaders网络引导加载程序
  • 开启rsync 服务并设置为开机启动,systemctl start rsyncd、systemctl enable rsyncd
  • debian 系统需要安装 debmirror 包,这里是 centos 所以不用安装
  • openssl passwd -1 -salt 需要安装系统登录名 需要安装系统登录密码生成字段写入/etc/cobbler/settings 中default_password_crypted字段
  • 应用修改 systemctl restart cobblerd cobbler sync

5.让 cobbler 管理 DHCP

1.编辑 cobbler 中 dhcp 模板
cd /etc/cobbler
cp dhcp.template dhcp.template.org
sed -i '/^subnet/s/192.168.1.0/172.16.80.0/' dhcp.template
sed -i '/routers/s/192.168.1.5/172.16.80.254/' dhcp.temlpate
sed -i '/domain-name-servers/s/192.168.1.1/172.16.18.245/' dhcp.template
sed -i '/dynamic-bootps/s/192.168.1.100 192.168.1.254/172.16.80.100 172.16.80.200/' dhcp.template

把里面的信息修改为和自己同一个局域网的

2.修改/etc/cobbler/settings 配置文件
sed -i '/manage_dhcp:/s/0/1/' /etc/cobbler/settings
systemctl restart cobblerd
sleep 2
cobbler sync

cat /etc/dhcp/dhcpd.conf

3.挂载 iso 镜像让 cobbler 处理生成

上传 iso 镜像文件到服务器,并挂载到目录

mkdir /mnt/iso
mount -o loop -t iso9660 CentOS-7-x86_64-Minimal-1810.iso /mnt/iso

用 cobbler 导入系统

cobbler import --path=/mnt/iso --name=CentOS-7.6-x86_64 --arch=x86_64
cobbler profile list
cobbler profile report

上面在导入系统的过程中 cobbler 帮我们自己生成了相关的配置,关联 dhcp,pxe安装菜单 生成 Kickstart 文件,可以看一下里面有哪些东西

cd /var/lib/cobbler/kickstarts/
less sample_end.ks
mv sample_end.ks sample_end.ks.bak

生成的 Kickstart 文件可以根据自己的需要进行修改,前提是要熟悉原生的 Kickstart 语法, 可以参考 kickstart使用详细指南

4.修改 cobbler 生成的 Kickstart 文件 CentOS-7.6-x86_64.ks
[root@k8s-node2 ~]# cat /var/lib/cobbler/kickstarts/CentOS-7.6-x86_64.ks
# This kickstart file should only be used with EL > 5 and/or Fedora > 7.
# For older versions please use the sample.ks kickstart file.

#platform=x86, AMD64, or Intel EM64T
# System authorization information
auth  --useshadow  --enablemd5
# System bootloader configuration
bootloader --location=mbr
# Partition clearing information
clearpart --all --initlabel
# Use text mode install
text
# Firewall configuration
firewall --disabled
# Run the Setup Agent on first boot
firstboot --disable
# System keyboard
keyboard us
# System language
lang en_US
# Use network installation
url --url=$tree
# If any cobbler repo definitions were referenced in the kickstart profile, include them here.
$yum_repo_stanza
# Network information
# $SNIPPET('network_config')
network  --bootproto=dhcp --device=enp1s0 --onboot=on  #配置网卡默认自动开启DHCP功能,配置网卡名称为enp1s0
# Reboot after installation
reboot

#Root password
rootpw --iscrypted $1$root$AKl5mABzn4OHUp7t27Sp1/ 
# SELinux configuration
selinux --disabled
# Do not configure the X Window System
skipx
# System timezone
timezone  Asia/Shanghai                                          #设置时区为中国上海时区
# Install OS instead of upgrade
install
# Clear the Master Boot Record
zerombr
clearpart --all --initlabel
part /boot --fstype="xfs" --size=2048                            #boot分区给2G大小
part swap  --fstype="swap" --size=16384                          #swap分区给内存大小的2倍
part / --fstype="xfs" --size=122880                              #/分区给120G大小
part /var --fstype="xfs" --size=1765376                          #var分区给剩下磁盘大小
# Allow anaconda to partition the system as needed
# autopart

%pre
$SNIPPET('log_ks_pre')
$SNIPPET('kickstart_start')
$SNIPPET('pre_install_network_config')
# Enable installation monitoring
$SNIPPET('pre_anamon')
%end

%packages
$SNIPPET('func_install_if_enabled')
%end

%post --nochroot
$SNIPPET('log_ks_post_nochroot')
%end

%post
$SNIPPET('log_ks_post')
# Start yum configuration
$yum_config_stanza
# End yum configuration
$SNIPPET('post_install_kernel_options')
$SNIPPET('post_install_network_config')
$SNIPPET('func_register_if_enabled')
$SNIPPET('download_config_files')
$SNIPPET('koan_environment')
$SNIPPET('redhat_register')
$SNIPPET('cobbler_register')
# Enable post-install boot notification
$SNIPPET('post_anamon')
# Start final steps
$SNIPPET('kickstart_done')
# End final steps
%end
5.cobbler 加载 CentOS-7.6-x86_64.ks 配置文件
cobbler profile edit --name=CentOS-7.6-x86_64 --kickstart=/var/lib/cobbler/kickstarts/CentOS-7.6-x86_64.ks
systemctl restart cobblerd
sleep 2
cobbler sync

查看cobbler生成配置

cobbler profile report

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!