Cobbler安装linux操作系统及使用DHCP中继实现跨网段安装

Cobbler是一个免费开源系统安装部署软件,用于自动化网络安装操作系统。 Cobbler 集成了 DNSDHCP,[1][2]软件包更新, 带外管理以及配置管理, 方便操作系统安装自动化。Cobbler 可以支持PXE启动, 操作系统重新安装, 以及虚拟化客户机创建,包括XenKVM or VMware. Cobbler透过koan程序以支持虚拟化客户机安装。Cobbler 可以支持管理复杂网路环境,如创建在链路聚合以太网桥接环境。

过完年要测试一些东西可能会多次重装系统,由于懒得装系统所以就搭建Cobbler来帮我自动安装操作系统,其实早就想搭建cobbler但还是因为懒所以到今天才做(懒惰是进步的动力)。其实毕业设计也用到了cobbler,但是后来莫名其妙搭建总是失败就不想折腾了,直到今天才静下心来搭建cobbler,搭建好了想想其实很简单嘛(当年毕业设计是有多水)!!!

Cobbler安装在CentOS7系统上,系统已经关闭防火墙、selinux以及安装了一下其他服务,具体一些操作使用之前一篇教程脚本执行的(链接)。使用命令yum install -y httpd dhcp tftp rsync syslinux cobbler cobbler-web pykickstart xinetd安装cobbler需要的软件,安装完成后使用命令systemctl enable cobblerd httpd xinetd tftp rsyncd设置服务开机自启,并使用命令systemctl start cobblerd httpd xinetd启动服务。服务正常启动后使用命令cobbler check初始化cobbler并检查配置,一般结果如下所示:

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

 

接下来是根据错误来修改配置文件:

  1. 使用命令vim /etc/cobbler/settings编辑cobbler配置文件,配置详情如下:
    server: 172.16.2.118
    next_server: 172.16.2.118
  2. 使用命令vim /etc/xinetd.d/tftp编辑ftp配置文件使ftp变为可用,配置详情如下,配置完成使用命令systemctl start tftp启动ftp服务:
    disable = no
  3. 使用命令cobbler get-loaders下载cobbler引导文件。
  4. 使用命令openssl passwd -1 -salt 'root' '123456'设置root密码(123456为密码,可根据自己需求修改),命令执行后生成字符串,使用命令vim /etc/cobbler/settings编辑cobbler配置文件替换default_password_crypted后面字符串:
    [[email protected] ~]# openssl passwd -1 -salt 'root' '123456'
    $1$root$j0bp.KLPyr.u9kgQ428D10
    
    vim /etc/cobbler/settings
    default_password_crypted: "$1$root$j0bp.KLPyr.u9kgQ428D10"
  5. 使用命令yum install -y fence-agents安装电源管理工具。

设置完成后使用命令systemctl restart cobblerd重启cobbler并再次使用命令cobbler check检查cobbler,一般结果如下显示,其中debmirror在CentOS中无用所以忽略:

[[email protected] ~]# systemctl restart cobblerd
[[email protected] ~]# 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
Restart cobblerd and then run 'cobbler sync' to apply changes.

此时cobbler设置完成,接下来需要设置DHCP服务,使用命令vim /etc/cobbler/settings编辑cobbler配置文件,设置启动dhcp,设置完成后使用命令vim /etc/cobbler/dhcp.template编辑dhcp配置模板,如下所示配置完成后使用命令systemctl restart cobblerd重启cobbler,重启完cobbler后使用命令cobbler sync同步配置文件,文件同步成功后使用命令systemctl start dhcpd启动DHCP:

