preface
我們之前批量安裝操作系統的時候都是采用pxe來安裝,pxe也是通過網絡安裝操作系統的,但是PXE依賴於DHCP,HTTP/TFTP,kicstart等支持。安裝流程如下所示:
對於上面的PXE安裝流程,我們需要知道我們做了以下的工作:
- 配置服務,如DHCP、TFTP、(HTTP、FTP、和NFS)
- 在dhcp和TFTP配置文件中填入客戶端機器的信息。
- 創建自動部署文件(比如kickstart)
- 將安裝的媒介解壓縮到HTTP/FTP/NFS存儲庫中。
PXE裝機也是一個不錯的選擇對於批量裝機的話,現在呢我們學習另一種安裝操作系統的軟件cobbler。它具有以下功能:
- 使用一個以前定義的模版來配置DHCP服務(如果啟用了管理DHCP)
- 將一個存儲庫(yum或rsync)建立鏡像或者解壓縮一個媒介,以注冊一個新的操作系統。
- 在DHCP配置文件中為需要安裝的機器創建一個條目並使用我們指定的參數(IP、mac地址)
- 在TFTP服務目錄下創建適當的PXE文件
- 重新啟動DHCP服務以反映更改
- 重新啟動機器以開始安裝(如果電源管理已經啟用的話。)
cobbler簡介:
- Disribution : 發行內核,initrd等東西
- Repository: 創建倉庫,比如yum倉庫等。
- system: 通過mac地址來定制化系統
- profile: 對需要安裝某個系統的所有配置。
基礎環境介紹
首先我先說說的環境吧
IP地址 | 主機名 |
---|---|
192.168.56.11 | linux-node1.example.com |
- 內核為3.10.0-514.2.2.el7.x86_64
- 網卡名字設置為了eth0。
- 關閉了selinux,iptables。
- 時間同步ntp.chinacache.com
- hosts文件里面需要寫下hostname,便於解析。
開始安裝cobbler
我們采用yum安裝的方式來安裝:
1.安裝cobbler以及相關的軟件
[root@linux-node1 ~]# yum -y install httpd dhcp tftp python-ctypes cobbler xinetd
2.啟動服務(先起服務是因為這樣才知道我們沒有配置哪些東東)
[root@linux-node1 ~]# systemctl start httpd
[root@linux-node1 ~]# systemctl enable httpd
[root@linux-node1 ~]# systemct1 enable cobblerd
[root@linux-node1 ~]# systemctl start cobblerd
3.看看哪些配置文件沒有改,下面的工作就是要處理掉這些提示信息。這樣cobbler就能夠正常工作了。
[root@linux-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.
# 設置PXE文件
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.
# 設置tftp
3 : change 'disable' to 'no' in /etc/xinetd.d/tftp
# 把網絡的boot-loaders通過cobbler get-loaders后放在/var/lib/cobbler/loaders
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.
# 啟動rsync服務
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.
4.修改配置文件
[root@linux-node1 ~]# vim /etc/cobbler/settings
server: 192.168.56.11 # 把這些地址都改為本機的IP
next_server: 192.168.56.11
5.再次檢測配置文件,顯然這提示的數量下降到了5,我們在逐個擊破。
[root@linux-node1 ~]# cobbler check #
The following are potential configuration items that you may want to fix:
1 : change 'disable' to 'no' in /etc/xinetd.d/tftp
2 : enable and start rsyncd.service with systemctl
3 : debmirror package is not installed, it will be required to manage debian deployments and repositories
4 : 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
5 : fencing tools were not found, and are required to use the (optional) power management features. install cman or fence-agents to use them
6.修改xinetd的配置文件以及啟動和啟動rsync
[root@linux-node1 ~]# vim /etc/xinetd.d/tftp
disable : no
[root@linux-node1 ~]# systemctl start rsyncd
[root@linux-node1 ~]# systemctl enable rsyncd.service
[root@linux-node1 ~]# systemctl restart xinetd
7.配置cobbler的密碼
[root@linux-node1 ~]# openssl passwd -1 -salt '123123' '123123'
$1$123123$MAV.kVI/b3swmFLErPD2b0
[root@linux-node1 ~]# vim /etc/cobbler/settings
default_password_crypted: "$1$123123$MAV.kVI/b3swmFLErPD2b0"
為什么我們這樣設置密碼呢?因為我們看cobbler這樣提示的:try: "openssl passwd -1 -salt 'random-phrase-here' 'your-password-here'" to generate new one
,所以我們使用這個命令來做。
8.下載loader
[root@cobbler ~]# cobbler get-loaders
9.最后查看cobbler check
[root@linux-node1 ~]# cobbler check
The following are potential configuration items that you may want to fix:
1 : debmirror package is not installed, it will be required to manage debian deployments and repositories
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.
我們可以看到還有2條提示信息,我們可以暫且忽略他們。不需要關注。
配置cobbler-DHCP
我們配置cobbler的DHCP,使其cobbler來控制dhcp服務在進行裝機的時候。
1.修改cobbler配置:
[root@linux-node1 cobbler]# vim /etc/cobbler/settings
manage_dhcp: 1 # set to 1 to enable Cobbler's DHCP management features.
2.修改dhcp.templates配置文件
[root@linux-node1 ~]# cd /etc/cobbler/
[root@linux-node1 cobbler]# vim dhcp.template # 主要是修改了下面幾項
subnet 192.168.56.0 netmask 255.255.255.0 { # 子網
option routers 192.168.56.2; # 網關
option domain-name-servers 192.168.56.2; # dns-server
option subnet-mask 255.255.255.0; # 子網掩碼
range dynamic-bootp 192.168.56.100 192.168.56.254; # 地址池
3.重啟服務並同步配置,改完dhcp必須要sync同步配置。
[root@linux-node1 cobbler]# systemctl restart cobblerd
[root@linux-node1 cobbler]# cobbler sync
task started: 2017-02-25_051458_sync
task started (id=Sync, time=Sat Feb 25 05:14:58 2017)
...省略N行提示
generating /etc/dhcp/dhcpd.conf # 注意這個dhcp,會自動生成我們剛才的配置。
*** TASK COMPLETE ***
4.檢測dhcp端口
[root@linux-node1 cobbler]# netstat -lnup |grep dhcp
udp 0 0 0.0.0.0:67 0.0.0.0:* 31034/dhcpd
udp 0 0 0.0.0.0:55385 0.0.0.0:* 31034/dhcpd
udp6 0 0 :::35318 :::* 31034/dhcpd
導入CentOs-7的鏡像
我們先上傳一個鏡像到linux-node1.example.com上。然后開始導入:
[root@linux-node1 cobbler]# mount -o loop /opt/CentOS-7.0-1406-x86_64-DVD.iso /mnt/
mount: /dev/loop0 is write-protected, mounting read-only
[root@linux-node1 cobbler]# cobbler import --path=/mnt --name=CentOS-7.0-1406-x86_64 --arch=x86_64
導入時間有點長,稍等下。
導入的文件是放在這里/var/www/cobbler/ks_mirror的:
[root@linux-node1 ks_mirror]# pwd
/var/www/cobbler/ks_mirror
[root@linux-node1 ks_mirror]# ls
CentOS-7.0-1406-x86_64 config
之所以導入到這里,是因為apache的配置文件,詳情可看/etc/httpd/conf.d/cobbler.conf
.
導入完鏡像以后,那么就使查看下cobbler
[root@linux-node1 cobbler]# cobbler list
distros:
CentOS-7.0-1406-x86_64
profiles:
CentOS-7.0-1406-x86_64
systems:
repos:
images:
mgmtclasses:
packages:
files:
導入kickstarts配置文件
上面有了鏡像,那么下一步我們就需要導入kickstarts了。
[root@linux-node1 kickstarts]# pwd
/var/lib/cobbler/kickstarts
[root@linux-node1 kickstarts]# ll -rt
total 52
-rw-r--r-- 1 root root 5879 Nov 16 11:09 sample.seed
-rw-r--r-- 1 root root 3419 Nov 16 11:09 sample_old.seed
-rw-r--r-- 1 root root 1784 Nov 16 11:09 sample.ks
-rw-r--r-- 1 root root 386 Nov 16 11:09 sample_esxi5.ks
-rw-r--r-- 1 root root 324 Nov 16 11:09 sample_esxi4.ks
-rw-r--r-- 1 root root 0 Nov 16 11:09 sample_esx4.ks
-rw-r--r-- 1 root root 1825 Nov 16 11:09 sample_end.ks
-rw-r--r-- 1 root root 2916 Nov 16 11:09 sample_autoyast.xml
-rw-r--r-- 1 root root 292 Nov 16 11:09 pxerescue.ks
-rw-r--r-- 1 root root 1424 Nov 16 11:09 legacy.ks
-rw-r--r-- 1 root root 22 Nov 16 11:09 esxi5-ks.cfg
-rw-r--r-- 1 root root 22 Nov 16 11:09 esxi4-ks.cfg
-rw-r--r-- 1 root root 115 Nov 16 11:09 default.ks
drwxr-xr-x 2 root root 54 Feb 25 04:09 install_profiles
到了這一步,我把自己寫的kickstart文件給上傳上去,然后執行下面命令導入剛才的:
[root@linux-node1 kickstarts]# cobbler profile report
[root@linux-node1 kickstarts]# cobbler profile list
CentOS-7.0-1406-x86_64
[root@linux-node1 kickstarts]# cobbler profile edit --name CentOS-7.0-1406-x86_64 --kickstart=/var/lib/cobbler/kickstarts/CentOS-7.1-x86_64_cobbler.cfg #這里寫我們剛才上傳的配置文件。
[root@linux-node1 kickstarts]# cobbler profile edit --name CentOS-7.0-1406-x86_64 --kopts='net.ifnames=0 biosdevname=0' # 添加內核參數在grub配置文件里面,換句話說就是系統啟動的時候。
[root@linux-node1 kickstarts]# cobbler profile report # 再次執行這個,看有沒有修改成功
我的kickstart文件內容如下:
#platform=x86, AMD64, or Intel EM64T
#System language
lang en_US
#System keyboard
keyboard us
#Sytem timezone
timezone Asia/Shanghai
#Root password
rootpw --iscrypted $default_password_crypted
#rootpw --iscrypted $1$ops-node$7hqdpgEmIE7Z0RbtQkxW20
#Use text mode install
text
#Install OS instead of upgrade
install
#Use NFS installation Media
url --url=$tree
#url --url=http://192.168.56.11/CentOS-7.1-x86_64
#System bootloader configuration
bootloader --location=mbr
#Clear the Master Boot Record
zerombr
#Partition clearing information
clearpart --all --initlabel
#Disk partitioning information
part /boot --fstype xfs --size 1024 --ondisk sda
part swap --size 1024 --ondisk sda
part / --fstype xfs --size 1 --grow --ondisk sda
#System authorization infomation
auth --useshadow --enablemd5
#Network information
$SNIPPET('network_config')
#network --bootproto=dhcp --device=eth0 --onboot=on
# Reboot after installation
reboot
#Firewall configuration
firewall --disabled
#SELinux configuration
selinux --disabled
#Do not configure XWindows
skipx
#Package install information
%pre
$SNIPPET('log_ks_pre')
$SNIPPET('kickstart_start')
$SNIPPET('pre_install_network_config')
# Enable installation monitoring
$SNIPPET('pre_anamon')
%end
%packages
@ base
@ core
sysstat
iptraf
ntp
lrzsz
ncurses-devel
openssl-devel
zlib-devel
OpenIPMI-tools
mysql
nmap
screen
%end
%post
systemctl disable postfix.service
# Start yum configuration
$yum_config_stanza
# End yum configuration
rpm -ihv https://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
%end
最后一步同步數據:
[root@linux-node1 kickstarts]# cobbler sync
安裝CentOs-7操作系統。
- 此時Cobbler已經能夠安裝操作系統了,下一步我們就創建一台虛擬機來安裝操作系統吧。
- 我們使用vmware來創建一個虛擬機,同時調整vmware的網絡參數,關閉vmware的DHCP功能,同事確保新創建的虛擬機和cobbler主機在同一網段。
- 啟動剛才新創建的虛擬機,選擇網絡安裝操作系統就可以了。
附加功能
1.修改安裝界面的標題
我們可以自定義安裝的tittle,如下所示:
[root@linux-node1 pxe]# vim /etc/cobbler/pxe/pxedefault.template
MENU TITLE | http://cobbler.github.io/ # 自定義標題。
TIMEOUT 200 # 超時時間20S,單位為毫秒
2.使用cobbler-web功能
我們安裝下cobbler-web功能
[root@linux-node1 pxe]# yum -y install cobbler-web
[root@linux-node1 pxe]#systemctl restart httpd.service # 重啟下apache即可,因為配置文件有變動
然后訪問登陸即可:
https://192.168.56.11/cobbler_web/,默認用戶名密碼是cobbler/cobbler
登陸的用戶名密碼在這里存放着:
[root@linux-node1 pxe]# tail /etc/cobbler/users.conf # 存放用戶名權限的
[admins]
admin = ""
cobbler = ""
[root@linux-node1 pxe]# tail /etc/cobbler/users.digest # 存放密碼的
cobbler:Cobbler:a2d6bae81669d707b72c0bd9806e01f3
看了 上面的用戶名密碼的存放文件后,那么下面我們修改下他的密碼,使用下面的命令:
[root@linux-node1 pxe]# htdigest /etc/cobbler/users.digest "Cobbler" cobbler #連續輸入2次密碼即可。
我這里輸入的密碼是123456
3.使用koan實現重新安裝系統
koan是安裝在某一個需要重新安裝系統的服務器上,比如我s1服務需要重新安裝系統,不可能說人去一趟機房安裝吧,而采用cobbler安裝的話,在重啟的時候需要人工選擇安裝哪個系統,不然默認從local啟動。那怎么辦呢?koan這個軟件就能夠很好的解決這個問題。
我們先下載阿里雲的repo:https://mirrors.aliyun.com/repo/,到這里下載一個合適的yum源,我這里的下載的是CentOs7的。然后開始安裝koan。
[root@localhost yum.repos.d]# yum -y install koan
安裝好以后,我們可以查看cobbler-server上有哪些系統提供了。
[root@localhost yum.repos.d]# koan --server=192.168.56.11 --list=profiles
- looking for Cobbler at http://192.168.56.11:80/cobbler_api
CentOS-7.0-1406-x86_64 # 羅列出來的系統
選擇重裝的系統:
[root@localhost yum.repos.d]# koan --replace-self --server=192.168.56.11 --profile=CentOS-7.0-1406-x86_64
[root@localhost grub2]# less /boot/grub2/grub.cfg # 我們可以查看這個啟動項里面,發現多了一些內容,這就是為啥開機后能夠自動選擇網絡安裝,是因為grub啟動項里設置了。
重啟系統就可以安裝了。在重啟的時候,我們可以看到這新添加的一個啟動項,且默認是走它:
4. 創建yum倉庫
cobbler不但可以裝機,還可以自建yum倉庫,這個倉庫可以從公網的yum源進行同步到公司內網,節省帶寬。同步命令如下所示:
cobbler repo add --name=CentOS-7-x86_64-epel --mirro=https://mirrors.aliyun.com/epel/7Server/x86_64/ --arch=x86_64 --breed=yum
cobbler repo add --name=openstack-newton --mirror=https://mirrors.aliyun.com/centos/7.3.1611/cloud/x86_64/openstack-newton/ --arch=x86_64 --breed=yum
cobbler reposync --tries=3 --no-fail
參數解釋下:
- repo add : 添加一個repo源
- name : 為這個yum源命名
- mirror : 指定一個源的地址。
- arch : 指定平台
- breed: 類型為yum
5. 通過mac地址來定制化、自動化系統
在這一步,我們需要提前知道需要裝機服務器的MAC地址,一般情況下,服務器供應商會提供了每個網卡的MAC地址,所以我們可以根據不同的MAC地址來給安裝 不同的操作系統,配置不同的靜態iP,設置不同的主機名等等。命令如下:
cobbler system add --name=linux-node3 --mac=00:50:56:24:82:3A \
--profile=CentOS-7.0-1406-x86_64 \
--ip-address=192.168.56.100 --subnet=255.255.255.0 \
--gateway=192.168.56.2 --interface=eth0 \
--static=1 --hostname=linux-node3.example.com \
--name-servers="192.168.56.2" \
--kickstart=/var/lib/cobbler/kickstarts/CentOS-7.1-x86_64_cobbler.cfg
參數解釋下:
- system add 添加一個系統定制
- name 定義這個新添加的系統定制的名字
- mac 指定MAC
- profile 指定profile,通過
cobbler profile list
查看 - ip-address 指定靜態IP
- subnet 指定子網掩碼
- gateway 指定網關
- interface 指定網卡
- static=1 設置為靜態IP
- hostname 這是主機名
- name-servers 設置dns服務器
- kickstart 設置kickstart,通過
cobbler profile report
來查看。
[root@linux-node1 ~]# cobbler system list # 創建成功后能夠看到剛才新建的系統模版
linux-node3
我們創建一個虛擬機,mac地址為00:50:56:24:82:3A,啟動后你就會發現自動進入安裝系統了,等安裝完以后,所有的配置都和我們當初設置的一樣。
6. cobbler-api
cobbler也是通過restful-api來調用對應的接口,下面請看兩個小腳本:
都是采用opython2.7版本運行的
[root@linux-node1 ~]# cat cobbler_list.py
#!/usr/bin/python
import xmlrpclib
server = xmlrpclib.Server("http://192.168.56.11/cobbler_api")
print server.get_distros()
print server.get_profiles()
print server.get_systems()
print server.get_images()
print server.get_repos()
下面看看創建system模版的:
[root@linux-node1 ~]# cat cobbler-api.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import xmlrpclib
class CobblerAPI(object):
def __init__(self,url,user,password):
self.cobbler_user= user
self.cobbler_pass = password
self.cobbler_url = url
def add_system(self,hostname,ip_add,mac_add,profile):
'''
Add Cobbler System Infomation
'''
ret = {
"result": True,
"comment": [],
}
#get token
remote = xmlrpclib.Server(self.cobbler_url)
token = remote.login(self.cobbler_user,self.cobbler_pass)
#add system
system_id = remote.new_system(token)
remote.modify_system(system_id,"name",hostname,token)
remote.modify_system(system_id,"hostname",hostname,token)
remote.modify_system(system_id,'modify_interface', {
"macaddress-eth0" : mac_add,
"ipaddress-eth0" : ip_add,
"dnsname-eth0" : hostname,
}, token)
remote.modify_system(system_id,"profile",profile,token)
remote.save_system(system_id, token)
try:
remote.sync(token)
except Exception as e:
ret['result'] = False
ret['comment'].append(str(e))
return ret
def main():
cobbler = CobblerAPI("http://192.168.56.11/cobbler_api","cobbler","123456")
ret = cobbler.add_system(hostname='cobbler-api-test',ip_add='192.168.56.101',mac_add='00:50:56:21:65:78',profile='CentOS-7.0-1406-x86_64')
print ret
if __name__ == '__main__':
main()
通過這樣api的方式,也能夠創建system模版實現安裝主機的功能。