自动化-Cobbler 无人值守安装

1. Cobbler介绍

Cobbler是一个Linux服务器安装的服务,可以通过网络启动(PXE)的方式来快速安装、重装物理服务器和虚拟机,同时还可以管理DHCP,DNS等。
Cobbler可以使用命令行方式管理,也提供了基于Web的界面管理工具(cobbler-web),还提供了API接口,可以方便二次开发使用。
Cobbler是较早前的kickstart的升级版,优点是比较容易配置,还自带web界面比较易于管理。
Cobbler内置了一个轻量级配置管理系统,但它也支持和其它配置管理系统集成,如Puppet,暂时不支持SaltStack。

1.1 Cobbler集成的服务
PXE服务支持
DHCP服务管理
DNS服务管理(可选bind,dnsmasq)
电源管理
Kickstart服务支持
YUM仓库管理
TFTP(PXE启动时需要)
Apache(提供kickstart的安装源,并提供定制化的kickstart配置)

1.2 系统环境准备

[[email protected] ~]# cat /etc/redhat-release 
CentOS release 6.9 (Final)
[[email protected] ~]# uname -r
2.6.32-696.el6.x86_64
[[email protected] ~]# getenforce 
Disabled
# 配置一个阿里的 epel 源 ,

[[email protected] ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo

 

2. Cobbler安装配置
2.1 安装Cobbler

[[email protected] ~]#  yum -y install cobbler cobbler-web dhcp tftp-server pykickstart httpd

安装时候报错:

---> Package deltarpm.x86_64 0:3.5-0.5.20090913git.el6 will be 安装
--> 完成依赖关系计算
错误:Package: cobbler-web-2.6.11-7.git95749a6.el6.noarch (epel)
          Requires: Django >= 1.4
 You could try using --skip-broken to work around the problem
 You could try running: rpm -Va --nofiles --nodigest               

解决办法:

这是因为cobbler 是用Python写的,所以需要Django的这个包,现在的yum好多yum 源都删除了这个文件 ,
直接下载一个,使用rpm 装一下就好了 。

再从新执行以下就安装成功了 ;
[[email protected] ~]# yum -y install cobbler cobbler-web dhcp tftp-server pykickstart httpd

已安装:
cobbler.x86_64 0:2.6.11-7.git95749a6.el6        cobbler-web.noarch 0:2.6.11-7.git95749a6.el6        dhcp.x86_64 12:4.1.1-61.P1.el6.centos  pykickstart.noarch 0:1.74.22-1.el6                         tftp-server.x86_64 0:0.49-8.el6                    

 

[email protected] ~]# rpm -ql cobbler                # 查看安装的文件,下面列出部分。

/etc/cobbler                                        # 配置文件目录
/etc/cobbler/settings                         # cobbler主配置文件,这个文件是YAML格式,Cobbler是python写的程序。
/etc/cobbler/dhcp.template              # DHCP服务的配置模板
/etc/cobbler/tftpd.template               # tftp服务的配置模板
/etc/cobbler/rsync.template             # rsync服务的配置模板
/etc/cobbler/iso                                 # iso模板配置文件目录
/etc/cobbler/pxe                                # pxe模板文件目录
/etc/cobbler/power                            # 电源的配置文件目录
/etc/cobbler/users.conf                      # Web服务授权配置文件
/etc/cobbler/users.digest                    # 用于web访问的用户名密码配置文件
/etc/cobbler/dnsmasq.template          # DNS服务的配置模板
/etc/cobbler/modules.conf                    # Cobbler模块配置文件
/var/lib/cobbler                                       # Cobbler数据目录
/var/lib/cobbler/config                            # 配置文件
/var/lib/cobbler/kickstarts                       # 默认存放kickstart文件
/var/lib/cobbler/loaders                          # 存放的各种引导程序
/var/www/cobbler                                   # 系统安装镜像目录
/var/www/cobbler/ks_mirror                # 导入的系统镜像列表
/var/www/cobbler/images                   # 导入的系统镜像启动文件
/var/www/cobbler/repo_mirror           # yum源存储目录
/var/log/cobbler                                    # 日志目录
/var/log/cobbler/install.log                   # 客户端系统安装日志
/var/log/cobbler/cobbler.log                # cobbler日志

 

