前言
Cobbler是一款自动化操作系统部署的实现工具,由Python语言开发,是对PXE的二次封装。融合多种特性,提供了CLI和Web的管理形式。同时,Cobbler也提供了API接口,方便二次开发使用。它不仅可以安装物理机,同时也支持kvm、xen虚拟化、Guest OS的安装。另外,它还能结合Puppet等集中化管理软件,实现自动化管理。
官网: http://cobbler.github.io/
一、准备阶段
-
虚拟机可连外网,NAT模式
-
系统环境:centos7.5
-
Cobbler服务器端:192.168.127.210
二、安装部署
1.基础环境配置
关闭防火墙、SElinux关闭,重启虚拟机。
[root@localhost ~]# systemctl disable --now firewalld.service
[root@localhost ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@localhost ~]# setenforce 0
[root@localhost ~]# reboot
2.安装eple源和cobbler相关依赖和其他工具,启动服务
#配置epel源
[root@localhost ~]# yum -y install epel-release vim
[root@localhost ~]# yum -y install httpd dhcp tftp python-ctypes cobbler xinetd cobbler-web pykickstart fence-agents-all net-tools
#启动httpd,cobblerd
[root@localhost ~]# systemctl enable --now httpd cobblerd
3.修改/etc/cobbler/settings
#修改server的ip地址为本机ip
[root@localhost ~]# sed -i 's/^server: 127.0.0.1/server: 192.168.127.210/' /etc/cobbler/settings
#设置tftp的ip地址为本机ip
[root@localhost ~]# sed -i 's/^next_server: 127.0.0.1/next_server: 192.168.127.210/' /etc/cobbler/settings
#修改cobbler配置文件,让cobbler控制dhcp
[root@localhost ~]# sed -i '/^manage_dhcp/s/0/1/g' /etc/cobbler/settings
[root@localhost ~]# sed -n '/^manage_dhcp/p' /etc/cobbler/settings
manage_dhcp: 1
4.生成一个加密的密码,写入到配置文件中/etc/cobbler/settings
[root@localhost ~]# openssl passwd -1 -salt 'Root@123' 'Root@123' #使用盐值加密
$1$Root@123$YTTPFr6OuGfidOhiBeSG3.
[root@localhost ~]# vim /etc/cobbler/settings
....
default_password_crypted: "$1$Root@123$YTTPFr6OuGfidOhiBeSG3."
....
# 改完配置文件同步一下
[root@localhost ~]# cobbler sync
5.启动rsync并设置开机自启
[root@localhost ~]# systemctl enable --now rsyncd
Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.
6.重启cobbler然后配置tftp
[root@localhost ~]# systemctl restart cobblerd.service
[root@localhost ~]# vim /etc/xinetd.d/tftp
service tftp
{
disable = no
socket_type = dgram
protocol = udp
wait = no
user = root
server = /usr/sbin/in.tftpd
server_args = -B 1380 -v -s /var/lib/tftpboot
per_source = 11
cps = 100 2
flags = IPv4
}
7.执行检查环境命令
[root@localhost ~]# cobbler check
The following are potential configuration items that you may want to fix:
1 : Some network boot-loaders are missing from /var/lib/cobbler/loaders. 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.
2 : 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.
1、centos7中的同样目录中的文件移动到/var/lib/cobbler/loaders
cp /usr/share/syslinux/pxelinux.0 /var/lib/cobbler/loaders
cp /usr/share/syslinux/menu.c32 /var/lib/cobbler/loaders
2、忽略
8.打开DHCP功能,并配置DHCP模板文件
# 修改dhcp.template
[root@localhost ~]# vim /etc/cobbler/dhcp.template
subnet 192.168.127.0 netmask 255.255.255.0 { //改成自己的网段
option routers 192.168.127.210; //本机得IP地址做为网关
option domain-name-servers 192.168.127.2; //dns 域名解析
option subnet-mask 255.255.255.0; //子网掩码
range dynamic-bootp 192.168.127.100 192.168.127.254; //地址范围
default-lease-time 21600;
max-lease-time 43200;
next-server $next_server;
# 重启并同步
[root@localhost ~]# systemctl restart cobblerd.service
[root@localhost ~]# cobbler sync
# 查看DHCP是以打开
[root@localhost ~]# netstat -anulp | grep dhcp
udp 0 0 0.0.0.0:67 0.0.0.0:* 2058/dhcpd
9.导入iso镜像
# 挂载镜像
[root@localhost ~]# mount CentOS-7-x86_64-DVD-1804.iso /mnt
mount: /dev/loop0 写保护,将以只读方式挂载
# 导入镜像,会把镜像导入到/var/www/cobbler中。
[root@localhost ~]# cobbler import --path=/mnt --name=centos-7 --arch=x86_64
task started: 2022-07-26_170314_import
task started (id=Media import, time=Tue Jul 26 17:03:14 2022)
Found a candidate signature: breed=suse, version=opensuse15.0
Found a candidate signature: breed=suse, version=opensuse15.1
Found a candidate signature: breed=redhat, version=rhel6
Found a candidate signature: breed=redhat, version=rhel7
Found a matching signature: breed=redhat, version=rhel7
Adding distros from path /var/www/cobbler/ks_mirror/centos-7-x86_64:
creating new distro: centos-7-x86_64
trying symlink: /var/www/cobbler/ks_mirror/centos-7-x86_64 -> /var/www/cobbler/links/centos-7-x86_64
creating new profile: centos-7-x86_64
associating repos
checking for rsync repo(s)
checking for rhn repo(s)
checking for yum repo(s)
starting descent into /var/www/cobbler/ks_mirror/centos-7-x86_64 for centos-7-x86_64
processing repo at : /var/www/cobbler/ks_mirror/centos-7-x86_64
need to process repo/comps: /var/www/cobbler/ks_mirror/centos-7-x86_64
looking for /var/www/cobbler/ks_mirror/centos-7-x86_64/repodata/*comps*.xml
Keeping repodata as-is :/var/www/cobbler/ks_mirror/centos-7-x86_64/repodata
*** TASK COMPLETE ***
[root@localhost ~]# ls -lrt /var/www/cobbler/ks_mirror/centos-7-x86_64
-rw-rw-r-- 1 root root 1690 12月 10 2015 RPM-GPG-KEY-CentOS-Testing-7
-rw-rw-r-- 1 root root 1690 12月 10 2015 RPM-GPG-KEY-CentOS-7
-rw-rw-r-- 1 root root 18009 12月 10 2015 GPL
-rw-rw-r-- 1 root root 227 8月 30 2017 EULA
-rw-rw-r-- 1 root root 14 5月 2 2018 CentOS_BuildTag
drwxr-xr-x 2 root root 43 5月 4 2018 LiveOS
drwxr-xr-x 2 root root 198 5月 4 2018 isolinux
drwxr-xr-x 3 root root 35 5月 4 2018 EFI
drwxr-xr-x 3 root root 57 5月 4 2018 images
drwxrwxr-x 2 root root 225280 5月 4 2018 Packages
drwxrwxr-x 2 root root 4096 5月 4 2018 repodata
-r--r--r-- 1 root root 2883 5月 4 2018 TRANS.TBL
10.创建kickstarts自动安装脚本
[root@localhost ~]# cd /var/lib/cobbler/kickstarts
[root@localhost ~]# vim centos-7-x86_64.ks
lang en_US
keyboard us
timezone Asia/Shanghai
rootpw --iscrypted $default_password_crypted
text
install
url --url=$tree
bootloader --location=mbr
zerombr
clearpart --all --initlabel
part /boot --fstype xfs --size 1024 --ondisk sda
part swap --size 16384 --ondisk sda
part / --fstype xfs --size 1 --grow --ondisk sda
auth --useshadow --enablemd5
$SNIPPET('netowork config')
network --bootproto=dhcp --device=ens33 --onboot=on
reboot
firewall --disabled
selinux --disabled
skipx
%pre
$SNIPPET('log_ks_pre')
$SNIPPET('kickstart_start')
$SNIPPET('pre_install_network_config')
$SNIPPET('pre_anamon')
%end
%packages
@base
@core
sysstat
iptraf
ntp
lrzsz
ncurses-devel
openssl-devel
zlib-devel
OpenIPMI-tools
mysql
nmap
screen
%end
%post
systemctl disable postfix.service
$yum_config_stanza
%end
11.检查ks文件是否有语法等问题
[root@localhost ~]# cobbler validateks
task started: 2022-07-26_171844_validateks
task started (id=Kickstart Validation, time=Tue Jul 26 17:18:44 2022)
----------------------------
osversion: rhel7
checking url: http://192.168.127.210/cblr/svc/op/ks/profile/centos-7-x86_64
running: /usr/bin/ksvalidator -v "rhel7" "http://192.168.127.133/cblr/svc/op/ks/profile/centos-7-x86_64"
received on stdout:
received on stderr:
*** all kickstarts seem to be ok ***
*** TASK COMPLETE ***
12.修改kickstart,把刚刚创建的ks文件设置为默认的文件kickstart
[root@localhost ~]# cobbler profile edit --name centos-7-x86_64 --kickstart=/var/lib/cobbler/kickstarts/centos-7-x86_64.ks
[root@localhost ~]# cobbler profile report
Name : centos-7-x86_64
TFTP Boot Files : {}
Comment :
DHCP Tag : default
Distribution : centos-7-x86_64
Enable gPXE? : 0
Enable PXE Menu? : 1
Fetchable Files : {}
Kernel Options : {}
Kernel Options (Post Install) : {}
Kickstart : /var/lib/cobbler/kickstarts/centos-7-x86_64.ks
Kickstart Metadata : {}
Management Classes : []
Management Parameters : <<inherit>>
Name Servers : []
Name Servers Search Path : []
Owners : ['admin']
Parent Profile :
Internal proxy :
Red Hat Management Key : <<inherit>>
Red Hat Management Server : <<inherit>>
Repos : []
Server Override : <<inherit>>
Template Files : {}
Virt Auto Boot : 1
Virt Bridge : xenbr0
Virt CPUs : 1
Virt Disk Driver Type : raw
Virt File Size(GB) : 5
Virt Path :
Virt RAM (MB) : 512
Virt Type : kvm
13.同步cobbler
[root@localhost ~]# cobbler sync
task started: 2022-07-26_172449_sync
task started (id=Sync, time=Tue Jul 26 17:24:49 2022)
running pre-sync triggers
cleaning trees
removing: /var/www/cobbler/images/centos-7-x86_64
removing: /var/lib/tftpboot/pxelinux.cfg/default
removing: /var/lib/tftpboot/grub/images
removing: /var/lib/tftpboot/grub/efidefault
removing: /var/lib/tftpboot/images/centos-7-x86_64
removing: /var/lib/tftpboot/s390x/profile_list
copying bootloaders
trying hardlink /var/lib/cobbler/loaders/pxelinux.0 -> /var/lib/tftpboot/pxelinux.0
copying: /var/lib/cobbler/loaders/pxelinux.0 -> /var/lib/tftpboot/pxelinux.0
trying hardlink /var/lib/cobbler/loaders/menu.c32 -> /var/lib/tftpboot/menu.c32
copying: /var/lib/cobbler/loaders/menu.c32 -> /var/lib/tftpboot/menu.c32
copying distros to tftpboot
copying files for distro: centos-7-x86_64
trying hardlink /var/www/cobbler/ks_mirror/centos-7-x86_64/images/pxeboot/vmlinuz -> /var/lib/tftpboot/images/centos-7-x86_64/vmlinuz
trying hardlink /var/www/cobbler/ks_mirror/centos-7-x86_64/images/pxeboot/initrd.img -> /var/lib/tftpboot/images/centos-7-x86_64/initrd.img
copying images
generating PXE configuration files
generating PXE menu structure
copying files for distro: centos-7-x86_64
trying hardlink /var/www/cobbler/ks_mirror/centos-7-x86_64/images/pxeboot/vmlinuz -> /var/www/cobbler/images/centos-7-x86_64/vmlinuz
trying hardlink /var/www/cobbler/ks_mirror/centos-7-x86_64/images/pxeboot/initrd.img -> /var/www/cobbler/images/centos-7-x86_64/initrd.img
Writing template files for centos-7-x86_64
rendering DHCP files
generating /etc/dhcp/dhcpd.conf
rendering TFTPD files
generating /etc/xinetd.d/tftp
processing boot_files for distro: centos-7-x86_64
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.manage_genders
running python trigger cobbler.modules.scm_track
running shell triggers from /var/lib/cobbler/triggers/change/*
*** TASK COMPLETE ***
14.重启所有服务
[root@centos-7 ~]# systemctl restart xinetd cobblerd httpd
[root@centos-7 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 5 127.0.0.1:25151 *:*
LISTEN 0 5 *:873 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 5 :::873 :::*
LISTEN 0 128 :::80 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 128 :::443 :::*