vim /etc/cobbler/settings
manage_dhcp: 1
subnet 172.16.2.0 netmask 255.255.255.0 {
    option routers 127.16.2.1;
    option domain-name-servers 119.29.29.29;
    option subnet-mask 255.255.255.0;
    range dynamic-bootp 172.16.2.3 172.16.2.253;
    default-lease-time 21600;
    max-lease-time 43200;
    next-server $next_server;
    class "pxeclients" {
        match if substring (option vendor-class-identifier, 0, 9) = "PXEClient";
        if option pxe-system-type = 00:02 {
            filename "ia64/elilo.efi";
        } else if option pxe-system-type = 00:06 {
            filename "grub/grub-x86.efi";
        } else if option pxe-system-type = 00:07 {
            filename "grub/grub-x86_64.efi";
        } else if option pxe-system-type = 00:09 {
            filename "grub/grub-x86_64.efi";
        } else {
            filename "pxelinux.0";
        }
    }
}

使用命令vim /etc/cobbler/settings配置tftp和rsync为cobbler管理,具体设置细节如下显示,设置完成使用命令systemctl start tftp rsyncd启动tftp和rsync服务:

manage_tftpd: 1
manage_rsync: 1

服务正常启动后使用命令systemctl restart cobblerd重启cobbler并用命令cobbler sync同步配置,同步配置最后看见*** TASK COMPLETE ***即为同步成功。

此时cobbler安装配置告一段落,接下来需要配置安装镜像,此处也用CentOS7.4_mini的镜像为例。

首先将镜像上传到cobbler服务所在系统文件夹下,使用如下命令挂载镜像:

mount -o loop /root/CentOS-7-x86_64-Minimal-1708.iso /mnt/centos7.4_mini/
#最后指定挂载目录,若目录不存在需要新建目录

镜像挂载完成上传kickstarts文件到/var/lib/cobbler/kickstarts/目录,以下为此处CentOS7使用的kickstarts文件:

[[email protected] ~]# cat centos7.ks 
install
text
url --url=$tree
lang en_US.UTF-8
keyboard us
rootpw --iscrypted $default_password_crypted
network --device ens192 --bootproto dhcp --onboot yes
firewall --disabled
authconfig --enableshadow --enablemd5
selinux --disabled
timezone Asia/Shanghai
bootloader --location=mbr --driveorder=sda
zerombr
clearpart --all --initlabel
part /boot --fstype="xfs" --asprimary --size=200 --ondisk=sda
part / --fstype="xfs" --grow --size=1 --ondisk=sda
ignoredisk --only-use=sda
reboot

