一、Cobbler简介

Red Hat 最新(Cobbler项目最初在2008年左右发布)发布了网络安装服务器套件 Cobbler(补鞋匠),它已将 Linux 网络安装的技术门槛,从大专以上文化水平,成功降低到初中以下,连补鞋匠都能学会。I am just joking!

二、Cobbler功能特性

使用 Cobbler,您无需进行人工干预即可安装机器。Cobbler 设置一个 PXE 引导环境(它还可使用 yaboot 支持 PowerPC),并控制与安装相关的所有方面,比如网络引导服务(DHCP 和 TFTP)与存储库镜像。当希望安装一台新机器时,Cobbler 可以:

使用一个以前定义的模板来配置 DHCP 服务(如果启用了管理 DHCP)
将一个存储库(yum 或 rsync)建立镜像或解压缩一个媒介,以注册一个新操作系统
在 DHCP 配置文件中为需要安装的机器创建一个条目,并使用您指定的参数(IP 和 MAC 地址)
在 TFTFP 服务目录下创建适当的 PXE 文件
重新启动 DHCP 服务以反映更改
重新启动机器以开始安装(如果电源管理已启用)

三、Cobbler安装

1.环境准备

[root@linux-node2 ~]# getenforce #注意关闭Selinux
Disabled
[root@linux-node2 ~]# systemctl status firewalld  #关闭防火墙
   firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
[root@linux-node2 ~]# ifconfig eth0|awk -F '[ :]+' 'NR==2 {print $3}'
192.168.56.12
#centos的网卡命名规则已改,为了标准化,把网卡名统一改为eth0,后面还要对内核参数做修改。

更新yum源

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

2.安装Cobbler

yum install -y httpd dhcp tftp cobbler cobbler-web pykickstart
cobbler			#cobbler程序包
cobbler-web		#cobbler的web服务包
pykickstart 	#cobbler检查kickstart语法错误
httpd			#Apache web服务
tftp			#tftp服务
dhcp			#dhcp服务

3.检测Cobbler

cobbler的运行依赖tftp、dhcp、rsync等服务 cobbler也可以自己管理这些服务,在/etc/cobbler/settings文件进行配置即可。

[root@linux-node2 ~]# systemctl start httpd
[root@linux-node2 ~]# systemctl start cobblerd

执行cobbler check

