自動化運維工具cobbler的使用

概述:

    cobbler 實現自動化安裝OS,是依賴於:網卡的PXE技術(dhcp client、tftp client ) + anaconda(linux上安裝操作系統的程序) + dhcp(爲需要安裝操作系統的主機分配IP) + tftp(提供基於網卡引導時所需要的啓動文件) + httpd(os install tree 的存放位置 ) 實現的。

使用 cobbler 的命令能夠自動化地實現:

        在tftp server 上提供基於網卡引導時所需要的程序文件

能夠在 httpd 服務器的文檔根目錄下,自動生成安裝操作OS時,所需要的install tree.

所以,使用cobbler 實現自動化安裝OS,要安裝:httpd、tftp、dhcp服務,還有rsync服務(cobbler可以從遠程鏡像install tree的)。

自動化安裝OS原理如下圖:

wKioL1RHk22SMbd1AAI0TsoilC8708.jpg


工作原理:

     1、首先基於網卡的PXE技術引導啓動目標機(需要安裝OS的服務器):

        (1)、支持PXE技術的網卡上的芯片的dhcp client 客戶端,請求dhcp server分配IP地址.

        (2)、dhcp server 會告訴客戶端,Bootloader所在的服務器,以及引導文件是什麼?

            這樣tftp client就知道了Bootloader的URL了。

        (3)、需要安裝OS的主機配置好IP後,tftp client 就聯繫tftp server 服務器請求基於

              網卡引導啓動OS所需要的文件。

        (4)、在目標機(需要安裝OS的服務器)啓動內核,從而能夠驅動目標機的磁盤,

              就可以訪問磁盤了。

     2、啓動完成以後,在用戶空間啓動anaconda 程序,該程序負責安裝OS

           當內核啓動完成,通過/sbin/loader 啓動anaconda系統安裝程序,該程序就會讀取

            ks.cfg文件,ks.cfg文件中定義有安裝源(install tree)所在的

            位置和安裝OS時的配置數據。這樣就實現了自動化給服務器安裝操作系統了。

            cobbloer的安裝樹(install tree)的位置在httpd服務器上的。所以,ks.cfg文件中指定

            安裝源的URL就是httpd服務器的地址。

配置使用cobbler實現自動化安裝OS所需要的程序包:

         cobbler-2.4.0-1.el6.noarch       cobbler服務程序包
         cobbler-web.2.4.0-1.el6.noarch   cobbler 提供的web gui 配置接口。


一、配置cobbler服務

1、啓動cobbler和httpd服務

啓動cobbler 服務

[root@localhost ~]# service cobblerd start
Starting cobbler daemon:                                   [  OK  ]


查看cobbler 服務監聽的端口

[root@localhost ~]# netstat -anptul | grep python
tcp        0      0 127.0.0.1:25151             0.0.0.0:*                   LISTEN      1784/python

啓動httpd服務

[root@localhost ~]# service httpd start
Starting httpd:                                            [  OK  ]

2、cobbler正常工作所必須的條件:

(1)、使用cobbler提供的測試命令【cobbler check】測試cobbler 的環境

[root@localhost ~]# 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 : 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.
4 : change 'disable' to 'no' in /etc/xinetd.d/rsync
5 : debmirror package is not installed, it will be required to manage debian deployments and repositories
6 : ksvalidator was not found, install pykickstart
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
8 : 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.

(2)、解決測試出來的問題

解決第1個問題:設置cobbler server的地址/etc/cobbler/settings 文件中的【server】指令定義的

[root@localhost ~]# vim /etc/cobbler/settings
server: 192.168.60.134

解決第2個問題:在cobbler的/etc/cobbler/settings配置文件指定tftp server 地址。

next_server: 192.168.60.134

解決第3個問題:缺少一些網絡引導所需要的文件,如果只引導x86/x86_64。保證安裝了syslinux 程序包就可以了。

[root@localhost ~]# yum install syslinux

解決第4個問題:要開啓rsync服務

[root@localhost ~]# chkconfig rsync on
[root@localhost ~]# chkconfig --list rsync
rsync           on

