使用Cobbler批量安装操作系统

前言

在实际生产中,我们常常会遇到这样一种情况,就是我们需要同时安装几十甚至上百台服务器,如果我们使用U盘或者光盘的方式的话,或许老板直接就将我们辞退了。这里我们介绍一种能够实现自动化安装操作系统的方式。
我们搭建Cobbler的实验环境是基于CentOS 7.3 -1611 的基础的。
同时这个实验可以帮助你一步步的完成所有的操作,不过,如果你想自定义安装的内容的话,建议你先看本文自定义kickstart文件部分,以便有所印象。

Cobbler 简介

Cobbler项目是在2008年由RedHat发布的网络安装服务器套件。是一种快速网络安装linux操作系统的服务,支持众多的Linux发行版:Red Hat、Fedora、CentOS、Debian、Ubuntu和SUSE,也可以支持网络安装Windows。

PXE 是由Intel开发的技术,基于Client/Server的网络模式,支持远程主机通过网络从远端服务器下载映像,并由此支持通过网络启动操作系统。也可以实现通过网络批量的安装操作系统。

Cobbler 正是基于PXE的二次封装,将多种安装参数封装到一个菜单当中。Cobbller提供了CLI和WEB两种安装方式,使用起来更加的友好。

Cobbler 工作流程

关于Cobbler的工作流程,可以用下面的图进行概括。

实验环境准备

因为我们正在搭建的是网络服务,所以需要有至少两台主机,其中一台主机承担服务器的角色。我在在这个实验中,使用的CentOS 7 搭建的网络服务,承担Server角色。

关闭防火墙和SELinux

防火墙和SELinux 在实验过程中有可能会导致意想不到的问题,所以我们这里将其关闭,实际生产中,请根据自己的实际情况进行调整。至于关闭防火墙和SELinux就不列出了,这很简单。

安装Cobbler

Cobbler 默认不在附带在光盘中,所以要通过EPEL源进行下载安装。 配置好epel源以后,执行yum install cobbler就可以安装Cobbler了。
在安装好Cobbler之后,我们就会发现,Cobbler还附带安装了很多其他的服务,如下图所示 。这也就是解释了在Cobbler的工作流程中为什么能够用到那么多服务,从图中可以看出,还缺少DHCP服务,所以接下里我们也要安装一下。其中,syslinux 就是安装的就是PXE服务。

设置常用服务

将Cobbler服务设为开机启动

systemctlenable cobblerd
systemctlstart cobblerd

将tftp 设为开机启动

systemctlenable tftp
systemctlstart tftp

将http 设为开机启动

systemctlenable httpd
systemctlstart httpd

安装DHCP服务 为稍后使用Cobbler来管理DHCP做准备

在稍后,我们会利用Cobbler来自动管理DHCP。

yuminstall dhcp

运行Cobbler Check 进行环境检查

Cobbler Check命令能够非常迅速的帮助我们找到目前环境中还缺少的配置,然后我们根据这里的提示进行相应的配置调整。 这里出现了很多的问题,看到之后不要惊慌,根据提示一点一点去处理就好了。另外,因为实际生产环境中,所有的主机都会有所不同,所以这里的提示信息应该也会有所不同,我们这里只是根据实验环境进行解决,其余的问题,可以留言一起讨论。

#执行Cobblercheck检查目前环境还缺少哪些东西
[root@localhost~]#cobblercheck
Thefollowingarepotentialconfigurationitemsthatyoumaywanttofix:

1:The'server'fieldin/etc/cobbler/settingsmustbesettosomethingotherthanlocalhost,orkickstartingfeatureswillnotwork. ThisshouldbearesolvablehostnameorIPforthebootserverasreachablebyallmachinesthatwilluseit.
2:ForPXEtobefunctional,the'next_server'fieldin/etc/cobbler/settingsmustbesettosomethingotherthan127.0.0.1,andshouldmatchtheIPofthebootserveronthePXEnetwork.
3:change'disable'to'no'in/etc/xinetd.d/tftp
4:somenetworkboot-loadersaremissingfrom/var/lib/cobbler/loaders,youmayrun'cobblerget-loaders'todownloadthem,or,ifyouonlywanttohandlex86/x86_64netbooting,youmayensurethatyouhaveinstalleda*recent*versionofthesyslinuxpackageinstalledandcanignorethismessageentirely. Filesinthisdirectory,shouldyouwanttosupportallarchitectures,shouldincludepxelinux.0,menu.c32,elilo.efi,andyaboot.The'cobblerget-loaders'commandistheeasiestwaytoresolvetheserequirements.
5:enableandstartrsyncd.servicewithsystemctl
6:debmirrorpackageisnotinstalled,itwillberequiredtomanagedebiandeploymentsandrepositories
7:Thedefaultpasswordusedbythesampletemplatesfornewlyinstalledmachines(default_password_cryptedin/etc/cobbler/settings)isstillsetto'cobbler'andshouldbechanged,try:"opensslpasswd-1-salt'random-phrase-here''your-password-here'"togeneratenewone
8:fencingtoolswerenotfound,andarerequiredtousethe(optional)powermanagementfeatures.installcmanorfence-agentstousethem

Restartcobblerdandthenrun'cobblersync'toapplychanges.

首先我们来看第一个问题。信息提示,在/etc/cobbler/settings这个配置文件中的server 字段一定要有一个其他主机能够访问到的主机名或者IP地址,否则KickStart的一些特性将无法使用。其实也很好理解,我们目前的主机是用来提供网络服务的。其他主机通过访问我们的Cobbler服务,进行下载安装操作系统,如果我们的Server字段别人不能访问,那怎么可以呢! 所以打开该配置文件,修改server字段为本机的IP地址。

修改完成之后,我们注意到,之前的提示信息末尾有一段提示信息Restart cobblerd and then run 'cobbler sync' to apply changes.,所以我们运行一下这两个命令。

#重启服务
[root@localhost~]#systemctlrestartcobblerd

#同步信息
[root@localhost~]#cobblersync
........... #中间省略输出信息 


#重新检查一遍,看看还缺少哪些东西。
[root@localhost~]#cobblercheck
Thefollowingarepotentialconfigurationitemsthatyoumaywanttofix:

1:ForPXEtobefunctional,the'next_server'fieldin/etc/cobbler/settingsmustbesettosomethingotherthan127.0.0.1,andshouldmatchtheIPofthebootserveronthePXEnetwork.
2:somenetworkboot-loadersaremissingfrom/var/lib/cobbler/loaders,youmayrun'cobblerget-loaders'todownloadthem,or,ifyouonlywanttohandlex86/x86_64netbooting,youmayensurethatyouhaveinstalleda*recent*versionofthesyslinuxpackageinstalledandcanignorethismessageentirely. Filesinthisdirectory,shouldyouwanttosupportallarchitectures,shouldincludepxelinux.0,menu.c32,elilo.efi,andyaboot.The'cobblerget-loaders'commandistheeasiestwaytoresolvetheserequirements.
3:enableandstartrsyncd.servicewithsystemctl
4:debmirrorpackageisnotinstalled,itwillberequiredtomanagedebiandeploymentsandrepositories
5:Thedefaultpasswordusedbythesampletemplatesfornewlyinstalledmachines(default_password_cryptedin/etc/cobbler/settings)isstillsetto'cobbler'andshouldbechanged,try:"opensslpasswd-1-salt'random-phrase-here''your-password-here'"togeneratenewone
6:fencingtoolswerenotfound,andarerequiredtousethe(optional)powermanagementfeatures.installcmanorfence-agentstousethem

Restartcobblerdandthenrun'cobblersync'toapplychanges.

重新检查之后,我们发现解决了2个问题。然后我们接着来看第一个问题。这个问题是说,next_server 字段也应该是一个具体的地址,而不应该是127.0.0.1.其实这个问题和上面我们修改过的问题是一样的。所以我们重新将next_server的地址修改为我们本机的地址。

修改之后,重新启动服务,并进行同步。

#重启服务
[root@localhost~]#systemctlrestartcobblerd

#同步信息[root@localhost~]#cobblersync
........... #中间省略输出信息 

#重新检查一遍,看看还缺少哪些东西。
[root@localhost~]#cobblercheck
Thefollowingarepotentialconfigurationitemsthatyoumaywanttofix:

