Cobbler自动化部署

Cobbler介绍

Cobbler是一个快速网络安装linux的服务,而且在经过调整也可以支持网络安装windows。该工具使用python开发,小巧轻便,使用简单的命令即可完成PXE网络安装环境的配置,同时还可以管理DHCP、DNS、以及yum仓库、构造系统ISO镜像。

Cobbler支持命令行管理,web界面管理,还提供了API接口,可以方便二次开发使用。

Cobbler客户端Koan支持虚拟机安装和操作系统重新安装,使重装系统更便捷。

Cobbler集成的服务:

  • PXE 服务
  • DHCP服务管理
  • DNS服务管理
  • HTTP服务管理
  • TFTP服务管理
  • Kickstart服务
  • yum仓库管理
  • 电源管理

Cobbler快速入门指南

简单的说,Cobbler是对kickstart的封装,简化安装步骤、使用流程,降低使用者的门槛

Cobbler安装部署

安装cobbler

rpm -ivh https://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
yum -y install cobbler cobbler-web dhcp tftp pykickstart httpd

#目录文件说明:
/etc/cobbler                  # 配置文件目录
/etc/cobbler/settings         # cobbler主配置文件
/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日志

配置cobbler

systemctl start httpd
systemctl start cobblerd

##设置开机自启:
systemctl enable httpd
systemctl enable cobblerd

##检查Cobbler的配置
# 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.
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 : SELinux is enabled. Please review the following wiki page for details on ensuring cobbler works correctly in your SELinux environment:
    https://github.com/cobbler/cobbler/wiki/Selinux
4 : change 'disable' to 'no' in /etc/xinetd.d/tftp
5 : 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.
6 : enable and start rsyncd.service with systemctl
7 : debmirror package is not installed, it will be required to manage debian deployments and repositories
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

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

解决上述问题:

第一、二个问题
# server,Cobbler服务器的IP。
sed -i 's/server: 127.0.0.1/server: 192.168.20.200/' /etc/cobbler/settings
# next_server,如果用Cobbler管理DHCP,修改本项,
sed -i 's/next_server: 127.0.0.1/next_server: 192.168.20.200/' /etc/cobbler/settings
# 用Cobbler管理DHCP
sed -i 's/manage_dhcp: 0/manage_dhcp: 1/' /etc/cobbler/settings
# 防止循环装系统,适用于服务器第一启动项是PXE启动。
sed -i 's/pxe_just_once: 0/pxe_just_once: 1/' /etc/cobbler/settings

第三个问题
sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config
#这个需要重启机器才能生效,所以建议修改完重启

第四个问题
/etc/xinetd.d/tftp
disable = no

第五个问题
cobbler get-loaders

第六个问题
systemctl start rsyncd.service 
systemctl enable rsyncd.service

第七、九个问题
和debian系统、fence设备相关,不需要,不用理会

第八个问题
# 设置新装系统的默认root密码123456。random-phrase-here为干扰码,可以自行设定。
[root@linux-node1 ~]# openssl passwd -1 -salt 'cobbler' '123456'
$1$cobbler$Npg9Pt9k98Mlg0ZeqHAuN1
[root@linux-node1 ~]# vim /etc/cobbler/settings 
default_password_crypted: "$1$cobbler$Npg9Pt9k98Mlg0ZeqHAuN1" 

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

配置DHCP

因为是用cobbler管理dhcp,所以要修改cobbler的dhcp模版,而不是直接修改dhcp本身的配置文件,因为cobbler会覆盖。

