原文:http://www.xuliangwei.com/xubusi/446.htmlhtml
運維自動化在生產環境中佔據着舉足輕重的地位,尤爲是面對幾百臺,幾千臺甚至幾萬臺的服務器時,僅僅是安裝操做系統,若是不經過自動化來完成,根本是不可想象的。node
面對生產環境中不一樣服務器的需求,該如何實現批量部署多版本的操做系統呢?Cobbler即可以的知足這一實際需求,實現多版本操做系統批量部署。python
在這就不過多介紹Kickstart 詳情見Blog Kickstart無人值守安裝系統linux
筆者QQ:572891887ios
Linux架構交流羣:471443208web
Cobbler是一個快速網絡安裝linux的服務,並且在通過調整也能夠支持網絡安裝windows。該工具使用python開發,小巧輕便(才15k行python代碼),使用簡單的命令便可完成PXE網絡安裝環境的配置,同時還能夠管理DHCP、DNS、TFTP、RSYNC以及yum倉庫、構造系統ISO鏡像。
Cobbler支持命令行管理,web界面管理,還提供了API接口,能夠方便二次開發使用。
Cobbler客戶端Koan支持虛擬機安裝和操做系統從新安裝,使重裝系統更便捷。shell
Cobbler各個組件之間關係apache
cobbler各主要組件關係圖vim
distro->profile-system(可選)windows
distro 發行版
面對不一樣的操做系統
面對同一個操做系統不一樣的版本
profile
核心特性是經過kickstart來部署
system
主要目的配置網絡接口
使用Cobbler,您無需進行人工干預便可安裝機器。Cobbler 設置一個 PXE 引導環境(它還可以使用 yaboot 支持 PowerPC),並控制與安裝相關的全部方面,好比網絡引導服務(DHCP 和 TFTP)與存儲庫鏡像。當但願安裝一臺新機器時,Cobbler 能夠:
使用一個之前定義的模板來配置 DHCP 服務(若是啓用了管理 DHCP)
將一個存儲庫(yum或 rsync)創建鏡像或解壓縮一個媒介,以註冊一個新操做系統
在 DHCP 配置文件中爲須要安裝的機器建立一個條目,並使用您指定的參數(IP 和 MAC 地址)
在 TFTFP 服務目錄下建立適當的 PXE 文件
從新啓動 DHCP服務以反映更改
從新啓動機器以開始安裝(若是電源管理已啓用)
Cobbler 支持衆多的發行版:Red
Hat、Fedora、CentOS、Debian、Ubuntu 和 SuSE。當添加一個操做系統(一般經過使用 ISO 文件)時,Cobbler 知道如何解壓縮合適的文件並調整網絡服務,以正確引導機器。
Cobbler 可以使用kickstart 模板。基於 Red Hat 或 Fedora 的系統使用 kickstart 文件來自動化安裝流程。經過使用模板,您就會擁有基本的kickstart 模板,而後定義如何針對一種配置文件或機器配置而替換其中的變量。例如,一個模板可能包含兩個變量 $domain和$machine_name。在 Cobbler 配置中,一個配置文件指定
domain=mydomain.com,而且每臺使用該配置文件的機器在machine_name
變量中指定其名稱。該配置文件中的全部機器都使用相同的 kickstart 安裝且針對
domain=mydomain.com 進行配置,但每臺機器擁有其本身的機器名稱。您仍然可使用 kickstart 模板在不一樣的域中安裝其餘機器並使用不一樣的機器名稱。
爲了協助管理系統,Cobbler可經過 fence scripts 鏈接到各類電源管理環境。
Cobbler 支持 apc_snmp、bladecenter、bullpap、drac、ether_wake、ilo、integrity、ipmilan、ipmitool、lpar、rsa、virsh 和 wti。要從新安裝一臺機器,可運行 reboot system foo命令,並且 Cobbler 會使用必要的憑據和信息來爲您運行恰當的 fence scripts(好比機器插槽數)。
除了這些特性,還可以使用一個配置管理系統 (CMS)。您有兩種選擇:該工具內的一個內部系統,或者集成一個現有的外部 CMS,好比 Chef 或Puppet。藉助內部系統,您能夠指定文件模板,這些模板會依據配置參數進行處理(與 kickstart 模板的處理方式同樣),而後複製到您指定的位置。若是必須自動將配置文件部署到特定機器,那麼此功能頗有用。
使用 koan 客戶端,Cobbler 可從客戶端配置虛擬機並從新安裝系統。我不會討論配置管理和koan 特性,由於它們不屬於本文的介紹範疇。可是,它們是值得研究的有用特性
[root@cobbler-node1 ~]# cat /etc/redhat-release #系統版本
CentOS Linux release 7.1.1503 (Core)
[root@cobbler-node1 ~]# uname –r #內核版本
3.10.0-229.el7.x86_64
[root@cobbler-node1 ~]# getenforce #檢測selinux是否關閉(必須關閉)
Disabled
[root@cobbler-node1 ~]# systemctl stop firewalld #關閉防火牆
[root@cobbler-node1 ~]# ifconfig eth0|awk -F '[ :]+' 'NR==2 {print $3}' #查看IP地址
10.0.0.101
[root@cobbler-node1 ~]# hostname #查看主機名
cobbler-node1
[root@cobbler-node1 ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo #cobbler安裝必須使用到epel源
提示:
虛擬機網卡採用NAT模式,由於咱們會搭建DHCP服務器,在同一局域網多個DHCP服務會有衝突,而且致使實踐失敗。
[root@cobbler-node1 ~]# yum install cobbler cobbler-web pykickstart httpd dhcp tftp xinetd
cobbler #cobbler程序包
cobbler-web #cobbler的web服務包
pykickstart #cobbler檢查kickstart語法錯誤
httpd #Apache web服務
dhcp #Dhcp服務
tftp #tftp服務
/etc/cobbler # 配置文件目錄
/etc/cobbler/settings # cobbler主配置文件
/etc/cobbler/dhcp.template # DHCP服務的配置模板
/etc/cobbler/tftpd.template # tftp服務的配置模板
/etc/cobbler/rsync.template # rsync服務的配置模板
/etc/cobbler/iso # iso模板配置文件目錄
/etc/cobbler/pxe # pxe模板文件目錄
/etc/cobbler/power # 電源的配置文件目錄
/etc/cobbler/users.conf # Web服務受權配置文件
/etc/cobbler/users.digest # web訪問的用戶名密碼配置文件
/etc/cobbler/dnsmasq.template # DNS服務的配置模板
/etc/cobbler/modules.conf # Cobbler模塊配置文件
/var/lib/cobbler # Cobbler數據目錄
/var/lib/cobbler/config # 配置文件
/var/lib/cobbler/kickstarts # 默認存放kickstart文件
/var/lib/cobbler/loaders # 存放的各類引導程序
/var/www/cobbler # 系統安裝鏡像目錄
/var/www/cobbler/ks_mirror # 導入的系統鏡像列表
/var/www/cobbler/images # 導入的系統鏡像啓動文件
/var/www/cobbler/repo_mirror # yum源存儲目錄
/var/log/cobbler # 日誌目錄
/var/log/cobbler/install.log # 客戶端系統安裝日誌
/var/log/cobbler/cobbler.log # cobbler日誌
cobbler的運行依賴於dhcp、tftp、rsync及dns服務,其中dhcp可由dhcpd(isc)提供,也可由dnsmasq提供;tftp可由tftp-server程序包提供,也可由cobbler功能提供,rsync有rsync程序包提供,dns可由bind提供,也可由dnsmasq提供
cobbler可自行管理這些服務中的部分甚至是所有,但須要配置文件/etc/cobbler/settings中的「manange_dhcp」、「manager_tftpd」、「manager_rsync」、「manager_dns」分別來進行定義,另外,因爲各類服務都有着不一樣的實現方式,如若須要進行自定義,須要經過修改/etc/cobbler/modules.conf配置文件中各服務的模塊參數的值來實現。
[root@cobbler-node1 ~]# systemctl start httpd #啓動apache服務
[root@cobbler-node1 ~]# systemctl start cobblerd #啓動cobbler程序
檢查配置文件,須要在cobblerd和httpd啓動的狀況下檢查
[root@cobbler-node1 ~]# 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 :enable and start rsyncd.service with systemctl
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
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.
如上各問題的解決方法以下所示:
一、修改/etc/cobbler/settings文件中的server參數的值爲提供cobbler服務的主機相應的IP地址或主機名,如server: 10.0.0.101;
[root@cobbler-node1 ~]# sed -i 's/server: 127.0.0.1/server: 10.0.0.101/' /etc/cobbler/settings
二、修改/etc/cobbler/settings文件中的next_server參數的值爲提供PXE服務的主機相應的IP地址,如next_server: 10.0.0.101;
[root@cobbler-node1 ~]# sed -i 's/next_server: 127.0.0.1/next_server: 10.0.0.101/' /etc/cobbler/settings
三、修改/etc/xinetd.d/tftp文件中的disable參數修改成 disable = no
四、執行 cobbler get-loaders 命令便可;不然,須要安裝syslinux程序包,然後複製/usr/share/syslinux/{pxelinux.0,memu.c32}等文件至/var/lib/cobbler/loaders/目錄中;
五、執行 systemctl enable rsyncd命令便可;
六、若是有強迫症能夠選擇 yum –y install debmirror 而後根據錯誤進行解決,通常錯誤以下。
註釋/etc/dedmirror.conf文件中的 @dists=」sid」; @arches=」i386」;
七、[root@cobbler-node1 ~]# openssl passwd -1 -salt '$(openssl rand -hex 4)' 'xuliangwei'
$1$$(openss$.wbDUBV/STL0YaNuAcusK/
[root@cobbler-node1~]# grep "default_password_crypted" /etc/cobbler/settings #替換/etc/cobbler/setting內的default_password_crypted
default_password_crypted:"$1$$(openss$.wbDUBV/STL0YaNuAcusK/"
八、yum –y install cman fence-agents
最後重啓Cobbler:systemctl restart cobblerd
[root@cobbler-node1~]# sed -i 's#manage_dhcp: 0#manage_dhcp: 1#g' /etc/cobbler/settings #使用cobbler管理dhcp
[root@cobbler-node1~]# vim /etc/cobbler/dhcp.template #修改cobbler的dhcp模版,由於cobbler會替換。
subnet 10.0.0.0 netmask 255.255.255.0 {
option routers 10.0.0.2;
option domain-name-servers 10.0.0.2;
option subnet-mask 255.255.255.0;
range dynamic-bootp 10.0.0.200 10.0.0.250;
default-lease-time 21600;
max-lease-time 43200;
next-server $next_server;
[root@cobbler-node1~]# systemctl restart xinetd #重啓xinetd
[root@cobbler-node1~]# systemctl restart cobblerd #重啓cobbler
[root@cobbler-node1~]# cobbler sync #同步最新cobbler配置,能夠看具體作了哪些操做
task started: 2016-04-29_234117_sync
task started (id=Sync, time=Fri Apr 29 23:41:17 2016)
running pre-sync triggers
cleaning trees
removing: /var/lib/tftpboot/pxelinux.cfg/default
removing: /var/lib/tftpboot/grub/images
removing: /var/lib/tftpboot/grub/grub-x86.efi
removing: /var/lib/tftpboot/grub/grub-x86_64.efi
removing: /var/lib/tftpboot/grub/efidefault
removing: /var/lib/tftpboot/s390x/profile_list
copying bootloaders
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 images
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 ***
cobbler變得可用的第一步爲定義distro,其能夠經過爲其指定外部的安裝引導內核及ramdisk文件的方式實現。
若是已經有完成的安裝樹(如os的安裝鏡像)則推薦使用improt之間導入的方式進行。
[root@cobbler-node1 ~]# mount /dev/cdrom /mnt/ #掛在ISO光盤至服務器
mount:/dev/sr0 is write-protected, mounting read-only
[root@cobbler-node1 ~]# cobbler import - -path=/mnt/ - -name=CentOS-7.1-x86_64-distro - -arch=x86_64
# - -path 鏡像路徑
# - -name 爲安裝源定義一個名字
# - -arch 指定安裝源是32位、64位、ia64, 目前支持的選項有: x86│x86_64│ia64
# 安裝源的惟一標示就是根據name參數來定義,本例導入成功後,安裝源的惟一標示就是:CentOS-7.1-distro-x86_64。
# 鏡像存放目錄,cobbler會將鏡像中的全部安裝文件拷貝到本地一份,放在/var/www/cobbler/ks_mirror下的CentOS-7.1-x86_64-distro-x86_64目錄下。所以/var/www/cobbler目錄必須具備足夠容納安裝文件的空間。
task started: 2016-04-30_010122_import
task started (id=Media import, time=Sat Apr 30 01:01:22 2016)
Found a candidate signature: breed=redhat, version=rhel6
Found a candidate signature: breed=redhat, version=rhel7
Found a matching signature: breed=redhat, version=rhel7
Adding distros from path /var/www/cobbler/ks_mirror/CentOS-7.1-x86_64-distro-x86_64:
creating new distro: CentOS-7.1-distro-x86_64
trying symlink: /var/www/cobbler/ks_mirror/CentOS-7.1-x86_64-distro-x86_64 -> /var/www/cobbler/links/CentOS-7.1-distro-x86_64
creating new profile: CentOS-7.1-distro-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-7.1-x86_64-distro-x86_64 for CentOS-7.1-distro-x86_64
processing repo at : /var/www/cobbler/ks_mirror/CentOS-7.1-x86_64-distro-x86_64
need to process repo/comps: /var/www/cobbler/ks_mirror/CentOS-7.1-x86_64-distro-x86_64
looking for /var/www/cobbler/ks_mirror/CentOS-7.1-x86_64-distro-x86_64/repodata/*comps*.xml
Keeping repodata as-is :/var/www/cobbler/ks_mirror/CentOS-7.1-x86_64-distro-x86_64/repodata
*** TASK COMPLETE ***
[root@cobbler-node1 ~]# cobbler distro list #列出全部的distro
CentOS-7.1-distro-x86_64
[root@cobbler-node1 ~]# cobbler profile list #導入distro會自動生成profile
CentOS-7.1-distro-x86_64
若是有kickstart文件,也可使用--kickstart=/path/to/kickstart_file進行導入,所以import會自動爲導入的distro生成一個profile
cobbler使用profile來爲特定的需求類別提供鎖須要安裝的配置,即在distro的基礎上經過提供kiskstart文件來生成一個特定的系統安裝配置。distro的profile能夠出如今pxe的引導菜單中做爲安裝的選擇之一
Cobbler-CentOS-7.1-x86_64.cfg
Cobbler-CentOS-6.7-x86_64.cfg
配置文件上傳至至附件,在文章底部
默認是有kickstart文件的,因此edit,若是沒有kickstart文件能夠add
[root@cobbler-node1 ~]# cobbler profile edit - -name=CentOS-7.1-x86_64-distro - -kickstart=/var/lib/cobbler/kickstarts/Cobbler-CentOS-7.1-x86_64.cfg #指定ks路徑
CentOS7系統網卡名變成eno...這種,爲了運維標準化,咱們須要修改成咱們經常使用的eth0,使用下面的參數。但要注意是CentOS7才須要下面的步驟,CentOS6不須要。
[root@cobbler-node1 ~]# cobbler profile edit - -name=CentOS-7.1-x86_64-distro - -kopts='net.ifnames=0 biosdevname=0' #修改centos7內核
新部署機器安裝yum源,並同步。建議使用內網yum源,在這裏使用阿里雲yum源
[root@cobbler-node1 ~]# cobbler repo add - -name=base - -mirror=http://mirrors.aliyun.com/centos/7/os/x86_64/Packages/ - -arch=x86_64 --breed=yum #添加yum源
[root@cobbler-node1 ~]# cobbler reposync #同步yum源
[root@cobbler-node1 ~]# cobbler sync #每次修改profile都須要同步
新建一臺虛擬機,經過網絡啓動便可。(我這裏虛擬機使用的是NAT模式,而且關閉DHCP功能)
用戶root
密碼xuliangwei
openssl生成時指定
圖1-1進入Cobbler選擇對應操做系統安裝
圖1-2爲登錄CentOS7系統
圖1-3檢查系統版本內核IP地址
因爲kickstart指定某臺服務器使用某個ks文件比較複雜,因此引用Cobbler就很簡單。經過物理MAC地址來區分。
筆者QQ:572891887
Linux架構交流羣:471443208
system主要目的配置網絡接口,經過system來固定機器的IP、掩碼、網關、DNS、主機名、等等實現基礎環境標準化
圖1-4查看設備Mac地址
根據機器的MAC地址,自動綁定IP,網關,dns等
[root@cobbler-node1 ~]# cobbler system add - -name=xuliangwei-pc - -mac=00:0C:29:6E:41:CB - -profile=Centos7.1-profile-x86_64 \
- -ip-address=10.0.0.110 - -subnet=255.255.255.0 - -gateway=10.0.0.2 - -interface=eth0 \
- -static=1 - -hostname=xuliangwei.com
- -name-servers="114.114.114.114 8.8.8.8"
[root@cobbler-node1 ~]# cobbler sync
[root@linux-node1 ~]# cobbler system list
xuliangwei-pc
圖1-5不進行詢問,自動安裝
圖1-6登錄系統檢查相關信息
有時候須要給我的打廣告,能夠經過這種方式
[root@cobbler-node1 ~]# grep "xuliangwei" /etc/cobbler/pxe/pxedefault.template #自定義裝機頁面
MENU TITLE Xuliangwei | http://xuliangwei.com
[root@cobbler-node1 ~]# cobbler sync #同步
圖1-7自定義登錄界面
新版cobbler的web界面使用的是https,登陸https://10.0.0.7/cobbler_web
cobbler_web支持多種認證方式,如authn_configfil、authn_ldap或authn_pam等,默認爲authn_denyall,即拒絕全部用戶登錄。
下面說明三種能認證用戶登陸cobbler_web的方式
1.使用authn_pam模塊認證cobbler_web用戶
首先修改modules中的[authentication]段中的module參數的值爲authn_pam
接着建立系統用戶,併爲用戶設定密碼
然後將設定的系統用戶添加至cobbler_web的admin組中,修改/etc/cobbler/users.conf
文件,將設定的用戶添加爲admin參數的值便可
2.使用authn_configfile模塊認證cobbler_web用戶
首先修改modules中的[authentication]段中的module參數的值爲authn_configfile
添加第一用戶時,須要爲htdigest命令使用「-c」 etc/cobbler/users.digest,後續添加其餘用戶則不能再使用,同步cobbler重啓httpd以及cobbler
3.使用cobbler默認的web帳號密碼認證
user:cobbler
pass:cobbler
圖2-1訪問https://IP/cobbler_web
圖2-2cobbler_web界面
圖2-3查看當前distro
圖2-3查看當前profile
圖2-3查看當前system
圖2-3查看當前repos
筆者QQ:572891887
Linux架構交流羣:471443208
Kickstart附件