cobbler自动化批量系统安装


一、要想批量部署操作系统,得具备以下条件:

客户机支持pxe网络引导

服务器端和客户端建立网络通信(DHCP)

服务器端要有可供客户机开机引导的引导文件

服务器端的可引导文件还必须能传递到客户机(TFTP)

客户机无人值守安装包括安装定制的软件或服务(KICKSTART文件)


    1.认识一下PXE

预启动执行环境(Preboot eXecution Environment,PXE,也被称为预执行环境)

提供了一种使用网络接口(Network Interface)启动计算机的机制。

这种机制让计算机的启动可以不依赖本地数据存储设备(如硬盘)或本地已安装的操作系统

PXE:支持工作站通过网络从远端服务器下载映像,并由此支持通过网络启动操作系统


PXE的工作原理

PXE Client发送广播包请求DHCP分配IP地址DHCP

Server回复请求,给出IP地址以及Boot

PXE Client从Server下载引导文件执行引导程序

    2.什么是Kickstart

KickStart是一种无人职守安装方式,是由redhat公司为了解决无人值守安装操作系统而设计的。

系统管理员通过创建一个单一的kickstart配置文件就可以实现自动化安装操作系统。

二、Cobbler介绍

1.什么是Cobbler

官方网站:https://cobbler.github.io/

GITHUB: https://github.com/cobbler/cobbler


Cobbler是一个快速网络安装linux的服务,而且在经过调整也可以支持网络安装windows。

该工具使用python开发,小巧轻便(才15k行代码),使用简单的命令即可完成PXE网络安装环境的配置,同时还可以管理DHCP,DNS,以及yum包镜像。

Cobbler是一个应用程序,可以大批量的部署操作系统,可以同时部署多个不同的操作系统,配合kickstart文件可以定制和无人值守的方式部署操作系统。

它对PXE进行二次开发和封装,同时提供二次开发接口。


Cobbler的配置结构基于一组注册的对象。每个对象表示一个与另一个实体相关联的实体(该对象指向另一个对象,或者另一个对象指向该对象)。

当一个对象指向另一个对象时,它就继承了被指向对象的数据,并可覆盖或添加更多特定信息。

以下对象类型的定义为:

发行版(Distro):

表示一个操作系统。它承载了内核和initrd 的信息,以及内核参数等其他数据。


配置文件(Profile):

包含一个发行版、一个kickstart 文件以及可能的存储库,还包含更多特定的内核参数等其他数据。


系统(System):

表示要配给的机器。它包含一个配置文件或一个镜像,还包含 IP 和 MAC 地址、电源管理(地址、凭据、类型)以及更为专业的数据等信息。


存储库(Repo):

保存一个 yum 或 rsync 存储库的镜像信息。


镜像(Image):

可替换一个包含不属于此类别的文件的发行版对象(例如,无法分为内核和 initrd 的对象)。


基于注册的对象以及各个对象之间的关联,Cobbler 知道如何更改文件系统以反映具体配置。

因为系统配置的内部是抽象的,所以您可以仅关注想要执行的操作。


2.   Cobbler原理

Cobbler由Python语言开发,是对PXE和Kickstart以及DHCP的封装。

融合很多特性,提供了CLI和Web的管理形式。更加方便的实行网络安装。

同时,Cobbler也提供了API接口,使用其它语言也很容易做扩展。

它不紧可以安装物理机,同时也支持kvm、xen虚拟化、Guest OS的安装。

更多的是它还能结合Puppet等集中化管理软件,实现自动化的管理。


Cobbler 支持众多的发行版:Red Hat、Fedora、CentOS、Debian、Ubuntu 和 SuSE。

当添加一个操作系统(通常通过使用 ISO 文件)时,Cobbler 知道如何解压缩合适的文件并调整网络服务,以正确引导机器。

Cobbler 使用命令行方式管理,也提供了基于Web的图形化配置管理工具(cobbler-web)。

通过配置cobbler自动部署DHCP、TFTP、HTTP,在安装过程中加载kiskstart无人值守安装应答文件实现无人值守。

从客户端使用PXE引导启动安装。


Server端


