Cobbler自动化批量安装Linux操作系统

由于业务需求需要批量部署linux服务器,所以开始学习是使用以下cobbler自动部署

简介

Cobbler是一个Linux服务器安装的服务,可以通过网络启动(PXE)的方式来快速安装、重装物理服务器和虚拟机,同时还可以管理DHCP,DNS等。

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

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

工作原理

Server端:


 
 
  1. 第一步,启动Cobbler服务
  2. 第二步,进行Cobbler错误检查,执行cobbler check命令
  3. 第三步,进行配置同步,执行cobbler sync命令
  4. 第四步,复制相关启动文件文件到TFTP目录中
  5. 第五步,启动DHCP服务,提供地址分配
  6. 第六步,DHCP服务分配IP地址
  7. 第七步,TFTP传输启动文件
  8. 第八步, Server端接收安装信息
  9. 第九步, Server端发送ISO镜像与Kickstart文件

Client端:


 
 
  1. 第一步,客户端以PXE模式启动
  2. 第二步,客户端获取IP地址
  3. 第三步,通过TFTP服务器获取启动文件
  4. 第四步,进入Cobbler安装选择界面
  5. 第五步,客户端确定加载信息
  6. 第六步,根据配置信息准备安装系统
  7. 第七步,加载Kickstart文件
  8. 第八步,传输系统安装的其它文件
  9. 第九步,进行安装系统

环境


 
 
  1. IP: 192.168 .231 .60
  2. [ root@n1 ~] # cat /etc/redhat-release
  3. CentOS Linux release 7.5 .1804 (Core)
  4. [ root@n1 ~] # uname -r
  5. 3.10 .0 -862.el7.x86_64
  6. #关闭防火墙
  7. [ root@n1 ~] # systemctl status firewalld
  8. ● firewalld.service - firewalld - dynamic firewall daemon
  9. Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
  10. Active: inactive (dead)
  11. Docs: man:firewalld( 1)
  12. #关闭selinux
  13. [ root@n1 ~] # getenforce
  14. Disabled
  15. 虚拟机网卡采用nat模式

部署Cobbler

命令管理


 
 
  1. usage
  2. =====
  3. cobbler <distro |profile|system |repo|p_w_picpath |mgmtclass|package |file> ...
  4. [add|edit |copy|getks* |list|remove |rename|report] [options |--help]
  5. cobbler <aclsetup|buildiso |import|list |replicate|report |reposync|sync |validateks|version |signature|get-loaders |hardlink> [options|--help]
  6. cobbler check 核对当前设置是否有问题
  7. cobbler list 列出所有的cobbler元素cobbler
  8. report 列出元素的详细信息
  9. cobbler sync 同步配置到数据目录,更改配置最好都要执行下
  10. cobbler reposync 同步yum仓库
  11. cobbler distro 查看导入的发行版系统信息
  12. cobbler system 查看添加的系统信息cobbler
  13. profile 查看配置信息

安装相关依赖包

注意一点:安装cobbler的环境不要用mini版centos 还需要添加两个依赖源:curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo


 
 
  1. [root@n1 ~] # yum install cobbler cobbler-web pykickstart httpd dhcp tftp-server -y
  2. cobbler #cobbler程序包
  3. cobbler-web #cobbler的web服务包
  4. pykickstart #cobbler检查kickstart语法错误