1:somenetworkboot-loadersaremissingfrom/var/lib/cobbler/loaders,youmayrun'cobblerget-loaders'todownloadthem,or,ifyouonlywanttohandlex86/x86_64netbooting,youmayensurethatyouhaveinstalleda*recent*versionofthesyslinuxpackageinstalledandcanignorethismessageentirely. Filesinthisdirectory,shouldyouwanttosupportallarchitectures,shouldincludepxelinux.0,menu.c32,elilo.efi,andyaboot.The'cobblerget-loaders'commandistheeasiestwaytoresolvetheserequirements.
2:enableandstartrsyncd.servicewithsystemctl
3:debmirrorpackageisnotinstalled,itwillberequiredtomanagedebiandeploymentsandrepositories
4:Thedefaultpasswordusedbythesampletemplatesfornewlyinstalledmachines(default_password_cryptedin/etc/cobbler/settings)isstillsetto'cobbler'andshouldbechanged,try:"opensslpasswd-1-salt'random-phrase-here''your-password-here'"togeneratenewone
5:fencingtoolswerenotfound,andarerequiredtousethe(optional)powermanagementfeatures.installcmanorfence-agentstousethem

Restartcobblerdandthenrun'cobblersync'toapplychanges.

出现的问题继续减少了。我们接着来看第一个信息。这个信息是说,在/var/lib/cobbler/loaders路径下,少了一些必须的文件,可以使用cobbler get-loaders命令从网络中下载相关的文件。所以,接下来我们运行一下cobbler get-loaders

#可以看到/var/lib/cobbler/loaders目录下没有任何内容
[root@localhost~]#ls/var/lib/cobbler/loaders

[root@localhost~]#cobblerget-loaders
taskstarted:2017-09-20_084605_get_loaders
taskstarted(id=DownloadBootloaderContent,time=WedSep2008:46:052017)downloadinghttps://cobbler.github.io/loaders/READMEto/var/lib/cobbler/loaders/README
downloadinghttps://cobbler.github.io/loaders/COPYING.eliloto/var/lib/cobbler/loaders/COPYING.elilo
downloadinghttps://cobbler.github.io/loaders/COPYING.yabootto/var/lib/cobbler/loaders/COPYING.yaboot
downloadinghttps://cobbler.github.io/loaders/COPYING.syslinuxto/var/lib/cobbler/loaders/COPYING.syslinux
downloadinghttps://cobbler.github.io/loaders/elilo-3.8-ia64.efito/var/lib/cobbler/loaders/elilo-ia64.efi
downloadinghttps://cobbler.github.io/loaders/yaboot-1.3.17to/var/lib/cobbler/loaders/yaboot
downloadinghttps://cobbler.github.io/loaders/pxelinux.0-3.86to/var/lib/cobbler/loaders/pxelinux.0
downloadinghttps://cobbler.github.io/loaders/menu.c32-3.86to/var/lib/cobbler/loaders/menu.c32
downloadinghttps://cobbler.github.io/loaders/grub-0.97-x86.efito/var/lib/cobbler/loaders/grub-x86.efi
downloadinghttps://cobbler.github.io/loaders/grub-0.97-x86_64.efito/var/lib/cobbler/loaders/grub-x86_64.efi

***TASKCOMPLETE***

此时查看/var/lib/cobbler/loaders目录下,会发现,已经下载了很多的内容。然后重启和同步服务,并运行cobbler check查看还剩下一些什么问题,我们接着一个个去解决。

#这个目录下最关键的文件就是 menu.c32 pxelinux.0
[root@localhost~]#ls/var/lib/cobbler/loaders
COPYING.elilo  COPYING.yaboot grub-x86_64.efi menu.c32  README
COPYING.syslinux elilo-ia64.efi grub-x86.efi  pxelinux.0 yaboot

[root@localhost~]#cobblercheck
Thefollowingarepotentialconfigurationitemsthatyoumaywanttofix:

1:enableandstartrsyncd.servicewithsystemctl
2:debmirrorpackageisnotinstalled,itwillberequiredtomanagedebiandeploymentsandrepositories
3:Thedefaultpasswordusedbythesampletemplatesfornewlyinstalledmachines(default_password_cryptedin/etc/cobbler/settings)isstillsetto'cobbler'andshouldbechanged,try:"opensslpasswd-1-salt'random-phrase-here''your-password-here'"togeneratenewone
4:fencingtoolswerenotfound,andarerequiredtousethe(optional)powermanagementfeatures.installcmanorfence-agentstousethem

