天天看點

使用cobbler自動化部署系統

一、Cobbler簡介

Red Hat 最新(Cobbler項目最初在2008年左右釋出)釋出了網絡安裝伺服器套件 Cobbler(補鞋匠),它已将 Linux 網絡安裝的技術門檻,從大專以上文化水準,成功降低到國中以下,連補鞋匠都能學會。I am just joking!

二、Cobbler功能特性

使用 Cobbler,您無需進行人工幹預即可安裝機器。Cobbler 設定一個 PXE 引導環境(它還可使用 yaboot 支援 PowerPC),并控制與安裝相關的所有方面,比如網絡引導服務(DHCP 和 TFTP)與存儲庫鏡像。當希望安裝一台新機器時,Cobbler 可以:

使用一個以前定義的模闆來配置 DHCP 服務(如果啟用了管理 DHCP)
将一個存儲庫(yum 或 rsync)建立鏡像或解壓縮一個媒介,以注冊一個新作業系統
在 DHCP 配置檔案中為需要安裝的機器建立一個條目,并使用您指定的參數(IP 和 MAC 位址)
在 TFTFP 服務目錄下建立适當的 PXE 檔案
重新啟動 DHCP 服務以反映更改
重新啟動機器以開始安裝(如果電源管理已啟用)      

三、Cobbler安裝

1.環境準備

[root@linux-node2 ~]# getenforce #注意關閉Selinux
Disabled
[root@linux-node2 ~]# systemctl status firewalld  #關閉防火牆
   firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
[root@linux-node2 ~]# ifconfig eth0|awk -F '[ :]+' 'NR==2 {print $3}'
192.168.56.12
#centos的網卡命名規則已改,為了标準化,把網卡名統一改為eth0,後面還要對核心參數做修改。      

更新yum源

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo      

2.安裝Cobbler

yum install -y httpd dhcp tftp cobbler cobbler-web pykickstart
cobbler			#cobbler程式包
cobbler-web		#cobbler的web服務包
pykickstart 	#cobbler檢查kickstart文法錯誤
httpd			#Apache web服務
tftp			#tftp服務
dhcp			#dhcp服務      

3.檢測Cobbler

cobbler的運作依賴tftp、dhcp、rsync等服務 cobbler也可以自己管理這些服務,在/etc/cobbler/settings檔案進行配置即可。

[root@linux-node2 ~]# systemctl start httpd
[root@linux-node2 ~]# systemctl start cobblerd      

執行cobbler check

[root@linux-node2 ~]# 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.
解決方法:修改/etc/cobbler/settings,将server對應的IP改為cobbler安裝主機的IP,即将server: 127.0.0.1改成server: 192.168.56.12,切記切記,server: 後面有空格!!
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.
解決方法:修改/etc/cobbler/settings,将next_server對應的IP改為cobbler安裝主機的IP,即将server: 127.0.0.1改成server: 192.168.56.12
3 : change 'disable' to 'no' in /etc/xinetd.d/tftp
解決方法:将/etc/xinetd.d/tftp中disable對應值改成no
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.
解決方法:執行cobbler get-loaders
5 : enable and start rsyncd.service with systemctl
解決方法:執行systemctl enable rsyncd;systemctl start rsyncd
6 : debmirror package is not installed, it will be required to manage debian deployments and repositories
解決方法:大便的包,可以不下載下傳
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
解決方法:給密碼加鹽,把生成的密碼串添加到/etc/cobbler/settings裡,切記切記,一定要全盤複制,連黑點也别放過。
[root@linux-node2 ~]# openssl passwd -1 -salt 'cobbler' 'cobbler'
$1$cobbler$M6SE55xZodWc9.vAKLJs6.
[root@linux-node2 ~]# vi /etc/cobbler/settings   #修改default_password_crypted: 注意空格
default_password_crypted: "$1$cobbler$M6SE55xZodWc9.vAKLJs6."
8 : fencing tools were not found, and are required to use the (optional) power management features. install cman or fence-agents to use them
解決辦法:電源管理工具,下載下傳fence-agents
yum -y install fence-agents
Restart cobblerd and then run 'cobbler sync' to apply changes.      

4.配置DHCP

