Cobbler-自动化部署神器
Cobbler介绍
Cobbler是一个Linux服务器快速网络安装的服务,而且在经过调整也可以支持网络安装windows。
该工具使用python开发,小巧轻便(才15k行python代码),可以通过网络启动(PXE)的方式来快速安装、重装物理服务器和虚拟机,同时还可以管理DHCP,DNS,TFTP、RSYNC以及yum仓库、构造系统ISO镜像。
Cobbler可以使用命令行方式管理,也提供了基于Web的界面管理工具(cobbler-web),还提供了API接口,可以方便二次开发使用。
Cobbler是较早前的kickstart的升级版,优点是比较容易配置,还自带web界面比较易于管理。
Cobbler内置了一个轻量级配置管理系统,但它也支持和其它配置管理系统集成,如Puppet,暂时不支持SaltStack。
Cobbler客户端Koan支持虚拟机安装和操作系统重新安装,使重装系统更便捷。
Cobbler用处
使用Cobbler,您无需进行人工干预即可安装机器。Cobbler设置一个PXE引导环境(它还可以使用yaboot支持PowerPC),并 控制与安装相关的所有方面,比如网络引导服务(DHCP和TFTP)与存储库镜像。当希望安装一台新机器时,Cobbler可以:
1)使用一个以前定义的模板来配置DHCP服务(如果启用了管理DHCP)。
2)将一个存储库(yum或rsync)建立镜像或解压缩一个媒介,以注册一个新操作系统。
3)在DHCP配置文件中为需要安装的机器创建一个条目,并使用指定的参数(IP和MAC)。
4)在TFTP服务目录下创建适当的PXE文件。
5)重新启动DHCP服务来反应新的更改。
6)重新启动机器以开始安装(如果电源管理已启动)。
实验环境(Centos6)
IP:10.0.0.146
1.基础环境准备
[root@Cobbler ~]# cat /etc/redhat-release CentOS release 6.9 (Final) #系统版本[root@Cobbler ~]# uname -r2.6.32-696.el6.x86_64 #内核[root@Cobbler ~]# cat /etc/selinux/config SELINUX=disabled #关闭selinux[root@Cobbler ~]# service iptables stop #关闭防火墙[root@Cobbler ~]# hostnameCobbler #查看主机名[root@Cobbler yum.repos.d]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo #cobbler使用的epel源
2.安装Cobbler,及所需要的依赖包
[root@Cobbler yum.repos.d]# yum install cobbler pykickstart httpd dhcp tftp-server -ycobbler #cobbler程序包cobbler-web #cobbler的web服务包(二次开发,可不用安装)pykickstart #cobbler检查kickstart语法错误httpd #Apache web服务dhcp #dhcp服务tftp-server #tftp服务
3.启动httpd,cobbler
cobbler的运行依赖于dhcp、tftp、rsync及dns服务,其中dhcp可由dhcpd(isc)提供,也可由dnsmasq提供;tftp可由tftp-server程序包提供,也可由cobbler功能提供,rsync有rsync程序包提供,dns可由bind提供,也可由dnsmasq提供
[root@Cobbler ~]# service httpd start #报错httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName[root@Cobbler ~]# vim /etc/httpd/conf/httpd.conf #添加以下一行ServerName localhost:80[root@Cobbler ~]# service httpd restart[root@Cobbler ~]# service cobblerd start
4.用cobbler check检查是否有错,并一一解决
[root@Cobbler ~]# cobbler checkThe 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/tftp4 : 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 : change 'disable' to 'no' in /etc/xinetd.d/rsync6 : file /etc/xinetd.d/rsync does not exist7 : debmirror package is not installed, it will be required to manage debian deployments and repositories8 : 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 one9 : fencing tools were not found, and are required to use the (optional) power management features. install cman or fence-agents to use themRestart cobblerd and then run 'cobbler sync' to apply changes.
-------------------------------------------------------------------------------------------------------------------------------------------------
解决:
1.修改/etc/cobbler/settings文件中的server参数的值为提供cobbler服务的主机相应的IP地址或主机名.
[root@Cobbler ~]# vim /etc/cobbler/settings server: 10.0.0.146
2.修改/etc/cobbler/settings文件中的next_server参数的值为提供PXE服务的主机相应的IP地址
[root@Cobbler ~]# vim /etc/cobbler/settings next_server: 10.0.0.146
3.修改/etc/xinetd.d/tftp文件中的disable参数修改为 disable = no
[root@Cobbler ~]# vim /etc/xinetd.d/tftp disable = no
4、执行 cobbler get-loaders 命令即可;否则,需要安装syslinux程序包,而后复制/usr/share/syslinux/{pxelinux.0,memu.c32}等文件至/var/lib/cobbler/loaders/目录中
[root@Cobbler ~]# cobbler get-loaders
5.修改etc/xinetd.d/rsync 文件中的disable参数修改为 disable = no
[root@Cobbler ~]# vim /etc/xinetd.d/rsync disable = no
6.实际上这个文件是有的,无视这个错误
7.安装debmirror后,再cobbler check会有以下报错
[root@Cobbler ~]# yum install -y debmirror comment out 'dists' on /etc/debmirror.conf for proper debian support comment out 'arches' on /etc/debmirror.conf for proper debian support
解决方法:注释这两行
#@dists="sid";#@arches="i386"
8.生成密码来取代默认的密码,更安全,#据提示your-password-here,这里是自己的密码,random-phrase-here,这里是随机的干扰码
[root@Cobbler ~]# openssl passwd -1 -salt 'cobbler' '123456'$1$cobbler$sqDDOBeLKJVmxTCZr52/11[root@Cobbler ~]# vim /etc/cobbler/settings default_password_crypted: "$1$cobbler$sqDDOBeLKJVmxTCZr52/11"
9.安装cman fence-agents
[root@Cobbler ~]# yum install -y cman ence-agents
-------------------------------------------------------------------------------------------------------------------------------------------------
5.修改完后,记得重启服务
[root@Cobbler ~]# /etc/init.d/httpd restart[root@Cobbler ~]# /etc/init.d/xinetd restart[root@Cobbler ~]# /etc/init.d/cobblerd restart
6.再核查一次
[root@Cobbler ~]# cobbler check 1 : file /etc/xinetd.d/rsync does not exist #可忽略Restart cobblerd and then run 'cobbler sync' to apply changes.
7.配置DHCP,修改cobbler的dhcp模版,这个模板会覆盖dhcp本身的配置文件
[root@Cobbler ~]# vim /etc/cobbler/dhcp.template ddns-update-style interim;allow booting;allow bootp;ignore client-updates;set vendorclass = option vendor-class-identifier;option pxe-system-type code 93 = unsigned integer 16;subnet 10.0.0.0 netmask 255.255.255.0 { option routers 10.0.0.1; option domain-name-servers 202.96.128.86; option subnet-mask 255.255.255.0; range dynamic-bootp 10.0.0.150 10.0.0.200; default-lease-time 21600; max-lease-time 43200; next-server 10.0.0.146; class "pxeclients" { match if substring (option vendor-class-identifier, 0, 9) = "PXEClient"; if option pxe-system-type = 00:02 { filename "ia64/elilo.efi"; } else if option pxe-system-type = 00:06 { filename "grub/grub-x86.efi"; } else if option pxe-system-type = 00:07 { filename "grub/grub-x86_64.efi"; } else { filename "pxelinux.0"; } }}
8.同步cobbler的配置
[root@Cobbler ~]# cobbler sync
9.编写cobbler的启动脚本,方便管理
[root@Cobbler init.d]# vim /etc/init.d/cobbler#!/bin/bash# chkconfig: 345 80 90# description:cobblercase $1 in start) /etc/init.d/httpd start /etc/init.d/xinetd start /etc/init.d/dhcpd start /etc/init.d/cobblerd start ;; stop) /etc/init.d/httpd stop /etc/init.d/xinetd stop /etc/init.d/dhcpd stop /etc/init.d/cobblerd stop ;; restart) /etc/init.d/httpd restart /etc/init.d/xinetd restart /etc/init.d/dhcpd restart /etc/init.d/cobblerd restart ;; status) /etc/init.d/httpd status /etc/init.d/xinetd status /etc/init.d/dhcpd status /etc/init.d/cobblerd status ;; sync) cobbler sync ;; *) echo "Input error,please in put 'start|stop|restart|status|sync'!" exit 2 ;;esac
10.cobbler的一些帮助命令
cobbler check 核对当前设置是否有问题
cobbler list 列出所有的cobbler元素cobbler
report 列出元素的详细信息
cobbler sync 同步配置到数据目录,更改配置最好都要执行下
cobbler reposync 同步yum仓库
cobbler distro 查看导入的发行版系统信息
cobbler system 查看添加的系统信息cobbler
profile 查看配置信息
11.挂载镜像并导入镜像
[root@Cobbler ~]# mount /dev/cdrom /mnt[root@Cobbler ~]# cobbler import --path=/mnt/ --name=CentOS-6.9-x86_64 --arch=x86_64
# --path 镜像路径
# --name 为安装源定义一个名字
# --arch 指定安装源是32位、64位、ia64, 目前支持的选项有: x86│x86_64│ia64
# 安装源的唯一标示就是根据name参数来定义,本例导入成功后,安装源的唯一标示就是:CentOS-6.9-x86_64,如果重复,系统会提示导入失败。
12.查看镜像列表
[root@Cobbler ~]# cobbler distro list CentOS-6.9-x86_64
13.镜像存放目录,cobbler会将镜像中的所有安装文件自动拷贝到本地一份,放在/var/www/cobbler/ks_mirror下的CentOS-6.9-x86_64目录下。因此/var/www/cobbler目录必须具有足够容纳安装文件的空间。
[root@Cobbler ~]# ls /var/www/cobbler/ks_mirror/CentOS-6.9-x86_64/CentOS_BuildTag EULA images Packages repodata RPM-GPG-KEY-CentOS-Debug-6 RPM-GPG-KEY-CentOS-Testing-6EFI GPL isolinux RELEASE-NOTES-en-US.html RPM-GPG-KEY-CentOS-6 RPM-GPG-KEY-CentOS-Security-6 TRANS.TBL
14.配置ks.cfg
[root@Cobbler kickstarts]# cat CentOS-6.9-x86_64.cfg # kickstart template for Fedora 8 and later.# (includes %end blocks)# do not use with earlier distros #platform=x86, AMD64, or Intel EM64T# System authorization information#auth --useshadow --enablemd5authconfig --enableshadow --passalgo=sha512# System bootloader configurationbootloader --location=mbr --driveorder=sda --append="nomodeset crashkernel=auto rhgb quiet"# Partition clearing informationclearpart --all --initlabel# Use text mode installtext# Firewall configurationfirewall --disabled# Run the Setup Agent on first bootfirstboot --disable# System keyboardkeyboard us# System languagelang en_US# Use network installationurl --url=$tree# If any cobbler repo definitions were referenced in the kickstart profile, include them here.$yum_repo_stanza# Network information$SNIPPET('network_config')# Reboot after installationrebootlogging --level=info #Root passwordrootpw --iscrypted $default_password_crypted# SELinux configurationselinux --disabled# Do not configure the X Window Systemskipx# System timezonetimezone Asia/Shanghai# Install OS instead of upgradeinstall# Clear the Master Boot Recordzerombr# Allow anaconda to partition the system as needed#autopartpart /boot --fstype=ext4 --asprimary --size=200part swap --asprimary --size=1024part / --fstype=ext4 --grow --asprimary --size=200 %pre$SNIPPET('log_ks_pre')$SNIPPET('kickstart_start')$SNIPPET('pre_install_network_config')# Enable installation monitoring$SNIPPET('pre_anamon')%end %packages@base@compat-libraries@core@debugging@development@dial-up@hardware-monitoring@performance@server-policysgpiodevice-mapper-persistent-datasystemtap-clienttreelrzsztelnetnmapdos2unix%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%end
15.在第一次导入系统镜像后,Cobbler会给镜像指定一个默认的kickstart自动安装文件在/var/lib/cobbler/kickstarts下的sample_end.ks
查看列表信息
[root@Cobbler kickstarts]# cobbler list distros: CentOS-6.9-x86_64profiles: CentOS-6.9-x86_64systems:repos:images:mgmtclasses:packages:files:
查看安装镜像文件信息
[root@Cobbler kickstarts]# cobbler profile report -name=CentOS-6.9-x86_64 Name : CentOS-6.9-x86_64TFTP Boot Files : {}Comment : DHCP Tag : defaultDistribution : CentOS-6.9-x86_64Enable gPXE? : 0Enable PXE Menu? : 1Fetchable Files : {}Kernel Options : {}Kernel Options (Post Install) : {}Kickstart : /var/lib/cobbler/kickstarts/sample_end.ks
16.编辑profile,修改关联的ks文件
[root@Cobbler kickstarts]# cobbler profile edit --name=CentOS-6.9-x86_64 --kickstart=/var/lib/cobbler/kickstarts/CentOS-6.9-x86_64.cfg
可以看到下面Kickstart那里的配置cfg文件地址被改变了
[root@Cobbler kickstarts]# cobbler profile report --name=CentOS-6.9-x86_64 Name : CentOS-6.9-x86_64TFTP Boot Files : {}Comment : DHCP Tag : defaultDistribution : CentOS-6.9-x86_64Enable gPXE? : 0Enable PXE Menu? : 1Fetchable Files : {}Kernel Options : {}Kernel Options (Post Install) : {}Kickstart : /var/lib/cobbler/kickstarts/CentOS-6.9-x86_64.cfg
17.同步下cobbler数据,每次修改完都要镜像同步
[root@Cobbler kickstarts]# cobbler sync
18.最后一步,看个人意愿,开机画面显示
[root@Cobbler kickstarts]# vim /etc/cobbler/pxe/pxedefault.template MENU TITLE Cobbler | Welcome to Cobbler
19.修改配置要同步
[root@Cobbler kickstarts]# cobbler sync
20.接下来就可以部署了,新建一台新的虚拟机,记得把虚拟编辑器里的自动获取ip去掉让虚拟机获取Cobbler提供的dhcp网段。。
可以看到获取的ip是dhcp网段内的
实验成功,搞了两个多小时,睡觉。。。。