2.2 配置Cobbler

[[email protected] ~]# /etc/init.d/httpd restart
停止 httpd:                                               [确定]
正在启动 httpd:                                       [确定]

[[email protected] ~]# /etc/init.d/cobblerd start
Starting cobbler daemon:                         [确定]

 

[[email protected] ~]# cobbler check           # 检查Cobbler的配置,如果看不到下面的结果,再次执行/etc/init.d/cobblerd restart

报错:
httpd does not appear to be running and proxying cobbler, or SELinux is in the way. Original traceback:
Traceback (most recent call last):
  File "/usr/lib/python2.6/site-packages/cobbler/cli.py", line 252, in check_setup
    s.ping()
  File "/usr/lib64/python2.6/xmlrpclib.py", line 1199, in __call__
    return self.__send(self.__name, args)
  File "/usr/lib64/python2.6/xmlrpclib.py", line 1489, in __request
    verbose=self.__verbose
  File "/usr/lib64/python2.6/xmlrpclib.py", line 1235, in request
    self.send_content(h, request_body)
  File "/usr/lib64/python2.6/xmlrpclib.py", line 1349, in send_content
    connection.endheaders()
  File "/usr/lib64/python2.6/httplib.py", line 967, in endheaders
    self._send_output()
  File "/usr/lib64/python2.6/httplib.py", line 831, in _send_output
    self.send(msg)
  File "/usr/lib64/python2.6/httplib.py", line 790, in send
    self.connect()
  File "/usr/lib64/python2.6/httplib.py", line 771, in connect
    self.timeout)
  File "/usr/lib64/python2.6/socket.py", line 567, in create_connection
    raise error, msg
error: [Errno 113] No route to host

解决办法:

是设置/etc/cobbler/settings出错了,server和next_server这两个IP设置的跟本机的IP不同,
修改这两个IP为本机IP后,再执行cobbler check一下,竟然超级顺畅,坑啊......

[[email protected] ~]# vim /etc/cobbler/settings      #修改这个配置文件 ,server和next_server  这两个IP和主机IP一样即可 。

[[email protected] ~]# cobbler check
The following are potential configuration items that you may want to fix:

1 : service dhcpd is not running
2 : change 'disable' to 'no' in /etc/xinetd.d/tftp
3 : 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.
4 : debmirror package is not installed, it will be required to manage debian deployments and repositories
5 : fencing tools were not found, and are required to use the (optional) power management features. install cman or fence-agents to use them

 

# 看着上面的结果,一个一个解决。

# 第2个问题解决办法 ;
[[email protected] ~]# vim /etc/xinetd.d/tftp
disable  = no
[[email protected] ~]# /etc/init.d/xinetd restart
停止 xinetd:                                                  [确定]
正在启动 xinetd:                                          [确定]


# 第3个问题解决办法;
[[email protected] ~]# cobbler get-loaders                          # 会自动从官网下载
[[email protected] ~]# cd /var/lib/cobbler/loaders               # 查看下载的内容
[[email protected] loaders]# ls
COPYING.elilo  COPYING.syslinux  COPYING.yaboot  elilo-ia64.efi  grub-x86_64.efi  grub-x86.efi  menu.c32  pxelinux.0  README  yaboot
[[email protected] loaders]# 

 

再次check 一下   ;

[[email protected] loaders]# cobbler check
The following are potential configuration items that you may want to fix:

1 : service dhcpd is not running
2 : debmirror package is not installed, it will be required to manage debian deployments and repositories
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.
[[email protected] loaders]# 
 

#1 :还有没配置DHCP 配置了 DHCP 就好了 ;
#2 : debmirror package is not installed, it will be required to manage debian deployments and repositories         # 和debian系统相关,不需要
#3 : fencing tools were not found, and are required to use the (optional) power management features. install cman or fence-agents to use  them                              # fence设备相关,不需要
 