Restartcobblerdandthenrun'cobblersync'toapplychanges.

接下来,我来看剩下的问题。前两个问题不是很重要,我们先忽略过去。来看第三个问题,这个问题是说,应该为每一个新安装的主机设置一个默认的用户口令。并且信息中提示了使用

openssl passwd -1 -salt 'random-phrase-here' 'your-password-here'

命令来生成加密的用户口令。 如果不设置,默认的用户口令就是cobbler

#生成密码是123456的用户口令
[root@localhost~]opensslpasswd-1 123456$1$wuX5VyFf$a0Y/HdzHRCbVWhSFnbV8n/

然后将生成的口令替换到/etc/cobbler/settingsdefault_password_crypted字段中。

重新启用并同步服务,然后看看还有什么问题。

[root@localhost~]#cobblercheck
Thefollowingarepotentialconfigurationitemsthatyoumaywanttofix:

1:enableandstartrsyncd.servicewithsystemctl
2:debmirrorpackageisnotinstalled,itwillberequiredtomanagedebiandeploymentsandrepositories
3:fencingtoolswerenotfound,andarerequiredtousethe(optional)powermanagementfeatures.installcmanorfence-agentstousethem

Restartcobblerdandthenrun'cobblersync'toapplychanges.

fencing tools是在集群环境中才会使用到的工具,我们暂且先不用管。这样一来,关于Cobbler的check工作我们就完成了。

通过Cobbler 管理DHCP

我们利用DHCP的cobbler 功能来自动管理DHCP,修改/etc/cobbler/settings配置文件中的manage_dhcp字段。 设置为1,则由Cobbler 来管理DHCP

修改Cobbler下/etc/cobbler/dhcp.template模板文件

/etc/cobbler/dhcp.template这个文件是配置DHCP的内容的。但是这个文件是Cobbler来提供的。也就是说,配置好了这个文件,重新启动Cobbler 服务,Cobbler就会自动地替我们管理DHCP。

#在/etc/cobbler/dhcp.template找到下面地这段内容进行修改整理。
#按照自己地要求进行DHCP地配置。

subnet172.18.2.0netmask255.255.255.0{
  optionsubnet-mask    255.255.255.0;
  rangedynamic-bootp    172.18.2.100172.18.2.254;
  default-lease-time    21600;
  max-lease-time      43200;
  next-server        $next_server;
  class"pxeclients"{
     matchifsubstring(optionvendor-class-identifier,0,9)="PXEClient";     
     ifoptionpxe-system-type=00:02{
         filename"ia64/elilo.efi";     
         }elseifoptionpxe-system-type=00:06{
         filename"grub/grub-x86.efi";     
         }elseifoptionpxe-system-type=00:07{
         filename"grub/grub-x86_64.efi";     
         }else{
         filename"pxelinux.0";     
         }
  }}

重新启动和同步Cobbler 服务。

#重新启动服务
[root@localhost~]#systemctlrestartcobblerd

[root@localhost~]#cobblersync
........#中间省略很多输出


#查看DHCP的配置文件
[root@localhost~]#cat/etc/dhcp/dhcpd.conf
#******************************************************************
#Cobblermanageddhcpd.conffile
#generatedfromcobblerdhcp.conftemplate(WedSep2001:36:152017)
#DoNOTmakechangesto/etc/dhcpd.conf.Instead,makeyourchanges
#in/etc/cobbler/dhcp.template,as/etc/dhcpd.confwillbe
#overwritten.
#******************************************************************

ddns-update-styleinterim;

allowbooting;
allowbootp;

ignoreclient-updates;setvendorclass=optionvendor-class-identifier;

optionpxe-system-typecode93=unsignedinteger16;