#vim /etc/cobbler/dhcp.template
...
subnet 192.168.20.0 netmask 255.255.255.0 {
     option routers             192.168.20.10;
     option domain-name-servers 192.168.20.10;
     option subnet-mask         255.255.255.0;
     range dynamic-bootp        192.168.20.50 192.168.20.60;
     default-lease-time         21600;
     max-lease-time             43200;
     next-server                $next_server;
     ...

同步cobbler配置

systemctl restart cobblerd
cobbler sync    # 同步最新cobbler配置,它会根据配置自动修改dhcp等服务。

可以看到dhcp的配置文件/etc/dhcp/dhcpd.conf是受cobbler管理的,已经同步为我们修改的配置。

# ******************************************************************
# Cobbler managed dhcpd.conf file
# generated from cobbler dhcp.conf template (Sat Jul 14 02:14:56 2018)
# Do NOT make changes to /etc/dhcpd.conf. Instead, make your changes
# in /etc/cobbler/dhcp.template, as /etc/dhcpd.conf will be
# overwritten.
# ******************************************************************

ddns-update-style interim;

allow booting;
allow bootp;

ignore client-updates;
set vendorclass = option vendor-class-identifier;

option pxe-system-type code 93 = unsigned integer 16;

subnet 192.168.20.0 netmask 255.255.255.0 {
     option routers             192.168.20.10;
     option domain-name-servers 192.168.20.10;
     option subnet-mask         255.255.255.0;
     range dynamic-bootp        192.168.20.50 192.168.20.60;
     default-lease-time         21600;
     max-lease-time             43200;
     next-server                192.168.20.200;

Cobbler管理

常用命令:

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

导入镜像

##7.3
mount /dev/cdrom /mnt/      # 挂载CentOS7的系统镜像
cobbler import --path=/mnt/ --name=CentOS-7.3-x86_64 --arch=x86_64
umount /mnt/

##6.8
mount /dev/cdrom /mnt/      # 挂载CentOS6的系统镜像
cobbler import --path=/mnt/ --name=CentOS-6.8-x86_64 --arch=x86_64
umount /mnt/

#参数说明:
    --path 镜像路径
    --name 为安装源定义一个名字
    --arch 指定安装源是32位、64位、ia64, 目前支持的选项有: x86│x86_64│ia64
# 安装源的唯一标示就是根据name参数来定义,本例导入成功后,安装源的唯一标示就是:CentOS-7.3-x86_64,如果重复,系统会提示导入失败。

查看镜像列表:

cobbler distro list
# 镜像存放目录,cobbler会将镜像中的所有安装文件拷贝到本地一份,放在/var/www/cobbler/ks_mirror下的对应目录中。

指定ks.cfg文件及调整内核参数

上传指定ks文件到/var/lib/cobbler/kickstarts/目录下:

CentOS-6.8-x86_64.cfg
CentOS-7.3-x86_64.cfg

编辑profile,修改关联的ks文件:

cobbler profile edit --name=CentOS-7.3-x86_64 --kickstart=/var/lib/cobbler/kickstarts/CentOS-7.3-x86_64.cfg
cobbler profile edit --name=CentOS-6.8-x86_64 --kickstart=/var/lib/cobbler/kickstarts/CentOS-6.8-x86_64.cfg
#默认为sample_end.ks文件

#centos7修改网卡名:
cobbler profile edit --name=CentOS-7.3-x86_64 --kopts='net.ifnames=0 biosdevname=0'

查看所有的profile设置:

cobbler profile report

修改完成之后需要同步:

cobbler sync

这里写图片描述

ks.cfg详解

官网文档:
CentOS7
CentOS6
https://access.redhat.com/documentation/en/red-hat-enterprise-linux/ #这里可以选择不同版本的文档进行查看

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关机。

Cobbler Web管理界面

在安装cobbler时已经安装,如果没有安装可以执行:

yum -y install  cobbler-web

配置文件说明:

/etc/cobbler/users.conf     # Web服务授权配置文件
/etc/cobbler/users.digest   # 用于web访问的用户名密码配置文件

设置Cobbler web用户登陆密码:

htdigest /etc/cobbler/users.digest "Cobbler" cobbler    #在Cobbler组添加cobbler用户,提示输入2遍密码确认

最后执行同步,重启服务:

cobbler sync
systemctl restart httpd
systemctl restart cobblerd
history 

访问地址:https://192.168.20.200/cobbler_web
这里写图片描述

使用Cobbler安装操作系统

现在新建一个虚拟机,打开电源,在控制台看到如下界面,选择要安装的系统版本,剩下需要做的就是等待了。
这里写图片描述

定制化安装:
Cobbler可以通过区分服务器的MAC地址来实现定制化安装,实现指定某台服务器使用指定ks文件

cobbler system add --name=cobbler-test \
    --mac=00:50:56:11:12:13 \
    --profile=CentOS-7.3-x86_64 \
    --ip-address=192.168.20.56 \
    --subnet=255.255.255.0 \
    --gateway=192.168.20.10 \
    --interface=eth0 \
    --static=1 \
    --hostname=cobbler-test \
    --name-servers=114.114.114.114 \
    --kickstart=/var/lib/cobbler/kickstarts/CentOS-7.3-x86_64.cfg
# --name 自定义,但不能重复

# 查看定义的列表
# cobbler system list
   cobbler-test

#cobbler sync

再次开机安装就不再询问选择了,直接安装

自动重装:

所有操作在需要重装系统的机器上执行

在需要重装的机器上安装koan

rpm -ivh https://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
yum install -y koan

列出cobbler中可以重装的系统

# koan --server=192.168.20.200 --list=profiles    #server指定cobbler地址
- looking for Cobbler at http://192.168.20.200:80/cobbler_api
CentOS-7.3-x86_64
CentOS-6.8-x86_64

指定要重装的系统:

koan --replace-self --server=192.168.20.200 --profile=CentOS-6.8-x86_64

最后重启即可

cobbler自定义yum仓库

#添加repo:
cobbler repo add --name=openstack-newton  \
    --mirror=https://mirrors.aliyun.com/centos/7.4.1708/cloud/x86_64/openstack-newton/ \
    --arch=x86_64 \
    --breed=yum

#同步repo:
cobbler reposync

#添加repo到对应的profile
cobbler profile edit --name=CentOS-7.3-x86_64 --repos=https://mirrors.aliyun.com/centos/7.4.1708/cloud/x86_64/openstack-newton/ 

#修改Kickstart文件。
%post
systemctl disable postfix.service
$yum_config_stanza         #添加 
%end

#添加定时任务,定期同步repo(cobbler reposync)

猜你喜欢

转载自blog.csdn.net/wfs1994/article/details/81041772