1. 自動化運維繫列之Cobbler自動裝機

preface

咱們以前批量安裝操做系統的時候都是採用pxe來安裝,pxe也是經過網絡安裝操做系統的,可是PXE依賴於DHCP,HTTP/TFTP,kicstart等支持。安裝流程以下所示:
image
imagenode

對於上面的PXE安裝流程,咱們須要知道咱們作了如下的工做:python

  1. 配置服務,如DHCP、TFTP、(HTTP、FTP、和NFS)
  2. 在dhcp和TFTP配置文件中填入客戶端機器的信息。
  3. 建立自動部署文件(好比kickstart)
  4. 將安裝的媒介解壓縮到HTTP/FTP/NFS存儲庫中。

PXE裝機也是一個不錯的選擇對於批量裝機的話,如今呢咱們學習另外一種安裝操做系統的軟件cobbler。它具備如下功能:mysql

  1. 使用一個之前定義的模版來配置DHCP服務(若是啓用了管理DHCP)
  2. 將一個存儲庫(yum或rsync)創建鏡像或者解壓縮一個媒介,以註冊一個新的操做系統。
  3. 在DHCP配置文件中爲須要安裝的機器建立一個條目並使用咱們指定的參數(IP、mac地址)
  4. 在TFTP服務目錄下建立適當的PXE文件
  5. 從新啓動DHCP服務以反映更改
  6. 從新啓動機器以開始安裝(若是電源管理已經啓用的話。)

cobbler簡介:

image

  1. Disribution : 發行內核,initrd等東西
  2. Repository: 建立倉庫,好比yum倉庫等。
  3. system: 經過mac地址來定製化系統
  4. profile: 對須要安裝某個系統的全部配置。

基礎環境介紹

首先我先說說的環境吧linux

IP地址 主機名
192.168.56.11 linux-node1.example.com
  1. 內核爲3.10.0-514.2.2.el7.x86_64
  2. 網卡名字設置爲了eth0。
  3. 關閉了selinux,iptables。
  4. 時間同步ntp.chinacache.com
  5. hosts文件裏面須要寫下hostname,便於解析。

開始安裝cobbler

咱們採用yum安裝的方式來安裝:
1.安裝cobbler以及相關的軟件ios

[root@linux-node1 ~]# yum -y install httpd dhcp tftp python-ctypes cobbler  xinetd

2.啓動服務(先起服務是由於這樣才知道咱們沒有配置哪些東東)git

[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就可以正常工做了。github

[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.修改配置文件web

[root@linux-node1 ~]# vim /etc/cobbler/settings
server: 192.168.56.11        # 把這些地址都改成本機的IP
next_server: 192.168.56.11

5.再次檢測配置文件,顯然這提示的數量降低到了5,咱們在逐個擊破。sql

[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的配置文件以及啓動和啓動rsyncapache

[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操做系統。

  1. 此時Cobbler已經可以安裝操做系統了,下一步咱們就建立一臺虛擬機來安裝操做系統吧。
  2. 咱們使用vmware來建立一個虛擬機,同時調整vmware的網絡參數,關閉vmware的DHCP功能,同事確保新建立的虛擬機和cobbler主機在同一網段。
  3. 啓動剛纔新建立的虛擬機,選擇網絡安裝操做系統就能夠了。

附加功能

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啓動項裏設置了。

重啓系統就能夠安裝了。在重啓的時候,咱們能夠看到這新添加的一個啓動項,且默認是走它:
image

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模版實現安裝主機的功能。

相關文章
相關標籤/搜索