cobbler可以管理dhcp服務,隻需在配置檔案中啟用此項 将/etc/cobbler/settings内manage_dhcp置為1即可 配置dhcp模版

[root@linux-node2 ~]# cat /etc/cobbler/dhcp.template
subnet 192.168.56.0 netmask 255.255.255.0 {
     option routers             192.168.56.2;
     option domain-name-servers 192.168.56.2;
     option subnet-mask         255.255.255.0;
     range dynamic-bootp        192.168.56.110 192.168.56.250;
     default-lease-time         21600;
     max-lease-time             43200;
     next-server                $next_server;      

5.同步cobbler

[root@linux-node2 ~]# yum install xinetd
[root@linux-node2 ~]# systemctl restart xinetd      
[root@linux-node2 ~]# cobbler sync
task started: 2016-05-19_102447_sync
task started (id=Sync, time=Thu May 19 10:24:47 2016)
running pre-sync triggers
cleaning trees
removing: /var/lib/tftpboot/grub/p_w_picpaths
copying bootloaders
trying hardlink /var/lib/cobbler/loaders/pxelinux.0 -> /var/lib/tftpboot/pxelinux.0
trying hardlink /var/lib/cobbler/loaders/menu.c32 -> /var/lib/tftpboot/menu.c32
trying hardlink /var/lib/cobbler/loaders/yaboot -> /var/lib/tftpboot/yaboot
trying hardlink /usr/share/syslinux/memdisk -> /var/lib/tftpboot/memdisk
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 p_w_picpaths
generating PXE configuration files
generating PXE menu structure
rendering DHCP files
generating /etc/dhcp/dhcpd.conf
rendering TFTPD files
generating /etc/xinetd.d/tftp
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.scm_track
running shell triggers from /var/lib/cobbler/triggers/change/*
*** TASK COMPLETE ***      

四、配置作業系統安裝源

1.管理distro

挂載鏡像,使用import導入到cobbler

[root@linux-node2 ~]# mount /dev/cdrom /mnt
mount: /dev/sr0 is write-protected, mounting read-only      
#--name 為安裝源定義一個名字CentOS-7-x86_64
#--arch 指定安裝源是32位、64位、ia64, 目前支援的選項有: x86│x86_64│ia64
# 安裝源的唯一标示就是根據name參數來定義,本例導入成功後,安裝源的唯一标示就是:CentOS-7-x86_64。
# 鏡像存放目錄,cobbler會将鏡像中的所有安裝檔案拷貝到本地一份,放在/var/www/cobbler/ks_mirror下的CentOS-7-x86_64目錄下。是以/var/www/cobbler目錄必須具有足夠容納安裝檔案的空間。
[root@linux-node2 ~]# cobbler import --path=/mnt/ --name=CentOS-6-x86_64 --arch=x86_64      
檢視所有的distro和profile
[root@linux-node2 ~]# cobbler profile list
   CentOS-7-x86_64
[root@linux-node2 ~]# cobbler distro list
   CentOS-7-x86_64
[root@linux-node2 ~]#      

2.管理profile

cobbler可以通過提供kiskstart檔案生成一個特定的系統安裝配置檔案

修改核心參數
[root@linux-node2 kickstarts]#cobbler profile edit --name=CentOS-7-x86_64 --kopts='net.ifnames=0 biosdevname=0'

[root@linux-node2 kickstarts]# cobbler profile edit --name=CentOS-7-x86_64 --kickstart=/var/lib/cobbler/kickstarts/CentOS-7-x86_64.cfg
#CentOS-7-x86_64.cfg内容為定制的系統的各種參數
添加CentOS-7-x86_64.cfg後執行同步
[root@linux-node2 kickstarts]# cobbler reposync
task started: 2016-05-19_110002_reposync
task started (id=Reposync, time=Thu May 19 11:00:02 2016)
hello, reposync
run, reposync, run!
*** TASK COMPLETE ***      

五、部署作業系統

建立一台虛拟機,注意在選擇作業系統位數時選擇Centos64位的作業系統。 建立後,打開電源,如果能看到選擇distro的界面,基本上問題不大。 但是,還需要注意,将vmware WorkStation的dhcp功能關掉。 另外,一個網段内有兩個cobbler伺服器,那麼有可能因dhcp沖突而安裝失敗,這種情況下,最好在dhcp.temple内設定兩個網段。

接下來該定制化安裝一個作業系統了

六、自動化安裝作業系統

伺服器裝機流程
1.伺服器采購
2.伺服器驗收并設定raid
3.服務商提供驗收單,運維驗收負責人簽字
4.伺服器上架
5.資産錄入
6.自動化安裝      

自動化安裝步驟

1.将新伺服器劃入裝機vlan
2.根據資産清單上的mac位址,自定義安裝
	A、機房
	B、機房區域
	C、機櫃
	D、伺服器位置
	E、伺服器網線接入端口
	F、該端口mac位址
	G、profile 作業系統 分區等 預配置設定的IP位址 主機名 子網 網關 dns 角色
3.自動化裝機平台,安裝
	建立一台虛拟機
	設定如下
	00:50:56:3A:07:6F
	linux-node3.oldboyedu.com
	255.255.255.0
	192.168.56.2
	192.168.56.2
添加一台主機指定mac(虛拟機可以生成一個mac位址),IP,主機名
cobbler system add --name=linux-node3.oldboyedu.com --mac=00:50:56:3A:07:6F --profile=CentOS-7-x86_64 \
--ip-address=192.168.56.120 --subnet=255.255.255.0 --gateway=192.168.56.2 --interface=eth0 \
--static=1 --hostname=linux-node3.oldboyedu.com --name-servers="192.168.56.2" \
--kickstart=/var/lib/cobbler/kickstarts/CentOS-7-x86_64.cfg
執行cobbler system list檢視添加的system
[root@linux-node2 kickstarts]# cobbler system list
   linux-node3.oldboyedu.com
注意:一定要執行cobbler sync進行同步配置,否則不生效
通過cat /etc/dhcp/dhcpd.conf可以檢視到配置資訊已添加
group {
    host generic1 {
        hardware ethernet 00:50:56:3A:07:6F;
        fixed-address 192.168.56.120;
        option host-name "linux-node3.oldboyedu.com";
        option subnet-mask 255.255.255.0;
        option routers 192.168.56.2;
        filename "/pxelinux.0";
        next-server 192.168.56.12;
    }
}
最後,啟動剛才建立的虛拟機,無需任何人工幹預,即可啪啪啪裝機了。      

七、cobbler的API

API定義檔案所在目錄

/etc/httpd/conf.d/,此目錄下的檔案
[root@linux-node2 conf.d]# ll
total 36
-rw-r--r-- 1 root root 2926 May 12 18:27 autoindex.conf
-rw-r--r-- 1 root root 1087 Jan 24 22:40 cobbler.conf
-rw-r--r-- 1 root root 1165 Jan 24 22:40 cobbler_web.conf
-rw-r--r-- 1 root root  366 May 12 18:28 README
-rw-r--r-- 1 root root 9438 May 12 18:16 ssl.conf
-rw-r--r-- 1 root root 1252 May 12 18:16 userdir.conf
-rw-r--r-- 1 root root  824 May 12 18:16 welcome.conf
API:
ProxyPass /cobbler_api http://localhost:25151/
ProxyPassReverse /cobbler_api http://localhost:25151/      

通過編寫python來自動化安裝系統

#!/usr/bin/env python 
# -*- coding: utf-8 -*-
import xmlrpclib 

class CobblerAPI(object):
    def __init__(self,url,user,password):
        self.cobbler_user= user
        self.cobbler_pass = password
        self.cobbler_url = url
    
    def add_system(self,hostname,ip_add,mac_add,profile):
        '''
        Add Cobbler System Infomation
        '''
        ret = {
            "result": True,
            "comment": [],
        }
        #get token
        remote = xmlrpclib.Server(self.cobbler_url) 
        token = remote.login(self.cobbler_user,self.cobbler_pass) 
		
		#add system
        system_id = remote.new_system(token) 
        remote.modify_system(system_id,"name",hostname,token) 
        remote.modify_system(system_id,"hostname",hostname,token) 
        remote.modify_system(system_id,'modify_interface', { 
            "macaddress-eth0" : mac_add, 
            "ipaddress-eth0" : ip_add, 
            "dnsname-eth0" : hostname, 
        }, token) 
        remote.modify_system(system_id,"profile",profile,token) 
        remote.save_system(system_id, token) 
        try:
            remote.sync(token)
        except Exception as e:
            ret['result'] = False
            ret['comment'].append(str(e))
        return ret

def main():
    cobbler = CobblerAPI("http://192.168.56.12/cobbler_api","cobbler","cobbler")
    ret = cobbler.add_system(hostname='cobbler-api-test',ip_add='192.168.56.121',mac_add='00:50:56:28:3D:4F',profile='CentOS-7-x86_64')
    print ret

if __name__ == '__main__':
    main()      

這個腳本中addsystem方法中需要四個參數:主機名,IP位址,mac位址和profile。 注意:mac位址可以通過建立一台虛拟機時生成而獲得 執行腳本python cobblerapi.py,腳本執行成功會有如下提示:

[root@linux-node2 ~]# python cobbler_api.py 
{'comment': [], 'result': True}      

通過cobbler system list檢視

[root@linux-node2 ~]# cobbler system list
   cobbler-api-test
   linux-node3.oldboyedu.com      

最後,打開電源,就可以啪啪啪裝機了,無需人工幹預。

八、通過koan重裝系統

假如某台機器需要重裝系統,使用cobbler也可以完成。這裡要用到koan這個工具,這個工具安裝在需要重裝的那個作業系統上,我用上面剛裝完的系統做一下測試。

首先,更新一下yum源

[root@cobbler-api-test ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo      

執行

[root@cobbler-api-test ~]# yum install -y koan      

指定cobbler所在伺服器的IP位址

[root@cobbler-api-test ~]# koan --replace-self --server=192.168.56.12 --profile=CentOS-7-x86_64

- looking for Cobbler at http://192.168.56.12:80/cobbler_api
- reading URL: http://192.168.56.12/cblr/svc/op/ks/profile/CentOS-7-x86_64
install_tree: http://192.168.56.12/cblr/links/CentOS-7-x86_64
downloading initrd initrd.img to /boot/initrd.img_koan
url=http://192.168.56.12/cobbler/p_w_picpaths/CentOS-7-x86_64/initrd.img
- reading URL: http://192.168.56.12/cobbler/p_w_picpaths/CentOS-7-x86_64/initrd.img
downloading kernel vmlinuz to /boot/vmlinuz_koan
url=http://192.168.56.12/cobbler/p_w_picpaths/CentOS-7-x86_64/vmlinuz
- reading URL: http://192.168.56.12/cobbler/p_w_picpaths/CentOS-7-x86_64/vmlinuz
- ['/sbin/grubby', '--add-kernel', '/boot/vmlinuz_koan', '--initrd', '/boot/initrd.img_koan', '--args', '"ksdevice=link lang= text net.ifnames=0 ks=http://192.168.56.12/cblr/svc/op/ks/profile/CentOS-7-x86_64 biosdevname=0 kssendmac "', '--copy-default', '--make-default', '--title=kick1465316032']
- ['/sbin/grubby', '--update-kernel', '/boot/vmlinuz_koan', '--remove-args=root']
- reboot to apply changes      

可以看到最後提示reboot讓改動生效。執行reboot。

接下來,通過虛拟機的界面可以看到系統開始重裝,啪啪啪,無需人工幹預。其實是在grub上新增了一條安裝項

生産環境中,專門劃分一個裝機vlan。

九、cobbler的web管理

通過web來管理cobbler,通路方式:

https://192.168.56.12/cobbler_web      

預設的使用者名和密碼都是cobbler。

如果想修改密碼,可以在這麼做:

找到使用者名,密碼所在檔案,在/etc/cobbler下

[root@linux-node2 cobbler]# cat users.
users.conf    users.digest  
[root@linux-node2 cobbler]# cat users.digest 
cobbler:Cobbler:a2d6bae81669d707b72c0bd9806e01f3
[root@linux-node2 cobbler]# htd
htdbm     htdigest  
[root@linux-node2 cobbler]# htd
htdbm     htdigest  
[root@linux-node2 cobbler]# htdigest /etc/cobbler/users.digest "Cobbler" cobbler
Changing password for user cobbler in realm Cobbler
New password: 
Re-type new password: 
[root@linux-node2 cobbler]#      

第一個"Cobbler"是使用者描述,第二個是使用者名。

繼續閱讀