查看cobbler安装的部分文件


 
 
  1. [ root@n1 ~] # rpm -ql cobbler # 查看安装的文件,下面列出部分。
  2. /etc/cobbler # 配置文件目录
  3. /etc/cobbler/settings # cobbler主配置文件,这个文件是yaml格式,cobbler是python写的程序。
  4. /etc/cobbler/dhcp.template # dhcp服务的配置模板
  5. /etc/cobbler/tftpd.template # tftp服务的配置模板
  6. /etc/cobbler/rsync.template # rsync服务的配置模板
  7. /etc/cobbler/iso # iso模板配置文件目录
  8. /etc/cobbler/pxe # pxe模板文件目录
  9. /etc/cobbler/power # 电源的配置文件目录
  10. /etc/cobbler/users.conf # web服务授权配置文件
  11. /etc/cobbler/users.digest # web访问的用户名密码配置文件
  12. /etc/cobbler/dnsmasq.template # DNS服务的配置模板
  13. /etc/cobbler/modules.conf # cobbler模块配置文件
  14. / var/lib/cobbler # cobbler数据目录
  15. / var/lib/cobbler/config # 配置文件
  16. / var/lib/cobbler/kickstarts # 默认存放kickstart文件
  17. / var/lib/cobbler/loaders # 存放的各种引导程序
  18. / var/www/cobbler # 系统安装镜像目录
  19. / var/www/cobbler/ks_mirror # 导入的系统镜像列表
  20. / var/www/cobbler/p_w_picpaths # 导入的系统镜像启动文件
  21. / var/www/cobbler/repo_mirror # yum源存储目录
  22. / var/log/cobbler # 日志目录
  23. / var/log/cobbler/install.log # 客户端系统安装日志
  24. / var/log/cobbler/cobbler.log # cobbler日志

启动相关服务


 
 
  1. [root@n1 ~] # systemctl start httpd
  2. [root@n1 ~] # systemctl start cobblerd

检测cobbler


 
 
  1. [root@n1 ~] # cobbler check
  2. The following are potential configuration items that you may want to fix:
  3. 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.
  4. 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.
  5. 3 : change 'disable' to 'no' in /etc/xinetd.d/tftp
  6. 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.
  7. 5 : enable and start rsyncd.service with systemctl
  8. 6 : debmirror package is not installed, it will be required to manage debian deployments and repositories
  9. 7 : 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
  10. 8 : fencing tools were not found, and are required to use the ( optional) power management features. install cman or fence-agents to use them
  11. Restart cobblerd and then run 'cobbler sync' to apply changes.

按照指示修改


 
 
  1. 1.修改/etc/cobbler/settings中的 "server"字段为提供cobbler服务的主机的IP或主机名
  2. [root@n1 ~] # sed -i 's/^server: 127.0.0.1/server: 192.168.231.60/' /etc/cobbler/settings
  3. [root@n1 ~] # grep "^server" /etc/cobbler/settings
  4. server: 192.168. 231.60
  5. 2.修改/etc/cobbler/settings中 "next_server"为PXE网络上启动服务器的IP(这里PXE服务器和cobbler在同一主机)
  6. [root@n1 ~] # sed -i 's/^next_server: 127.0.0.1/next_server: 192.168.231.60/' /etc/cobbler/settings
  7. [root@n1 ~] # grep "^next_server" /etc/cobbler/settings
  8. next_server: 192.168. 231.60
  9. 3.修改/etc/xinet.d/tftp文件中的disable的参数为 no
  10. [root@n1 ~] # cp /etc/xinetd.d/tftp{,.bak} #备份
  11. [root@n1 ~] # vim /etc/xinetd.d/tftp
  12. disable = no
  13. 4.执行cobbler get-loaders命令
  14. 5.启动rsync服务
  15. [root@n1 ~] # systemctl start rsyncd.service
  16. [root@n1 ~] # systemctl enable rsyncd.service
  17. 6.安装包用来管理debian部署和存储库,这里不需要,就不安装了
  18. 7.修改cobbler中的加密密码(即为自动安装系统后的root登录密码)
  19. #生成加密密码openssl passwd -1 -salt 'random-phrase-here' 'your-password-here'
  20. [root@n1 ~] # openssl passwd -1 -salt '123456' 'realxw'
  21. $1$123456$jMyuS0sKNP4A/OOlui3lR/
  22. [root@n1 ~] # vim /etc/cobbler/settings
  23. default_password_crypted: "$1$123456$jMyuS0sKNP4A/OOlui3lR/"
  24. 8.安装cman fence-agents(可选),电源管理功能
  25. [root@n1 ~] # yum install cman fence-agents -y