解決第5個問題:如果安裝的是 debian 操作系統的話,需要把debmirrot包安裝好。

[root@localhost admin]# yum install debmirror-2.14-2.el6.noarch.rpm perl-LockFile-Simple-0.207-2.el6.noarch.rpm

解決第6個問題:需要安裝pykickstart程序

[root@localhost ~]# yum install pykickstart

解決第7個問題:因爲安裝系統時,要求設置管理員(root)密碼。這裏就是設置安裝操作系統時,設置的管理員密碼的。

[root@localhost ~]# openssl passwd -1 -salt 'abc'
Password:
$1$abc$FEGY5dR6AYgowx6hkRl2d/
[root@localhost ~]# vim /etc/cobbler/settings
default_password_crypted: "$1$abc$FEGY5dR6AYgowx6hkRl2d/"

第8個問題,是可忽略的。


(3)、重新啓動cobblerd服務

[root@localhost ~]# service cobblerd restart
Stopping cobbler daemon:                                   [  OK  ]
Starting cobbler daemon:                                   [  OK  ]

(4)、同步一下,讓cobbler check 檢測的環境是修改後的。

[root@localhost ~]# cobbler sync
task started: 2014-10-05_214230_sync
task started (id=Sync, time=Sun Oct  5 21:42:30 2014)
running pre-sync triggers
cleaning trees 
mkdir: /var/lib/tftpboot/pxelinux.cfg
mkdir: /var/lib/tftpboot/grub
mkdir: /var/lib/tftpboot/s390x
mkdir: /var/lib/tftpboot/ppc
mkdir: /var/lib/tftpboot/etc
removing: /var/lib/tftpboot/grub/images
copying bootloaders                              --------> 自動複製基於網卡引導所需要的文件到tftp server的共享文件目錄中。
copying: /usr/share/syslinux/pxelinux.0 -> /var/lib/tftpboot/pxelinux.0
copying: /usr/share/syslinux/menu.c32 -> /var/lib/tftpboot/menu.c32
copying: /usr/share/syslinux/memdisk -> /var/lib/tftpboot/memdisk
copying distros to tftpboot
copying images
generating PXE configuration files  -----> 自動產生基於PXE引導的配置文件
generating PXE menu structure
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 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 ***

(5)、再檢測是否滿足cobbler工作所需要的環境

[root@localhost admin]# cobbler check
The following are potential configuration items that you may want to fix:

1 : 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.
2 : comment out 'dists' on /etc/debmirror.conf for proper debian support
3 : comment out 'arches' on /etc/debmirror.conf for proper debian support
4 : 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.

(6)、解決上述的問題:

[root@localhost ~]# cp /usr/share/syslinux/pxelinux.0 /usr/share/syslinux/menu.c32 /var/lib/cobbler/loaders/
[root@localhost ~]# ll /var/lib/cobbler/loaders/
total 88
-rw-r--r-- 1 root root 60928 Oct  5 22:14 menu.c32
-rw-r--r-- 1 root root 26828 Oct  5 22:14 pxelinux.0

註釋掉debmirror.conf文件中指定配置選項

[root@localhost admin]# vim /etc/debmirror.conf
#@arches="i386";
#@dists="sid";

(7)、再次檢測

重新cobblerd 服務

[root@localhost admin]# service cobblerd restart
Stopping cobbler daemon:                                   [  OK  ]
Starting cobbler daemon:                                   [  OK  ]
[root@localhost ~]# cobbler sync
task started: 2014-10-05_221901_sync
task started (id=Sync, time=Sun Oct  5 22:19:01 2014)
running pre-sync triggers
cleaning trees
removing: /var/lib/tftpboot/pxelinux.cfg/default
removing: /var/lib/tftpboot/grub/images
removing: /var/lib/tftpboot/grub/efidefault
removing: /var/lib/tftpboot/s390x/profile_list
copying bootloaders                  ------------------> 複製基於網卡啓動的引導文件
trying hardlink /var/lib/cobbler/loaders/pxelinux.0 -> /var/lib/tftpboot/pxelinux.0
copying: /var/lib/cobbler/loaders/pxelinux.0 -> /var/lib/tftpboot/pxelinux.0
trying hardlink /var/lib/cobbler/loaders/menu.c32 -> /var/lib/tftpboot/menu.c32
copying: /var/lib/cobbler/loaders/menu.c32 -> /var/lib/tftpboot/menu.c32
copying: /usr/share/syslinux/memdisk -> /var/lib/tftpboot/memdisk
copying distros to tftpboot
copying images
generating PXE configuration files
generating PXE menu structure
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 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 ***

