Cobbler原理與實戰

Cobbler實戰

Cobbler是一個快速網絡安裝linux的服務,而且在經過調整也可以支持網絡安裝windows。

使用簡單的命令即可完成PXE網絡安裝環境的配置,同時還可以管理DHCP、DNS、TFTP、RSYNC以及yum倉庫、構造系統ISO鏡像。 Cobbler支持命令行管理,web界面管理,還提供了API接口,可以方便二次開發使用,Cobbler客戶端Koan支持虛擬機安裝和操作系統重新安裝。

 

Cobbler可以幹什麼

  • 服務器上架後,可以手動選擇需要安裝的系統(如:Centos6.8 或 Centos 7.3)

  • 服務器上架後,能夠根據需求,安裝配置操作系統(如:修改IP地址、主機名、選擇安裝包)

  • 系統安裝後,可以自定義的執行腳本,完成系統基礎軟件初始化(如:Zabbix安裝配置、SaltStack安裝配置)

  • 可以當內部YUM源,並在系統安裝時進行初始化

  • 可以重裝系統

  • Cobbler支持API,可以無縫融合到自建運維平臺中

  • Cobbler支持網卡的路由配置、DNS配置、bonding

 

Cobbler基礎原理

Cobbler是通過將DHCP、TFTP、DNS、HTTP等服務進行集成,創建一箇中央管理節點,其可以實現的功能有配置服務,創建存儲庫,解壓縮操作系統媒介,代理或集成一個配置管理系統,控制電源管理等。 Cobbler的最終目的是實現無需進行人工干預即可安裝機器。在進行進一步的操作之前,我們有必要先了解下pxe和kickstart 。

PXE概述

預啓動執行環境(Preboot eXecution Environment,PXE,也被稱爲預執行環境)是讓計算機通過網卡獨立地使用數據設備(如硬盤)或者安裝操作系統。Intel 搞出來的。

其工作原理:

在這裏插入圖片描述

  • PXE Client發送廣播包請求DHCP分配IP地址DHCP

  • Server回覆請求,給出IP地址以及Boot

  • Server的地址PXE下載引導文件執行引導程序

  • 總結來說,PXE主要是通過廣播的方式發送一個包,並請注獲取一個地址,而後交給TFTP程序下載一個引導文件。

Kickstart 概述

Kickstart 是紅帽開發的一種工具,我們可以簡單理解爲一個自動安裝應答配置管理程序。通過讀取這個配置文件,系統知道怎麼去分區,要安裝什麼包,配什麼IP,優化什麼內核參數等等。其主要有以下部分組成:

  • Kickstart 安裝選項,包含語言的選擇,防火牆,密碼,網絡,分區的設置等;

  • %Pre 部分,安裝前解析的腳本,通常用來生成特殊的ks配置,比如由一段程序決定磁盤分區等;

  • %Package 部分,安裝包的選擇,可以是 @core 這樣的group的形式,也可以是這樣 vim-* 包的形式;

  • %Post 部分,安裝後執行的腳本,通常用來做系統的初始化設置。比如啓動的服務,相關的設定等。

 

Cobbler的設計方式

Cobbler 的配置結構基於一組註冊的對象。每個對象表示一個與另一個實體相關聯的實體(該對象指向另一個對象,或者另一個對象指向該對象)。當一個對象指向另一個對象時,它就繼承了被指向對象的數據,並可覆蓋或添加更多特定信息。以下對象類型的定義爲:

  • 發行版:表示一個操作系統。它承載了內核和 initrd 的信息,以及內核參數等其他數據。

  • 配置文件:包含一個發行版、一個 kickstart 文件以及可能的存儲庫,還包含更多特定的內核參數等其他數據。

  • 系統:表示要配給的機器。它包含一個配置文件或一個鏡像,還包含 IP 和 MAC 地址、電源管理(地址、憑據、類型)以及更爲專業的數據等信息。

  • 存儲庫:保存一個 yum 或 rsync 存儲庫的鏡像信息。

  • 鏡像:可替換一個包含不屬於此類別的文件的發行版對象(例如,無法分爲內核和 initrd 的對象)

 

Cobbler工作原理

在這裏插入圖片描述

server端:

第一步,啓動Cobbler服務 
第二步,進行Cobbler錯誤檢查,執行cobbler check命令 
第三步,進行配置同步,執行cobbler sync命令 
第四步,複製相關啓動文件文件到TFTP目錄中 
第五步,啓動DHCP服務,提供地址分配 
第六步,DHCP服務分配IP地址 
第七步,TFTP傳輸啓動文件 
第八步,Server端接收安裝信息 
第九步,Server端發送ISO鏡像與Kickstart文件

 

Client端:

第一步,客戶端以PXE模式啓動 
第二步,客戶端獲取IP地址 
第三步,通過TFTP服務器獲取啓動文件 
第四步,進入Cobbler安裝選擇界面 
第五步,客戶端確定加載信息 
第六步,根據配置信息準備安裝系統 
第七步,加載Kickstart文件 
第八步,傳輸系統安裝的其它文件 
第九步,進行安裝系統

 

Cobbler安裝

基礎環境部署

關閉selinux:

setenforce 0

yum配置:

#講舊倉庫配置轉移到某目錄中 
mkdir /etc/yum.repos.d/old && mv /etc/yum.repos.d/C* /etc/yum.repos.d/old/ 

#安裝wget,添加aliyun Base倉庫及epel倉庫 
yum install -y wget 
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo 
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

 

Cobbler服務部署

安裝服務:

yum install cobbler cobbler-web dhcp tftp-server pykickstart httpd xinetd python-ctypes -y

服務自啓動:

systemctl enable httpd 
systemctl enable xinetd 
systemctl enable rsyncd 
systemctl enable tftp 
systemctl enable cobblerd

啓動服務:

systemctl restart httpd 
systemctl restart xinetd 
systemctl restart tftp 
systemctl restart cobblerd

執行cobbler check檢查運行環境:

[root@l27-server1 ~]# 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 : SELinux is enabled. Please review the following wiki page for details on ensuring cobbler works correctly in your SELinux environment:
    https://github.com/cobbler/cobbler/wiki/Selinux
4 : change 'disable' to 'no' in /etc/xinetd.d/tftp
5 : 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.
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.

問題1,2,7:

默認密碼,setting,和dhcp

生成新的默認密碼

[root@l27-server1 ~]# openssl passwd -1 -salt `openssl rand 15 -base64` '123' 
$1$Yo1o0fBf$pm5RkWqGdviwsu4YiWhF9/

注意: 此密碼是系統安裝後的默認密碼,建議使用強密碼

修改setting配置文件

sed -i 's%^server: 127.0.0.1%server: 192.168.217.155%g' /etc/cobbler/settings 
sed -i 's%^next_server: 127.0.0.1%next_server: 192.168.217.155%g' /etc/cobbler/settings 
sed -i 's%manage_dhcp: 0%manage_dhcp: 1%g' /etc/cobbler/settings 
sed -i 's%^default_password_crypted.*%default_password_crypted: "$1$Yo1o0fBf$pm5RkWqGdviwsu4YiWhF9/"%g' /etc/cobbler/settings

配置DHCP /etc/dhcp/dhcpd.conf(根據需求更改)

subnet 192.168.217.0 netmask 255.255.255.0 { 
	option routers 192.168.217.254; #網關地址 
	option domain-name-servers 114.114.114.114; #dns地址 
	option subnet-mask 255.255.255.0; #掩碼 
	range dynamic-bootp 192.168.217.10 192.168.217.100; #地址分配範圍 		default-lease-time 21600; 
	max-lease-time 43200; 
	next-server $next_server; #引用上面setting配置的nex-server值 
	filename "/pxelinux.0"; 
}

還要修改/etc/cobbler/dhcp.template 文件