%post
systemctl disable firewalld
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
sed -i 's/enabled=1/enabled=0/' /etc/yum/pluginconf.d/fastestmirror.conf
rm -rf /etc/yum.repos.d/*
mkdir /root/.ssh && chmod 700 /root/.ssh
mkdir /root/.pip
curl http://172.16.5.253/share/pip.conf > /root/.pip/pip.conf
curl http://172.16.5.253/share/centos7.repo > /etc/yum.repos.d/centos7.repo
curl http://172.16.5.253/share/epel.repo > /etc/yum.repos.d/epel.repo
curl http://172.16.5.253/share/docker.repo > /etc/yum.repos.d/docker.repo
curl http://172.16.5.253/share/resolv.conf > /etc/resolv.conf
curl http://172.16.5.253/share/ntp.conf > /etc/ntp.conf
curl http://172.16.5.253/share/hosts > /etc/hosts
curl http://172.16.5.253/share/sshd_config > /etc/ssh/sshd_config
curl http://mirrors.ustc.edu.cn/epel/RPM-GPG-KEY-EPEL-7 > /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
yum makecache
yum -y install vim wget lrzsz bmon python-pip net-tools dstat tmux htop ntp
yum -y update
systemctl enable ntpd
wget http://172.16.5.253/share/authorized_keys -O /root/.ssh/authorized_keys && chmod 600 .ssh/authorized_keys
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
timedatectl set-ntp false
timedatectl set-ntp true
timedatectl set-local-rtc 1
timedatectl set-local-rtc 0
hwclock --hctosys
touch OK
reboot
%end

此时在命令行下操作完成,接下来在浏览器操作即可。

在浏览器打开https://172.16.2.118/cobbler_web链接出现cobbler登录页面,用户名和密码皆为cobbler,登录成功后点击Import DVD,在右侧填写相关信息,从上到下依次为名称、架构、系统类别、镜像存放路径,设置完成后点击Run完成导入镜像:

《Cobbler安装linux操作系统及使用DHCP中继实现跨网段安装》

镜像导入完成后点击左侧Profiles编辑之前导入镜像,出现之前导入的镜像后点击编辑按钮,在编辑页选择导入的kickstarts文件,完成后点击左上Save保存(此处配置完成可以从网络启动安装系统,具体设置主机名及分配网络需要后面做更详细操作):

《Cobbler安装linux操作系统及使用DHCP中继实现跨网段安装》《Cobbler安装linux操作系统及使用DHCP中继实现跨网段安装》

Proofiles配置完成后点击Systems-Create New System创建任务,在General添加任务名、选择Profile、允许网络启动、选择Kickstart文件;在Networking(global)设置系统主机名、网关、DNS地址;在Networking-Edit Interface选择网卡(第一次添加需要在第一行添加网卡名)、虚拟机或者物理机网卡MAC地址、系统IP地址、选择Static、设置子网掩码。完成后点击左上Save保存,完成后点击左侧Sync同步配置。

《Cobbler安装linux操作系统及使用DHCP中继实现跨网段安装》 《Cobbler安装linux操作系统及使用DHCP中继实现跨网段安装》 《Cobbler安装linux操作系统及使用DHCP中继实现跨网段安装》 《Cobbler安装linux操作系统及使用DHCP中继实现跨网段安装》

同步完成后在物理机或者虚拟机选择从网络启动,一般出现如下画面说明已正常从网络启动,并且得到地址已经为172.16.2.212,至此cobbler安装配置完成。

《Cobbler安装linux操作系统及使用DHCP中继实现跨网段安装》

一般一个cobbler服务可能会为多个网段提供安装系统操作,此时需要dhcp中继服务,首先需要在cobbler服务所在系统使用命令vim /etc/cobbler/dhcp.template编辑DHCP模板文件,添加新的网段,细节如下显示,添加完成重启cobbler、cobbler sync同步配置、重启DHCP服务。在其他网段系统使用命令yum -y install dhcp安装DHCP服务并使用如下命令配置DHCP中继:

#/etc/cobbler/dhcp.template新添加配置
subnet 172.16.5.0 netmask 255.255.255.0 {
    option routers 127.16.5.1;
    option domain-name-servers 119.29.29.29;
    option subnet-mask 255.255.255.0;
    range dynamic-bootp 172.16.5.3 172.16.5.253;
    default-lease-time 21600;
    max-lease-time 43200;
}

subnet 172.16.4.0 netmask 255.255.255.0 {
    option routers 127.16.4.1;
    option domain-name-servers 119.29.29.29;
    option subnet-mask 255.255.255.0;
    range dynamic-bootp 172.16.4.3 172.16.4.253;
    default-lease-time 21600;
    max-lease-time 43200;
}

#其他网段使用命令作为DHCP中继服务器
dhcrelay -i ens192 172.16.2.118 ens192
#-i后为与DHCP中继通信网卡名  IP地址为Cobbler DHCP服务地址
[[email protected] ~]# dhcrelay -i ens192 172.16.2.118
Internet Systems Consortium DHCP Relay Agent 4.2.5
Copyright 2004-2013 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/
Listening on LPF/ens192/00:0c:29:1c:4b:bf
Sending on LPF/ens192/00:0c:29:1c:4b:bf
Sending on Socket/fallback
#此处说明DHCP中继完成

至此一切顺利可以在不同网段使用cobbler安装操作系统。

写的仓促肯定有不少错误,等过完年有时间再检查校正。

最后祝大家新年快乐!!!!!!

点赞
    1. admin说道:

      233333

  1. maclean说道:

    你好,我安装cobbler系统跨网段遇到问题,能请教下吗 cobbler能跨交互机装机吗

    1. admin说道:

      找一台连接2个网段的机器设置dhcp中继就可以了,记忆中一条命令就可以但是时间长了有点忘了。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注