1. 程式人生 > >Cobbler-自動化部署神器

Cobbler-自動化部署神器

cobbler-自動化部署神器

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 -r
2.6.32-696.el6.x86_64     #內核
[root@Cobbler ~]# cat /etc/selinux/config 
SELINUX=disabled     #關閉selinux
[root@Cobbler ~]# service iptables stop    #關閉防火墻
[root@Cobbler ~]# hostname
Cobbler    #查看主機名
[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 -y
cobbler               #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 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 : change 'disable' to 'no' in /etc/xinetd.d/rsync
6 : file /etc/xinetd.d/rsync does not exist
7 : debmirror package is not installed, it will be required to manage debian deployments and repositories
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.修改/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:cobbler
case $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-6
EFI              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  --enablemd5
authconfig --enableshadow --passalgo=sha512
# System bootloader configuration
bootloader --location=mbr --driveorder=sda --append="nomodeset crashkernel=auto rhgb quiet"
# Partition clearing information
clearpart --all --initlabel
# Use text mode install
text
# Firewall configuration
firewall --disabled
# Run the Setup Agent on first boot
firstboot --disable
# System keyboard
keyboard us
# System language
lang en_US
# Use network installation
url --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 installation
reboot
logging --level=info
 
#Root password
rootpw --iscrypted $default_password_crypted
# SELinux configuration
selinux --disabled
# Do not configure the X Window System
skipx
# System timezone
timezone  Asia/Shanghai
# Install OS instead of upgrade
install
# Clear the Master Boot Record
zerombr
# Allow anaconda to partition the system as needed
#autopart
part /boot --fstype=ext4 --asprimary --size=200
part swap --asprimary --size=1024
part / --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-policy
sgpio
device-mapper-persistent-data
systemtap-client
tree
lrzsz
telnet
nmap
dos2unix
%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_64
profiles:
   CentOS-6.9-x86_64
systems:
repos:
images:
mgmtclasses:
packages:
files:


查看安裝鏡像文件信息

[root@Cobbler kickstarts]# cobbler profile report -name=CentOS-6.9-x86_64  
Name                           : CentOS-6.9-x86_64
TFTP Boot Files                : {}
Comment                        : 
DHCP Tag                       : default
Distribution                   : CentOS-6.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


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_64
TFTP Boot Files                : {}
Comment                        : 
DHCP Tag                       : default
Distribution                   : CentOS-6.9-x86_64
Enable gPXE?                   : 0
Enable PXE Menu?               : 1
Fetchable 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網段內的

技術分享圖片



實驗成功,搞了兩個多小時,睡覺。。。。



Cobbler-自動化部署神器