cobbler(批量部署系统)

1. cobbler简介

Cobbler是一个Linux服务器安装的服务,支持众多的Linux发行版:
redhat,fedora,centos,ubantu,suse等,也支持网络安装windows
由python编写,可以通过网络启动(PXE)的方式来快速安装、重装物理服务器和虚拟机,同时还可以管理DHCP,DNS等。

Cobbler可以使用命令行方式管理,也提供了基于Web的界面管理工具(cobbler-web),还提供了API接口,可以方便二次开发使用。

Cobbler是较早前的kickstart的升级版,优点是比较容易配置,还自带web界面比较易于管理。

Cobbler内置了一个轻量级配置管理系统,但它也支持和其它配置管理系统集成,如Puppet,暂时不支持SaltStack。

http://cobbler.github.io/

cobbler相关术语

  • 发行版:表示一个操作系统版本,它承载了内核和initrd的信息,以及内核参数等其他数据

  • 配置文件∶包含一个发行版、一个kickstart文件以及可能的存储库,还包含更多特定的内核参数等其他数据

  • 系统: 表示要配置的主机,它包含一个配置文件或一个镜像,还包含IP和MAC地址、电源管理(地址、凭据、类型)以及更为专业的数据等信息

  • 存储库︰保存一个yum 或rsync存储库的镜像信息

  • 镜像∶可替换一个包含不属于此类别的文件的发行版对象(例如,无法分为内核和initrd的对象)

  • cobbler集成的服务

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

cobbler配置文件详解

cobbler配置文件目录在/etc/cobbler

配置文件 作用
/etc/cobbler/settings cobbler 主配置文件
/etc/cobbler/iso/ iso模板配置文件
/etc/cobbler/pxe pxe模板配置文件
/etc/cobbler/power 电源配置文件
/etc/cobbler/user.conf web服务授权配置文件
/etc/cobbler/users.digest web访问的用户名密码配置文件
/etc/cobbler/dhcp.template dhcp服务器的的配置模板
/etc/cobbler/dnsmasq.template dns服务器的配置模板
/etc/cobbler/tftpd.template tftp服务的配置模板
/etc/cobbler/modules.conf 模块的配置文件

cobbler数据目录

目录 作用
/var/lib/cobbler/config/ 用于存放用户定义的cobbler命令
/var/lib/cobbler/kickstart/ 默认存放kickstart文件
/var/lib/cobbler/loaders/ 导入的发行版系统的所有数据
/var/www/cobbler/images/ 导入发行版的kernel和initrd镜像用于远程网络启动
/var/www/cobbler/repo_mirror/ yum仓库存储目录

cobbler日志文件

日志文件路径 说明
/var/log/cobbler/installing 客户端安装日志
/var/log/cobbler/cobbler.log cobbler日志

cobbler命令详解

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

2. cobbler部署

注意服务端系统为centos7,centos8由于tftp无法启动,所以无法部署

//关闭防火墙和selinux
[root@cobbler ~]# systemctl disable --now firewalld.service 
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@cobbler ~]# vim /etc/selinux/config 
[root@cobbler ~]# reboot
[root@localhost ~]# getenforce 
Disabled  //确保为关闭状态

//安装epel源
[root@cobbler ~]# yum -y install epel-release

//安装cobbler以及相关的软件
[root@localhost ~]# yum -y install httpd dhcp-* tftp xinetd syslinux pykickstart 
[root@localhost ~]# yum -y install cobbler cobbler-web

//启动服务并设置开机自启
[root@localhost ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@localhost ~]# systemctl enable --now cobblerd.service 
Created symlink /etc/systemd/system/multi-user.target.wants/cobblerd.service → /usr/lib/systemd/system/cobblerd.service.
[root@localhost ~]# ss -antl
State             Recv-Q            Send-Q                       Local Address:Port                          Peer Address:Port            Process            
LISTEN            0                 5                                127.0.0.1:25151                              0.0.0.0:*                                  
LISTEN            0                 128                                0.0.0.0:22                                 0.0.0.0:*                                  
LISTEN            0                 128                                      *:443                                      *:*                                  
LISTEN            0                 128                                      *:80                                       *:*                                  
LISTEN            0                 128                                   [::]:22                                    [::]:*      