subnet172.18.2.0netmask255.255.255.0{
  optionsubnet-mask    255.255.255.0;
  rangedynamic-bootp    172.18.2.100172.18.2.254;
  default-lease-time    21600;
  max-lease-time      43200;
  next-server        172.18.2.77;
  class"pxeclients"{
     matchifsubstring(optionvendor-class-identifier,0,9)="PXEClient";     
     ifoptionpxe-system-type=00:02{
         filename"ia64/elilo.efi";     
         }elseifoptionpxe-system-type=00:06{
         filename"grub/grub-x86.efi";     
         }elseifoptionpxe-system-type=00:07{
         filename"grub/grub-x86_64.efi";     
         }else{
         filename"pxelinux.0";     
         }
  }}
  
  #groupforCobblerDHCPtag:default
  group{
  }

制作 yum 源

与PXE不同,Cobbler可以自动帮助我们生成系统安装的yum 源,而不需要我们自己去制作,我们只要指定光盘路径就好了

#执行下面的命令制作CentOS7.3的启动光盘
[root@localhost~]#cobblerimport--path=/misc/cd--name=centos7.3

#然后更换ISO镜像,制作6.9的光盘内容
[root@localhost~]#cobblerimport--path=/misc/cd--name=centos6.9

Cobbler 制作的光盘yum源到底放在了什么地方呢??

#进入到下面的这个路径下,可以看到这里生成了两个源,其实也就是将光盘里的内容复制过来了而已。
[root@localhost~]#cd/var/www/cobbler/ks_mirror/
[root@localhostks_mirror]#ls
centos6.9 centos7.3 config

#执行下面的命令可以看到,这里已经成功的制作了两个系统yum源。
[root@localhostks_mirror]#cobblerdistrolist
 centos6.9-x86_64
 centos7.3-x86_64

修改启动菜单

yum 源文件创建成功之后,可以进行自定义的配置修改。

#查看生成的启动菜单文件
[root@localhosttftpboot]#cat/var/lib/tftpboot/pxelinux.cfg/default
DEFAULTmenu
PROMPT0
MENUTITLECobbler|http://cobbler.github.io/ #可以修改为自己的标签。
TIMEOUT200
TOTALTIMEOUT6000
ONTIMEOUTlocal

LABELlocal    
    MENULABEL(local)
    MENUDEFAULT
    LOCALBOOT-1

LABELcentos6.9-x86_64
    kernel/images/centos6.9-x86_64/vmlinuz
    MENULABELcentos6.9-x86_64
    appendinitrd=/images/centos6.9-x86_64/initrd.imgksdevice=bootiflang= kssendmactext ks=http://172.18.2.77/cblr/svc/op/ks/profile/centos6.9-x86_64
    ipappend2

LABELcentos7.3-x86_64
    kernel/images/centos7.3-x86_64/vmlinuz
    MENULABELcentos7.3-x86_64
    appendinitrd=/images/centos7.3-x86_64/initrd.imgksdevice=bootiflang= kssendmactext ks=http://172.18.2.77/cblr/svc/op/ks/profile/centos7.3-x86_64
    ipappend2



MENUend

启动网络安装

重新新建一个虚拟机,作为客户端,然后启动网络安装,就可以看到我们刚刚制作的界面了。

自定义KickStart文件
首先准备KickStart文件

在上面的实验中,我们没有进行任何的安装配置,全部都是Cobbler帮助我们完成的。可是在实际生产中,我们对每台主机的硬盘分区,安装的软件都有相应的要求,所以我们需要自己来定制KickStart文件,这样我们就能够定制地批量安装操作系统了。
我们就利用实验环境(CentOS 7.3)中的KickStart文件来定制我们自己的KickStart文件。

#首先进入到Cobbler的KickStart目录下
[root@localhost]cd/var/lib/cobbler/kickstarts/

#将根目录下的ks文件复制到当前目录下
[root@localhostkickstarts]cp/root/anaconda-ks.cfg centos7.cfg


[root@localhostkickstarts]#catcentos7.cfg
#version=DEVEL
#Systemauthorizationinformation
auth--enableshadow--passalgo=sha512

#UseCDROMinstallationmediaurl

--url=$tree	#url可以指定详细路径也可以使用$tree变量来进行替代

#Usegraphicalinstall
reboot	#安装成功之后可以重启
text	#采用字符界面进行安装
#RuntheSetupAgentonfirstboot
firstboot--enable
ignoredisk--only-use=sda

#Keyboardlayouts
keyboard--vckeymap=us--xlayouts='us'

#Systemlanguage
langen_US.UTF-8

