Centos7部署cobbler批量装机

cobbler

Cobbler是一个快速网络安装linux的服务,而且在经过调整也可以支持网络安装windows。

使用简单的命令即可完成PXE网络安装环境的配置,同时还可以管理DHCP、DNS、TFTP、RSYNC以及yum仓库、构造系统ISO镜像。 Cobbler支持命令行管理,web界面管理,还提供了API接口,可以方便二次开发使用,Cobbler客户端Koan支持虚拟机安装和操作系统重新安装

cobbler可以干什么

  • 服务器上架后,可以手动选择需要安装的系统(如:Centos6.8 或 Centos 7.3)
  • 服务器上架后,能够根据需求,安装配置操作系统(如:修改IP地址、主机名、选择安装包)
  • 系统安装后,可以自定义的执行脚本,完成系统基础软件初始化(如:Zabbix安装配置、SaltStack安装配置)
  • 可以当内部YUM源,并在系统安装时进行初始化
  • 可以重装系统
  • Cobbler支持API,可以无缝融合到自建运维平台中
  • Cobbler支持网卡的路由配置、DNS配置、bonding

cobbler基础原理

Cobbler是通过将DHCP、TFTP、DNS、HTTP等服务进行集成,创建一个中央管理节点,其可以实现的功能有配置服务,创建存储库,解压缩操作系统媒介,代理或集成一个配置管理系统,控制电源管理等。 Cobbler的最终目的是实现无需进行人工干预即可安装机器。在进行进一步的操作之前,我们有必要先了解下pxe和kickstart 。

cobbler集成的服务

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

PEX概念
预启动执行环境(Preboot eXecution Environment,PXE,也被称为预执行环境)是让计算机通过网卡独立地使用数据设备(如硬盘)或者安装操作系统。Intel 搞出来的。

Kickstart 概述
Kickstart 是红帽开发的一种工具,我们可以简单理解为一个自动安装应答配置管理程序。通过读取这个配置文件,系统知道怎么去分区,要安装什么包,配什么IP,优化什么内核参数等等。其主要有以下部分组成:

  • Kickstart 安装选项,包含语言的选择,防火墙,密码,网络,分区的设置等;
  • %Pre 部分,安装前解析的脚本,通常用来生成特殊的ks配置,比如由一段程序决定磁盘分区等;
  • %Package 部分,安装包的选择,可以是 @core 这样的group的形式,也可以是这样 vim-* 包的形式;
  • %Post 部分,安装后执行的脚本,通常用来做系统的初始化设置。比如启动的服务,相关的设定等。

Cobbler的设计方式

Cobbler 的配置结构基于一组注册的对象。每个对象表示一个与另一个实体相关联的实体(该对象指向另一个对象,或者另一个对象指向该对象)。当一个对象指向另一个对象时,它就继承了被指向对象的数据,并可覆盖或添加更多特定信息。以下对象类型的定义为:

  • 发行版:表示一个操作系统。它承载了内核和 initrd 的信息,以及内核参数等其他数据。
  • 配置文件:包含一个发行版、一个 kickstart 文件以及可能的存储库,还包含更多特定的内核参数等其他数据。
  • 系统:表示要配给的机器。它包含一个配置文件或一个镜像,还包含 IP 和 MAC 地址、电源管理(地址、凭据、类型)以及更为专业的数据等信息。
  • 存储库:保存一个 yum 或 rsync 存储库的镜像信息
  • 镜像:可替换一个包含不属于此类别的文件的发行版对象(例如,无法分为内核和 initrd 的对象)

cobbler工作原理
在这里插入图片描述

部署

关闭防火墙和selinux,然后重启

[root@localhost ~]# 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@localhost ~]# vim /etc/selinux/config 
SELINUX=disabled

[root@localhost ~]# reboot
连接断开

安装eple源和cobbler相关的软件

[root@localhost ~]# yum -y install epel-release
[root@localhost ~]# yum -y install httpd dhcp-* tftp xinetd pykickstart

//开启cobbler源,然后再继续安装软件包
[root@localhost ~]# yum -y install module enable cobbler
[root@localhost ~]# yum  -y install cobbler cobbler-web

启动HTTP的和cobblerd并设置开机自启