//将tftp和serverip设置为本机ip
[root@localhost ~]# sed -i 's/^server: 127.0.0.1/server: 192.168.216.205/' /etc/cobbler/settings
[root@localhost ~]# sed -i 's/^next_server: 127.0.0.1/next_server: 192.168.216.205/' /etc/cobbler/settings

//检查cobbler
[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, 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.
2 : enable and start rsyncd.service with systemctl
3 : debmirror package is not installed, it will be required to manage debian deployments and repositories
4 : 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.
第3条可以忽略
[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.
[root@localhost ~]# yum -y install fence-agents 
根据第一条提示运行cobbler get-loaders报错无法获取文件,这里我们把centos8的/var/lib/cobbler/loaders复制过来,注意不要pxelinux.0和menu.c32,这两个文件我们从本机获取


[root@localhost ~]# cp /usr/share/syslinux/pxelinux.0 /var/lib/cobbler/loaders/
[root@localhost ~]# cp /usr/share/syslinux/menu.c32 /var/lib/cobbler/loaders/
[root@localhost loaders]# ls
COPYING.syslinux  COPYING.yaboot  grub-x86_64.efi  grub-x86.efi  menu.c32  pxelinux.0  README  yaboot

[root@localhost ~]# systemctl restart cobblerd

//生成加密密码,复制到配置文件
[root@localhost ~]#  openssl passwd -1
Password: 
Verifying - Password: 
$1$P3UX97u6$dUflTw2C9Qz59eNskOcdB0
[root@localhost ~]# vim /etc/cobbler/setting 
103 default_password_crypted: "$1$P3UX97u6$dUflTw2C9Qz59eNskOcdB0"//修改为我们生成的密码

//管理dhcp设为开机
220 manage_dhcp: 1  //0改为1

//打开tftp
[root@localhost loaders]#  sed -i '/disable/s/yes/no/g' /etc/xinetd.d/tftp


//设置dhcp
23 subnet 192.168.216.0 netmask 255.255.255.0 {
    
    
 24      option routers             192.168.216.205;  // 前面为本机IP同一网段
 25      option domain-name-servers 192.168.216.1;  // 本机IP
 26      option subnet-mask         255.255.255.0;  // 网关
 27      range dynamic-bootp        192.168.216.210  192.168.216.230;  // 分配的IP地址范围
 28      default-lease-time         21600;
 29      max-lease-time             43200;
 30      next-server                $next_server;
// 启动和启用 Cobbler 服务并同步配置,改完配置文件必须要sync同步配置
 [root@localhost loaders]# cobbler sync

 [root@localhost ~]# systemctl restart cobblerd.service 

//导入镜像

```bash
[root@localhost ~]# cobbler import --help
Usage: cobbler import [options]

Options:
  -h, --help            show this help message and exit
  --arch=ARCH           指定安装源是32位、64位、ia64, 目前支持的选项有: x86│x86_64│ia64
  --breed=BREED         the breed being imported
  --os-version=OS_VERSION
                        the version being imported
  --path=PATH          镜像路径
  --name=NAME          为镜像定义的名字
  --available-as=AVAILABLE_AS
                        tree is here, don't mirror
  --autoinstall=AUTOINSTALL_FILE
                        assign this autoinstall file
  --rsync-flags=RSYNC_FLAGS
                        pass additional flags to rsync

//挂载系统镜像
[root@localhost ~]# mount /dev/cdrom /mnt
[root@localhost loaders]# cobbler import --path=/mnt --name=centos-7 --arch=x86_64
task started: 2021-10-13_233912_import
task started (id=Media import, time=Wed Oct 13 23:39:12 2021)
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 loaders]# ls /var/www/cobbler/ks_mirror/
centos-7-x86_64  config


//查看profile
[root@localhost loaders]# cobbler list
distros:
   centos-7-x86_64

profiles:
   centos-7-x86_64

systems:

repos:

images:

mgmtclasses:

packages:

files:

//创建kickstarts自动安装脚本,与根目录下的anaconda-ks.cfg 类似
[root@localhost loaders]# cat > /var/lib/cobbler/kickstarts/centos-7-x86_64.ks <<'EOF'
> auth --enableshadow --passalgo=sha512
> bootloader --location=mbr
> clearpart --all --initlabel
> part /boot --asprimary --fstype="ext4" --size=500
> part swap --fstype="swap" --size=4096
> part / --fstype="ext4" --grow --size=15000
> text
> firewall --disabled
> firstboot --disable
> keyboard us
> lang en_US
> url --url=http://192.168.216.205/cobbler/ks_mirror/centos-7-x86_64    // yum仓库的地址
> $yum_repo_stanza
> reboot
> rootpw --iscrypted $6$zFIzSmthwBm0C9jA$DfTGxMfXRirzCD8HF4KrpZpQLXsvYtrU9WFa25pLtCttaeQ9cYBEqNune5rmz8JcMWhzdKTw.Oiun.b0srvwe0   // 设置root用户的管理员密码
> selinux --disabled
> skipx  // 跳过图形化
> timezone Asia/Shanghai --isUtc --nontp
> install
> zerombr
> 
> %packages
> @^minimal
> @core
> kexec-tools
> 
> %end
> 
> %addon com_redhat_kdump --enable --reserve-mb='auto'
> 
> %end
> 
> %anaconda
> pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
> pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
> pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
> %end
> EOF

//检查语法错误
[root@localhost loaders]#  cobbler validateks
task started: 2021-10-13_234242_validateks
task started (id=Kickstart Validation, time=Wed Oct 13 23:42:42 2021)
----------------------------
osversion: rhel7
checking url: http://192.168.216.205/cblr/svc/op/ks/profile/centos-7-x86_64
running: /usr/bin/ksvalidator -v "rhel7" "http://192.168.216.205/cblr/svc/op/ks/profile/centos-7-x86_64"
received on stdout: 
received on stderr: 
*** all kickstarts seem to be ok *** //ok表示没问题
*** TASK COMPLETE ***

//确认镜像文件名称是否无误
[root@localhost loaders]# cobbler profile list
   centos-7-x86_64

//修改profile默认路径,将我们新建的ks文件设为默认的kickstarts安装文件
[root@localhost loaders]#  cobbler profile edit --name centos-7-x86_64 --kickstart=/var/lib/cobbler/kickstarts/centos-7-x86_64.ks

//配置网卡名称为传统网卡名称eth0
[root@localhost loaders]# cobbler profile edit --name centos-7-x86_64 --kopts='net.ifnames=0 biosdevname=0'

//检查当前系统cobbler配置文件信息
[root@localhost loaders]# 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                 : {
    
    'biosdevname': '0', 'net.ifnames': '0'}
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

//同步
[root@localhost loaders]# cobbler sync
//将用到的所有服务重启一遍
[root@localhost loaders]# systemctl restart cobblerd httpd xinetd

新创建虚拟机,内存给2g以上不然会报错,不要连接iso光盘
看到pxe界面即表示成功

四、定制安装

  • 获取服务器mac地址
  • 配置cobbler
  • 安装






打开服务端同步,重启服务

[root@localhost ~]# cobbler sync
task started: 2021-10-14_103507_sync
task started (id=Sync, time=Thu Oct 14 10:35:07 2021)
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/pxelinux.cfg/01-00-50-56-33-fe-9d
removing: /var/lib/tftpboot/grub/images
removing: /var/lib/tftpboot/grub/grub-x86_64.efi
removing: /var/lib/tftpboot/grub/grub-x86.efi
removing: /var/lib/tftpboot/grub/efidefault
removing: /var/lib/tftpboot/grub/01-00-50-56-33-FE-9D
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
trying hardlink /var/lib/cobbler/loaders/grub-x86_64.efi -> /var/lib/tftpboot/grub/grub-x86_64.efi
trying hardlink /var/lib/cobbler/loaders/grub-x86.efi -> /var/lib/tftpboot/grub/grub-x86.efi
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: /var/lib/tftpboot/pxelinux.cfg/01-00-50-56-33-fe-9d
generating: /var/lib/tftpboot/grub/01-00-50-56-33-FE-9D
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 ***
[root@localhost ~]# systemctl restart xinetd
[root@localhost ~]# systemctl restart httpd
[root@localhost ~]# systemctl restart cobblerd

打开之前生成mac地址的虚拟机,发现直接跳过了pxe界面

猜你喜欢

转载自blog.csdn.net/weixin_46115601/article/details/120733734
今日推荐