2.3 配置DHCP

   # 修改cobbler的dhcp模版,不要直接修改dhcp本身的配置文件,因为cobbler会覆盖。

[[email protected] cobbler]# cat >>/etc/cobbler/dhcp.template<<EOF
subnet 172.16.1.0 netmask 255.255.255.0 {
        range 172.16.1.120 172.16.1.200;
        option subnet-mask 255.255.255.0;
        default-lease-time 21600;
        max-lease-time 43200;
        next-server 172.16.1.61;
        filename "/pxelinux.0";
}
EOF
1. # 以上配置注释
2. range 10.0.0.100 10.0.0.200;                       # 可分配的起始IP-结束IP
3. option subnet-mask 255.255.255.0;           # 设定netmask
4. default-lease-time 21600;                            # 设置默认的IP租用期限
5. max-lease-time 43200;                               # 设置最大的IP租用期限
6. next-server 10.0.0.7;                                    # 告知客户端TFTP服务器的ip
7. filename "/pxelinux.0";                                  # 告知客户端从TFTP根目录下载pxelinux.0文件

[[email protected] loaders]# /etc/init.d/dhcpd start
正在启动 dhcpd:                                           [确定]

 

[[email protected] loaders]#  cobbler check         #check   发现 DHCP  报错没了  ;
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 : fencing tools were not found, and are required to use the (optional) power management features. install cman or fence-agents to use them

 

2.4 同步cobbler配置

[[email protected] loaders]# cobbler sync
 

2.5 开机启动

# 启动相关服务并设置开机启动(可选) 与第二种方法二选一
chkconfig httpd on
chkconfig xinetd on
chkconfig cobblerd on
chkconfig dhcpd on
/etc/init.d/httpd restart
/etc/init.d/xinetd restart
/etc/init.d/cobblerd restart
/etc/init.d/dhcpd restart
# 编写Cobbler相关服务启动脚本(可选)
cat >>/etc/init.d/cobbler<<EOF
#!/bin/bash
# chkconfig: 345 80 90
# description:cobbler
case \$1 in
  start)
    /etc/init.d/httpd start
    /etc/init.d/xinetd start
    /etc/init.d/dhcpd start
    /etc/init.d/cobblerd start
    ;;
  stop)
    /etc/init.d/httpd stop
    /etc/init.d/xinetd stop
    /etc/init.d/dhcpd stop
    /etc/init.d/cobblerd stop
    ;;
  restart)
    /etc/init.d/httpd restart
    /etc/init.d/xinetd restart
    /etc/init.d/dhcpd restart
    /etc/init.d/cobblerd restart
    ;;
  status)
    /etc/init.d/httpd status
    /etc/init.d/xinetd status
    /etc/init.d/dhcpd status
    /etc/init.d/cobblerd status
    ;;
  sync)
    cobbler sync
    ;;
  *)
    echo "Input error,please in put 'start|stop|restart|status|sync'!"
    exit 2
    ;;
esac
EOF
# chmod +x /etc/init.d/cobbler
# chkconfig cobbler on

 

3. Cobbler的命令行管理
3.1 查看命令帮助

[[email protected] loaders]# cobbler
usage
=====
cobbler <distro|profile|system|repo|image|mgmtclass|package|file> ... 
        [add|edit|copy|getks*|list|remove|rename|report] [options|--help]
cobbler <aclsetup|buildiso|import|list|replicate|report|reposync|sync|validateks|version|signature|get-loaders|hardlink> [options|--help]
[[email protected] loaders]# 
 

cobbler check              #核对当前设置是否有问题
cobbler list                   #列出所有的cobbler元素
cobbler report             #列出元素的详细信息
cobbler sync               #同步配置到数据目录,更改配置最好都要执行下
cobbler reposync        #同步yum仓库
cobbler distro              #查看导入的发行版系统信息
cobbler system          #查看添加的系统信息
cobbler profile           #查看配置信息

 