第一步,启动Cobbler服务

第二步,进行Cobbler错误检查,执行cobbler check命令

第三步,进行配置同步,执行cobbler sync命令

第四步,复制相关启动文件文件到TFTP目录中

第五步,启动DHCP服务,提供地址分配

第六步,DHCP服务分配IP地址

第七步,TFTP传输启动文件

第八步,Server端接收安装信息

第九步,Server端发送ISO镜像与Kickstart文件


Client端

第一步,客户端以PXE模式启动

第二步,客户端获取IP地址

第三步,通过TFTP服务器获取启动文件

第四步,进入Cobbler安装选择界面

第五步,客户端确定加载信息

第六步,根据配置信息准备安装系统

第七步,加载Kickstart文件

第八步,传输系统安装的其它文件

第九步,进行安装系统


三、cobbler部署安装


1、采用yum epel源安装Cobbler、dhcp、httpd等服务及相关依赖包

# yum install epel-release -y
# yum clean all
# yum install cobbler cobbler-web httpd rsync tftp-server xinetd dhcp python-ctypes debmirror pykickstart -y


2、启动服务,检查cobbler配置

# systemctl enable cobblerd
# systemctl start cobblerd
# systemctl enable httpd
# systemctl start httpd
# cobbler check

报错:

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 : Some network boot-loaders are missing from /var/lib/cobbler/loaders, you may run 'cobbler get-loaders' to download them, or, if you only want to handle x86/x86_64 netbooting, you may ensure that you have installed a *recent* version of the syslinux package installed and can ignore this message entirely.  Files in this directory, should you want to support all architectures, should include pxelinux.0, menu.c32, elilo.efi, and yaboot. The 'cobbler get-loaders' command is the easiest way to resolve these requirements.
5 : enable and start rsyncd.service with systemctl
6 : comment out 'dists' on /etc/debmirror.conf for proper debian support
7 : comment out 'arches' on /etc/debmirror.conf for proper debian support
8 : 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
9 : fencing tools were not found, and are required to use the (optional) power management features. install cman or fence-agents to use them

解决:

1:设置cobbler服务器IP

# vim /etc/cobbler/settings

修改 server: 127.0.0.1 为 server: “cobbler服务器IP”

2:设置DHCP服务器IP

# vim /etc/cobbler/settings

修改 next_server: 127.0.0.1 为 next_server: “DHCP服务器IP”

3:设置tftp服务被xinetd托管

# vim /etc/xinetd.d/tftp

修改disable的值为no

4:安装网络引导文件boot-loaders

# cobbler get-loaders

5:设置开机启动和开启rsync服务

# systemctl enable rsyncd
# systemctl start rsyncd

6和7:注释/etc/debmirror.conf文件中dists和arches

这是对debian系统的支持,不安装该类系统可以忽略

8:设置自动安装机器后的root默认密码

# openssl passwd -1 -salt 'password' '123456'
$1$password$7ZK8mMeLGXxbNPeJEFJOU0

将产生的加密字符串修改到/etc/cobbler/settings文件中

default_password_crypted: "$1$password$7ZK8mMeLGXxbNPeJEFJOU0"

9:电源管理工具

# yum install fence-agents-all -y

同步修改的,再次检测

# cobbler sync
# systemctl restart cobblerd
# cobbler check
No configuration problems found.  All systems go.


3、配置相关服务

tftp,httpd,dhcp,还包括DNS这些服务都可以由cobbler代为管理,也可以独立管理。

这里都将这些服务设置为单独管理。

# vim /etc/cobbler/settings
manage_dhcp: 0
manage_dns: 0
.....
manage_tftpd: 0
manage_rsync: 0

配置dhcp服务

# cp /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample /etc/dhcp/dhcpd.conf
# vim /etc/dhcp/dhcpd.conf#添加下面这段
subnet 192.168.199.0 netmask 255.255.255.0 {
  range 192.168.199.10 192.168.199.254;
  option routers 192.168.199.1;
  option broadcast-address 192.168.199.255;
  default-lease-time 3600;
  max-lease-time 7200;
  next-server 192.168.199.214;       #指向pxe服务器
  filename "pxelinux.0";
}