subnet 192.168.217.0 netmask 255.255.255.0 {
     option routers             192.168.217.155;
     option domain-name-servers 114.114.114.114;
     option subnet-mask         255.255.255.0;
     range dynamic-bootp        192.168.217.10 192.168.217.100;
     default-lease-time         21600;
     max-lease-time             43200;
     next-server                $next_server;
     class "pxeclients" {

問題3:

關閉selinux

[root@l27-server1 ~]# setenforce 0 
#編輯/etc/selinux/config,並重啓生效 
SELINUX=disabled
init 6

問題4

啓動tftp服務

編輯tftp服務配置文件 /etc/xinetd.d/tftp

service tftp 
{ 
	socket_type = dgram 
	protocol = udp 
	wait = yes 
	user = root 
	server = /usr/sbin/in.tftpd 
	server_args = -s /var/lib/tftpboot 
	disable = no #修改此處值爲
	no per_source = 11
	cps = 100 2 
	flags = IPv4 
}

問題5:

獲取引導文件

[root@l27-server1 ~]# cobbler get-loaders

截圖

在這裏插入圖片描述

問題6:

debian相關文件獲取(如不需要部署debian系統可省略)

[root@l27-server1 ~]# yum -y install debmirror 
#修改/etc/debmirror.conf配置文件,註釋掉@dists和@arches兩行 
[root@l27-server1 ~]# vi /etc/debmirror.conf 
... 
#@dists="sid"; 
@sections="main,main/debian-installer,contrib,non-free"; 
#@arches="i386"; 
...

問題8:

電源管理工具(可選)

yum -y install fence-agents

重啓服務並同步狀態

[root@l27-server1 ~]# systemctl restart cobblerd 
[root@l27-server1 ~]# cobbler check
No configuration problems found.  All systems go.

這就完成

 

cobbler基礎配置

同步配置是配置生效

cobbler sync

配置distro:

主要用來定義和設置系統發行版本,裏包含了系統的內核,引導文件,以及安裝包等內容,就相當於一個LINUX系統的ISO鏡像。如:多個發行版本,同一發行版不同版本號。

1.首先上傳系統iso鏡像文件,此處以ubuntu12.04鏡像爲例,其他鏡像做法類似

wget http://releases.ubuntu.com/precise/ubuntu-12.04.5-server-amd64.iso

[root@l27-server1 ~]# mkdir -p /system/ubuntu1204 ; mount -t iso9660 ~/ubuntu-12.04.5-server-amd64.iso /system/ubuntu1204

2.導入鏡像

[root@l27-server1 ~]# cobbler import --name=ubuntu1204 --path=/system/ubuntu1204 --arch=x86_64 
task started: 2020-04-30_102441_import
task started (id=Media import, time=Thu Apr 30 10:24:41 2020)
Found a candidate signature: breed=freebsd, version=10.1
Found a candidate signature: breed=freebsd, version=10.0
Found a candidate signature: breed=freebsd, version=8.3
Found a candidate signature: breed=freebsd, version=8.2
Found a candidate signature: breed=freebsd, version=8.4
Found a candidate signature: breed=freebsd, version=11.0
Found a candidate signature: breed=freebsd, version=10.3
Found a candidate signature: breed=freebsd, version=11.2
Found a candidate signature: breed=freebsd, version=11.1
Found a candidate signature: breed=freebsd, version=12.0
Found a candidate signature: breed=freebsd, version=10.2
Found a candidate signature: breed=freebsd, version=11.3
Found a candidate signature: breed=freebsd, version=9.0
Found a candidate signature: breed=freebsd, version=9.1
Found a candidate signature: breed=freebsd, version=9.2
Found a candidate signature: breed=freebsd, version=9.3
Found a candidate signature: breed=ubuntu, version=precise
running: /usr/bin/file /var/www/cobbler/ks_mirror/ubuntu1204-x86_64/dists/precise/Release.gpg
received on stdout: /var/www/cobbler/ks_mirror/ubuntu1204-x86_64/dists/precise/Release.gpg: PGP signature

received on stderr: 
running: /usr/bin/file /var/www/cobbler/ks_mirror/ubuntu1204-x86_64/dists/precise/Release
received on stdout: /var/www/cobbler/ks_mirror/ubuntu1204-x86_64/dists/precise/Release: ASCII text

received on stderr: 
Found a matching signature: breed=ubuntu, version=precise
Adding distros from path /var/www/cobbler/ks_mirror/ubuntu1204-x86_64:
creating new distro: ubuntu1204-x86_64
trying symlink: /var/www/cobbler/ks_mirror/ubuntu1204-x86_64 -> /var/www/cobbler/links/ubuntu1204-x86_64
creating new profile: ubuntu1204-x86_64
associating repos
checking for apt repo(s)
adding apt repo for ubuntu1204-x86_64
Added repos for ubuntu1204-x86_64
*** TASK COMPLETE ***

3.查看distro配置

[root@localhost ~]# cobbler distro report 
Name                           : ubuntu1204-x86_64
Architecture                   : x86_64
TFTP Boot Files                : {}
Breed                          : ubuntu
Comment                        : 
Fetchable Files                : {}
Initrd                         : /var/www/cobbler/ks_mirror/ubuntu1204-x86_64/install/netboot/ubuntu-installer/amd64/initrd.gz
Kernel                         : /var/www/cobbler/ks_mirror/ubuntu1204-x86_64/install/netboot/ubuntu-installer/amd64/linux
Kernel Options                 : {}
Kernel Options (Post Install)  : {}
Kickstart Metadata             : {'tree': 'http://@@http_server@@/cblr/links/ubuntu1204-x86_64'}
Management Classes             : []
OS Version                     : precise
Owners                         : ['admin']
Red Hat Management Key         : <<inherit>>
Red Hat Management Server      : <<inherit>>
Template Files                 : {}

查看ubuntu相關文件:

[root@localhost ~]# cobbler list 
distros: 
	ubuntu1204-x86_64 #鏡像 

profiles: 
	ubuntu1204-x86_64 #配置,用於配置關聯自動應答ks文件 

systems: #可以爲不同的主機根據MAC設置地址及安裝參數 

repos: 
	ubuntu1204-x86_64 #倉庫,用於自定義倉庫 

images: 

mgmtclasses: 

packages: 

files:

(自定義)配置profifile:定義自動安裝應答文件

主要用來定義kickstart配置文件,安裝時依據配置文件進行定製化安裝,配置此項時要和distro進行關聯。

[root@localhost ~]# cobbler profile report 
Name                           : ubuntu1204-x86_64
TFTP Boot Files                : {}
Comment                        : 
DHCP Tag                       : default
Distribution                   : ubuntu1204-x86_64
Enable gPXE?                   : 0
Enable PXE Menu?               : 1
Fetchable Files                : {}
Kernel Options                 : {}
Kernel Options (Post Install)  : {}
Kickstart                      : /var/lib/cobbler/kickstarts/sample.seed #關聯的kickstart文件 (ubuntu和centos的ks文件格式有很大區別,不能混用)
Kickstart Metadata             : {}
Management Classes             : []
Management Parameters          : <<inherit>>
Name Servers                   : []
Name Servers Search Path       : []
Owners                         : ['admin']
Parent Profile                 : 
Internal proxy                 : 
Red Hat Management Key         : <<inherit>>
Red Hat Management Server      : <<inherit>>
Repos                          : []
Server Override                : <<inherit>>
Template Files                 : {}
Virt Auto Boot                 : 1
Virt Bridge                    : xenbr0
Virt CPUs                      : 1
Virt Disk Driver Type          : raw
Virt File Size(GB)             : 5
Virt Path                      : 
Virt RAM (MB)                  : 512
Virt Type                      : kvm

配置repo:定義本地或遠程倉庫源

查看默認repo

[root@localhost ~]# cobbler repo report 
Name                           : ubuntu1204-x86_64
Apt Components (apt only)      : ['main', 'universe']
Apt Dist Names (apt only)      : ['precise', 'precise-updates', 'precise-security']
Arch                           : x86_64
Breed                          : apt
Comment                        : 
Createrepo Flags               : <<inherit>>
Environment Variables          : {}
Keep Updated                   : True
Mirror                         : http://archive.ubuntu.com/ubuntu
Mirror locally                 : True
Owners                         : ['admin']
Priority                       : 99
External proxy URL             : 
RPM List                       : []
Yum Options                    : {'--verbose': '~', '--ignore-release-gpg': '~'}

自定義repo

cobbler repo add --help #創建
repo cobbler repo edit --help #修改repo

(自定義)配置system

主要用來定義內核參數,電源管理,計算機名,網絡接口參數等(客戶機),如:根據mac地址配置IP地址,通過和

profifile,distro關聯實現自動化安裝。

cobbler system add --help #創建
system cobbler system edit --help #修改system

測試

新建一臺虛擬機,不用選光盤映像文件,直接打開

啓動看到如下界面

在這裏插入圖片描述

選擇第二個菜單,是之前製作的distro,接下來服務器會自動按照之前的ks文件自動安裝

然後就等等等。

在這裏插入圖片描述

進入cobbler界面
https://192.168.217.155/cobbler_web

在這裏插入圖片描述

也可以在這創建新系統

在這裏插入圖片描述

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