#Networkinformation
#修改网络环境
network --bootproto=dhcp--device=ens33--onboot=yes--ipv6=auto--activate
network --hostname=localhost.localdomain



selinux--disabled#禁用SElinux也可以禁用防火墙

#Rootpassword	#设置root用户的初始密码rootpw--iscrypted$6$BlwRFg7fgO1i8eQa$F9yjtePt1aOnsIwmNO7mexdnBOsJTVSSyTnwp2hS2lSY87thmNylXw43.Ycj6lfY1wk2NmoQjO/WzEEGBUlZt.

#Systemservices
services--disabled="chronyd"

#Systemtimezone
timezoneAsia/Shanghai--isUtc--nontp
#XWindowSystemconfigurationinformation
xconfig --startxonboot
#Systembootloaderconfiguration
bootloader--append="crashkernel=auto"--location=mbr--boot-drive=sda
#Partitionclearinginformation
zerombr	#清楚MBR分区
clearpart--all#清楚系统中原有分区

#Diskpartitioninginformation
part/--fstype="xfs"--ondisk=sda--size=56320
partswap--fstype="swap"--ondisk=sda--size=2000
part/app--fstype="xfs"--ondisk=sda--size=51200
part/boot--fstype="xfs"--ondisk=sda--size=1024

%packages
@^graphical-server-environment
@base
@core
@desktop-debugging
@dial-up
@fonts
@gnome-desktop
@guest-agents
@guest-desktop-agents
@hardware-monitoring
@input-methods
@internet-browser
@multimedia
@print-client
@x11
kexec-tools

%end


%post
#删除旧的yum仓库#可根据实际情况稍作调整。
rm-rf/etc/yum.repos.d/*
cat>/etc/yum.repos.d/base.repo<<eof [base]="" name="base" baseurl="file:///misc/cd" gpgcheck="0" eof="" %end
将KickStart文件和yum源做关联

在Cobbler 自动创建了yum源之后,会自动有一个ks文件与该源相对应。现在我们需要将自己制作的KS文件与原有的yum源进行关联。

#--name我们需要添加的启动菜单的名字
#--distro 我们自定制的ks文件关联的yum库
#--kickstart我们自定制的ks文件的路径
[root@localhostkickstarts]#cobblerprofileadd--name=centos7.3-x86_64-desktop--distro=centos7.3-x86_64--kickstart=/var/lib/cobbler/kickstarts/centos7.cfg


#查看Cobbler中现在还有多少个启动项
[root@localhostkickstarts]#cobblerprofilelist
 centos6.9-x86_64
 centos7.3-x86_64
 centos7.3-x86_64-desktop

重新修改/var/lib/tftpboot/pxelinux.cfg/default文件

在执行Cobbler sync 同步操作之后,Cobbler会自动的将/var/lib/tftpboot/pxelinux.cfg/default文件重新生成,所以,最好重新按照自己的需求修改一下这个文件。然后就可以开始进行网络安装操作系统了。

从图中可以清楚地看到我们新增地一个启动选项。至此,我们搭建Cobbler 的环境就成功了。

注意事项

在实际生产中,一般是局域网环境,所以在配置DHCP服务的时候,要注意局域网的工作环境。

Cobbler实际上就是对PXE的封装,它帮助我们实现了很多的内容,免去了我们很多的手动配置,但是这样的话,我们也不能清楚的了解底层的运行机制,出了问题,不便于处理,所以最好还是尝试手动配置一下PXE来搭建网络操作系统安装环境,以便了解的更详细一下其中的运行机制。

最好手动配置ks文件,这样能够最大限度上定制自己的集群操作系统。

结语

Cobbler 其实还支持基于Web的管理方式。在配置好Cobbler 服务环境之后,安装一个cobbler-web的软件包,然后就可以进行相应的配置管理了,这里我们不做详细介绍,感兴趣的朋友可以去查阅一下资料。 Cobbler_web 的图形管理界面如下图所示,从图中我们能够看到我们自己指定的菜单和系统yum源。

通过上面的操作,我们已经能够完全搭建一个基于Cobbler的网络服务,用来给生产环境中批量的安装操作系统了。

:http://www.linuxidc.com/Linux/2017-10/147354.htm