启动服务:

# systemctl enable dhcpd
# systemctl start dhcpd
# lsof -i:67
COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
dnsmasq 1452 nobody    3u  IPv4  24096      0t0  UDP *:bootps 
dhcpd   4769  dhcpd    7u  IPv4 137802      0t0  UDP *:bootps


4.启动所有相关服务,并设置开机启动

# systemctl enable tftp
# systemctl start tftp
# systemctl enable rsyncd
# systemctl start rsyncd
# systemctl enable cobblerd
# systemctl start cobblerd
# systemctl enable httpd
# systemctl start httpd

自此cobbler部署安装完成


四、cobbler自动化部署

1.配置cobbler_web的认证功能

cobbler_web支持多种认证方式,如authn_configfile、authn_ldap或authn_pam等,这里使用authn_pam模块实现认证。


# vim /etc/cobbler/modules.conf 
[authentication]
module = authn_pam

添加系统用户:

# useradd baby
# echo "baby" | passwd --stdin baby
Changing password for user baby.
passwd: all authentication tokens updated successfully

.

添加该系统用户至/etc/cobbler/users.conf文件

# vim /etc/cobbler/users.conf 
[admins]
admin = "baby"

配置完成重启cobbler服务:

# systemctl restart cobblerd.service

使用浏览器访问https://192.168.199.214/cobbler_web

2.Cobbler常用命令

distro,profile,system是cobbler三大核心组件,一般情况下这三个部分配置好就可以实现自动化部署了。


Distro

主要用来定义和设置系统发行版本,里包含了系统的内核,引导文件,以及安装包等内容,就相当于一个LINUX系统的ISO镜像。

如:多个发行版本,同一发行版不同版本号。


Profile

主要用来定义kickstart配置文件,安装时依据配置文件进行定制化安装,配置此项时要和distro进行关联。


System

主要用来定义内核参数,电源管理,计算机名,网络接口参数等(客户机),如:根据mac地址配置IP地址,通过和profile,distro关联实现自动化安装。


Repo

主要用来配置yum镜像信息。


Cobbler的帮助信息查看都是这个格式:cobbler 子命令 –-help


查看 distro的帮助信息

# 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


distro命令:

该命令主要用于对distro进行增加,编辑,拷贝,查找,移除,重命名操作。

使用格式为(以add为例):

cobbler distro add --name=string --kernel=path --initrd=path
[--kopts=string] [--kopts-post=string] [--ksmeta=string]
[--arch=x86|x86_64|ia64] [--breed=redhat|debian|suse]
[--template-files=string]

其中:

Name用来指定系统的版本。如:RHEL7

Kernel用来指定一个内核镜像文件的绝对路径。

Initrd用来指定一个RAM镜像文件的绝对路径。

Kopts用来指定内核命令行参数

Kopts-post同上

Arch用来指定操作系统版本平台,如:X86_64

Ksmeta是用来替代kickstart变量的高级特性

Breed用来指定操作系统厂家,如:redhat Ubuntu,centos等

举例:

# cobbler distro add --name=Centos7-x86_64 --kernel=/mnt/isolinux --initrd=/mnt/isolinux --arch=x86_64 --breed=redhat 
# cobbler distro list    #查看所有distro
# cobbler distro rename –name=Centos7-x86_64 –newname=Centos7.1-x86_64


操作添加distro

挂载光盘镜像

# mount /dev/cdrom /mnt/

添加一个distro,耗费的时间取决于镜像的大小

# cobbler import --name=centos-7 --arch=x86_64 --path=/mnt
# cobbler distro list

导入完成后,在/var/www/cobbler/ks_mirror目录下会生成一个--name指定的名称的目录

这个目录与挂载在本地的光盘镜像目录一致。

进行测试安装


profile命令:

该命令主要用于对profile进行增加,编辑,拷贝,查找,移除,重命名等操作。


使用格式为(以add为例):