再次使用【cobbler check】命令檢測,是否已經符合cabbler工作的環境

[root@localhost ~]# cobbler check
The following are potential configuration items that you may want to fix:

1 : 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.
2 : 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.

說明:

   上述這兩個問題可以忽略的。


二、配置dhcp server 

1、複製樣例配置文件到dhcp的配置文件目錄下

[root@localhost ~]# cp /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample /etc/dhcp/dhcpd.conf
cp: overwrite `/etc/dhcp/dhcpd.conf'? y

2、編輯dhcp的配置文件,讓dhcp服務器爲:192.168.60.0/32網絡分配地址並且告訴dhcp 的客戶端tftp server的IP地址以及從tftp server下載什麼文件,

[root@localhost ~]# vim /etc/dhcp/dhcpd.conf
option domain-name "9527du.com";
option domain-name-servers www.9527du.com;

default-lease-time 600;
max-lease-time 7200;                           ----------> IP地址的租約時長
subnet 192.168.60.0 netmask 255.255.255.0 {
  range 192.168.60.78 192.168.60.167;           --------->只負責分配這個段的IP
  option routers 192.168.60.134;
}
next_server 192.168.60.134;                    -----------> 指明tftp server 的地址
filename="pxelinux.0";                         -----------> 聯繫 tftp server 服務器時,請求的文件。其實該文件就是基於網卡PXE技術引導啓動OS的引導文件。

3、啓動dhcp 服務

[root@localhost ~]# service dhcpd start
Starting dhcpd:                                            [  OK  ]

4、查看dhcp 是否工作OK?

(1)、查看dhcp server 監聽的端口

[root@localhost ~]# netstat  -anptul | grep dhcpd
udp        0      0 0.0.0.0:67                  0.0.0.0:*                               3193/dhcpd

(2)、查看dhcp輸出的日誌信息

[root@localhost ~]# tail -3 /var/log/messages
Oct  5 22:39:24 localhost dhcpd: Listening on LPF/eth0/00:0c:29:59:35:18/192.168.60.0/24
Oct  5 22:39:24 localhost dhcpd: Sending on   LPF/eth0/00:0c:29:59:35:18/192.168.60.0/24
Oct  5 22:39:24 localhost dhcpd: Sending on   Socket/fallback/fallback-net

說明:

    從上述,可以判斷dhcp工作是OK的。


三、生成安裝樹(install tree)以及配置profile文件

1、查看tftp server 服務器的共享目錄下是否有引導OS所需要的文件

[root@localhost ~]# cat  /var/lib/tftpboot/pxelinux.cfg/default
DEFAULT menu
PROMPT 0
MENU TITLE Cobbler | http://www.cobblerd.org/
TIMEOUT 200
TOTALTIMEOUT 6000
ONTIMEOUT local

LABEL local
        MENU LABEL (local)
        MENU DEFAULT
        LOCALBOOT -1

MENU end

是否有啓用Linux所需要的:initrd和vmlinuz文件的。

[root@localhost ~]# ll  /var/lib/tftpboot/images/
total 0

2、爲安裝OS提供install tree

cobbler可以使用兩種方式獲取install tree:

      A、使用sync 服務從遠程同步install tree 到本地;

      B、從OS安裝光盤中導入安裝源(install tree)

這裏選擇從OS安裝光盤導入安裝源

(1)、以只讀方式掛載OS安裝光盤

[root@localhost ~]# mount -ro /dev/cdrom /mnt/cdrom/
[root@localhost ~]# mount | grep cdrom
/dev/sr0 on /mnt/cdrom type iso9660 (ro)

(2)、使用cobbler的import命令從OS安裝光盤中導入(複製)install tree(安裝OS所需要的程序包)

[root@localhost ~]# cobbler import --path=/mnt/cdrom/ --name=centos-6.5-x86_64
task started: 2014-10-05_223311_import
task started (id=Media import, time=Sun Oct  5 22:33:11 2014)
Found a candidate signature: breed=redhat, version=rhel6
Found a matching signature: breed=redhat, version=rhel6
Adding distros from path /var/www/cobbler/ks_mirror/centos-6.5-x86_64:  ---------> 生成的OS鏡像文件的目錄
creating new distro: centos-6.5-x86_64
trying symlink: /var/www/cobbler/ks_mirror/centos-6.5-x86_64 -> /var/www/cobbler/links/centos-6.5-x86_64
creating new profile: centos-6.5-x86_64
associating repos
checking for rsync repo(s)
checking for rhn repo(s)
checking for yum repo(s)
starting descent into /var/www/cobbler/ks_mirror/centos-6.5-x86_64 for centos-6.5-x86_64
processing repo at : /var/www/cobbler/ks_mirror/centos-6.5-x86_64
need to process repo/comps: /var/www/cobbler/ks_mirror/centos-6.5-x86_64
looking for /var/www/cobbler/ks_mirror/centos-6.5-x86_64/repodata/*comps*.xml
running: ceatererpo -c cache -s sha --groupfile /var/www/cobbler/ks_mirror/centos-6.5-x86_64/repodata/b4e0b9342ef85d3059ff095fa7f140f654c2cb492837de689a58c581207d9632-c6-x86_64-comps.xml /var/www/cobbler/ks_mirror/centos-6.5-x86_64
received on stdout: Spawning worker 0 with 3995 pkgs
Workers Finished     ---------------------------> 導入完成
Gathering worker results

Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete

received on stderr:
*** TASK COMPLETE ***

說明:

    其實,就是複製光盤中的的程序包到/var/www/cobbler目錄下,

    還生成一個配置文件/etc/httpd/conf.d/cobbler.conf。

如下圖:

wKiom1RHlyjz7oSqAAJQuCbI5Mc537.jpg

(3)、查看導入的安裝樹,也就是cabbler提供可安裝的OS發行版。

[root@localhost ~]# cobbler distro list
   centos-6.5-x86_64

(4)、查看tftp 服務器的共享目錄是否有了引導啓動Linux所需要的文件

從OS安裝光盤光盤導入時,同時會把內核vimlinuz和initrd.img複製到tftp server 共享目錄下

[root@localhost ~]# tree   /var/lib/tftpboot/images/
/var/lib/tftpboot/images/
└── centos-6.5-x86_64
    ├── initrd.img
    └── vmlinuz

1 directory, 2 files

查看引導文件

[root@localhost ~]# cat  /var/lib/tftpboot/pxelinux.cfg/default
DEFAULT menu
PROMPT 0
MENU TITLE Cobbler | http://www.cobblerd.org/
TIMEOUT 200
TOTALTIMEOUT 6000
ONTIMEOUT local

LABEL local
        MENU LABEL (local)
        MENU DEFAULT
        LOCALBOOT -1

LABEL centos-6.5-x86_64
        kernel /images/centos-6.5-x86_64/vmlinuz    
        MENU LABEL centos-6.5-x86_64 
        append initrd=/images/centos-6.5-x86_64/initrd.img ksdevice=bootif lang=  kssendmac text  ks=http://192.168.60.134/cblr/svc/op/ks/profile/centos-6.5-x86_64  
        ipappend 2

MENU end

說明:

   把kickstart文件做爲參數傳遞給內核.

3、配置profile 文件,目的是讓kickstarts文件與安裝樹install tree 關聯起來。

由於安裝OS時,會自動在/etc/ 目錄下生成以.cfg結尾的文件,這就是kickstart文件。該文件保存了安裝OS時,配置的所有參數。anaconda 程序就是根據該文件完成OS的自動化安裝的。

(1)、提供kickstart文件:把kickstart文件複製到cobbler默認的kickstart文件存放目錄。

[root@localhost ~]# cp anaconda-ks.cfg /var/lib/cobbler/kickstarts/

(2)、生成profile文件

[root@localhost ~]# cobbler profile add --name=centos-6.5-x86_64-basic --distro=centos-6.5-x86_64 --kickstart=/var/lib/cobbler/kickstarts/anaconda-ks.cfg

說明:

   --distro 指定該kickstat文件關聯的安裝樹install tree(也就是那個發行版的OS)是什麼

   --kickstart 指定kickstat文件

   說白了,這個kickstat是爲安裝那個發行版使用的文件。

其實,就是在/var/lib/tftpboot/pxelinux.cfg/default 文件中加入一個條目:LABEL 。

(3)、列出cobbler有哪些profile文件。

[root@localhost ~]# cobbler profile list
   centos-6.5-x86_64
   centos-6.5-x86_64-basic

查看default配置文件

[root@localhost ~]# cat /var/lib/tftpboot/pxelinux.cfg/default
DEFAULT menu
PROMPT 0
MENU TITLE Cobbler | http://www.cobblerd.org/
TIMEOUT 200
TOTALTIMEOUT 6000
ONTIMEOUT local

LABEL local
        MENU LABEL (local)
        MENU DEFAULT
        LOCALBOOT -1

LABEL centos-6.5-x86_64
        kernel /images/centos-6.5-x86_64/vmlinuz
        MENU LABEL centos-6.5-x86_64
        append initrd=/images/centos-6.5-x86_64/initrd.img ksdevice=bootif lang=  kssendmac text  ks=http://192.168.60.134/cblr/svc/op/ks/profile/centos-6.5-x86_64
        ipappend 2

LABEL centos-6.5-x86_64-basic
        kernel /images/centos-6.5-x86_64/vmlinuz
        MENU LABEL centos-6.5-x86_64-basic
        append initrd=/images/centos-6.5-x86_64/initrd.img ksdevice=bootif lang=  kssendmac text  ks=http://192.168.60.134/cblr/svc/op/ks/profile/centos-6.5-x86_64-baic
        ipappend 2
MENU end

說明:

    從上述輸出信息可分析,該cobbler提供了兩個kickstart文件,也就是說安裝OS時,要以選擇安裝:centos-6.5-x86_64 還是 centos-6.5-x86_64-basic


四、查看cobbler依賴的服務是否準備好?

[root@www ~]# chkconfig tftp on
[root@www ~]# chkconfig --list  tftp
tftp            on

查看

[root@localhost ~]# netstat -anptul | grep -e 'dhcpd' -e 'xinetd' -e 'httpd'
tcp        0      0 :::80                       :::*                        LISTEN      2209/httpd        
tcp        0      0 :::873                      :::*                        LISTEN      1651/xinetd   -----> rsync服務監聽的端口    
udp        0      0 0.0.0.0:67                  0.0.0.0:*                               3334/dhcpd        
udp        0      0 0.0.0.0:69                  0.0.0.0:*                               1651/xinetd   ------> tftp server 監聽的端口

五、測試cobbler

1、目標機的dhcp client 端連繫 dhcp server分配IP地址

wKioL1RHmrXTfLBzAAGWO0w62YI326.jpg

2、選擇安裝的發行版;

wKiom1RHmuSBXraRAAG3Rqxgrsc509.jpg

3、開始基於網卡引導啓動OS

wKioL1RHnEeibUjKAAIrjH2DtVk216.jpg

4、運行anaconda系統安裝程序,安裝OS

wKiom1RHnObBX3LEAAOvqY-bX_8385.jpg


wKiom1RHnZ_wLiG7AAG-gRbpmhw726.jpg

5、分區格式化硬盤:

wKiom1RHnguCEl1eAAHh5hDUDjI004.jpg

5、檢測安裝的程序包的依賴關係;

wKiom1RHnxzgs47cAAI2ATWl164058.jpg

5、從httpd服務器下載安裝的程序包,進行安裝

wKiom1RHn5OglMQRAAHn2Qg_jWA641.jpg

wKiom1RHn6fD5R6jAAIiAMtLX5U841.jpg

OK!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章