[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.
解决方法:修改/etc/cobbler/settings,将server对应的IP改为cobbler安装主机的IP,即将server: 127.0.0.1改成server: 192.168.56.12,切记切记,server: 后面有空格!!
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.
解决方法:修改/etc/cobbler/settings,将next_server对应的IP改为cobbler安装主机的IP,即将server: 127.0.0.1改成server: 192.168.56.12
3 : change 'disable' to 'no' in /etc/xinetd.d/tftp
解决方法:将/etc/xinetd.d/tftp中disable对应值改成no
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.
解决方法:执行cobbler get-loaders
5 : enable and start rsyncd.service with systemctl
解决方法:执行systemctl enable rsyncd;systemctl start rsyncd
6 : debmirror package is not installed, it will be required to manage debian deployments and repositories
解决方法:大便的包,可以不下载
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
解决方法:给密码加盐,把生成的密码串添加到/etc/cobbler/settings里,切记切记,一定要全盘复制,连黑点也别放过。
[root@linux-node2 ~]# openssl passwd -1 -salt 'cobbler' 'cobbler'
$1$cobbler$M6SE55xZodWc9.vAKLJs6.
[root@linux-node2 ~]# vi /etc/cobbler/settings   #修改default_password_crypted: 注意空格
default_password_crypted: "$1$cobbler$M6SE55xZodWc9.vAKLJs6."
8 : fencing tools were not found, and are required to use the (optional) power management features. install cman or fence-agents to use them
解决办法:电源管理工具,下载fence-agents
yum -y install fence-agents
Restart cobblerd and then run 'cobbler sync' to apply changes.

4.配置DHCP

cobbler可以管理dhcp服务,只需在配置文件中启用此项 将/etc/cobbler/settings内manage_dhcp置为1即可 配置dhcp模版

[root@linux-node2 ~]# cat /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.110 192.168.56.250;
     default-lease-time         21600;
     max-lease-time             43200;
     next-server                $next_server;

5.同步cobbler

[root@linux-node2 ~]# yum install xinetd
[root@linux-node2 ~]# systemctl restart xinetd
[root@linux-node2 ~]# cobbler sync
task started: 2016-05-19_102447_sync
task started (id=Sync, time=Thu May 19 10:24:47 2016)
running pre-sync triggers
cleaning trees
removing: /var/lib/tftpboot/grub/p_w_picpaths
copying bootloaders
trying hardlink /var/lib/cobbler/loaders/pxelinux.0 -> /var/lib/tftpboot/pxelinux.0
trying hardlink /var/lib/cobbler/loaders/menu.c32 -> /var/lib/tftpboot/menu.c32
trying hardlink /var/lib/cobbler/loaders/yaboot -> /var/lib/tftpboot/yaboot
trying hardlink /usr/share/syslinux/memdisk -> /var/lib/tftpboot/memdisk
trying hardlink /var/lib/cobbler/loaders/grub-x86.efi -> /var/lib/tftpboot/grub/grub-x86.efi
trying hardlink /var/lib/cobbler/loaders/grub-x86_64.efi -> /var/lib/tftpboot/grub/grub-x86_64.efi
copying distros to tftpboot
copying p_w_picpaths
generating PXE configuration files
generating PXE menu structure
rendering DHCP files
generating /etc/dhcp/dhcpd.conf
rendering TFTPD files
generating /etc/xinetd.d/tftp
cleaning link caches
running post-sync triggers
running python triggers from /var/lib/cobbler/triggers/sync/post/*
running python trigger cobbler.modules.sync_post_restart_services
running: dhcpd -t -q
received on stdout: 
received on stderr: 
running: service dhcpd restart
received on stdout: 
received on stderr: Redirecting to /bin/systemctl restart  dhcpd.service

running shell triggers from /var/lib/cobbler/triggers/sync/post/*
running python triggers from /var/lib/cobbler/triggers/change/*
running python trigger cobbler.modules.scm_track
running shell triggers from /var/lib/cobbler/triggers/change/*
*** TASK COMPLETE ***

四、配置操作系统安装源

1.管理distro

挂载镜像,使用import导入到cobbler

[root@linux-node2 ~]# mount /dev/cdrom /mnt
mount: /dev/sr0 is write-protected, mounting read-only
#--name 为安装源定义一个名字CentOS-7-x86_64
#--arch 指定安装源是32位、64位、ia64, 目前支持的选项有: x86│x86_64│ia64
# 安装源的唯一标示就是根据name参数来定义,本例导入成功后,安装源的唯一标示就是:CentOS-7-x86_64。
# 镜像存放目录,cobbler会将镜像中的所有安装文件拷贝到本地一份,放在/var/www/cobbler/ks_mirror下的CentOS-7-x86_64目录下。因此/var/www/cobbler目录必须具有足够容纳安装文件的空间。
[root@linux-node2 ~]# cobbler import --path=/mnt/ --name=CentOS-6-x86_64 --arch=x86_64
查看所有的distro和profile
[root@linux-node2 ~]# cobbler profile list
   CentOS-7-x86_64
[root@linux-node2 ~]# cobbler distro list
   CentOS-7-x86_64
[root@linux-node2 ~]#

2.管理profile

cobbler可以通过提供kiskstart文件生成一个特定的系统安装配置文件

修改内核参数
[root@linux-node2 kickstarts]#cobbler profile edit --name=CentOS-7-x86_64 --kopts='net.ifnames=0 biosdevname=0'

[root@linux-node2 kickstarts]# cobbler profile edit --name=CentOS-7-x86_64 --kickstart=/var/lib/cobbler/kickstarts/CentOS-7-x86_64.cfg
#CentOS-7-x86_64.cfg内容为定制的系统的各种参数
添加CentOS-7-x86_64.cfg后执行同步
[root@linux-node2 kickstarts]# cobbler reposync
task started: 2016-05-19_110002_reposync
task started (id=Reposync, time=Thu May 19 11:00:02 2016)
hello, reposync
run, reposync, run!
*** TASK COMPLETE ***

五、部署操作系统

新建一台虚拟机,注意在选择操作系统位数时选择Centos64位的操作系统。 创建后,打开电源,如果能看到选择distro的界面,基本上问题不大。 但是,还需要注意,将vmware WorkStation的dhcp功能关掉。 另外,一个网段内有两个cobbler服务器,那么有可能因dhcp冲突而安装失败,这种情况下,最好在dhcp.temple内设置两个网段。

接下来该定制化安装一个操作系统了

六、自动化安装操作系统

服务器装机流程
1.服务器采购
2.服务器验收并设置raid
3.服务商提供验收单,运维验收负责人签字
4.服务器上架
5.资产录入
6.自动化安装

自动化安装步骤

1.将新服务器划入装机vlan
2.根据资产清单上的mac地址,自定义安装
	A、机房
	B、机房区域
	C、机柜
	D、服务器位置
	E、服务器网线接入端口
	F、该端口mac地址
	G、profile 操作系统 分区等 预分配的IP地址 主机名 子网 网关 dns 角色
3.自动化装机平台,安装
	新建一台虚拟机
	设置如下
	00:50:56:3A:07:6F
	linux-node3.oldboyedu.com
	255.255.255.0
	192.168.56.2
	192.168.56.2
添加一台主机指定mac(虚拟机可以生成一个mac地址),IP,主机名
cobbler system add --name=linux-node3.oldboyedu.com --mac=00:50:56:3A:07:6F --profile=CentOS-7-x86_64 \
--ip-address=192.168.56.120 --subnet=255.255.255.0 --gateway=192.168.56.2 --interface=eth0 \
--static=1 --hostname=linux-node3.oldboyedu.com --name-servers="192.168.56.2" \
--kickstart=/var/lib/cobbler/kickstarts/CentOS-7-x86_64.cfg
执行cobbler system list查看添加的system
[root@linux-node2 kickstarts]# cobbler system list
   linux-node3.oldboyedu.com
注意:一定要执行cobbler sync进行同步配置,否则不生效
通过cat /etc/dhcp/dhcpd.conf可以查看到配置信息已添加
group {
    host generic1 {
        hardware ethernet 00:50:56:3A:07:6F;
        fixed-address 192.168.56.120;
        option host-name "linux-node3.oldboyedu.com";
        option subnet-mask 255.255.255.0;
        option routers 192.168.56.2;
        filename "/pxelinux.0";
        next-server 192.168.56.12;
    }
}
最后,启动刚才创建的虚拟机,无需任何人工干预,即可啪啪啪装机了。

七、cobbler的API

API定义文件所在目录

/etc/httpd/conf.d/,此目录下的文件
[root@linux-node2 conf.d]# ll
total 36
-rw-r--r-- 1 root root 2926 May 12 18:27 autoindex.conf
-rw-r--r-- 1 root root 1087 Jan 24 22:40 cobbler.conf
-rw-r--r-- 1 root root 1165 Jan 24 22:40 cobbler_web.conf
-rw-r--r-- 1 root root  366 May 12 18:28 README
-rw-r--r-- 1 root root 9438 May 12 18:16 ssl.conf
-rw-r--r-- 1 root root 1252 May 12 18:16 userdir.conf
-rw-r--r-- 1 root root  824 May 12 18:16 welcome.conf
API:
ProxyPass /cobbler_api http://localhost:25151/
ProxyPa***everse /cobbler_api http://localhost:25151/

通过编写python来自动化安装系统

#!/usr/bin/env python 
# -*- coding: utf-8 -*-
import xmlrpclib 

class CobblerAPI(object):
    def __init__(self,url,user,password):
        self.cobbler_user= user
        self.cobbler_pass = password
        self.cobbler_url = url
    
    def add_system(self,hostname,ip_add,mac_add,profile):
        '''
        Add Cobbler System Infomation
        '''
        ret = {
            "result": True,
            "comment": [],
        }
        #get token
        remote = xmlrpclib.Server(self.cobbler_url) 
        token = remote.login(self.cobbler_user,self.cobbler_pass) 
		
		#add system
        system_id = remote.new_system(token) 
        remote.modify_system(system_id,"name",hostname,token) 
        remote.modify_system(system_id,"hostname",hostname,token) 
        remote.modify_system(system_id,'modify_interface', { 
            "macaddress-eth0" : mac_add, 
            "ipaddress-eth0" : ip_add, 
            "dnsname-eth0" : hostname, 
        }, token) 
        remote.modify_system(system_id,"profile",profile,token) 
        remote.save_system(system_id, token) 
        try:
            remote.sync(token)
        except Exception as e:
            ret['result'] = False
            ret['comment'].append(str(e))
        return ret

def main():
    cobbler = CobblerAPI("http://192.168.56.12/cobbler_api","cobbler","cobbler")
    ret = cobbler.add_system(hostname='cobbler-api-test',ip_add='192.168.56.121',mac_add='00:50:56:28:3D:4F',profile='CentOS-7-x86_64')
    print ret

if __name__ == '__main__':
    main()

这个脚本中addsystem方法中需要四个参数:主机名,IP地址,mac地址和profile。 注意:mac地址可以通过创建一台虚拟机时生成而获得 执行脚本python cobblerapi.py,脚本执行成功会有如下提示:

[root@linux-node2 ~]# python cobbler_api.py 
{'comment': [], 'result': True}

通过cobbler system list查看

[root@linux-node2 ~]# cobbler system list
   cobbler-api-test
   linux-node3.oldboyedu.com

最后,打开电源,就可以啪啪啪装机了,无需人工干预。

八、通过koan重装系统

假如某台机器需要重装系统,使用cobbler也可以完成。这里要用到koan这个工具,这个工具安装在需要重装的那个操作系统上,我用上面刚装完的系统做一下测试。

首先,更新一下yum源

[root@cobbler-api-test ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

执行

[root@cobbler-api-test ~]# yum install -y koan

指定cobbler所在服务器的IP地址

[root@cobbler-api-test ~]# koan --replace-self --server=192.168.56.12 --profile=CentOS-7-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-x86_64
install_tree: http://192.168.56.12/cblr/links/CentOS-7-x86_64
downloading initrd initrd.img to /boot/initrd.img_koan
url=http://192.168.56.12/cobbler/p_w_picpaths/CentOS-7-x86_64/initrd.img
- reading URL: http://192.168.56.12/cobbler/p_w_picpaths/CentOS-7-x86_64/initrd.img
downloading kernel vmlinuz to /boot/vmlinuz_koan
url=http://192.168.56.12/cobbler/p_w_picpaths/CentOS-7-x86_64/vmlinuz
- reading URL: http://192.168.56.12/cobbler/p_w_picpaths/CentOS-7-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.12/cblr/svc/op/ks/profile/CentOS-7-x86_64 biosdevname=0 kssendmac "', '--copy-default', '--make-default', '--title=kick1465316032']
- ['/sbin/grubby', '--update-kernel', '/boot/vmlinuz_koan', '--remove-args=root']
- reboot to apply changes

可以看到最后提示reboot让改动生效。执行reboot。

接下来,通过虚拟机的界面可以看到系统开始重装,啪啪啪,无需人工干预。其实是在grub上新增了一条安装项

生产环境中,专门划分一个装机vlan。

九、cobbler的web管理

通过web来管理cobbler,访问方式:

https://192.168.56.12/cobbler_web

默认的用户名和密码都是cobbler。

如果想修改密码,可以在这么做:

找到用户名,密码所在文件,在/etc/cobbler下

[root@linux-node2 cobbler]# cat users.
users.conf    users.digest  
[root@linux-node2 cobbler]# cat users.digest 
cobbler:Cobbler:a2d6bae81669d707b72c0bd9806e01f3
[root@linux-node2 cobbler]# htd
htdbm     htdigest  
[root@linux-node2 cobbler]# htd
htdbm     htdigest  
[root@linux-node2 cobbler]# htdigest /etc/cobbler/users.digest "Cobbler" cobbler
Changing password for user cobbler in realm Cobbler
New password: 
Re-type new password: 
[root@linux-node2 cobbler]#

第一个"Cobbler"是用户描述,第二个是用户名。

我改成了123456

登陆到web界面后,在左侧可以看到Distros,Profiles,Systems,Repos,Kickstart文件等等,都可以通过界面来编辑,非常方便。