cobbler profile add --name=string --distro=string [--kickstart=path]
[--kopts=string] [--ksmeta=string] [--virt-file-size=gigabytes]
[--virt-ram=megabytes] [--virt-type=string] [--virt-cpus=integer]
[--virt-path=string] [--virt-bridge=string] [--server]
[--parent=profile]


Name用来指定一个配置文件的描述名称

Distro用来关联distro

Kickstart用来指定kickstart文件路径

Virt-*与虚拟化有关的设置

Server当客户机所在子网无法访问cobbler服务器的时候,这个设置才用得着。

Parent这是一个高级特性,用来指定上一级的配置文件,可以继承父级指定的参数


举例:

# cobbler profile add --name=Centos7-x86_64 --distro=Centos7-x86_64 --kickstart=/var/lib/cobbler/kickstarts/centos7-x86_64.cfg 
# cobbler profile list

操作添加profile

Cobbler默认的ks文件是/var/lib/cobbler/kickstarts/sample_end.ks或者是/var/lib/cobbler/kickstarts/sample.ks。

可以通过cobbler profile report命令来查看和指定ks文件。

ks文件的版本订制可以从sample_end.ks中复制修改实现

# cd /var/lib/cobbler/kickstarts/
# cp sample_end.ks centos-7.ks
# vim centos-7.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#系统认证方式,useshadow 使用隐藏密码 enablemd5加密方式
# System bootloader configuration
bootloader --location=mbr#mbr引导记录
# Partition clearing information
clearpart --all --initlabel#清除系统上所有分区,初始化磁盘卷
# Use text mode install
text#文本模式安装
# Firewall configuration
firewall --enabled#开启防火墙
# Run the Setup Agent on first boot
firstboot --disable#第一次重启系统后启动代理
# System keyboard
keyboard us#键盘布局类型
# System language
lang en_US#系统默认语言
# Use network installation
url --url=$tree#选择http的方式获得镜像源
# If any cobbler repo definitions were referenced in the kickstart profile, include them here.
$yum_repo_stanza#yum源配置
# Network information
$SNIPPET('network_config')#网络设置
# Reboot after installation
reboot#系统安装完成后重启
#Root password
rootpw --iscrypted $default_password_crypted#root密码
# SELinux configuration
selinux --disabled#selinux
# Do not configure the X Window System
skipx#跳过图形下的初始设置
# System timezone
timezone  America/New_York#时区
# Install OS instead of upgrade
install#安装系统或更新系统
# Clear the Master Boot Record
zerombr#清除原有mbr
# 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


进行自定义修改,然后导入ks文件与centos-7-x86_64关联

# cobbler profile add --name=centos-7 --distro=centos-7-x86_64 --kickstart=/var/lib/cobbler/kickstarts/centos-7.ks
# cobbler profile list
   centos-7
   centos-7-x86_64
# cobbler sync

进行测试安装


system命令:

此命令主要用于定义客户机的网络接口参数,如指定IP MASK GATEWAY等。

cobbler system add --name=string --profile=string [--mac=macaddress]
[--ip-address=ipaddress] [--hostname=hostname] [--kopts=string]
[--ksmeta=string] [--kickstart=path] [--netboot-enabled=Y/N]
[--server=string] [--gateway=string] [--dns-name=string]
[--static-routes=string] [--power-address=string]
[--power-type=string] [--power-user=string] [--power-pass=string]
[--power-id=string]

Name指定一个名字

Profile指定一个profile

Mac指定客户机的MAC地址,这是实现自动化安装的必要参数,否则客户机通过网络引导后会停留在引导菜单那一步。

Ip-address配置客户机的IP地址

Hostname配置客户机的计算机名

Kickstart指定kickstart配置文件的路径

Netboot-enable是否开启网络启动

Server指定cobbler服务器的地址

Gateway指定客户机的网关

Dns-name指定客户机的DNS域名

Static-routers无需关心,大部分机器不需设置此项

[--power-*]这些参数与电源管理相关。

举例:

# cobbler system add --name=Centos7-x86_64 --kickstart=/var/lib/cobbler/kickstarts/centos7-x86_64.cfg --mac=00:0C:29:41:FC:6C --hostname=backup --profile=Centos7-x86_64 --interface ens33 
# cobbler list 
distros: 
Centos7-x86_64  
profiles: 
Centos7
Centos7-x86_64 
systems: 
Centos7-x86_64 
repos: 
p_w_picpaths: 
mgmtclasses: 
packages: 
files:

定制化安装

VMware中的客户机在设置中可以看到网卡的MAC地址

# cobbler system add --name=test --mac=00:0C:29:33:75:94  --profile=CentOS-7 --ip-address=192.168.199.215 --subnet=255.255.255.0 --gateway=192.168.199.1 --interface=ens33 --static=1 --hostname=linux_node --name-servers="114.114.114.114"
# cobbler sync

进行测试安装

repo命令

此命令主要用于指定一个仓库源镜像,不仅仅用import方式导入的安装树中的源

还可以是rsync://URL SSH本地源,http://URL ,FTP://URL,或者是本地源。

命令格式:

cobbler repo add --mirror=url --name=string [--rpmlist=list]
[--creatrepo-flags=string] [--keep-updated=Y/N] [--priority=number]
[--arch=string] [--mirror-locally=Y/N] [--breed=yum|rsync|rhn]


Mirror指定镜像仓库源的地址

Name定义名称

Rpmlist后面跟的是以空格分隔的软件包的名称列表,仅在http://url,ftp://url模式下生效。这种方式可以节省时间,空间和带宽。因为在这个列表中的包会被安装,其它的都不安装。

Createrepo-flags当cobbler reposync运行时向其传递一个特殊的参数,默认为“-c cache”

Keep-updated是否时刻保持repo的更新

Priority优先级,数字越小优先级越高,默认99.作用于所有cobbler镜像源。

Arch定义仓库使用什么样的架构,默认使用当前系统架构

Mirror-locally定义使用本地源还是互联网源

Breed通常无需定义,系统就知道我们想干什么,当然你也可以指定特殊的。

举例:

# cobbler repo add --mirror=https://mirrors.aliyun.com/centos/7.3.1611/os/x86_64/ --name=centos7.3-x86_64 
# cobbler repo list 
centos7.3-x86_64 
# cobbler reposync #同步repo,会从网上下载centos7.3.1611的安装树到这个位置/var/www/cobbler/repo_mirror

下载完毕后可以安装测试

koan客户端重装系统

1.安装koan

# yum install epel-release -y
# yum install koan -y

2.查看远程Cobbler系统上已有的系统版本对象

# koan --server=192.168.199.214 --list=profiles
- looking for Cobbler at http://192.168.199.214:80/cobbler_api
centos-7
centos-7-x86_64

3.为客户端指定一个要安装的系统版本

# koan -r --server=192.168.199.214 --profile=centos-7


4.重启进入自动安装

# reboot

附录:cobbler配置文件说明

#rpm -ql cobbler
/etc/cobbler配置文件目录
/etc/cobbler/settingscobbler主配置文件
/etc/cobbler/dhcp.templateDHCP服务的配置模板
/etc/cobbler/tftpd.templatetftp服务的配置模板
/etc/cobbler/rsync.templatersync服务的配置模板
/etc/cobbler/isoiso模板配置文件目录
/etc/cobbler/pxepxe模板文件目录
/etc/cobbler/power电源的配置文件目录
/etc/cobbler/users.confWeb服务授权配置文件
/etc/cobbler/users.digestweb访问的用户名密码配置文件
/etc/cobbler/dnsmasq.templateDNS服务的配置模板
/etc/cobbler/modules.confCobbler模块配置文件
/var/lib/cobblerCobbler数据目录
/var/lib/cobbler/config配置文件
/var/lib/cobbler/kickstarts默认存放kickstart文件
/var/lib/cobbler/loaders存放的各种引导程序
/var/www/cobbler系统安装镜像目录
/var/www/cobbler/ks_mirror导入的系统镜像列表
/var/www/cobbler/p_w_picpaths导入的系统镜像启动文件
/var/www/cobbler/repo_mirroryum源存储目录
/var/log/cobbler日志目录
/var/log/cobbler/install.log客户端系统安装日志
/var/log/cobbler/cobbler.logcobbler日志