3.2  导入镜像 

[[email protected] ~]# mkdir -p /var/www/html/CentOs6.9                                #创建目录
[[email protected] ~]# mount /dev/cdrom /var/www/html/CentOs6.9/               #挂载点 
mount: block device /dev/sr0 is write-protected, mounting read-only
[[email protected] ~]#  df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        20G  5.7G   13G  32% /
tmpfs           491M     0  491M   0% /dev/shm
/dev/sda1       190M   34M  146M  19% /boot
/dev/sr0        3.7G  3.7G     0 100% /mnt
/dev/sr0        3.7G  3.7G     0 100% /var/www/html/CentOs6.9         

访问这个网页      http://10.0.0.61/CentOs6.9/          ,可以看到如下图的页面,离成功又进了一步

            

 

3.3 指定ks.cfg文件及调整内核参数 (这一步很重要) ;

创建ks.cfg文件
通常,我们在安装操作系统的过程中,需要大量的和服务器交互操作,为了减少这个交互过程,kickstart就诞生了。
使用这种kickstart,只需事先定义好一个Kickstart自动应答配置文件ks.cfg(通常存放在安装服务器上),并让安装程序知道该配置文件的位置,在安装过程中安装程序就可以自己从该文件中读取安装配置,这样就避免了在安装过程中多次的人机交互,从而实现无人值守的自动化安装。
生成kickstart配置文件的三种方法:
  ● 方法1、 每安装好一台Centos机器,Centos安装程序都会创建一个kickstart配置文件,记录你的真实安装配置。如果你希望实现和某系                      统类似的安装,可以基于该系统的kickstart配置文件来生成你自己的kickstart配置文件。
                (生成的文件名字叫anaconda-ks.cfg位于/root/anaconda-ks.cfg)
  ● 方法2、Centos提供了一个图形化的kickstart配置工具。在任何一个安装好的Linux系统上运行该工具,就可以很容易地创建你自己的kickstart配置文件。kickstart配置工具命令为redhat-config-kickstart(RHEL3)或system-config-kickstart(RHEL4,RHEL5).网上有很多用CentOS桌面版生成ks文件的文章,如果有现成的系统就没什么可说。但没有现成的,也没有必要去用桌面版,命令行也很简单。
  ● 方法3、阅读kickstart配置文件的手册。用任何一个文本编辑器都可以创建你自己的kickstart配置文件。

#我们使用 第三个方法 

[[email protected] ks_config]# cat  CentOS-6.9-ks.cfg
# Kickstart Configurator for CentOS 6.7 by yao zhang
install
url --url="http://172.16.1.61/CentOS6.9/"
text
lang en_US.UTF-8
keyboard us
zerombr
bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"
network --bootproto=dhcp --device=eth1 --onboot=yes --noipv6 --hostname=CentOS6
timezone --utc Asia/Shanghai
authconfig --enableshadow --passalgo=sha512
rootpw  --iscrypted $6$X20eRtuZhkHznTb4$dK0BJByOSAWSDD8jccLVFz0CscijS9ldMWwpoCw/ZEjYw2BTQYGWlgKsn945fFTjRC658UXjuocwJbAjVI5D6/
clearpart --all --initlabel
part /boot --fstype=ext4 --asprimary --size=200
part swap --size=1024
part / --fstype=ext4 --grow --asprimary --size=200
firstboot --disable
selinux --disabled
firewall --disabled
logging --level=info
reboot

%packages                    #包组段 的开始   @  表示是表明这是包组
@base
@compat-libraries
@debugging
@development
tree                                    #这几个都是镜像里边有的 
nmap
sysstat
lrzsz
dos2unix
telnet


 post 是脚本段的开始 
 用的较多  之前的命令都执行完了之后,在执行一些想要执行的命令,装完系统后需要安装什么,或者是需要执行什么命令,都可以在这里添加上执行 。是在客户端执行
