1. Cobbler介绍

Cobbler是一个Linux服务器安装的服务,可以通过网络启动(PXE)的方式来快速安装、重装物理服务器和虚拟机,同时还可以管理DHCP,DNS等。

Cobbler可以使用命令行方式管理,也提供了基于Web的界面管理工具(cobbler-web),还提供了API接口,可以方便二次开发使用。

Cobbler是较早前的kickstart的升级版,优点是比较容易配置,还自带web界面比较易于管理。

Cobbler内置了一个轻量级配置管理系统,但它也支持和其它配置管理系统集成,如Puppet,暂时不支持SaltStack。

Cobbler集成的服务:

PXE服务支持
DHCP服务管理
DNS服务管理(可选bind,dnsmasq)
电源管理
Kickstart服务支持
YUM仓库管理
TFTP(PXE启动时需要)
Apache(提供kickstart的安装源,并提供定制化的kickstart配置)

2、系统环境准备

[root@linux-node2 ~]# cat /etc/redhat-release 
CentOS Linux release 7.4.1708 (Core) 
[root@linux-node2 ~]# uname -r
3.10.0-693.5.2.el7.x86_64
[root@linux-node2 ~]#  getenforce
Disabled
[root@linux-node2 ~]# systemctl status firewalld
Unit firewalld.service could not be found.
[root@linux-node2 ~]# ifconfig eth0|awk -F "[ :]+" 'NR==2 {print $3}'
192.168.56.12
[root@linux-node2 ~]# hostname
linux-node2
#配置阿里云epel源
[root@linux-node2 ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

3、Cobbler安装配置

3.1Cobbler安装

[root@linux-node2 ~]# yum install -y httpd tftp dhcp cobbler cobbler-web pykickstart
[root@linux-node2 ~]# rpm -ql cobbler
/etc/cobbler                  # 配置文件目录
/etc/cobbler/settings         # cobbler主配置文件,这个文件是YAML格式,Cobbler是python写的程序。
/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日志

3.2 cobbler配置

[root@linux-node2 ~]# systemctl start httpd
[root@linux-node2 ~]# systemctl start cobblerd
[root@linux-node2 ~]# cobbler check      #通过该命令可以查看cobbler的服务是否正常,需要解决哪些问题
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.
# 看着上面的结果,一个一个解决。
#1.2问题server,Cobbler服务器的IP。
[root@linux-node2 ~]# vim /etc/cobbler/settings 
384 server: 192.168.56.12
272 next_server: 192.168.56.12
#3问题解决启用tftp
[root@linux-node2 ~]# yum install -y xinetd
[root@linux-node2 ~]# vim /etc/xinetd.d/tftp 
disable                 = no
[root@linux-node2 ~]# systemctl start xinetd
#4问题解决bootload的插件安装,会自动从官网下载
[root@linux-node2 ~]# cobbler get-loaders
task started: 2018-01-11_155827_get_loaders
task started (id=Download Bootloader Content, time=Thu Jan 11 15:58:27 2018)
downloading https://cobbler.github.io/loaders/README to /var/lib/cobbler/loaders/README
downloading https://cobbler.github.io/loaders/COPYING.elilo to /var/lib/cobbler/loaders/COPYING.elilo
downloading https://cobbler.github.io/loaders/COPYING.yaboot to /var/lib/cobbler/loaders/COPYING.yaboot
downloading https://cobbler.github.io/loaders/COPYING.syslinux to /var/lib/cobbler/loaders/COPYING.syslinux
downloading https://cobbler.github.io/loaders/elilo-3.8-ia64.efi to /var/lib/cobbler/loaders/elilo-ia64.efi
downloading https://cobbler.github.io/loaders/yaboot-1.3.17 to /var/lib/cobbler/loaders/yaboot
downloading https://cobbler.github.io/loaders/pxelinux.0-3.86 to /var/lib/cobbler/loaders/pxelinux.0
downloading https://cobbler.github.io/loaders/menu.c32-3.86 to /var/lib/cobbler/loaders/menu.c32
downloading https://cobbler.github.io/loaders/grub-0.97-x86.efi to /var/lib/cobbler/loaders/grub-x86.efi
downloading https://cobbler.github.io/loaders/grub-0.97-x86_64.efi to /var/lib/cobbler/loaders/grub-x86_64.efi
*** TASK COMPLETE ***
#5问题解决rsyncd服务依赖
[root@linux-node2 ~]# systemctl enable rsyncd.service
Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.
[root@linux-node2 ~]# systemctl start  rsyncd.service
#7问题解决设置新装系统的默认root密码123123。下面的命令来源于提示7。random-phrase-here为干扰码,可以自行设定。
[root@linux-node2 ~]# openssl passwd -1 -salt '123123' '123123'
$1$123123$MAV.kVI/b3swmFLErPD2b0
[root@linux-node2 ~]# vim /etc/cobbler/settings 
101 default_password_crypted: "$1$123123$MAV.kVI/b3swmFLErPD2b0"

#重启cobbler服务,再检查
[root@linux-node2 ~]# systemctl restart cobblerd
[root@linux-node2 ~]# 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    # 和debian系统相关,不需要
2 : fencing tools were not found, and are required to use the (optional) power management features. install cman or fence-agents to use them  # fence设备相关,不需要

Restart cobblerd and then run 'cobbler sync' to apply changes.

3.3配置DHCP

# 修改cobbler的dhcp模版,不要直接修改dhcp本身的配置文件,因为cobbler会覆盖。
[root@linux-node2 ~]# vim /etc/cobbler/settings 
manage_dchp: 1   #启用dchp服务
[root@linux-node2 ~]# vim /etc/cobbler/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;
     option subnet-mask         255.255.255.0;
     range dynamic-bootp        192.168.56.100 192.168.56.254;
     default-lease-time         21600;
     max-lease-time             43200;
     next-server                $next_server;

3.4同步cobbler配置

[root@linux-node2 ~]# systemctl restart cobblerd
[root@linux-node2 ~]# cobbler sync
task started: 2018-01-11_161740_sync
task started (id=Sync, time=Thu Jan 11 16:17:40 2018)
running pre-sync triggers
cleaning trees
removing: /var/lib/tftpboot/grub/images
copying bootloaders
trying hardlink /var/lib/cobbler/loaders/pxelinux.0 -> /var/lib/tftpboot/pxelinux.0
trying hardlink /var/lib/cobbler/loaders/menu.c32 -> /var/lib/tftpboot/menu.c32
trying hardlink /var/lib/cobbler/loaders/yaboot -> /var/lib/tftpboot/yaboot
trying hardlink /usr/share/syslinux/memdisk -> /var/lib/tftpboot/memdisk
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 ***

4、Cobbler导入镜像

4.1、Cobbler命令

cobbler check    核对当前设置是否有问题
cobbler list     列出所有的cobbler元素
cobbler report   列出元素的详细信息
cobbler sync     同步配置到数据目录,更改配置最好都要执行下
cobbler reposync 同步yum仓库
cobbler distro   查看导入的发行版系统信息
cobbler system   查看添加的系统信息
cobbler profile  查看配置信息

4.2、Cobbler导入镜像

#此处上传的centos官网镜像千万千万别用Minimal,虽然有几百兆,但是无法安装kickstarts的文件指定的软件包,已踩坑!!!
[root@linux-node2 ~]# mount -o loop /opt/CentOS-7-x86_64-DVD-1708.iso /mnt/    # 挂载CentOS7.4的系统镜像。
mount: /dev/loop0 写保护,将以只读方式挂载
[root@linux-node2 ~]# cobbler import --path=/mnt/ --name=Centos-7.4-x86_64 --arch=x86_64
# --path 镜像路径
# --name 为安装源定义一个名字
# --arch 指定安装源是32位、64位、ia64, 目前支持的选项有: x86│x86_64│ia64
# 安装源的唯一标示就是根据name参数来定义,本例导入成功后,安装源的唯一标示就是:CentOS-7.1-x86_64,如果重复,系统会提示导入失败。
task started: 2018-01-11_163234_import
task started (id=Media import, time=Thu Jan 11 16:32:34 2018)
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.4-x86_64:
creating new distro: Centos-7.4-x86_64
trying symlink: /var/www/cobbler/ks_mirror/Centos-7.4-x86_64 -> /var/www/cobbler/links/Centos-7.4-x86_64
creating new profile: Centos-7.4-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.4-x86_64 for Centos-7.4-x86_64
processing repo at : /var/www/cobbler/ks_mirror/Centos-7.4-x86_64
need to process repo/comps: /var/www/cobbler/ks_mirror/Centos-7.4-x86_64
looking for /var/www/cobbler/ks_mirror/Centos-7.4-x86_64/repodata/*comps*.xml
Keeping repodata as-is :/var/www/cobbler/ks_mirror/Centos-7.4-x86_64/repodata
*** TASK COMPLETE ***
[root@linux-node2 ~]# ll /var/www/cobbler/ks_mirror/     #导入镜像的位置
总用量 4
drwxr-xr-x 8 root root 4096 9月   5 21:36 Centos-7.4-x86_64
drwxr-xr-x 2 root root   35 1月  11 16:34 config
[root@linux-node2 ~]# cobbler list     #查看镜像
distros:
   Centos-7.4-x86_64

profiles:
   Centos-7.4-x86_64

systems:

repos:

images:

mgmtclasses:

packages:

files:

4.3、指定ks.cfg文件及调整内核参数

(1)默认的ks.cfg文件
[root@linux-node2 ~]# ll /var/lib/cobbler/kickstarts
总用量 56
-rw-r--r-- 1 root root  115 9月  17 01:33 default.ks
-rw-r--r-- 1 root root   22 9月  17 01:33 esxi4-ks.cfg
-rw-r--r-- 1 root root   22 9月  17 01:33 esxi5-ks.cfg
drwxr-xr-x 2 root root   54 1月  11 15:54 install_profiles
-rw-r--r-- 1 root root 1424 9月  17 01:33 legacy.ks
-rw-r--r-- 1 root root  292 9月  17 01:33 pxerescue.ks
-rw-r--r-- 1 root root 2825 9月  17 01:33 sample_autoyast.xml
-rw-r--r-- 1 root root 1856 9月  17 01:33 sample_end.ks
-rw-r--r-- 1 root root    0 9月  17 01:33 sample_esx4.ks
-rw-r--r-- 1 root root  324 9月  17 01:33 sample_esxi4.ks
-rw-r--r-- 1 root root  386 9月  17 01:33 sample_esxi5.ks
-rw-r--r-- 1 root root  386 9月  17 01:33 sample_esxi6.ks
-rw-r--r-- 1 root root 1913 9月  17 01:33 sample.ks
-rw-r--r-- 1 root root 3419 9月  17 01:33 sample_old.seed
-rw-r--r-- 1 root root 6658 9月  17 01:33 sample.seed
(2)查看所有profile的设置
[root@linux-node2 ~]# cobbler profile report 
Name                           : Centos-7.4-x86_64
TFTP Boot Files                : {}
Comment                        : 
DHCP Tag                       : default
Distribution                   : Centos-7.4-x86_64
Enable gPXE?                   : 0
Enable PXE Menu?               : 1
Fetchable Files                : {}
Kernel Options                 : {}
Kernel Options (Post Install)  : {}
Kickstart                      : /var/lib/cobbler/kickstarts/sample_end.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
[root@linux-node2 ~]# cobbler profile list    #查看可用的镜像
   Centos-7.4-x86_64
(3)# 编辑profile,修改关联的ks文件
[root@linux-node2 ~]# cobbler profile edit --name=Centos-7.4-x86_64 --kickstart=/var/lib/cobbler/kickstarts/Centos-7.4-x86_64.cfg 
[root@linux-node2 ~]# cobbler profile report 
Name                           : Centos-7.4-x86_64
TFTP Boot Files                : {}
Comment                        : 
DHCP Tag                       : default
Distribution                   : Centos-7.4-x86_64
Enable gPXE?                   : 0
Enable PXE Menu?               : 1
Fetchable Files                : {}
Kernel Options                 : {}
Kernel Options (Post Install)  : {}
Kickstart                      : /var/lib/cobbler/kickstarts/Centos-7.4-x86_64.cfg    #关联后,此处会变成修改的配置
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
(4)修改安装系统的内核参数,修改网卡名称
[root@linux-node2 ~]# cobbler profile edit --name=Centos-7.4-x86_64 --kopts='net.ifnames=0 biosdevname=0'
[root@linux-node2 ~]# cobbler profile report 
Name                           : Centos-7.4-x86_64
TFTP Boot Files                : {}
Comment                        : 
DHCP Tag                       : default
Distribution                   : Centos-7.4-x86_64
Enable gPXE?                   : 0
Enable PXE Menu?               : 1
Fetchable Files                : {}
Kernel Options                 : {'biosdevname': '0', 'net.ifnames': '0'}     #此处会增加内核选项参数
Kernel Options (Post Install)  : {}
Kickstart                      : /var/lib/cobbler/kickstarts/Centos-7.4-x86_64.cfg
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
[root@linux-node2 ~]# cobbler sync    #同步

5、安装系统

5.1去除vm的DHCP

Cobbler无人值守安装

5.2开始安装

新建一台虚拟机(不解释)内存给2G不然坑呀
Cobbler无人值守安装
选择Centos-7.4-x86_64
Cobbler无人值守安装

5.3.Centos-7.4-x86_64.cfg文件

# Cobbler for Kickstart Configurator for CentOS 7.4 by Jason Zhao
# System language
lang en_US
# System keyboard
keyboard us
# System timezone
timezone Asia/Shanghai
#Root password
rootpw  --iscrypted $default_password_crypted
#Use text mode install
text
#Install OS instead of upgrade
install
#Use NFS installation Media
url --url=$tree
#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 --enablemd5
auth --useshadow --enablemd5
# Network information
$SNIPPET('network_config')
reboot
firewall --disabled
selinux --disabled
skipx

%pre
$SNIPPET('log_ks_pre')
$SNIPPET('kickstart_start')
$SNIPPET('pre_install_network_config')
$SNIPPET('pre_anamon')
%end

%packages
@base
@core
tree
nmap
sysstat
lrzsz
dos2unix
telnet
iptraf
ncurses-devel
openssl-devel
zlib-devel
OpenIPMI-tools
screen
%end

%post
systemctl disable postfix.service
%end

6、使用koan重装系统

在新安装的系统中安装koan
[root@localhost ~]# yum install -y epel-release
[root@localhost ~]# yum install -y koan
[root@localhost ~]# koan --server=192.168.56.12 --list=profiles   #查看可使用的安装包
- looking for Cobbler at http://192.168.56.12:80/cobbler_api
Centos-7.4-x86_64
Centos-7.2-x86_64
[root@localhost ~]# koan --replace-self --server=92.168.56.12 --profile=Centos-7.4-x86_64
- looking for Cobbler at http://192.168.56.12:80/cobbler_api
- reading URL: http://192.168.56.12/cblr/svc/op/ks/profile/Centos-7.4-x86_64
install_tree: http://192.168.56.12/cblr/links/Centos-7.4-x86_64
downloading initrd initrd.img to /boot/initrd.img_koan
url=http://192.168.56.12/cobbler/images/Centos-7.4-x86_64/initrd.img
- reading URL: http://192.168.56.12/cobbler/images/Centos-7.4-x86_64/initrd.img
downloading kernel vmlinuz to /boot/vmlinuz_koan
url=http://192.168.56.12/cobbler/images/Centos-7.4-x86_64/vmlinuz
- reading URL: http://192.168.56.12/cobbler/images/Centos-7.4-x86_64/vmlinuz
- ['/sbin/grubby', '--add-kernel', '/boot/vmlinuz_koan', '--initrd', '/boot/initrd.img_koan', '--args', '"ks=http://192.168.56.12/cblr/svc/op/ks/profile/Centos-7.4-x86_64 ksdevice=link kssendmac lang= text "', '--copy-default', '--make-default', '--title=kick1515987084']
- ['/sbin/grubby', '--update-kernel', '/boot/vmlinuz_koan', '--remove-args=root']
- reboot to apply changes
[root@localhost ~]# reboot   #重启自动安装

#那么koan是怎么实现重装的呢?koan会在grub增加一个启动项,从告诉系统从该启动项进行启动
[root@localhost grub2]# cat grub.cfg |grep "linux16"
    linux16 /vmlinuz_koan ro crashkernel=auto rhgb quiet LANG=en_US.UTF-8 ks=http://192.168.56.12/cblr/svc/op/ks/profile/Centos-7.4-x86_64 ksdevice=link kssendmac lang= text
    linux16 /vmlinuz-3.10.0-693.el7.x86_64 root=UUID=e9311a84-0787-488b-ab34-3c80d81d86eb ro crashkernel=auto rhgb quiet LANG=en_US.UTF-8
    linux16 /vmlinuz-0-rescue-ee1532c207654bf4b416357cc26f7eee root=UUID=e9311a84-0787-488b-ab34-3c80d81d86eb ro crashkernel=auto rhgb quiet

Cobbler无人值守安装
到此就能实现自动重装了!

7、Cobbler的图形界面

cobbler的图形界面需要安装cobbler_web,前面已经安装,启动httpd后,直接访问:https://192.168.56.12/cobbler_web,默认的用户名密码是cobbler,cobbler
在图形界面可以对导入镜像,kickstarts文件的修改与配置等操作。

Cobbler无人值守安装

[root@linux-node2 ~]# cat /etc/cobbler/users.digest      #cobbler的密码存放位置
cobbler:Cobbler:a2d6bae81669d707b72c0bd9806e01f3
[root@linux-node2 ~]# htdigest /etc/cobbler/users.digest "Cobbler" cobbler    #修改cobbler界面的密码
Changing password for user cobbler in realm Cobbler
New password: 123123
Re-type new password: 123123
[root@linux-node2 ~]# cat /etc/cobbler/users.digest 
cobbler:Cobbler:37af732f18fe2c533372649699188af3

Cobbler无人值守安装

8、Cobbler的基本组件

Cobbler无人值守安装

Disribution : 发行内核,initrd等东西
Repository: 创建仓库,比如yum仓库等。
system: 通过mac地址来定制化系统
profile: 对需要安装某个系统的所有配置。

9、创建yum仓库

cobbler不但可以装机,还可以自建yum仓库,这个仓库可以从公网的yum源进行同步到公司内网,节省带宽。同步命令如下所示:

#添加阿里云的epel源
cobbler repo add --name=Centos-7.x86-64-epel --mirror=https://mirrors.aliyun.com/epel/7Server/x86_64/ --arch=x86_64 --breed=yum

#添加阿里云的openstack-newton源
cobbler repo add --name=openstack-newton --mirror=https://mirrors.aliyun.com/centos/7.4.1708/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

#演示:
[root@linux-node2 opt]# cobbler repo add --name=openstack-newton --mirror=https://mirrors.aliyun.com/centos/7.4.1708/cloud/x86_64/openstack-newton/ --arch=x86_64 --breed=yum   #添加openstack-newton源
[root@linux-node2 opt]# cobbler profile edit --name=Centos-7.4-x86_64 --repos=openstack-newton    #关联repos仓库
[root@linux-node2 opt]# cobbler profile report
Name                           : Centos-7.4-x86_64
TFTP Boot Files                : {}
Comment                        : 
DHCP Tag                       : default
Distribution                   : Centos-7.4-x86_64
Enable gPXE?                   : 0
Enable PXE Menu?               : 1
Fetchable Files                : {}
Kernel Options                 : {}
Kernel Options (Post Install)  : {}
Kickstart                      : /var/lib/cobbler/kickstarts/Centos-7.4-x86_64.cfg
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                          : ['openstack-newton']             #添加成功
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

以上的操作也可以在cobbler界面上,把yum源添加到Centos-7.4-x86_64的profile中。如图:

Cobbler无人值守安装

10、Cobbler定制安装

(1)生成mac地址

Cobbler无人值守安装

(2)定制系统

[root@linux-node2 ~]# cobbler system add --name=linux-node4 --mac=00:50:56:33:F1:45 \
> --profile=Centos-7.3-x86_64 \
> --ip-address=192.168.56.110 --subnet=255.255.255.0 \
> --gateway=192.168.56.2 --interface=eth0 \
> --static=1 --hostname=linux-node4.example.com \
> --name-servers="192.168.56.2" \
> --kickstart=/var/lib/cobbler/kickstarts/Centos-7.3-x86_64.cfg 
[root@linux-node2 ~]# cobbler system list
   linux-node4

参数解释:

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来查看。

(3)开机自动安装

创建一个虚拟机,mac地址为00:50:56:33:F1:45,启动后你就会发现自动进入安装系统了,等安装完以后,所有的配置都和我们当初设置的一样。
Cobbler无人值守安装

11、Cobbler的API

cobbler也是通过restful-api来调用对应的接口,下面请看两个小脚本:
都是采用opython2.7版本运行的

[root@linux-node2 ~]# cat cobbler_list.py
#!/usr/bin/python
import xmlrpclib
server = xmlrpclib.Server("http://192.168.56.12/cobbler_api")
print server.get_distros()
print server.get_profiles()
print server.get_systems()
print server.get_images()
print server.get_repos()

使用api的方式,定制安装系统,python脚本如下:

[root@linux-node2 ~]# 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.12/cobbler_api","cobbler","123123")
    ret = cobbler.add_system(hostname='cobbler-api-test',ip_add='192.168.56.101',mac_add='00:50:56:33:F1:45',profile='CentOS-7.3-x86_64')
    print ret

if __name__ == '__main__':
main()