天天看點

運維自動化之中級進階—Cobbler實戰,實作批量多版本os自動部署。

Cobbler簡介

   Cobbler由python語言開發,是對PXE和Kickstart以及DHCP的封裝。融合很多特性,提供了CLI和Web的管理形式。更加友善的實行網絡安裝。适用場景:需要大批量的部署作業系統。

Collber 對象關系圖

<a href="http://s3.51cto.com/wyfs02/M00/47/89/wKiom1P8LieApkJMAACq4Zg-hmw940.jpg" target="_blank"></a>

Distros(發行版):表示一個作業系統。它承載了核心和 initrd 的資訊,以及核心參數等其他資料。

profile:定義Kickstart檔案

repos:安裝源

system:表示要配給的機器。它包含一個配置檔案或一個鏡像,還包含 IP 和 MAC 位址、電源管理(位址、憑據、類型)以及更為專業的資料等資訊。

image:可替換一個包含不屬于此類别的檔案的發行版對象(例如,無法分為核心和 initrd 的對象)。

內建的服務

PXE 服務

DHCP服務管理

DNS服務管理

HTTP服務管理

TFTP服務管理

Kickstart服務

yum倉庫管理

電源管理

工作流程

(1).需要安裝系統的用戶端主機的網卡支援網絡安裝,也就是支援以PXE模式啟動。

(2).用戶端主機需要有一個IP位址來與Cobbler Server進行通信,這時網絡中必須要有一個主機能自動配置設定IP。DHCP就是一個能為用戶端主機自動配置設定IP位址的服務,DHCP就是安裝在Cobbler Server主機上。

(3).用戶端得到IP位址後,需要提供一個引導程式pxelinux.0(類似于grub) 此檔案由syslinux程式提供,是以需要安裝syslinux并且能加載核心及核心鏡像檔案(vmlinuz與initrd.img),用戶端可以通過TFTP協定到TFTP Server上下載下傳核心及核心鏡像檔案(vmlinuz與initrd.img),是以需要一個TFTP 服務,TFTP也是安裝在Cobbler Server主機上,用戶端隻是知道可以用TFTP協定下載下傳檔案,但是TFTP Server的IP是那個,這個還得需要DHCP伺服器指名告訴用戶端那個是TFTP Server(注,是以我們在配置DHCP模闆時指定next-server,同時指定了filename,這裡的filename就是指定的pxelinux.0引導程式),而TFTP服務需要超級程序進行管理,此程序是xinetd,是以需要在Cobbler Server上安裝xinet服務。

(4).然後通過distro找到安裝的發行版,通過設定的profile找到kickstart檔案,完成自動安裝。

安裝cobbler

yum install cobbler httpd cobbler-web pykickstart debmirror dhcp -y

yum install -y ed patch perl perl-Compress-Zlib perl-Digest-SHA1 perl-LockFile-Simple perl-libwww-perl  //Cobbler環境支援包

cobbler配置檔案目錄介紹:

<a href="http://s3.51cto.com/wyfs02/M02/47/8A/wKiom1P8P4nDqHH_AAG4moa4goM988.jpg" target="_blank"></a>

/etc/cobbler/settings cobbler主配置檔案

/etc/cobbler/iso/ iso模闆配置檔案

/etc/cobbler/pxe pxe模闆檔案

/etc/cobbler/power 電源的配置檔案

/etc/cobbler/users.conf Web 服務授權配置檔案

/etc/cobbler/users.digest 用于web通路的使用者名密碼配置檔案

/etc/cobbler/dhcp.template DHCP服務的配置模闆

/etc/cobbler/dnsmasq.template DNS服務的配置模闆

/etc/cobbler/tftpd.template tftp服務的配置模闆

/etc/cobbler/modules.conf Cobbler子產品配置檔案

開始配置

1、編輯/etc/cobbler/settings 定義server 指定提供服務的ip位址,不能使用localhost。

2、編輯/etc/cobbler/settings定義next_server 指定TFTP伺服器位址。

3、運作cobbler get-loaders 下載下傳需要的程式,需要通路網際網路。 如果通路不了網際網路可以自行安裝,syslinux 

自行複制檔案pxelinux.0 menu.c32 到 /var/lib/cobbler/loaders

如:

#yum install syslinux -y

#cd /usr/share/syslinux/

#cp pxelinux.0 menu.c32 /var/lib/cobbler/loaders

4、啟動rsync  chkconfig rsync on

5、編輯/etc/debmirror.conf  注釋 ’dists‘ 和 ‘arches’

<a href="http://s3.51cto.com/wyfs02/M01/47/8C/wKioL1P8QtGiEqYSAAFGcDCbrA4484.jpg" target="_blank"></a>