%post   
wget -O /tmp/optimization.sh http://172.16.1.61/ks_config/optimization.sh &>/dev/null
/bin/sh /tmp/optimization.sh
%end

 

 

ks.cfg文件组成大致分为3段

命令段 
键盘类型,语言,安装方式等系统的配置,有必选项和可选项,如果缺少某项必选项,安装时会中断并提示用户选择此项的选项

软件包段
%packages
@groupname:指定安装的包组
package_name:指定安装的包
-package_name:指定不安装的包
在安装过程中默认安装的软件包,安装软件时会自动分析依赖关系。

脚本段(可选)
%pre:安装系统前执行的命令或脚本(由于只依赖于启动镜像,支持的命令很少)
%post:安装系统后执行的命令或脚本(基本支持所有命令)

关键字    含义
install    告知安装程序,这是一次全新安装,而不是升级upgrade。
url --url=" "    通过FTP或HTTP从远程服务器上的安装树中安装。
url --url="http://10.0.0.7/CentOS-6.7/"
url --url ftp://<username>:<password>@<server>/<dir>
nfs    从指定的NFS服务器安装。
nfs --server=nfsserver.example.com --dir=/tmp/install-tree
text     使用文本模式安装。
lang    设置在安装过程中使用的语言以及系统的缺省语言。lang en_US.UTF-8
keyboard    设置系统键盘类型。keyboard us
zerombr      清除mbr引导信息。
bootloader    系统引导相关配置。
bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"
--location=,指定引导记录被写入的位置.有效的值如下:mbr(缺省),partition(在包含内核的分区的第一个扇区安装引导装载程序)或none(不安装引导装载程序)。
--driveorder,指定在BIOS引导顺序中居首的驱动器。
--append=,指定内核参数.要指定多个参数,使用空格分隔它们。
network    为通过网络的kickstart安装以及所安装的系统配置联网信息。
network --bootproto=dhcp --device=eth0 --onboot=yes --noipv6 --hostname=CentOS6
--bootproto=[dhcp/bootp/static]中的一种,缺省值是dhcp。bootp和dhcp被认为是相同的。
static方法要求在kickstart文件里输入所有的网络信息。
network --bootproto=static --ip=10.0.0.100 --netmask=255.255.255.0 --gateway=10.0.0.2 --nameserver=10.0.0.2
请注意所有配置信息都必须在一行上指定,不能使用反斜线来换行。
--ip=,要安装的机器的IP地址.
--gateway=,IP地址格式的默认网关.
--netmask=,安装的系统的子网掩码.
--hostname=,安装的系统的主机名.
--onboot=,是否在引导时启用该设备.
--noipv6=,禁用此设备的IPv6.
--nameserver=,配置dns解析.
timezone    设置系统时区。timezone --utc Asia/Shanghai
authconfig    系统认证信息。authconfig --enableshadow --passalgo=sha512
设置密码加密方式为sha512 启用shadow文件。
rootpw    root密码
clearpart    清空分区。clearpart --all --initlabel
--all 从系统中清除所有分区,--initlable 初始化磁盘标签
part    磁盘分区。
part /boot --fstype=ext4 --asprimary --size=200
part swap --size=1024
part / --fstype=ext4 --grow --asprimary --size=200
--fstype=,为分区设置文件系统类型.有效的类型为ext2,ext3,swap和vfat。
--asprimary,强迫把分区分配为主分区,否则提示分区失败。
--size=,以MB为单位的分区最小值.在此处指定一个整数值,如500.不要在数字后面加MB。
--grow,告诉分区使用所有可用空间(若有),或使用设置的最大值。
firstboot    负责协助配置redhat一些重要的信息。
firstboot --disable
selinux    关闭selinux。selinux --disabled
firewall    关闭防火墙。firewall --disabled
logging    设置日志级别。logging --level=info
reboot    设定安装完成后重启,此选项必须存在,不然kickstart显示一条消息,并等待用户按任意键后才重新引导,也可以选择halt关机。

 

======================================== 未完待续 ==========================================