修改一些其他配置


 
 
  1. [root@n1 ~] # vim /etc/cobbler/settings
  2. manage_dhcp: 1 #用cobbler管理dhcp
  3. pxe_just_once: 1 #防止循环安装系统,适用于服务器第一启动选项是pxe启动

同步cobbler配置


 
 
  1. [root@n1 ~] # cobbler sync
  2. [root@n1 ~] # systemctl restart cobblerd.service

重新检测cobbler


 
 
  1. [root@n1 ~]# cobbler check
  2. The following are potential configuration items that you may want to fix:
  3. 1 : debmirror package is not installed, it will be required to manage debian deployments and repositories
  4. Restart cobblerd and then run 'cobbler sync' to apply changes.

#管理deban,这里不需要安装

配置dhcp


 
 
  1. [root@n1 ~]# cp /etc/cobbler/dhcp.template{,.bak}
  2. [root@n1 ~]# vim /etc/cobbler/dhcp.template
  3. subnet 192.168 .231 .0 netmask 255.255 .255 .0 {
  4. option routers 192.168 .231 .2;
  5. option domain-name-servers 192.168 .231 .2;
  6. option subnet-mask 255.255 .255 .0;
  7. range dynamic-bootp 192.168 .231 .100 192.168 .231 .110;
  8. default-lease- time 21600;
  9. max-lease- time 43200;
  10. next- server $next_server;

同步cobbler配置

[root@n1 ~]# cobbler sync

重新检测cobbler


 
 
  1. [root@n1 ~]# cobbler check
  2. The following are potential configuration items that you may want to fix:
  3. 1 : debmirror package is not installed, it will be required to manage debian deployments and repositories
  4. Restart cobblerd and then run 'cobbler sync' to apply changes.

设置开机自启


 
 
  1. systemctl enable dhcpd
  2. systemctl enable rsyncd.service
  3. systemctl enable tftp.service
  4. systemctl enable httpd.service
  5. systemctl enable cobblerd.service

重启所有服务,防止一些服务没有开启


 
 
  1. systemctl restart tftp .service
  2. systemctl restart dhcpd .service
  3. systemctl restart rsyncd .service
  4. systemctl restart httpd .service
  5. systemctl restart cobblerd .service

挂载光盘镜像

[root@n1 ~]# mkdir /iso  #创建挂载目录

[root@n1 ~]# mount -t iso9660 -o loop /soft/CentOS-7-x86_64-Minimal-1804.iso /iso/   #这里是CentOS7.5mini镜像

mount: /dev/loop0 is write-protected, mounting read-only

导入镜像


 
 
  1. [root@n1 ~]# cobbler import --path=/iso/ --name=CentOS7.5 --arch=x86_64
  2. --path:镜像路径
  3. --name:为安装源定义一个名字
  4. --arch :指定安装源是64位或32位

查看镜像列表

[root@n1 ~]# cobbler distro list

  CentOS7.5-x86_64

查看镜像存放目录

[root@n1 ~]# cd /var/www/cobbler/ks_mirror/

[root@n1 ks_mirror]# ls

CentOS7.5-x86_64  config

配置ks文件

查看ks文件位置


 
 
  1. [ root@n1 ~] # cd /var/lib/cobbler/kickstarts/
  2. [ root@n1 kickstarts] # ls
  3. default.ks legacy.ks sample_esx4.ks sample.ks
  4. esxi4-ks.cfg pxerescue.ks sample_esxi4.ks sample_old.seed
  5. esxi5-ks.cfg sample_autoyast.xml sample_esxi5.ks sample.seed
  6. install_profiles sample_end.ks (默认使用的ks文件) sample_esxi6.ks

配置自定义ks文件,以sample_end.ks为模板


 
 
  1. [root@n1 kickstarts] # cp sample_end.ks CentOS7.5-x86_64.ks
  2. [root@n1 kickstarts] # vim CentOS7.5-x86_64.ks #只修改如下内容
  3. firewall --disable #关闭防火墙
  4. timezone Asia/Shanghai #设置时区
  5. # autopart #把自动分区注释,手动如下设置
  6. part /boot --fstype=ext4 --asprimary --size= 200
  7. part swap --asprimary --size= 1024
  8. part / --fstype=ext4 --grow --asprimary --size= 10240
  9. %packages #安装需要安装的软件包
  10. $SNIPPET( 'func_install_if_enabled') #这里默认
  11. %end
  12. 解释
  13. --asprimary,强迫把分区分配为主分区,否则提示分区失败.
  14. --fstype=,为分区设置文件系统类型.有效的类型为ext2,ext3,swap和vfat
  15. --grow 让分区自动增长利用可用的磁盘空间,或是增长到设置的maxsize值;
  16. --size= 设置分区的最小值,默认单位为M,但是不能写单位;
  17. %packages部分,这部分选择需要安装的软件包.

这是我的硬盘分区配置
part /boot --fstype=“xfs” --ondisk=sda --size=2048
part / --fstype==“xfs” --ondisk=sda --size=3024
part swap --fstype=“swap” --ondisk=sda --size=1024
part biosboot --fstype=“biosboot” --ondisk=sda --size=1需要这个系统才能正常安装
part /data/core --fstype=“xfs” --grow --ondisk=sda

如想详细了解怎样配置kickstart,可参考  https://blog.csdn.net/taiyang1987912/article/details/42176709

#第一次导入系统镜像后,cobbler会给镜像指定一个默认的kickstart自动安装文件(/var/lib/cobbler/kickstarts/sample_end.ks)


 
 
  1. [root@n1 ~]# cobbler list
  2. distros:
  3. CentOS7 .5-x86_64
  4. profiles:
  5. CentOS7 .5-x86_64
  6. systems:
  7. repos:
  8. images:
  9. mgmtclasses:
  10. packages:
  11. files:

查看安装镜像文件信息


 
 
  1. [root@n1 ~] # cobbler distro report --name=CentOS7.5-x86_64
  2. Name : CentOS7. 5-x86_64
  3. Architecture : x86_64
  4. TFTP Boot Files : {}
  5. Breed : redhat
  6. Comment :
  7. Fetchable Files : {}
  8. Initrd : /var/www /cobbler/ks_mirror/CentOS7. 5-x86_64/images/pxeboot/initrd.img
  9. Kernel : /var/www /cobbler/ks_mirror/CentOS7. 5-x86_64/images/pxeboot/vmlinuz
  10. Kernel Options : {}
  11. Kernel Options (Post Install) : {}
  12. Kickstart Metadata : { 'tree': 'http://@@http_server@@/cblr/links/CentOS7.5-x86_64'}
  13. Management Classes : []
  14. OS Version : rhel7
  15. Owners : [ 'admin']
  16. Red Hat Management Key : <<inherit >>
  17. Red Hat Management Server : <<inherit>>
  18. Template Files : {}

查看镜像的profile设置

更详细信息


 
 
  1. [root@n1 ~]# cobbler profile report -- name=CentOS7. 5-x86_64
  2. Name : CentOS7. 5-x86_64
  3. TFTP Boot Files : {}
  4. Comment :
  5. DHCP Tag : default
  6. Distribution : CentOS7. 5-x86_64
  7. Enable gPXE? : 0
  8. Enable PXE Menu? : 1
  9. Fetchable Files : {}
  10. Kernel Options : {}
  11. Kernel Options (Post Install) : {}
  12. Kickstart : / var/lib/cobbler/kickstarts/sample_end.ks
  13. Kickstart Metadata : {}
  14. Management Classes : []
  15. Management Parameters : <<inherit>>
  16. Name Servers : []
  17. Name Servers Search Path : []
  18. Owners : [ 'admin']
  19. Parent Profile :
  20. Internal proxy :
  21. Red Hat Management Key : <<inherit>>
  22. Red Hat Management Server : <<inherit>>
  23. Repos : []
  24. Server Override : <<inherit>>
  25. Template Files : {}
  26. Virt Auto Boot : 1
  27. Virt Bridge : xenbr0
  28. Virt CPUs : 1
  29. Virt Disk Driver Type : raw
  30. Virt File Size(GB) : 5
  31. Virt Path :
  32. Virt RAM (MB) : 512
  33. Virt Type : kvm

编辑profile,修改关联的ks文件(指定自定义ks文件)

[root@n1 ~]# cobbler profile edit --name=CentOS7.5-x86_64 --kickstart=/var/lib/cobbler/kickstarts/CentOS7.5-x86_64.ks

查看profile,发现Kickstart关联文件被修改

[root@n1 ~]# cobbler profile report --name=CentOS7.5-x86_64
Name                           : CentOS7.5-x86_64
TFTP Boot Files                : {}
Comment                        :
DHCP Tag                       : default
Distribution                   : CentOS7.5-x86_64
Enable gPXE?                   : 0
Enable PXE Menu?               : 1
Fetchable Files                : {}
Kernel Options                 : {}
Kernel Options (Post Install)  : {}
Kickstart                      : /var/lib/cobbler/kickstarts/CentOS7.5-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@n1 ~]# cobbler sync

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