6、修改預設密碼,編輯檔案/etc/cobbler/settings,找到字段default_password_crypted,将生成的密碼串替換掉預設的。

openssl passwd -1 -salt `openssl rand -hex 5`

7、安裝fencing 裝置 cman fence-agents //這一項無關緊要,很少用上。

配置tftp-server

首先啟用tftp,可以編輯配置檔案/etc/xinetd.d/tftp修改disable那一項。 或者用指令。

chkconfig tftp on

service xinetd start

也可以在settings檔案中啟用manage_tftpd: 1 ,使用cobbler管理tftp。

配置DHCP服務

在settings檔案中啟用 manage_dhcp: 1

編輯cobbler管理dhcp服務的模版檔案/etc/cobbler/dhcp.template

<a href="http://s3.51cto.com/wyfs02/M01/47/8C/wKiom1P8UVaxukPaAALRnus722c612.jpg" target="_blank"></a>

導入RHEL6.4鏡像檔案

cobbler import --name="rhel6.4-i386" --path=/mnt/dvd

導入的檔案儲存在/var/www/cobbler/ks_mirror/

我們可以通過浏覽器通路到

<a href="http://s3.51cto.com/wyfs02/M00/47/8C/wKiom1P8Ul6gU5EbAAGeyDWBkpo497.jpg" target="_blank"></a>

制作kickstart檔案(略)

導入profile

cobbler profile add --name=rhel6.4-i386-basic --distro=rhel6.4-i386 --kickstart=/root/rhel6-basic.cfg 

删除預設生成的profile

cobbler profile remove --name=rhel6.4-i386

配置完成後記得執行 cobbler sync 同步配置到資料目錄。

cobbler會自動給/var/lib/tftpboot/pxelinux.cfg/default 添加lable

來看看裡面的資訊:

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 rhel6.4-i386-basic

        kernel /images/rhel6.4-i386/vmlinuz

        MENU LABEL rhel6.4-i386-basic

        append initrd=/images/rhel6.4-i386/initrd.img ksdevice=bootif lang=  kssendmac text  ks=http://192.168.1.201/cblr/svc/op/ks/profile/rhel6.4-i386-basic

        ipappend 2

MENU end

這樣我們的cobbler就配置完成了,開啟另一台虛拟機,設定網卡啟動。

<a href="http://s3.51cto.com/wyfs02/M01/47/8E/wKioL1P8VZ_C1EesAADq_0ECVwE434.jpg" target="_blank"></a>

<a href="http://s3.51cto.com/wyfs02/M00/47/8C/wKiom1P8VT-gDkUrAAFVAS4srm8894.jpg" target="_blank"></a>

配置認證,通過Web接口導入centos6.5,實作一鍵安裝。

編輯/etc/cobbler/modules.conf

[authentication]

module = authn_pam  //設定使用系統使用者認證機制。

建立一個使用者。

useradd tuchao;echo 123456 |passwd --stdin tuchao

編輯/etc/cobbler/users.conf 設定cobbler認證使用者。

[admins]

admin = "tuchao"

使用浏覽器進入Web管理接口。

<a href="http://s3.51cto.com/wyfs02/M01/47/AD/wKiom1P938vC-1xlAADkIwV7HZI451.jpg" target="_blank"></a>

通過web接口導入鏡像檔案,生成distro。

<a href="http://s3.51cto.com/wyfs02/M01/47/B9/wKiom1P_ANTz0W_KAAMCa2JVpZw612.jpg" target="_blank"></a>

填好後點選“Run”即可開始複制檔案,系統會自動在/var/log/cobbler/tasks/目錄下生成以目前時間命名的日志檔案如:2014-08-28_165922_import.log。

相應的cobbler sync操作也會被記錄日志。如 2014-08-28_171540_sync.log

<a href="http://s3.51cto.com/wyfs02/M00/47/B9/wKiom1P-9qfSv2NDAAJ26I19lSo530.jpg" target="_blank"></a>

然後我們可以通過cobbler distro 指令檢視是否添加成功了。 通過web接口的distro按鈕也可以。

建立ks檔案:

#platform=x86, AMD64, or Intel EM64T

#version=DEVEL

# Firewall configuration

firewall --enabled --service=ssh

# Install OS instead of upgrade

install

# Use network installation

url --url="http://192.168.1.201/cobbler/ks_mirror/centos6.5-x86_64/"

# Root password

rootpw --iscrypted $1$hnJSXM6C$gt3.CTocsJz4Kg/tV3Udl0

# System authorization information

auth  --useshadow  --passalgo=sha512

# Use graphical install

graphical

firstboot --disable

# System keyboard

keyboard us

# System language

lang en_US

