cobbler+pxe實(shí)現(xiàn)自動(dòng)化裝機(jī)

一、 PXE:

Preboot Excution Environment預(yù)啟動(dòng)執(zhí)行環(huán)境 ,由intel公司研發(fā)

pxe工作模式:基于C/S的網(wǎng)絡(luò)模式,支持遠(yuǎn)程主機(jī)通過網(wǎng)絡(luò)從遠(yuǎn)端服務(wù)器下載映像,并由此支持通過網(wǎng)絡(luò)啟動(dòng)操作系統(tǒng)
pxe技術(shù)的實(shí)現(xiàn)流程:
  1. 客戶端的網(wǎng)卡要支持網(wǎng)絡(luò)引導(dǎo)機(jī)制;如果有操作系統(tǒng)的話,應(yīng)該調(diào)整為第一引導(dǎo)設(shè)備;在主機(jī)被喚醒之后,開始加載網(wǎng)絡(luò)引導(dǎo)應(yīng)用時(shí),此網(wǎng)卡會(huì)通過在本地局域網(wǎng)中廣播一個(gè)rarp協(xié)議,第一步獲得一個(gè)ip地址;
  2. 獲得ip地址的同時(shí)還會(huì)從dhcp那里獲得引導(dǎo)文件名稱和文件服務(wù)器地址;隨后會(huì)去找文件服務(wù)器主機(jī),加載對(duì)應(yīng)的文件,所需要的文件加載完成后,在內(nèi)存中展開,而后基于此文件,可以實(shí)現(xiàn)去加載一個(gè)內(nèi)核文件,此內(nèi)核文件也一樣通過tftp文件服務(wù)器獲取,內(nèi)核通常依賴于initrd虛根來完成對(duì)于真實(shí)根所在設(shè)備的驅(qū)動(dòng)加載;
  3. 加載完成以后,這個(gè)內(nèi)核文件通常是專用于為系統(tǒng)安裝所設(shè)定的,因此,如果配置了網(wǎng)絡(luò)屬性,這個(gè)內(nèi)核文件還需要基于網(wǎng)絡(luò)配置在內(nèi)核上的ip地址,基于網(wǎng)絡(luò)把自己扮演成某種協(xié)議的客戶端,加載一個(gè)能夠啟動(dòng)安裝程序的程序包,在本地完成安裝并啟動(dòng)這個(gè)應(yīng)用程序,而此程序在已經(jīng)不再tftp文件服務(wù)器上面;
  4. 網(wǎng)絡(luò)引導(dǎo)安裝方式中,依賴于網(wǎng)絡(luò)上應(yīng)該存在一個(gè)基礎(chǔ)程序包鏡像倉庫,因此,此時(shí)還依賴于外部有一個(gè)基于ftp或http再或者nfs服務(wù)所提供的yum倉庫;還要通過這個(gè)yum倉庫加載安裝程序,以及安裝程序啟動(dòng)完后,很可能要讀取kickstart文件,可以根據(jù)kickstart文件內(nèi)容,解決依賴關(guān)系以后,基于這個(gè)yum倉庫,完成后續(xù)的所有安裝過程;
  • 因此,對(duì)于centos系列主機(jī),整個(gè)pxe技術(shù)大概要依賴以上所提到的服務(wù),dhcp服務(wù)、tftp服務(wù)、能夠提供yum倉庫的服務(wù);還要提供:kickstart文件(如果不想使用自動(dòng)化可以不用提供此文件。

配置示例:

  1. dhcp server:

     [root@localhost ~]# yum -y install dhcp
     [root@localhost ~]# cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf
         #在打開的dhcpd.conf配置文件中添加如下內(nèi)容
         subnet 172.16.100.0 netmask 255.255.255.0 {
                 range 172.16.100.10 172.16.100.20;
                 option routers 172.16.100.1;
                 filename "pxelinux.0";
                 next-server 172.16.100.10;
         }
     [root@localhost ~]# systemctl start dhcpd
     [root@localhost ~]# ss -unlp
         State       Recv-Q Send-Q         Local Address:Port                        Peer Address:Port              
         UNCONN      0      0                          *:67                                     *:*                   users:(("dhcpd",pid=4557,fd=7))
         UNCONN      0      0                          *:68                                     *:*                   users:(("dhclient",pid=4485,fd=6))
     #至此dhcp啟動(dòng)成功,添加開機(jī)自啟動(dòng)
     [root@localhost ~]# systemctl enable dhcpd
     Created symlink from /etc/systemd/system/multi-user.target.wants/dhcpd.service to /usr/lib/systemd/system/dhcpd.service.
     [root@localhost ~]# systemctl is-enabled dhcpd
     enabled
    
  2. tftp-server

     [root@localhost ~]# yum -y install tftp tftp-server
     [root@localhost ~]# systemctl start tftp
     [root@localhost ~]# ss -unl
     State       Recv-Q Send-Q         Local Address:Port                        Peer Address:Port              
     UNCONN      0      0                          *:67                                     *:*                  
     UNCONN      0      0                          *:68                                     *:*                  
     UNCONN      0      0                         :::69                                    :::*   
     #   tftp監(jiān)聽在udp69端口,至此tftp-server服務(wù)啟動(dòng),默認(rèn)工作目錄為:
     /var/lib/tftpboot/
     [root@localhost ~]# systemctl enable tftp
     Created symlink from /etc/systemd/system/sockets.target.wants/tftp.socket to /usr/lib/systemd/system/tftp.socket.
     [root@localhost ~]# systemctl is-enabled tftp
     indirect
    
  3. vsftpd server
    此處使用vsftpd服務(wù)來提供yum源訪問

    [root@localhost ~]# yum -y install vsftpd
    #vsftpd默認(rèn)工作目錄為:
    /var/ftp/pub/
    [root@localhost ~]# systemctl start vsftpd
    [root@localhost ~]# ss -tnlp
    ...
    LISTEN      0      32                        :::21                                    :::*                   users:(("vsftpd",pid=5338,fd=3))
    #vsftpd服務(wù)監(jiān)聽在tcp/21端口
    #提供光盤鏡像里的yum源訪問路徑
    [root@localhost ~]# mount -r /dev/sr0 /var/ftp/pub/centos/7
    [root@localhost ~]# ls /var/ftp/pub/centos/7
     CentOS_BuildTag  EULA  images    LiveOS    repodata              RPM-GPG-KEY-CentOS-Testing-7
     EFI              GPL   isolinux  Packages  RPM-GPG-KEY-CentOS-7  TRANS.TBL
    
    #提供一個(gè)kickstart文件放在/var/ftp/pub/kickstart/目錄下
    [root@localhost ~]# mkdir /var/ftp/pub/kickstart
    [root@localhost ~]# cd /var/ftp/pub/kickstart/
    [root@localhost kickstart]# ls
    centos7-ks.cfg
    [root@localhost kickstart]# chmod 644 centos7-ks.cfg 
    
  4. 準(zhǔn)備安裝系統(tǒng)需要的內(nèi)核等文件,放在tftp服務(wù)工作目錄中

     [root@localhost ~]# yum -y install syslinux
     [root@localhost ~]# cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
     [root@localhost ~]# cp /usr/share/syslinux/{memdisk,mboot.c32,chain.c32,menu.c32} /var/lib/tftpboot/
     [root@localhost ~]# cp /var/ftp/pub/centos/7/images/pxeboot/{initrd.img,vmlinuz} /var/lib/tftpboot/
     #提供目錄,創(chuàng)建專門用來引導(dǎo)時(shí)顯示的菜單默認(rèn)文件:默認(rèn)配置要放pxelinux.cfg的目錄,文件名為default:
     [root@localhost ~]# mkdir /var/lib/tftpboot/pxelinux.cfg
     [root@localhost ~]# vim /var/lib/tftpboot/pxelinux.cfg/default
         default menu.c32
         prompt 5
         timeout 30
         MENU TITLE CentOS 7 PXE Menu
    
         LABEL linux
         MENU LABEL Install Centos 7 x86_64
         KERNEL vmlinuz
         APPEND initrd=initrd.img ks=ftp://172.16.100.10/pub/kickstart /centos7-ks.cfg   inst.repo=ftp://172.16.100.10/pub/centos/7
    
  5. 創(chuàng)建一個(gè)新的虛擬機(jī)以網(wǎng)絡(luò)自動(dòng)安裝系統(tǒng)測(cè)試


    menu.png
start.png
start2.png

至此自動(dòng)安裝開始 PXE測(cè)試通過
kickstart文件內(nèi)容附錄如下:

#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
url --url="ftp://172.16.100.10/pub/centos/7"
# Use graphical install
text
firewall --disabled
selinux --disabled
reboot
# Run the Setup Agent on first boot
firstboot --disable
ignoredisk --only-use=sda
# Keyboard layouts
keyboard --vckeymap=cn --xlayouts='cn'
# System language
lang zh_CN.UTF-8

timezone Asia/Shanghai --isUtc
# Network information
network  --bootproto=dhcp --device=ens33 --onboot=on --ipv6=auto --no-activate
network  --hostname=localhost.localdomain

# Root password
rootpw --iscrypted $6$CMUBzCualheXycom$I1OADgCKcXi7lK9LKgqt.efENmylM.09mCt8.aesLmsQJ8uLYgu2Ck9G.tarugXP73TN4rFxQVVsAdIU8yN/V/
# System services
services --enabled="chronyd"
# System timezone
timezone Asia/Shanghai --isUtc
user --groups=wheel --name=inspur --password=$6$bx479ICdaF3SObqw$2F9YqiKtNp/SI5wPc.QJZggrWEhIfcXSbE1t74k222jaALrna.mqkH7PfWo.19mzCGjHrO1WwP1MXMDGZIrb7/ --iscrypted --gecos="inspur"
# X Window System configuration information
xconfig  --startxonboot
# System bootloader configuration
bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sda
# Partition clearing information
clearpart --all --initlabel
zerombr
# Disk partitioning information
part /boot --fstype="xfs" --ondisk=sda --size=1024
part swap --fstype="swap" --ondisk=sda --size=2049
part /home --fstype="xfs" --ondisk=sda --size=10240
part pv.254 --fstype="lvmpv" --ondisk=sda --size=20484
volgroup centos --pesize=4096 pv.254
logvol /  --fstype="xfs" --size=20480 --name=root --vgname=centos

%packages

@base
@core
@dial-up
@directory-client
@fonts
@gnome-desktop
@guest-agents
@input-methods
@internet-browser
@java-platform
@multimedia
@network-file-system-client
@networkmanager-submodules
@x11
chrony
kexec-tools

%end

%addon com_redhat_kdump --enable --reserve-mb='auto'

%end

%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end

二、Cobbler

快速網(wǎng)絡(luò)安裝linux操作系統(tǒng)的服務(wù),支持多種的linux發(fā)行版,也可以支持網(wǎng)絡(luò)安裝windows

  • Cobbler是PXE的二次封裝,將多種參數(shù)封裝到一個(gè)菜單;是用python語言編寫
Cobbler的工作流程:
cobbler.png
  1. client裸機(jī)配置了從網(wǎng)絡(luò)啟動(dòng)后,開機(jī)后會(huì)廣播包請(qǐng)求DHCP服務(wù)器(cobbler server)發(fā)送其分配好的一個(gè)ip
  2. DHCP收到請(qǐng)求后發(fā)送responese,包括其ip
  3. client拿到ip后再向cobbler server發(fā)送請(qǐng)求OS引導(dǎo)文件的請(qǐng)求
  4. cobbler server告訴client 裸機(jī)OS引導(dǎo)文件的名字和TFTP server的ip和port
  5. client通過上面告知的tftp server地址通信,下載引導(dǎo)文件
  6. client執(zhí)行該引導(dǎo)文件,確定加載的信息,選擇要按住的os,期間會(huì)在想cobbler server請(qǐng)求kickstart文件和 os image
  7. cobbler server發(fā)送請(qǐng)求的kickstart 和 os image
  8. celient加載kickstart文件
  9. client接收os image,安裝該os image
cobbler安裝

cobbler 基于EPEL源,需要先配置好epel的yum源; cobbler服務(wù)集成:PXE,DHCP,rsync,Http,DNS,Kickstart,IPMI電源管理
......

一、安裝:

[root@localhost ~]# yum install cobbler cobbler-web pykickstart debmirror
[root@localhost ~]# systemctl start httpd cobblerd 
[root@localhost ~]# cobbler check   #檢查存在的問題
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 : comment 'dists' on /etc/debmirror.conf for proper debian support
6 : comment 'arches' on /etc/debmirror.conf for proper debian support
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.

如上各問題的解決方法如下所示:
1、修改/etc/cobbler/settings文件中的server參數(shù)的值為提供cobbler服務(wù)的主機(jī)相應(yīng)的IP地址或主機(jī)名,如172.16.100.15;
2、修改/etc/cobbler/settings文件中的next_server參數(shù)的值為提供PXE服務(wù)的主機(jī)相應(yīng)的IP地址,如172.16.100.16;
3、如果當(dāng)前節(jié)點(diǎn)可以訪問互聯(lián)網(wǎng),執(zhí)行“cobbler get-loaders”命令即可;否則,需要安裝syslinux程序包,而后復(fù)制/usr/share/syslinux/{pxelinux.0,memu.c32}等文件至/var/lib/cobbler/loaders/目錄中;
4、執(zhí)行“chkconfig rsync on”命令即可;
5、注釋/etc/debmirror.conf文件中的“@dists="sid";”一行;
6、注釋/etc/debmirror.conf文件中的“@arches="i386";”一行;
7、執(zhí)行“openssl passwd -1 -salt $(openssl rand -hex 4)”生成密碼,并用其替換/etc/cobbler/settings文件中default_password_crypted參數(shù)的值;
8、執(zhí)行“yum install cman fence-agents”命令安裝相應(yīng)的程序包即可;

接著重啟cobblerd,而后執(zhí)行“cobbler sync”同步新的配置至cobbler。

二、配置及啟動(dòng)cobbler所依賴的各服務(wù)

  • cobbler的運(yùn)行依賴于dhcp、tftp、rsync及dns服務(wù)。其中dhcp可由dhcpd(isc)提供,也可由dnsmasq提供;tftp可由tftp-server程序包提供,也可由cobbler自帶的tftp功能提供;rsync由rsync程序包提供;dns可由bind提供,也可由dnsmasq提供。

  • cobbler可自行管理這些服務(wù)中的部分甚至是全部,但需要配置/etc/cobbler/settings文件中的“manage_dhcp”、“manage_tftpd”、“manage_rsync”和“manage_dns”分別進(jìn)行定義。另外,由于每種服務(wù)都有著不同的實(shí)現(xiàn)方式,如若需要進(jìn)行自定義,需要通過修改/etc/cobbler/modules.conf配置文件中各服務(wù)的模塊參數(shù)的值來實(shí)現(xiàn)。

本文采用了獨(dú)立管理的方式,即不通過cobbler來管理這些服務(wù)

  1. 配置dhcp服務(wù)
    定義好所需的“subnet”及其它參數(shù)或選項(xiàng),而后啟動(dòng)dhcpd守護(hù)進(jìn)程即可。本示例中所用的dhcpd的配置如下所示:

     option domain-name "magedu.com";
     option domain-name-servers 192.168.10.254,172.16.0.1;
     
     default-lease-time 43200;
     max-lease-time 86400;
     
     log-facility local7;
     
     subnet 172.16.0.0 netmask 255.255.0.0 {
         range 172.16.100.121 172.16.200;
         option routers 172.16.100.10;
     }
    
     next-server 172.16.100.10;
     filename="pxelinux.0";
    

接著使用“service dhcpd start”啟動(dòng)服務(wù)即可。

  1. 配置tftp服務(wù)

     # chkconfig tftp on
     # service xinetd restart
    

三、配置cobbler
cobbler的各主要組件間的關(guān)系如下圖所示。

cobbler。1.png

  1. 管理distro
    使cobbler變得可用的第一步為定義distro,其可以通過為其指定外部的安裝引導(dǎo)內(nèi)核及ramdisk文件的方式實(shí)現(xiàn)。而如果已經(jīng)有完整的系統(tǒng)安裝樹(如CentOS6的安裝鏡像)則推薦使用import直接導(dǎo)入的方式進(jìn)行。
  • 例如,對(duì)于已經(jīng)掛載至/media/cdrom目錄的CentOS 6.5 x86_64的安裝鏡像,則可以使用類似如下命令進(jìn)行導(dǎo)入
    cobbler import --name=centos-6.5-x86_64 --path=/media/cdrom
  • 可使用“cobbler distro list”列出所有的distro。
  • 如果有kickstart文件,也可以使用“--kickstart=/path/to/kickstart_file”進(jìn)行導(dǎo)入,因此import會(huì)自動(dòng)為導(dǎo)入的distro生成一個(gè)profile。
  1. 管理profile
    cobbler使用profile來為特定的需求類別提供所需要安裝配置,即在distro的基礎(chǔ)上通過提供kickstart文件來生成一個(gè)特定的系統(tǒng)安裝配置。distro的profile可以出現(xiàn)在PXE的引導(dǎo)菜單中作為安裝的選擇之一。
    因此,如果需要為前面創(chuàng)建的centos-6.5-x86_64這個(gè)distro提供一個(gè)可引導(dǎo)安裝條目,其用到的kickstart文件為/tmp/centos-6.5-x86_64.cfg(只提供了最基本的程序包),則可通過如下命令實(shí)現(xiàn)。
    cobbler profile add --name=centos-6.5-x86_64-basic --distro=centos-6.5-x86_64 --kickstart=/tmp/centos-6.5-x86_64.cfg
    可使用“cobbler profile list”查看已經(jīng)創(chuàng)建的profile。

四、使用cobbler_web

  1. 配置cobbler_web的認(rèn)證功能
    cobbler_web支持多種認(rèn)證方式,如authn_configfile、authn_ldap或authn_pam等,默認(rèn)為authn_denyall,即拒絕所有用戶登錄。下面說明兩種能認(rèn)證用戶登錄cobbler_web的方式。
  • 使用authn_pam模塊認(rèn)證cobbler_web用戶
    首先修改modules中[authentication]段的module參數(shù)的值為authn_pam。
    接著添加系統(tǒng)用戶,用戶名和密碼按需設(shè)定即可,例如下面的命令所示。
    # useradd cblradmin
    # echo 'cblrpass' | passwd --stdin cblradmin

  • 而后將cblradmin用戶添加至cobbler_web的admin組中。修改/etc/cobbler/users.conf文件,將cblradmin用戶名添加為admin參數(shù)的值即可,如下所示。
    [admins] admin = "cblradmin"

  • 最后重啟cobblerd服務(wù),通過http://YOUR_COBBLERD_IP/cobbler_web訪問即可。

  1. 使用authn_configfile模塊認(rèn)證cobbler_web用戶
    首先修改modules.conf中[authentication]段的module參數(shù)的值為authn_configfile。
    接著創(chuàng)建其認(rèn)證文件/etc/cobbler/users.digest,并添加所需的用戶即可。需要注意的是,添加第一個(gè)用戶時(shí),需要為htdigest命令使用“-c”選項(xiàng),后續(xù)添加其他用戶時(shí)不能再使用;另外,cobbler_web的realm只能為Cobbler。如下所示。
    # htdigest -c /etc/cobbler/users.digest Cobbler cblradmin

最后重啟cobblerd服務(wù),通過http://YOUR_COBBLERD_IP/cobbler_web訪問即可。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容