Cobbler自动化部署
cobbler简介
- cobbler是一个linux服务器快速网络安装的服务,由python开发,小巧轻便,可以通过PXE方式来快速安装、重装物理服务器,同时还可以管理dhcp、DNS、TFTP、RSYNC以及yum仓库、构造系统镜像,也提供了web界面管理工具(cobbler-web),还提供了API接口,方便二次开发
- Cobbler是一个Linux服务器安装的服务,可以通过网络启动(PXE)的方式来快速安装、重装物理服务器和虚拟机,同时还可以管理DHCP,DNS等。
- Cobbler可以使用命令行方式管理,也提供了基于Web的界面管理工具(cobbler-web),还提供了API接口,可以方便二次开发使用。
- Cobbler是较早前的kickstart的升级版,优点是比较容易配置,还自带web界面比较易于管理。
- Cobbler内置了一个轻量级配置管理系统,但它也支持和其它配置管理系统集成,如Puppet。
参考
https://www.cnblogs.com/yanjieli/p/11016825.html
https://blog.csdn.net/openbox2008/article/details/80150199
https://wsgzao.github.io/post/kickstart/
https://anjia0532.github.io/2019/02/22/cobbler-win10-win-server-2019/
http://liaoph.com/linux-kickstart/
bbler集成的服务
- pxe服务
- DHCP服务
- DNS服务管理
- kickstart服务支持
- yum
- TFTP
- 电源管理
- apache
环境配置
# 关闭selinux
sudo sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
# 关闭防火墙
sudo systemctl stop firewalld.service && systemctl disable firewalld.service
安装cobbler
yum -y install epel-release
yum -y install cobbler cobbler-web tftp-server dhcp httpd xinetd
systemctl start httpd cobblerd
systemctl enable httpd cobblerd
查看安装后相关文件
[root@cobbler ~]# rpm -ql cobbler
/etc/cobbler # 配置文件目录
/etc/cobbler/settings # cobbler主配置文件,这个文件是YAML格式,Cobbler是python写的程序。
/etc/cobbler/dhcp.template # DHCP服务的配置模板
/etc/cobbler/tftpd.template # tftp服务的配置模板
/etc/cobbler/rsync.template # rsync服务的配置模板
/etc/cobbler/iso # iso模板配置文件目录
/etc/cobbler/pxe # pxe模板文件目录
/etc/cobbler/power # 电源的配置文件目录
/etc/cobbler/users.conf # Web服务授权配置文件
/etc/cobbler/users.digest # 用于web访问的用户名密码配置文件
/etc/cobbler/dnsmasq.template # DNS服务的配置模板
/etc/cobbler/modules.conf # Cobbler模块配置文件
/var/lib/cobbler # Cobbler数据目录
/var/lib/cobbler/config # 配置文件
/var/lib/cobbler/kickstarts # 默认存放kickstart文件
/var/lib/cobbler/loaders # 存放的各种引导程序
/var/www/cobbler # 系统安装镜像目录
/var/www/cobbler/ks_mirror # 导入的系统镜像列表
/var/www/cobbler/images # 导入的系统镜像启动文件
/var/www/cobbler/repo_mirror # yum源存储目录
/var/log/cobbler # 日志目录
/var/log/cobbler/install.log # 客户端系统安装日志
/var/log/cobbler/cobbler.log # cobbler日志
配置cobbler
[root@cobbler ~]# 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 : enable and start rsyncd.service with systemctl
6 : debmirror package is not installed, it will be required to manage debian deployments and repositories
7 : ksvalidator was not found, install pykickstart
8 : 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
9 : 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. server
[root@cobbler ~]# cobbler setting edit --name=server --value=192.168.2.128
2. next_server
[root@cobbler ~]# cobbler setting edit --name=next_server --value=192.168.2.128
3. tftp_server
[root@cobbler ~]# sed -ri '/disable/c\disable = no' /etc/xinetd.d/tftp
[root@cobbler ~]# systemctl enable xinetd
[root@cobbler ~]# systemctl restart xinetd
4. boot-loaders
[root@cobbler ~]# cobbler get-loaders
5. rsyncd
[root@cobbler ~]# systemctl start rsyncd
[root@cobbler ~]# systemctl enable rsyncd
6. debmirror [optional]
# 这个是可选项的,可以忽略。这里就忽略了
7. pykickstart
[root@cobbler ~]# yum -y install pykickstart
8. default_password_crypted #注意:这里设置的密码,也就是后面安装完系统的初始化登录密码
[root@cobbler ~]# openssl passwd -1 -salt `openssl rand -hex 4` 'admin'
$1$675f1d08$oJoAMVxdbdKHjQXbGqNTX0
[root@cobbler ~]# cobbler setting edit --name=default_password_crypted --value='$1$675f1d08$oJoAMVxdbdKHjQXbGqNTX0'
9. fencing tools [optional]
[root@cobbler ~]# yum -y install fence-agents
解决完成再次查看
[root@cobbler ~]# 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.
配置DHCP
[root@cobbler ~]# cobbler setting edit --name=manage_dhcp --value=1
# 修改cobbler的dhcp模块,不要直接修改dhcp本身的配置文件,因为cobbler会覆盖
[root@cobbler ~]# vim /etc/cobbler/dhcp.template
...
subnet 192.168.2.0 netmask 255.255.255.0 { #这里改为分配的网段和掩码
#option routers 192.168.1.5; #如果有网关,这里改为网关地址
#option domain-name-servers 192.168.1.1; #如果有DNS,这里改为DNS地址
option subnet-mask 255.255.255.0; #改为分配的IP的掩码
range dynamic-bootp 192.168.2.100 192.168.2.254; #改为分配的IP的范围
...
同步cobbler配置
[root@cobbler ~]# cobbler rsync
No such command: rsync
[root@cobbler ~]# cobbler sync
task started: 2019-06-12_152623_sync
task started (id=Sync, time=Wed Jun 12 15:26:23 2019)
running pre-sync triggers
cleaning trees
removing: /var/www/cobbler/images/centos6.9-x86_64
removing: /var/lib/tftpboot/pxelinux.cfg/default
removing: /var/lib/tftpboot/grub/images
removing: /var/lib/tftpboot/grub/grub-x86.efi
removing: /var/lib/tftpboot/grub/grub-x86_64.efi
removing: /var/lib/tftpboot/grub/efidefault
removing: /var/lib/tftpboot/images/centos6.9-x86_64
removing: /var/lib/tftpboot/s390x/profile_list
copying bootloaders
trying hardlink /var/lib/cobbler/loaders/grub-x86.efi -> /var/lib/tftpboot/grub/grub-x86.efi
trying hardlink /var/lib/cobbler/loaders/grub-x86_64.efi -> /var/lib/tftpboot/grub/grub-x86_64.efi
copying distros to tftpboot
copying files for distro: centos6.9-x86_64
trying hardlink /var/www/cobbler/ks_mirror/centos6.9-x86_64/images/pxeboot/vmlinuz -> /var/lib/tftpboot/images/centos6.9-x86_64/vmlinuz
trying hardlink /var/www/cobbler/ks_mirror/centos6.9-x86_64/images/pxeboot/initrd.img -> /var/lib/tftpboot/images/centos6.9-x86_64/initrd.img
copying images
generating PXE configuration files
generating PXE menu structure
copying files for distro: centos6.9-x86_64
trying hardlink /var/www/cobbler/ks_mirror/centos6.9-x86_64/images/pxeboot/vmlinuz -> /var/www/cobbler/images/centos6.9-x86_64/vmlinuz
trying hardlink /var/www/cobbler/ks_mirror/centos6.9-x86_64/images/pxeboot/initrd.img -> /var/www/cobbler/images/centos6.9-x86_64/initrd.img
Writing template files for centos6.9-x86_64
rendering DHCP files
generating /etc/dhcp/dhcpd.conf
rendering TFTPD files
generating /etc/xinetd.d/tftp
processing boot_files for distro: centos6.9-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 ***
cobbler命令帮助
命令 | 说明 |
---|---|
cobbler check | 核对当前设置是否有问题 |
cobbler list | 列出所有的cobbler元素 |
cobbler report | 列出元素的详细信息 |
cobbler sync | 同步配置到数据目录,更改配置最好都执行一下 |
cobbler reposync | 同步yum仓库 |
cobbler distro | 查看导入的发行版系统信息 |
cobbler system | 查看添加的系统信息 |
cobbler profile | 查看配置信息 |
cobbler配置安装centos6.x
-
- 创建挂载点,并进行挂载
[root@cobbler ~]# mkdir /centos6
[root@cobbler ~]# mount -o loop CentOS-6.9-x86_64-bin-DVD1.iso /centos6
- 2)查看挂载后的目录
[root@cobbler ~]# ls /centos6/
CentOS_BuildTag images repodata RPM-GPG-KEY-CentOS-Testing-6
EFI isolinux RPM-GPG-KEY-CentOS-6 TRANS.TBL
EULA Packages RPM-GPG-KEY-CentOS-Debug-6
GPL RELEASE-NOTES-en-US.html RPM-GPG-KEY-CentOS-Security-6
- 3)导入镜像
[root@cobbler ~]# cobbler import --path=/centos6 --name=centos6.9 --arch=x86_64
# --path 镜像路径
# --name 为安装源定义一个名字
# --arch 指定安装源是32位、64位、ia64, 目前支持的选项有: x86│x86_64│ia64
# 安装源的唯一标示就是根据name参数来定义,本例导入成功后,安装源的唯一标示就是:centos6.9,如果重复,系统会提示导入失败。
- 4)查看导入后镜像信息
[root@cobbler ~]# cobbler distro report --name=centos6.9-x86_64
Name : centos6.9-x86_64
Architecture : x86_64
TFTP Boot Files : {}
Breed : redhat
Comment :
Fetchable Files : {}
Initrd : /var/www/cobbler/ks_mirror/centos6.9-x86_64/images/pxeboot/initrd.img
Kernel : /var/www/cobbler/ks_mirror/centos6.9-x86_64/images/pxeboot/vmlinuz
Kernel Options : {}
Kernel Options (Post Install) : {}
Kickstart Metadata : {'tree': 'http://@@http_server@@/cblr/links/centos6.9-x86_64'}
Management Classes : []
OS Version : rhel6
Owners : ['admin']
Red Hat Management Key : <<inherit>>
Red Hat Management Server : <<inherit>>
Template Files : {}
- 5)查看profile信息
[root@cobbler ~]# cobbler profile report --name=centos6.9-x86_64
Name : centos6.9-x86_64
TFTP Boot Files : {}
Comment :
DHCP Tag : default
Distribution : centos6.9-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
- 6)copy一份profile文件(ks),进行修改
[root@cobbler ~]# cd /var/lib/cobbler/kickstarts/
[root@cobbler kickstarts]# ls
default.ks install_profiles sample_autoyast.xml sample_esxi4.ks sample.ks
esxi4-ks.cfg legacy.ks sample_end.ks sample_esxi5.ks sample_old.seed
esxi5-ks.cfg pxerescue.ks sample_esx4.ks sample_esxi6.ks sample.seed
[root@cobbler kickstarts]# cp sample_end.ks centos6.ks
# 编辑centos6的kickstart文件
[root@cobbler kickstarts]# vim centos6.ks
# This kickstart file should only be used with EL > 5 and/or Fedora > 7.
# For older versions please use the sample.ks kickstart file.
# Install OS instead of upgrade
install
# Use text mode install
text
# System keyboard
keyboard us
# System language
lang en_US
# System timezone
timezone Asia/ShangHai
#Root password
rootpw --iscrypted $default_password_crypted
# System authorization information
auth --useshadow --enablemd5
# Firewall configuration
firewall --disabled
# SELinux configuration
selinux --disabled
# Use network installation
url --url=$tree
# Clear the Master Boot Record
zerombr
# System bootloader configuration
bootloader --location=mbr
# Partition clearing information
clearpart --all --initlabel
part /boot --fstype=ext4 --size=500
part swap --fstype=swap --size=2048
part / --fstype=ext4 --grow --size=200
# If any cobbler repo definitions were referenced in the kickstart profile, include them here.
$yum_repo_stanza
# Network information
$SNIPPET('network_config')
# Do not configure the X Window System
skipx
# Run the Setup Agent on first boot
firstboot --disable
# Reboot after installation
reboot
%pre
$SNIPPET('log_ks_pre')
$SNIPPET('kickstart_start')
$SNIPPET('pre_install_network_config')
# Enable installation monitoring
$SNIPPET('pre_anamon')
%end
%packages
$SNIPPET('func_install_if_enabled')
@core
@base
tree
nmap
wget
lftp
lrzsz
telnet
%end
%post --nochroot
$SNIPPET('log_ks_post_nochroot')
%end
%post
$SNIPPET('log_ks_post')
# Start yum configuration
$yum_config_stanza
# End yum configuration
$SNIPPET('post_install_kernel_options')
$SNIPPET('post_install_network_config')
$SNIPPET('func_register_if_enabled')
$SNIPPET('download_config_files')
$SNIPPET('koan_environment')
$SNIPPET('redhat_register')
$SNIPPET('cobbler_register')
# Enable post-install boot notification
$SNIPPET('post_anamon')
# Start final steps
$SNIPPET('kickstart_done')
# End final steps
sed -ri "/^#UseDNS/c\UseDNS no" /etc/ssh/sshd_config
sed -ri "/^GSSAPIAuthentication/c\GSSAPIAuthentication no" /etc/ssh/sshd_config
%end
# 基于 Kickstart 自动化安装 CentOS 实践 - https://wsgzao.github.io/post/kickstart/
#Kickstart file automatically generated by anaconda.
#version=DEVEL
#Install OS instead of upgrade
# 表示是安装,而不是升级
install
#Use text mode install
# 文本方式安装
text
#Use network installation
# 使用网络安装
#url --url=ftp://ip/centos
#Local installation Use CDROM installation media
# 使用光盘安装
cdrom
#Installation Number configuration
# 如果是 RedHat 的系统,会要求输入 key,这里配置为跳过,如果不配置安装时会停在那里要求用户输入 key
#key –skip
#System language
# 语言环境
#lang en_US.UTF-8
lang zh_CN.UTF-8
#System keyboard
# 键盘类型
keyboard us
#Network information
# 网络配置
#network --device eth0 --bootproto dhcp --onboot yes
#Root password
#root 密码
rootpw chinaums
#Firewall configuration
# 禁用防火墙
firewall --disabled
#SELinux configuration
# 禁用 selinux
selinux --disabled
#Run the Setup Agent on first boot
# 禁用第一次启动时设置系统的向导
firstboot --disable
#System authorization information
# 用户认证配置,useshadow 表示使用本地认证,--passalgo 表示密码加密算法
authconfig --enableshadow --passalgo=sha512
#System timezone
# 设置时区为上海
timezone --isUtc Asia/Shanghai
#System bootloader configuration
# 指明 bootloader 的安装位置,指明驱动器的排序,指明操作系统安装完成之后,向内核传递的参数
bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"
#Clear the Master Boot Record
# 清除 MBR 引导记录
zerombr yes
#Partition clearing information
# 清除硬盘上的所有数据
clearpart --all --initlabel
#Disk partitioning information
# 自定义分区
# 创建一个 200M 大小的分区挂载 / boot 类型为 ext4
part /boot --fstype=ext4 --size=200 --ondisk=sda
# 创建一个 20000M 大小的 SWAP 分区
part swap --size=20000 --ondisk=sda
# 创建 / 目录
part / --fstype=ext4 --grow --size=1 --ondisk=sda
#Reboot after installation
# 设置完成之后重启
reboot --eject
#This packages is for CentOS 6.4
# 为 CentOS 6.4 定制的软件包
%packages
@base
@core
@chinese-support
@server-policy
telnet
# 增加安装后运行脚本
%post
#config service
# 自定义服务
service NetworkManager stop
chkconfig NetworkManager off
service network restart
#eject cdrom
# 安装完成弹出光碟
#eject
#reboot
# 执行完毕后重启
#reboot -f
# 结束自动化部署
%end
- 7)编辑centos6镜像所使用的kickstart文件
# 动态编辑指定使用新的kickstart文件
[root@cobbler ~]# cobbler profile edit --name=centos6.9-x86_64 --kickstart=/var/lib/cobbler/kickstarts/centos6.ks
# 验证是否更改成功
[root@cobbler ~]# cobbler profile report --name=centos6.9-x86_64 |grep Kickstart
Kickstart : /var/lib/cobbler/kickstarts/centos6.ks
- 8)再次同步cobbler配置
cobbler sync
- 9)新建虚拟机进行测试
自动化安装
网卡改名eth0
# 修改安装系统的内核参数,在CentOS7系统有一个地方变了,就是网卡名变成eno16777736这种形式,但是为了运维标准化,我们需要将它变成我们常用的eth0,因此使用下面的参数。
但要注意是CentOS7才需要下面的步骤,CentOS6不需要。
# cobbler profile edit --name=CentOS-7-x86_64 --kopts='net.ifnames=0 biosdevname=0'
通过服务器上的MAC地址,进行定制安装 ,编写下面的命令
cobbler system –help 查看命令
usage
=====
cobbler system add
cobbler system copy
cobbler system dumpvars
cobbler system edit
cobbler system find
cobbler system getks
cobbler system list
cobbler system poweroff
cobbler system poweron
cobbler system powerstatus
cobbler system reboot
cobbler system remove
cobbler system rename
cobbler system report
修改vim /etc/cobbler/settings 文件
#修改以下几项
default_password_crypted: "$1$cobbler$M6SE55xZodWc9.vAKLJs6." #root密码,用openssl passwd -1 -salt 'cobbler' 'cobbler' 生成
manage_dhcp: 1 #由0改为1,表示由cobbler管理dhcp
next_server: 192.168.128.141 #cobbler管理dhcp,指定下一个要访问的服务地址
server: 192.168.128.141 #指定cobbler服务器地址
pxe_just_once: 1 #防止循环安装
** 添加mac自动安装**
cobbler system add --name=51.22 --mac=20:04:0f:ea:b1:f4 --profile=centos7.6-x86_64 --ip-address=172.20.51.22 --subnet=255.255.128.0 --gateway=172.20.1.1 --interface=eth0 --static=1 --hostname=zetyun --name-servers="114.114.114.114"
- –name 自定义,但不能重复
- –profile 指定要安装的镜像文件,可以用cobbler list查看一下
- –mac 要安装服务器的MAC地址
- –kickstart 指定要使用的kickstart文件,kickstart文件中还可以指定yum源
其它是网络参数,可要可不要
查看定义的列表
# [root@localhost ks_mirror]# cobbler system list
web_centos6.5
# cobbler sync