# SELinux configuration

selinux --enforcing

# Installation logging level

logging --level=info

# System timezone

timezone  Asia/Shanghai

# Network information

#network  --bootproto=dhcp --device=eth0 --onboot=on

# System bootloader configuration

bootloader --append="crashkernel=auto rhgb quiet" --location=mbr --driveorder="sda"

# Clear the Master Boot Record

zerombr

# Partition clearing information

clearpart --all --initlabel

part /boot --fstype=ext4 --size=500

part pv.008002 --grow --size=1

volgroup vg_server1 --pesize=4096 pv.008002

logvol / --fstype=ext4 --name=lv_root --vgname=vg_server1 --grow --size=1024 --maxsize=51200

logvol swap --name=lv_swap --vgname=vg_server1 --grow --size=2016 --maxsize=2016

%packages

@additional-devel

@base

@basic-desktop

@chinese-support

@core

@debugging

@desktop-debugging    後面的軟體包由于太長,我就省略了,軟體包大家可以自行定義。

注意:如果是拿系統的ks檔案來修改的,一定要删除下面一行:

repo --name="CentOS"  --baseurl=cdrom:sr0 --cost=100

這是代表從CD光牒找安裝源的意思

不然就會出現如下錯誤:

<a href="http://s3.51cto.com/wyfs02/M02/47/B9/wKiom1P--jmg1qltAAGEOwxll7E671.jpg" target="_blank"></a>

定義profile檔案

<a href="http://s3.51cto.com/wyfs02/M02/47/BB/wKioL1P--XqgfldiAALSMcYZD24151.jpg" target="_blank"></a>

配置完成後記得sync,可以用指令cobbler sync,在web接口也有sync按鈕。

導入Centos5.3鏡像,配置一鍵安裝。

挂載好CD光牒,這裡我們用指令導入。

cobbler import --name=centos5.3-i386 --path=/mnt/dvd/

待指令行界面出現"*** TASK COMPLETE ***"表示導入完成了,也可以去檢視日志确認。

建立Kickstart檔案:

auth  --useshadow  --enablemd5

bootloader --append="rhgb quiet" --location=mbr --driveorder=sda

part /boot --fstype ext3 --size=100 --ondisk=sda

part pv.2 --size=0 --grow --ondisk=sda

volgroup VolGroup00 --pesize=32768 pv.2

logvol / --fstype ext3 --name=LogVol00 --vgname=VolGroup00 --size=1024 --grow

logvol swap --fstype swap --name=LogVol01 --vgname=VolGroup00 --size=1024 --grow --maxsize=2048

firewall --enabled --ssh

# Run the Setup Agent on first boot

url --url=http://192.168.1.201/cobbler/ks_mirror/centos5.3-i386/

network --bootproto=dhcp --device=eth0 --onboot=on

#Root password

rootpw --iscrypted $1$OKFwyttn$.BUnf8M4cA7aU8ffQDJx2.

timezone --isUtc Asia/Shanghai

# X Window System configuration information

xconfig  --defaultdesktop=GNOME --depth=8 --resolution=640x480 --startxonboot

@office

@development-libs

@editors

@gnome-software-development

@text-internet

@x-software-development

@gnome-desktop

定義profile過程和上面一樣(略),執行cobbler sync。

我們再來檢視 /var/lib/tftpboot/pxelinux.cfg/default 檔案,cobbler會自動生成lable。

LABEL centos5.3-i386-common

        kernel /images/centos5.3-i386/vmlinuz

        MENU LABEL centos5.3-i386-common

        append initrd=/images/centos5.3-i386/initrd.img ksdevice=bootif lang=  kssendmac text  ks=http://192.168.1.201/cblr/svc/op/ks/profile/centos5.3-i386-common

LABEL centos6.5-x86-64

        kernel /images/centos6.5-x86_64/vmlinuz

        MENU LABEL centos6.5-x86-64

        append initrd=/images/centos6.5-x86_64/initrd.img ksdevice=bootif lang=  kssendmac text  ks=http://192.168.1.201/cblr/svc/op/ks/profile/centos6.5-x86-64

開啟一台虛拟機配置網卡啟動,測試效果。

<a href="http://s3.51cto.com/wyfs02/M01/47/B9/wKiom1P-_ciye1NdAAETasnglbQ919.jpg" target="_blank"></a>

<a href="http://s3.51cto.com/wyfs02/M02/47/BB/wKioL1P-_vWQ2ud6AAG7RB-gV0k679.jpg" target="_blank"></a>

這篇寫到這裡也就完了,請大家多多指點,共同進步。

本文轉自qw87112 51CTO部落格,原文連結:http://blog.51cto.com/tchuairen/1545323