修改PXE默认启动选项

默认情况下PXE启动的是Local(启动后会出现local和自定义CentOS7.5-x86_64两个选项,需手动,要无人工干涉,就需要修改)

修改前


 
 
  1. [root@n1 ~] # cd /var/lib/tftpboot/pxelinux.cfg/
  2. [root@n1 pxelinux.cfg] # cat default
  3. DEFAULT menu
  4. PROMPT 0
  5. MENU TITLE Cobbler | http: //cobbler.github.io/
  6. TIMEOUT 200
  7. TOTALTIMEOUT 6000
  8. ONTIMEOUT local
  9. LABEL local
  10. MENU LABEL ( local)
  11. MENU DEFAULT
  12. LOCALBOOT - 1
  13. LABEL CentOS7. 5-x86_64
  14. kernel /images/CentOS7. 5-x86_64/vmlinuz
  15. MENU LABEL CentOS7. 5-x86_64
  16. append initrd= /images/CentOS7. 5-x86_64/initrd.img ksdevice=bootif lang= kssendmac text ks=http: // 192.168. 231.60/cblr/svc/op/ks/profile/CentOS7. 5-x86_64
  17. ipappend 2
  18. MENU end

修改后


 
 
  1. [root@n1 pxelinux.cfg]# cat default
  2. DEFAULT menu
  3. PROMPT 0
  4. MENU TITLE Cobbler | http: //cobbler.github.io/
  5. TIMEOUT 200
  6. TOTALTIMEOUT 6000
  7. ONTIMEOUT CentOS7 .5-x86_64
  8. LABEL CentOS7 .5-x86_64
  9. kernel /images/CentOS7 .5-x86_64/vmlinuz
  10. MENU LABEL CentOS7 .5-x86_64
  11. append initrd=/images/CentOS7 .5-x86_64/initrd.img ksdevice=bootif lang= kssendmac text ks=http: //192.168.231.60/cblr/svc/op/ks/profile/CentOS7.5-x86_64
  12. ipappend 2
  13. MENU end

不用cobbler sync,否则会恢复原来的配置

在VMware上模拟测试Cobbler自动安装系统

图片.png

图片.png

图片.png

要设置足够的内存,否则会出现如下错误:

Snipaste_2019-04-13_08-52-35.png

图片.png

用root登录,密码为/etc/cobbler/settings中的default_password_crypted(使用没有加密的密码)

图片.png

 

参考:

https://blog.51cto.com/renjunjie622/1782190

https://www.cnblogs.com/kevingrace/p/5685480.html

https://blog.csdn.net/taiyang1987912/article/details/42176709

https://blog.csdn.net/yuanfangPOET/article/details/89279733

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值