[root@centos-7 ~]# systemctl enable --now httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@centos-7 ~]# systemctl enable --now cobblerd
Created symlink from /etc/systemd/system/multi-user.target.wants/cobblerd.service to /usr/lib/systemd/system/cobblerd.service.
[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      128     *:22                  *:*                  
LISTEN      0      100    127.0.0.1:25                  *:*                  
LISTEN      0      128    :::80                 :::*                  
LISTEN      0      128    :::22                 :::*                  
LISTEN      0      100       ::1:25                 :::*                  
LISTEN      0      128    :::443                :::*                 

更改配置文件中的IP地址,改成本机的IP地址

[root@localhost cobbler]# pwd
/etc/cobbler
[root@localhost cobbler]# vim settings
server: 192.168.164.128
next_server: 192.168.164.128

生成一个加密的密码,写入到配置文件中/etc/cobbler/settings

[root@centos-7 cobbler]# openssl passwd -1
Password: 
Verifying - Password: 
$1$.PmhBwFL$FlvmDEZ/lihDR8pHzFHki0
[root@centos-7 cobbler]# vim settings 
default_password_crypted: "$1$.PmhBwFL$FlvmDEZ/lihDR8pHzFHki0"

//改完配置文件同步一下
[root@centos-7 cobbler]# cobbler sync

启动rsync并设置开机自启

[root@centos-7 ~]# systemctl enable --now rsyncd
Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.

重启cobbler然后配置tftp

[root@centos-7 ~]# systemctl restart cobblerd.service 
[root@centos-7 ~]# vim /etc/xinetd.d/tftp 
service tftp
{
        disable                 = no
        socket_type             = dgram
        protocol                = udp
        wait                    = yes
        user                    = root
        server                  = /usr/sbin/in.tftpd
        server_args             = -B 1380 -v -s /var/lib/tftpboot
        per_source              = 11
        cps                     = 100 2
        flags                   = IPv4
}

执行检查环境命令

[root@centos-7 cobbler]# 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 : 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.



//解决办法
1、centos8中的同样目录中的文件移动到/var/lib/cobbler/loaders
      cd  loaders
      cp /usr/share/syslinux/pxelinux.0 .    //把本地的文件复制到当前目录一共8个文件 下面的图
      cp /usr/share/syslinux/menu.c32 .
      ......
      ......

2、第二个可以忽略不管,此次部署用不到
3、 yum -y install fence-agents

在这里插入图片描述

重启并同步一下

[root@centos-7 loaders]# systemctl restart cobblerd.service
[root@centos-7 loaders]# cobbler sync

打开DHCP功能,并配置DHCP模板文件

[root@centos-7 cobbler]# cd /etc/cobbler/
[root@centos-7 cobbler]# vim settings 
manage_dhcp: 1    //用cobbler管理dhcp

[root@centos-7 cobbler]# pwd
/etc/cobbler
[root@centos-7 cobbler]# vim dhcp.template
subnet 192.168.164.0 netmask 255.255.255.0 {       //改成自己的网段
     option routers             192.168.164.128;    //本机得IP地址做为网关
     option domain-name-servers 192.168.164.2;      //dns 域名解析
     option subnet-mask         255.255.255.0;      //子网掩码
     range dynamic-bootp        192.168.164.100 192.168.164.254;   //地址范围
     default-lease-time         21600;
     max-lease-time             43200;
     next-server                $next_server;

//重启并同步
[root@centos-7 cobbler]# systemctl restart cobblerd.service
[root@centos-7 cobbler]# cobbler  sync

//查看DHCP是以打开
[root@centos-7 cobbler]# netstat -anulp | grep dhcp
udp        0      0 0.0.0.0:67              0.0.0.0:*                           46506/dhcpd        
 
#如果找不到命令则需要安装依赖包  yum -y install net-tools

导入镜像

//挂载光盘
[root@centos-7 ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
//导入镜像,会把镜像导入到/var/www/cobbler中。
[root@centos-7 ~]# cobbler import --path=/mnt --name=centos-7 --arch=x86_64
task started: 2021-10-13_201750_import
task started (id=Media import, time=Wed Oct 13 20:17:50 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 ***

#   --path      //镜像路径
#    --name      //为安装源定义一个名字
#    --arch      //指定安装源平台

查看镜像列表,当中列着发行版和配置文件

[root@centos-7 ~]# cobbler list
distros:
   centos-7-x86_64   //发行版

profiles:
   centos-7-x86_64   //配置文件

systems:

repos:

images:

mgmtclasses:

packages:

files:

//配置文件位置,里面是镜像文件内容
[root@centos-7 centos-7-x86_64]# pwd
/var/www/cobbler/ks_mirror/centos-7-x86_64

[root@centos-7 ks_mirror]# cd centos-7-x86_64/
[root@centos-7 centos-7-x86_64]# ll
总用量 316
-rw-rw-r-- 1 root root     14 52 2018 CentOS_BuildTag
drwxr-xr-x 3 root root     35 54 2018 EFI
-rw-rw-r-- 1 root root    227 830 2017 EULA
-rw-rw-r-- 1 root root  18009 1210 2015 GPL
drwxr-xr-x 3 root root     57 54 2018 images
drwxr-xr-x 2 root root    198 54 2018 isolinux
drwxr-xr-x 2 root root     43 54 2018 LiveOS
drwxrwxr-x 2 root root 225280 54 2018 Packages
drwxrwxr-x 2 root root   4096 54 2018 repodata
-rw-rw-r-- 1 root root   1690 1210 2015 RPM-GPG-KEY-CentOS-7
-rw-rw-r-- 1 root root   1690 1210 2015 RPM-GPG-KEY-CentOS-Testing-7
-r--r--r-- 1 root root   2883 54 2018 TRANS.TBL


创建kickstarts自动安装脚本
这个脚本是系统安装时的操作,也就是装系统的时候让你设置的时区、密码、最小化等等。这里写成了一个脚本

[root@centos-7 kickstarts]# pwd
/var/lib/cobbler/kickstarts

[root@centos-7 kickstarts]# cat centos-7-x86_64.ks 
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.164.128/cobbler/ks_mirror/centos-7-x86_64   
$yum_repo_stanza
reboot

//密码,这要把本机/root/anaconda-ks.cfg中的密码复制过来,这样新的虚拟机就和本机密码一样了
rootpw --iscrypted $6$zFsJQ/RU46s0ghKG$EivXcNIpN7//7GqVtVNhLgKoiLU6KHUCHy8UtADI4QMCrxN.qbHb9wDnY7AqYkHjUA7KUE5tWVN9MuCYhF6c..


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


检查ks文件是否有语法等问题

[root@centos-7 kickstarts]# cobbler validateks
task started: 2021-10-13_204834_validateks
task started (id=Kickstart Validation, time=Wed Oct 13 20:48:34 2021)
----------------------------
osversion: rhel7
checking url: http://192.168.164.128/cblr/svc/op/ks/profile/centos-7-x86_64
running: /usr/bin/ksvalidator -v "rhel7" "http://192.168.164.128/cblr/svc/op/ks/profile/centos-7-x86_64"
received on stdout: 
received on stderr: 
*** all kickstarts seem to be ok ***   //没有问题,进行下一步即可
*** TASK COMPLETE ***

查看当前cobbler有哪些配置文件

[root@centos-7 kickstarts]# cobbler profile list
   centos-7-x86_64
//查看当前系统使用的配置文件是不是刚刚创建的脚本,如果不是要修改
[root@centos-7 kickstarts]# 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/sample_end.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

//修改kickstart,把刚刚创建的ks文件设置为默认的文件kickstart
[root@localhost ~]# cobbler profile edit --name rhel-7-x86_64 --kickstart=/var/lib/cobbler/kickstarts/rhel-7-x86_64.ks

设置网卡,设置成传统命名网卡eth,这样网卡的名字就都一样了便于日后统一管理

[root@centos-7 kickstarts]# cobbler profile edit --name centos-7-x86_64 --kopts='net.ifnames=0 biosdevname=0'

此时再去查看系统信息,内核参数和kickstart都已经设置好了

[root@centos-7 ~]# 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
......
......

同步cobbler

[root@centos-7 ~]# cobbler sync
task started: 2021-10-13_210633_sync
task started (id=Sync, time=Wed Oct 13 21:06:33 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/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/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 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@centos-7 ~]# systemctl restart xinetd.service   //tftp是基于xinetd的,也就受xinetd管理
[root@centos-7 ~]# systemctl restart cobblerd.service 
[root@centos-7 ~]# systemctl restart httpd.service 
[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                :::*                  

创建虚拟机
镜像不选 网络选择NAT模式
在这里插入图片描述
启动虚拟机待出现蓝色界面就代表部署成功 开始装系统
在这里插入图片描述

定制安装

定制安装步骤:

  • 统计服务器mac地址
  • 配置cobbler
  • 安装
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

同步配置和重启服务

[root@localhost ~]# cobbler sync
[root@localhost ~]# systemctl restart httpd
[root@localhost ~]# systemctl restart cobblerd
[root@localhost ~]# systemctl restart xinetd

最后打开刚刚创建的虚拟机开机即可
在这里插入图片描述
在这里插入图片描述
此时这台虚拟机的IP地址是120和网页配置一样。完成!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值