1. Cobbler快速入门
使用 Cobbler,您无需进行人工干预即可安装机器。Cobbler设置一个PXE引导环境,并控制与安装相关的所有软件和环境准备,比如网络引导服务(DHCP 和 TFTP)与存储库镜像。当希望安装一台新机器时,Cobbler 可以:
- 使用一个以前定义的模板来配置 DHCP 服务(如果启用了管理 DHCP)
- 将一个存储库(yum 或 rsync)建立镜像或解压缩一个媒介,以注册一个新操作系统
- 在 DHCP 配置文件中为需要安装的机器创建一个条目,并使用您指定的参数(IP 和 MAC 地址)
- 在 TFTFP 服务目录下创建适当的 PXE 文件
- 重新启动 DHCP 服务以反映更改
- 重新启动机器以开始安装(如果电源管理已启用)
1.1. Cobbler部署
1.1.1. Cobbler功能
Cobbler 支持众多的发行版:Red Hat、Fedora、CentOS、Debian、Ubuntu 和 SuSE。当添加一个操作系统(通常通过使用 ISO 文件)时,Cobbler 知道如何解压缩合适的文件并调整网络服务,以正确引导机器。
Cobbler 可使用 kickstart 模板。基于 Red Hat 或 Fedora 的系统使用 kickstart 文件来自动化安装流程。通过使用模板,您就会拥有基本的 kickstart 模板,然后定义如何针对一种配置文件或机器配置而替换其中的变量。例如,一个模板可能包含两个变量 $domain 和 $machine_name。在 Cobbler 配置中,一个配置文件指定 domain=mydomain.com,并且每台使用该配置文件的机器在 machine_name 变量中指定其名称。该配置文件中的所有机器都使用相同的 kickstart 安装且针对 domain=mydomain.com 进行配置,但每台机器拥有其自己的机器名称。您仍然可以使用 kickstart 模板在不同的域中安装其他机器并使用不同的机器名称。
使用 koan 客户端,Cobbler 可从客户端配置虚拟机并重新安装系统。我不会讨论配置管理和 koan 特性,因为它们不属于本文的介绍范畴。但是,它们是值得研究的有用特性。
1.1.2. Cobbler安装
- 安装EPEL源
[root@linux-node2 ~]# rpm -ivh http://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
- yum安装cobbler
[root@linux-node2 ~]# yum install -y httpd dhcp tftp python-ctypes cobbler cobbler-web pykickstart fence-agents xinetd debmirror
- 启动并配置cobbler
[root@linux-node2 ~]# systemctl enable httpd cobblerd
[root@linux-node2 ~]# systemctl start httpd cobblerd
- 检查Cobbler状态
[root@linux-node2 ~]# cobbler check
The following are potential configuration items that you may want to fix:
1 : The 'server' field in /etc/cobbler/settings must be set to something other than localhost, or kickstarting features will not work. This should be a resolvable hostname or IP for the boot server as reachable by all machines that will use it.
2 : For PXE to be functional, the 'next_server' field in /etc/cobbler/settings must be set to something other than 127.0.0.1, and should match the IP of the boot server on the PXE network.
3 : change 'disable' to 'no' in /etc/xinetd.d/tftp
4 : enable and start rsyncd.service with systemctl
5 : comment out 'dists' on /etc/debmirror.conf for proper debian support
6 : comment out 'arches' on /etc/debmirror.conf for proper debian support
7 : The default password used by the sample templates for newly installed machines (default_password_crypted in /etc/cobbler/settings) is still set to 'cobbler' and should be changed, try: "openssl passwd -1 -salt 'random-phrase-here' 'your-password-here'" to generate new one
Restart cobblerd and then run 'cobbler sync' to apply changes.
这段话的意思就是需要处理上面的所有问题,然后重启cobblerd服务,然后执行cobbler sync同步修改操作。
将上面提到的7个问题进行修复
- 问题1和2解决
均是需要修改cobbler的配置文件。
[root@linux-node2 ~]# vim /etc/cobbler/settings
#修改以下两行即可,可以直接搜索127.0.0.1来定位
server: 192.168.56.12 #设置cobbler server的IP地址
next_server: 192.168.56.12 #设置PXE server的IP地址
- 问题3解决
[root@linux-node2 ~]# vim /etc/xinetd.d/tftp
service tftp
{
socket_type = dgram
protocol = udp
wait = yes
user = root
server = /usr/sbin/in.tftpd
server_args = -s /var/lib/tftpboot
disable = no
per_source = 11
cps = 100 2
flags = IPv4
}
- 问题4解决
启动rsyncd服务,并设置开机自动启动
[root@linux-node2 ~]# systemctl enable rsyncd
[root@linux-node2 ~]# systemctl start rsyncd
- 问题5、6解决
安装debmirror是debian系列系统使用的
[root@linux-node2 ~]# yum install -y debmirror
[root@linux-node2 ~]# vim /etc/debmirror.conf
#请注释掉下面两行配置
#@dists="sid";
#@arches="i386"
- 问题7解决
[root@linux-node2 ~]# openssl passwd -1 -salt 'example' '123456.coM'
$1$example$I.i3m26O7QYNja8p5Cj9.0
[root@ops-node1 ~]# vim /etc/cobbler/settings
将下面字段替换为上面生成的字段:
default_password_crypted: "$1$example$I.i3m26O7QYNja8p5Cj9.0"
[root@linux-node1 ~]# systemctl restart cobblerd
- 准备Cobbler的启动文件 由于当前版本执行cobbler get-loaders会失败,官方移除了下载地址。所以可以手工处理,从之前手工部署的linux-node1上把对应的文件copy到本机。
[root@linux-node2 ~]# scp -r /var/lib/tftpboot/pxelinux/ root@192.168.56.12:/var/lib/cobbler/loaders/
- 再次检查Cobbler
[root@linux-node2 ~]# systemctl restart cobblerd
[root@linux-node2 ~]# cobbler check
No configuration problems found. All systems go.
1.1.3. Cobbler管理DHCP
- 开启管理DHCP服务,这样Cobbler就可以接管DHCP的管理工作
[root@linux-node2 ~]# vim /etc/cobbler/settings
manage_dhcp: 1
- 首先修改dhcp的配置文件
修改dhcp的模板配置文件,设置相对应的DHCP的IP地址和分配的网段。
[root@linux-node2 ~]# vim /etc/cobbler/dhcp.template
subnet 192.168.56.0 netmask 255.255.255.0 {
option routers 192.168.56.2;
option domain-name-servers 192.168.56.2;
option subnet-mask 255.255.255.0;
range dynamic-bootp 192.168.56.100 192.168.56.254;
default-lease-time 21600;
max-lease-time 43200;
next-server $next_server;
…
重启Cobbler并进行同步操作
[root@linux-node2 ~]# systemctl restart cobblerd
[root@linux-node2 ~]# cobbler sync
执行完毕cobbler sync后,会自动生成/etc/dhcpd.conf。并重启dhcp服务。所以说使用Cobbler管理DHCP后,请勿修改/etc/dhcpd.conf。以后所有dhcp相关的配置都是修改Cobbler的DHCP模板文件/etc/cobbler/dhcp.template。
1.1.4. Cobbler导入镜像
- 下载并导入镜像
[root@linux-node2 ~]# cd /usr/local/src
[root@linux-node2 ~]# wget https://mirrors.aliyun.com/centos/7.9.2009/isos/x86_64/CentOS-7-x86_64-Minimal-2009.iso
[root@linux-node2 ~]# mmount -o loop /usr/local/src/CentOS-7-x86_64-Minimal-2009.iso /mnt/
[root@linux-node2 ~]# cobbler import --path=/mnt/ --name=CentOS-7.9-x86_64 --arch=x86_64
参数说明:
- --name 为安装源定义一个名字
- --arch 指定安装源是32位还是64位、ia64, 目前支持的选项有: x86│x86_64│ia64
小提示:Cobbler 会把安装的镜像ISO拷贝到源安装镜像目录下: /var/www/cobbler/ks_mirror/
- 查看导入后结果
[root@linux-node2 ~]# cobbler list
distros:
CentOS-7.9-x86_64
profiles:
CentOS-7.9-x86_64
1.1.5. 自定义kickstart文件
Cobbler的Kickstart文件和默认的不同,我们需要修改,主要是增加上Cobbler的变量。导入镜像后,我们一般会自定义kickstart文件给这个镜像。首先可以将自定义后的Cobbler的kickstart文件放置在/var/lib/cobbler/kickstarts目录下,这也是Cobbler的默认存放kickstart文件的地方。
- 自定义Kickstart文件
[root@linux-node2 ~]# vim /var/lib/cobbler/kickstarts/CentOS-7.9-x86_64-Cobbler.cfg
#Kickstart Configurator by Jason Zhao
#platform=x86, AMD64, or Intel EM64T
#System language
lang en_US
#System keyboard
keyboard us
#Sytem timezone
timezone Asia/Shanghai
#Root password
rootpw --iscrypted $default_password_crypted
#Use text mode install
text
#Install OS instead of upgrade
install
#Use NFS installation Media
url --url=$tree
#System bootloader configuration
bootloader --location=mbr --driveorder=sda --append="net.ifnames=0 biosdevname=0"
#Clear the Master Boot Record
zerombr
#Partition clearing information
clearpart --all --initlabel
#Disk partitioning information
part /boot --fstype=xfs --size=1024
part swap --asprimary --fstype="swap" --size=1024
part / --fstype=xfs --size=1 --grow
#System authorization infomation
auth --useshadow --enablemd5
#Network information
$SNIPPET('network_config')
# Reboot after installation
reboot
#Firewall configuration
firewall --disabled
#SELinux configuration
selinux --disabled
#Service configuration
services --disabled=postfix
#Do not configure XWindows
skipx
#Package install information
%pre
$SNIPPET('log_ks_pre')
$SNIPPET('kickstart_start')
$SNIPPET('pre_install_network_config')
# Enable installation monitoring
$SNIPPET('pre_anamon')
%end
%packages
@^minimal
@core
chrony
kexec-tools
%end
- 然后编辑profile来制定kickstart文件。
[root@ops-node1 ~]# cobbler profile edit --name=CentOS-7.9-x86_64 --kickstart=/var/lib/cobbler/kickstarts/CentOS-7.9-x86_64-Cobbler.cfg
- 增加安装时的内核参数
[root@linux-node2 ~]# cobbler profile edit --name=CentOS-7.9-x86_64 --kopts='net.ifnames=0 biosdevname=0'
- 最后,一定要执行同步,才能将Cobbler设置完成
[root@linux-node2 ~]# cobbler sync
1.2. 2.2 使用Cobbler自动化安装CentOS
现在我们就可以使用Cobbler进行安装了。实验环境下,你可以新创建一个虚拟机(注意网卡的选择)来进行测试了。Cobbler非常人性化的创建了一个Local,即使从硬盘启动,这样也避免了有服务器从网卡启动开始自动安装的问题。
注意,要求测试的主机,内存不能低于2G,不然无法进行安装。Bug记录:https://bugzilla.redhat.com/show_bug.cgi?id=1595369
1.2.1. 2.2.1 使用Cobbler安装CentOS
启动物理服务器
选择CentOS-7.9-x86_64之后即可重新开始安装。
在生产环境如果想要自动开始安装,我们的通常做法是有一个专门进行服务器安装的VLAN,安装完毕后,进入待调配状态,然后根据流程进入到生产环境。但是提醒大家,自动化开始进行操作系统安装风险依然很大。
1.2.2. 2.2.2 Koan重新安装系统
在实践的运维工作中,你肯定需要重新安装操作系统,有了Cobbler你就不用去机房了,直接使用Koan就可以进行自动化重新安装操作系统。需要再待重装的服务器上安装koan。
安装Koan软件包
[root@localhost ~]# rpm -ivh http://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
[root@localhost ~]# yum install -y koan
列出可以安装的系统
[root@localhost ~]# koan --server=192.168.56.12 --list=profiles
- looking for Cobbler at http://192.168.56.12:80/cobbler_api
CentOS-7.7-x86_64
指定需要重新安装的操作系统
[root@localhost ~]# koan --replace-self --server=192.168.56.12 --profile=CentOS-7.9-x86_64
- looking for Cobbler at http://192.168.56.12:80/cobbler_api
- reading URL: http://192.168.56.12/cblr/svc/op/ks/profile/CentOS-7.9-x86_64
install_tree: http://192.168.56.12/cblr/links/CentOS-7.9-x86_64
downloading initrd initrd.img to /boot/initrd.img_koan
url=http://192.168.56.12/cobbler/images/CentOS-7.9-x86_64/initrd.img
- reading URL: http://192.168.56.12/cobbler/images/CentOS-7.9-x86_64/initrd.img
downloading kernel vmlinuz to /boot/vmlinuz_koan
url=http://192.168.56.12/cobbler/images/CentOS-7.9-x86_64/vmlinuz
- reading URL: http://192.168.56.12/cobbler/images/CentOS-7.9-x86_64/vmlinuz
- ['/sbin/grubby', '--add-kernel', '/boot/vmlinuz_koan', '--initrd', '/boot/initrd.img_koan', '--args', '"ksdevice=link lang= text net.ifnames=0 ks=http://192.168.56.11/cblr/svc/op/ks/profile/CentOS-7.4-x86_64 biosdevname=0 kssendmac "', '--copy-default', '--make-default', '--title=kick1526449287']
- ['/sbin/grubby', '--update-kernel', '/boot/vmlinuz_koan', '--remove-args=root']
- reboot to apply changes
如下图所示,Koan会创建一个新的启动选项,重启后,直接开始自动安装。
我们先不要执行reboot先研究下是如何实现的。你打开Grub的配置文件
[root@localhost ~]# vim /boot/grub2/grub.cfg
你会发现这样的配置
menuentry 'kick1526449287' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-693.el7.x86_64-advanced-49e658db-2c61-475a-9325-903fdbeb7cd4' {
load_video
set gfxpayload=keep
insmod gzio
insmod part_msdos
insmod xfs
set root='hd0,msdos1'
if [ x$feature_platform_search_hint = xy ]; then
search --no-floppy --fs-uuid --set=root --hint-bios=hd0,msdos1 --hint-efi=hd0,msdos1 --hint-baremetal=ahci0,msdos1 --hint='hd0,msdos1' 0a801c26-5320-448d-b261-883499529bc6
else
search --no-floppy --fs-uuid --set=root 0a801c26-5320-448d-b261-883499529bc6
fi
linux16 /vmlinuz_koan ro crashkernel=auto biosdevname=0 net.ifnames=0 rhgb quiet LANG=en_US.UTF-8 ksdevice=link lang= text net.ifnames=0 ks=http://192.168.56.11/cblr/svc/op/ks/profile/CentOS-7.4-x86_64 biosdevname=0 kssendmac
initrd16 /initrd.img_koan
也就是说,koan帮我们下载了安装需要的文件,并且修改了Grub的开机启动选项,增加了安装的配置,并且设置为默认启动选项,这样我们执行重启默认就开始安装了。现在执行重启来验证一下。
[root@hadoop-node1 ~]# reboot
你可以看到如下图所示的界面,然后开始进行自动化重新安装。
1.3. 2.3 Cobbler Web介绍
Cobbler还提供了可视化的Web节界面叫做Cobbler Web。我们使用yum安装完毕后,会再/etc/http/conf.d/目录下生成cobbler_web.conf配置文件,可以通过https://IP/cobbler_web来进行访问。
安装Cobbler Web
[root@linux-node1 ~]# yum install -y cobbler-web
配置Cobbler Web
Cobbler web的权限管理有两个配置文件 /etc/cobbler/users.conf和/etc/cobbler/users.digest 后者为Cobbler权限配置文件,我们需要使用htdigest来为用户设置密码
[root@linux-node1 ~]# htdigest /etc/cobbler/users.digest "Cobbler" cobbler
Changing password for user cobbler in realm Cobbler
New password:
Re-type new password:
然后就可以使用cobbler用户和你设置的密码登陆了。 https://192.168.56.11/cobbler_web
1.4. 2.4 深入理解Cobbler
经过前面的内容,我们已经可以顺利的使用Cobbler进行操作系统的安装,使用Koan进行操作系统自动重新安装的操作了,那么我们还需要更多的掌握一些Cobbler的知识,才能在生产的应用中,更如鱼得水。
1.4.1. 2.4.1 Cobbler 配置文件
- Cobbler的目录 Cobbler安装完毕后会在系统生成如下目录:
[root@linux-node2 ~]# find / -name cobbler
/etc/selinux/targeted/active/modules/100/cobbler
/etc/cobbler
/var/lib/cobbler
/var/log/cobbler
/var/www/cobbler
/usr/bin/cobbler
/usr/lib/python2.7/site-packages/cobbler
/usr/share/cobbler
- Cobbler配置文件目录
Cobbler的配置文件存放在/etc/cobbler下。
- /etc/cobbler/settings为主配置文件;
- 在/etc/cobbler下你还能看到dhcp、dns、pxe、dnsmasq的模板配置文件;
- /etc/cobbler/users.digest为用于web访问的用户名密码配置文件;
- /etc/cobbler/modules.conf 为模块配置文件;
/etc/cobbler/users.conf为Cobbler WebUI/Web service授权配置文件。
修改Cobbler提示
如果你想修改Cobbler的提示,可以直接编辑下面文件。
[root@linux-node2 ~]# vim /etc/cobbler/pxe/pxedefault.template
DEFAULT menu
PROMPT 0
MENU TITLE DevOpsEDU | http://www.devopsedu.com
TIMEOUT 200
TOTALTIMEOUT 6000
ONTIMEOUT $pxe_timeout_profile
LABEL local
MENU LABEL (local)
MENU DEFAULT
LOCALBOOT -1
$pxe_menu_items
MENU end
- Cobbler数据目录/var/lib/cobbler,
此目录存储和Cobbler profiles、systems、distros相关的配置。
- configs/ - 此目录用于存储distros、repos、systems和profiles相关信息。
- backup/ - 备份目录
- snippets/ - 用于放置一些可以在kickstarts导入的脚本小片段
- triggers/ - 此目录用来放置一些可执行脚本
kickstarts/ - 此目录用来放置kickstart模板文件
Repo数据目录/var/www/cobbler
导入的发行版,repos镜像和kickstart文件都放置在/var/www/cobbler目录下。确保/var目录有足够的空间来存储这些文件。
- images/ - 存储所有导入发行版的Kernel和initrd镜像用于远程网络启动
- ks_mirror/ - 存储导入的发行版
repo_mirror/ - yum repos存储目录
Cobbler日志目录
/var/log/cobbler用于存放日志文件/var/log/cobbler/cobbler.log
1.4.2. 2.4.2 Cobbler设计方式 {$cs}
Cobbler 有多个对象组成的,对象和对象之间可以相互引用:
- Repo(存储库):保存一个 yum 或 rsync 存储库的镜像信息。例如我们可以将Zabbix的软件仓库同步到我们本地,就是一个Repo。
- Distro(发行版):表示一个操作系统。它承载了内核和 initrd 的信息,以及内核参数等其他数据。
- profile(配置文件):包含一个distro(发行版)、一个 kickstart 文件以及可能的Repo(存储库),还包含更多特定的内核参数等其他数据。
- system(系统):表示要安装的机器。它包含一个配置文件或一个镜像,还包含 IP 和 MAC 地址、电源管理(地址、凭据、类型)以及更为专业的数据等信息。
- Image(镜像):可替换一个包含不属于此类别的文件的发行版对象(例如,无法分为内核和 initrd 的对象)。 基于注册的对象以及各个对象之间的关联,Cobbler 知道如何更改文件系统以反映具体配置。因为系统配置的内部是抽象的,所以您可以仅关注想要执行的操作。
1.4.3. 2.4.3 Cobbler distro
使用Cobbler的第一步就是定义Distro,回想下我们最早执行的cobbler import,就会帮我们创建一个distro,我们可以使用下面的命令进行查看:
[root@linux-node2 ~]# cobbler distro list
CentOS-7-x86_64
Cobbler所有的命令都可以使用help这样的方式获取帮助。
[root@linux-node2 ~]# cobbler distro help
usage
=====
cobbler distro add
cobbler distro copy
cobbler distro edit
cobbler distro find
cobbler distro list
cobbler distro remove
cobbler distro rename
cobbler distro report
我们可以使用report命令来看distro都包含哪些内容。
[root@linux-node2 ~]# cobbler distro report
Name : CentOS-7.7-x86_64
Architecture : x86_64
TFTP Boot Files : {}
Breed : redhat
Comment :
Fetchable Files : {}
Initrd : /var/www/cobbler/ks_mirror/CentOS-7.7-x86_64/images/pxeboot/initrd.img
Kernel : /var/www/cobbler/ks_mirror/CentOS-7.7-x86_64/images/pxeboot/vmlinuz
Kernel Options : {}
Kernel Options (Post Install) : {}
Kickstart Metadata : {'tree': 'http://@@http_server@@/cblr/links/CentOS-7.7-x86_64'}
Management Classes : []
OS Version : rhel7
Owners : ['admin']
Red Hat Management Key : <<inherit>>
Red Hat Management Server : <<inherit>>
Template Files : {}
可以看到distro定义的仅仅是我们要安装操作系统发行版的kernel和initrd。
1.4.4. Cobbler profile
那么在cobbler import的同时也默认创建了一个和distro同名的profile,那么cobbler profile里面包括了distribution、kickstart file和repo。我们也可以把profile理解为一个配置集合,比如在distro的基础上增加可一个kiskstart文件来生成一个特定的系统安装配置。 比如前面我们多次使用cobbler profile edit为指定的Profile设置kickstart文件和内核参数,profile的名称即使我们在使用cobbler进行自动化安装选择的菜单名称。
[root@linux-node1 ~]# cobbler profile report
1.4.5. Cobbler repo
Cobbler repos可以帮我们管理yum仓库,把创建企业内部的yum源变成了一件极其简单的工作,比如通常生成环境我们想把EPEL仓库同步到本地,这样就避免每次安装软件包占用公网贷款。 添加EPEL源:
# cobbler repo add --name=CentOS-7-x86_64-epel \
--mirror=http://mirrors.aliyun.com/epel/7/x86_64/ --arch=x86_64 --breed=wget
参数说明: --name 为安装源定义一个名字 --arch 指定安装源是32位还是64位、ia64, 目前支持的选项有: x86│x86_64│ia64 reposync 操作很重要,因为它会从远程存储库中复制文件。如果创建了存储库对象但未运行 reposync,那么您的存储库将是空的,而且您的安装可能会失败。 根据需要同步的仓库大小,注意硬盘空间,第一次同步时间比较长,建议放到screen里面允许。
添加repo到profile 把我们自定义的repo添加到对应的profile后,那么使用对应profile安装的机器,默认就会添加该repo,前提是需要进行设置
# cobbler profile edit --name=CentOS-7-x86_64 --repos="openstack-liberty"
[root@linux-node1 ~]# vim /etc/cobbler/settings
yum_post_install_mirror: 1 #默认是开启的。
添加更新仓库源计划任务 如果使用的外包源链接,可以定期的进行同步,放在crontab里面每天执行。 echo "0 2 0 cobbler reposync --tries=3 --no-fail" >> /var/spool/cron/root
设置装机自动设置Yum 修改ks脚本,增加
%post
# Start yum configuration
$yum_config_stanza
# End yum configuration
%end
这样开机的时候就可以调用yum_config_stanza这个snippets进行设置了。等等,这个snippets是什么东东?
1.4.6. 2.1.6 Cobbler snippets
snippets可以说是cobbler管理中的精华部分,很少有文档提及,因为大多数人使用简单的cobbler功能已经足够了,如果你想对安装过程和安装后进行定制,可以自己编写Snippet来实现。现在我们可以参考Cobbler自带的kickstart模板,给我们的模板增加上snippets的功能
[root@linux-node1 ~]# vim /var/lib/cobbler/kickstarts/sample_end.ks
%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
将%post开始到%end的内容复制下来,编辑到 /var/lib/cobbler/kickstarts/CentOS-7.9-x86_64-Cobbler.cfg文件中。在后面的自动化实践和高级话题中,我们将用到这个功能。