在 CentOS Linux 上安装 Cobbler 批量部署系统,现在有很多开源工具可


每次給自己的电脑重装系统都是一件很无聊的事情,如果需要重装上百台虚拟机和服务器不但很无聊而且很耗时,面对现在云时代大量服务器和虚拟机的出现,运维必须要自动化。现在有很多开源工具可以帮助我们实现自动化安装系统,比如 FAI, Cobbler, Spacewalk, Ubuntu Orchestra 等,我们打算把 Cobbler 安装在某台虚拟机上,为我们新购的16台刀片服务器自动安装系统。

什么是 Cobbler?

Cobbler 是一个系统启动服务(boot server),可以通过网络启动(PXE)的方式用来快速安装、重装物理服务器和虚拟机,支持安装不同的 Linux 发行版和 Windows. Cobbler 是个轻量级 Python 程序,总共大概1.5万行代码,还可以用来管理 DHCP, DNS, yum 源等。Cobbler 使用命令行方式管理,也提供了基于 Web 的界面管理工具(cobbler-web),不过命令行方式已经很方便,实在没有必要为了不实用的 Web 界面再添加一个 Web 服务器。

修改 DHCP 服务器配置

使用 Cobbler 最好配合现有局域网上的 DHCP 服务器一起使用,这样不会因为 Cobbler 干扰现有局域网。我们不打算用 Cobbler 来管理整个网络的 DHCP,因为我们已经有了 DHCP 服务器,所以只要在现有的 DHCP 服务器上做以下配置即可,下面记得调整 192.168.2.22 这个 ip 地址指向 Cobbler 服务器:

# for Cobbler setup
host cobbler {
    option host-name "cobbler";
    ddns-hostname "cobbler";
    hardware ethernet 00:0c:29:2d:2c:39; #MAC address of cobbler server
            fixed-address 192.168.2.22; #IP of Cobbler server
            allow booting;
    allow bootp;
    class "PXE" {
        match if substring(option vendor-class-identifier, 0, 9) = "PXEClient";
        next-server 192.168.2.22; #IP of Cobbler server
            filename "pxelinux.0";
    }
}

安装和配置 Cobbler

Cobbler 不在 CentOS 6.2 的基本源中,需要导入 EPEL 源:

# rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-5.noarch.rpm
Retrieving http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-5.noarch.rpm
warning: /var/tmp/rpm-tmp.lo2Hd0: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Preparing...                ########################################### [100%]
   1:epel-release           ########################################### [100%]

# yum update
# yum upgrade

安装 cobbler:

# yum install cobbler

修改配置,主要修改 cobbler 服务器所在的 IP 地址:

# vi /etc/cobbler/settings
...
next_server: 192.168.2.22 #IP of Cobbler server
server: 192.168.2.22 #IP of Cobbler server
...

启用 httpd, xinetd 和 cobbler 服务并确认已经加到系统自动启动服务中:

# /etc/init.d/httpd start
# /etc/init.d/xinetd start
# /etc/init.d/cobblerd start

# chkconfig httpd on
# chkconfig xinetd on
# chkconfig cobblerd on

修改 rsync 和 tftp 这两个服务的 xinetd 配置:

# vi /etc/xinetd.d/rsync
service rsync
{
        disable = no
...
}

# vi /etc/xinetd.d/tftp
service tftp
{
        ...
        disable = no
        ...
}

关闭防火墙和 SELinux 后重启系统:

# /etc/init.d/iptables stop
# chkconfig iptables off

# vi /etc/sysconfig/selinux
...
SELINUX=disabled
...

# reboot

检查和修改 Cobbler 配置

系统重启后用 cobbler check 检查发现3个配置信息问题,第一个是如果要部署 Debian/ubuntu 系统需要 debmirror 软件包;第二个是需要修改 cobbler 的默认密码;第三个是可选,想使用电源管理功能的话需要安装 cman 或 fence-agents:

# cobbler get-loaders

# cobbler check
The following are potential configuration items that you may want to fix:

1 : debmirror package is not installed, it will be required to manage debian deployments and repositories
2 : 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
3 : fencing tools were not found, and are required to use the (optional) power management features. install cman or fence-agents to use them

Restart cobblerd and then run 'cobbler sync' to apply changes.

现在来修复上面三个问题,我们希望能让这台 cobbler 服务器能同时部署 CentOS/Fedora 和 Debian/Ubuntu 系统,所以需要安装 debmirror,安装 debmirror-20090807-1.el5.noarch.rpm 前需要先安装依赖包:

# yum install wget
# yum install ed patch perl perl-compress-Zlib perl-Cwd perl-Digest-MD5 
perl-Digest-SHA1 perl-LockFile-Simple perl-libwww-perl

# wget ftp://fr2.rpmfind.net/linux/epel/5/ppc/debmirror-20090807-1.el5.noarch.rpm

# rpm -ivh debmirror-20090807-1.el5.noarch.rpm

修改 /etc/debmirror.conf 配置文件,注释掉 @dists 和 @arches 两行:

# vi /etc/debmirror.conf
...
#@dists="sid";
@sections="main,main/debian-installer,contrib,non-free";
#@arches="i386";
...

用 openssl 生成一串密码后加入到 cobbler 的配置文件(/etc/cobbler/settings)里,替换 default_password_crypted 字段:

# openssl passwd -1 -salt 'www.example.com' 'example'
$1$www.example$T5FgCHY2P0NDr6JmbN0Bl0

# vi /etc/cobbler/settings
default_password_crypted: "$1$www.example$T5FgCHY2P0NDr6JmbN0Bl0"

安装 cman:

# yum install cman

修复完成,再用 cobbler check 检查一下,确认没问题后用 cobbler sync 做同步操作:

# cobbler check
No configuration problems found.  All systems go.

# cobbler sync

导入 ISO

挂载 CentOS-6.2-x86_64-bin-DVD1.iso 安装光盘然后导入到 cobbler(注意这个 iso 文件有 4GB 多,导入可能需要一段时间),导入成功后 cobbler list 查看一下:

# mount -o loop -t iso9660 CentOS-6.2-x86_64-bin-DVD1.iso /mnt

# cobbler import --path=/mnt --name=CentOS-6.2-x86_64-bin-DVD1 –Arch=x86_64

# cobbler sync

# cobbler list
distros:
   CentOS-6.2-bin-DVD1-x86_64

profiles:
   CentOS-6.2-bin-DVD1-x86_64

systems:

repos:

images:

测试

最后创建一台虚拟机测试一下,把虚拟机设置成网络 PXE 启动(和 cobbler 在同一个网络),启动后就可以看到 Cobbler 引导界面,看到界面后选择 CentOS-6.2-bin-DVD1-x86_64 条目就可以顺利开始无人工干预安装系统,Cobbler 引导界面如下:

boot os from cobbler net pxe

安装完系统后默认的密码是啥呢?根据 sample.ks 的配置提示,这个密码 $default_password_crypted 就是我们上面用 openssl passwd -1 -salt ‘www.example.com’ ‘example’ 生成的密码,所以这里的 root 密码是 example:

# cat /var/lib/cobbler/kickstarts/sample.ks
...
#Root password
rootpw --iscrypted $default_password_crypted

相关内容