Cobbler根据服务器MAC自动安装不同的操作系统Centos7.4/Centos6.5

一、 Cobbler概述


Cobbler是一个Linux服务器安装的服务,可以通过网络启动(PXE)的方式来快速安装、重装物理服务器和虚拟机,同时还可以管理DHCP,DNS等。
Cobbler可以使用命令行方式管理,也提供了基于Web的界面管理工具(cobbler-web),还提供了API接口,可以方便二次开发使用。
Cobbler是较早前的kickstart的升级版,优点是比较容易配置,还自带web界面比较易于管理。

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

PXE+KICKSTART 参见:https://blog.csdn.net/openbox2008/article/details/80089567


二、 Cobbler集成的服务

(1)PXE服务支持
(2)DHCP服务管理
(3)DNS服务管理(可选bind,dnsmasq)
(4)电源管理
(5)Kickstart服务支持
(6)YUM仓库管理
(7)TFTP(PXE启动时需要)

(8)Apache(提供kickstart的安装源,并提供定制化的kickstart配置)

三、安装前的准备

(0)环境准备

    1.dell r720 服务器1台

    2.vware workstation 10

    3.手动安装一台centos7.4做为部署服务器,配置以下各步骤后,再启多个新的虚拟,实现自动安装

虚拟机网卡采用NAT模式,不要使用桥接模式,DHCP服务器,在同一局域网多个DHCP服务会有冲突。
VMware的NAT模式的dhcp服务要关闭,避免干扰。

(1)由于涉及到主机间不同基于不同协议的通信,所以为了避免不必要的麻烦,我们选择关闭防火墙以及selinux。

#systemctl disable firewalld          # 关闭防火墙
#vim /etc/selinux/config               # 修改文件永久关闭selinux
SELINUX=disabled                         # 设置SELINUX的值为disabled
(2)查看一下生效情况
# firewall-cmd --state 或 systemctl status firewalld
not running
# getenforce
Disabled

(3)配置静态网卡
#vim /etc/sysconfig/network-scripts/ifcfg-eth0  # 设置静态IP
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=192.168.128.140
NETMASK=255.255.255.0
GATEWAY=192.168.128.2
DNS1=202.96.128.166
#systemctl restart network     #重启网络服务

网卡改名eth0,参见:https://blog.csdn.net/openbox2008/article/details/80051259

(4)阿里云的epel源

访问阿里mirrors镜像网址:http://mirrors.aliyun.com  ,找到epel下的epel-release-latest-7.noarch.rpm  包,wget下载下来再安装,或直接用rpm安装.

rpm -ivh https://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm

安装完后,会在/etc/yum.repos.d/ 下生成两个文件 : epel.repo  , epel-testing.repo

(5)注意事项

虚拟机网卡采用NAT模式,不要使用桥接模式,因为稍后我们会搭建DHCP服务器,在同一局域网多个DHCP服务会有冲突。

VMware的NAT模式的dhcp服务也关闭,避免干扰。

四、安装httpd、dhcp、xinetd、tftp-server 、pykickstart

(1)安装HTTP服务
yum -y install httpd
#systemctl enable httpd             
#systemctl start httpd             # 启动http服务
(2)安装xinetd、tftp-server服务, 配置xinetd,由xinetd管理tftp

# yum install -y xinetd
# yum install -y tftp-server 

# vim /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  #默认disable是yes的,把它改为no即可
        per_source              = 11
        cps                     = 100 2
        flags                   = IPv4
}
systemctl enable tftp
systemctl enable xinetd
systemctl start tftp
systemctl start xinetd

(3)安装DHCP服务

# yum install dhcp -y

# 在安装cobbler后,修改cobbler中,带的dhcp模版,不要直接修改dhcp本身的配置文件,因为cobbler会覆盖。
# vim /etc/cobbler/dhcp.template  ,不用手动复制,通过cobbler sync同步即可
# 仅列出修改过的字段
……
 subnet 192.168.128.0 netmask 255.255.255.0 {:
  range 192.168.128.10 192.168.128.50;  #ip地址
  option routers 192.168.128.2;        #网关
  option domain-name-servers 192.168.128.2; #DNS
}
……

#systemctl start dhcpd
#systemctl enable dhcpd

(4)安装pykickstart

yum -y install pykickstart

五、安装Cobbler

(1)安装 cobbler, cobbler-web

yum -y install cobbler cobbler-web

查看 cobbler安装信息

# 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日志

#systemctl restart cobblerd
#systemctl enable cobblerd

# systemctl status cobblerd

(2)修改dhcp配置

vim /etc/cobbler/dhcp.template

subnet 192.168.128.0 netmask 255.255.255.0 {
     option routers             192.168.128.2;
     option domain-name-servers 192.168.128.2;
     option subnet-mask         255.255.255.0;
     range dynamic-bootp        192.168.128.10 192.168.128.50;
     default-lease-time         21600;
     max-lease-time             43200;
     next-server                $next_server;
     class "pxeclients" {
          match if substring (option vendor-class-identifier, 0, 9) = "PXEClient";
          if option pxe-system-type = 00:02 {
                  filename "ia64/elilo.efi";
          } else if option pxe-system-type = 00:06 {
                  filename "grub/grub-x86.efi";
          } else if option pxe-system-type = 00:07 {
                  filename "grub/grub-x86_64.efi";
          } else if option pxe-system-type = 00:09 {
                  filename "grub/grub-x86_64.efi";
          } else {
                  filename "pxelinux.0";
          }
     }
}

(3)配置 cobbler

修改vim /etc/cobbler/settings 文件
#修改以下几项
default_password_crypted: "$1$cobbler$M6SE55xZodWc9.vAKLJs6."  #root密码,用openssl passwd -1 -salt 'cobbler' 'cobbler' 生成
manage_dhcp: 1  #由0改为1,表示由cobbler管理dhcp
next_server: 192.168.128.141  #cobbler管理dhcp,指定下一个要访问的服务地址
server: 192.168.128.141 #指定cobbler服务器地址

pxe_just_once: 1 #防止循环安装

Cobbler根据服务器MAC自动安装不同的操作系统Centos7.4/Centos6.5

# cobbler check      #检查cobbler settings的配置

[[email protected] ~]# cobbler check
The following are potential configuration items that you may want to fix:
1 : 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.
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

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

 #按上述提示,分别去做,然后再检查cobbler settings的配置

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.


***********************************

用脚本快速修改cobbler/settings文件

# server,Cobbler服务器的IP。
sed -i 's/server: 127.0.0.1/server: 192.168.128.141/' /etc/cobbler/settings
# next_server,如果用Cobbler管理DHCP,修改本项,作用不解释,看kickstart。
sed -i 's/next_server: 127.0.0.1/next_server: 192.168.128.141/' /etc/cobbler/settings
# 用Cobbler管理DHCP
sed -i 's/manage_dhcp: 0/manage_dhcp: 1/' /etc/cobbler/settings
# 防止循环装系统,适用于服务器第一启动项是PXE启动。
sed -i 's/pxe_just_once: 0/pxe_just_once: 1/' /etc/cobbler/settings

***********************************
(4)settings文件中密码的修改
# 设置新装系统的默认root密码cobber。下面的命令来源于cobbler check提示6。
random-phrase-here为干扰码,可以自行设定。
# openssl passwd -1 -salt 'cobbler' 'cobbler'     $1$cobbler$M6SE55xZodWc9.vAKLJs6.
# vim /etc/cobbler/settings 
default_password_crypted: "
$1$cobbler$M6SE55xZodWc9.vAKLJs6.

(5)自动下载启动文件cobbler get-loaders 

#cobbler get-loaders  # 会自动从官网下载
# cd /var/lib/cobbler/loaders/  # 下载的内容

[[email protected] loaders]# ls
COPYING.elilo     COPYING.yaboot  grub-x86_64.efi  menu.c32    README
COPYING.syslinux  elilo-ia64.efi  grub-x86.efi     pxelinux.0  yaboot*

(6)xinetd配置

# vim /etc/xinetd.d/rsync
disable = no
# systemctl  restart xinetd


(7)使用cobbler sync 进行配置同步

同步最新cobbler配置,它会根据配置自动修改dhcp等服务。

#systemctl restart cobblerd

#cobbler sync

task started: 2018-05-01_021414_sync
task started (id=Sync, time=Tue May  1 02:14:14 2018)
running pre-sync triggers
cleaning trees
removing: /var/www/cobbler/images/CentOS-7-x86_64
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/images/CentOS-7-x86_64
removing: /var/lib/tftpboot/s390x/profile_list
copying bootloaders

...

(8)导入centos操作系统镜像

#mount /dev/cdrom /mnt/  # 挂载CentOS7的系统镜像。

# cobbler import --path=/mnt/ --name= CentOS-7-x86_64  --arch=x86_64

# --path 镜像路径
# --name 为安装源定义一个名字
# --arch 指定安装源是32位、64位、ia64, 目前支持的选项有: x86│x86_64│ia64
# 安装源的唯一标示就是根据name参数来定义,本例导入成功后,安装源的唯一标示就是: CentOS-7-x86_64 
,如果重复,系统会提示导入失败。

#cobbler list
distros:
   CentOS-7-x86_64
profiles:
   CentOS-7-x86_64
# 镜像存放目录,cobbler会将镜像中的所有安装文件拷贝到本地一份,放在/var/www/cobbler/ks_mirror下的CentOS-7-x86_64目录下。因此/var/www/cobbler目录必须具有足够容纳安装文件的空间。
(9)配置kickstart文件

在第一次导入系统镜像后,Cobbler会给镜像指定一个默认的kickstart自动安装文件在/var/lib/cobbler/kickstarts下的sample_end.ks。

/var/lib/cobbler/kickstarts/目录中自带许多ks文件模板,将自已的ks文件上传到这个目录
# 编辑profile,修改关联的ks文件
# cobbler profile edit --name=CentOS-7-x86_64 --kickstart=/var/lib/cobbler/kickstarts/myks.cfg

(10)网卡改名eth0

# 修改安装系统的内核参数,在CentOS7系统有一个地方变了,就是网卡名变成eno16777736这种形式,但是为了运维标准化,我们需要将它变成我们常用的eth0,因此使用下面的参数。

但要注意是CentOS7才需要下面的步骤,CentOS6不需要。
# cobbler profile edit --name=CentOS-7-x86_64 --kopts='net.ifnames=0 biosdevname=0'

Cobbler根据服务器MAC自动安装不同的操作系统Centos7.4/Centos6.5

六、cobbler web
访问:https://192.168.128.141/cobbler_web  ,默认用户名:cobbler 默认密码 :cobbler

Cobbler根据服务器MAC自动安装不同的操作系统Centos7.4/Centos6.5


七、cobbler MAC地址制安装

(1)导入一个新的镜像文件

[[email protected] cobbler]# mount /dev/sr0 /mnt
mount: /dev/sr0 is write-protected, mounting read-only
[[email protected] cobbler]# cobbler import --name=CentOS-6.5-x86_64 --arch=x86_64 --path=/mnt/

导完后,可以在 cd /var/www/cobbler/ks_mirror 看到导入的镜像文件
[[email protected] ks_mirror]# ll
total 4
dr-xr-xr-x  7 root root 4096 Mar 29  2017 CentOS-6.5-x86_64
drwxr-xr-x  8 root root  254 Sep  5  2017 CentOS-7-x86_64

[[email protected] ks_mirror]# cobbler list
distros:
   CentOS-6.5-x86_64
   CentOS-7-x86_64
profiles:
   CentOS-6.5-x86_64
   CentOS-7-x86_64

(2)通过服务器上的MAC地址,进行定制安装 ,编写下面的命令

cobbler system --help 查看命令

usage
=====
cobbler system add
cobbler system copy
cobbler system dumpvars
cobbler system edit
cobbler system find
cobbler system getks
cobbler system list
cobbler system poweroff
cobbler system poweron
cobbler system powerstatus
cobbler system reboot
cobbler system remove
cobbler system rename
cobbler system report

cobbler system add --name=web_centos6.5 --mac=00:0C:29:DE:BB:5B  --profile=CentOS-6.5-x86_64 --ip-address=192.168.128.15 --subnet=255.255.255.0 --gateway=192.168.128.2 --interface=eth0 --static=1 --hostname=web1.example.com --name-servers="202.96.128.166 8.8.8.8"

# --name 自定义,但不能重复
# --profile 指定要安装的镜像文件,可以用cobbler list查看一下

# --mac 要安装服务器的MAC地址

# --kickstart 指定要使用的kickstart文件,kickstart文件中还可以指定yum源

# 其它是网络参数,可要可不要
# 查看定义的列表
# [[email protected] ks_mirror]# cobbler system list
   web_centos6.5
# cobbler sync

Cobbler根据服务器MAC自动安装不同的操作系统Centos7.4/Centos6.5

mac地址为mac=00:0C:29:DE:BB:5B的机器,正在安装centos6.5

Cobbler根据服务器MAC自动安装不同的操作系统Centos7.4/Centos6.5
Cobbler根据服务器MAC自动安装不同的操作系统Centos7.4/Centos6.5
安装完成后,IP地址和相应的MAC地址,网卡的配置

八、cobbler命令详解

 查看命令帮助


  1. # cobbler
  2. usage
  3. =====
  4. cobbler <distro|profile|system|repo|image|mgmtclass|package|file> ...
  5. [add|edit|copy|getks*|list|remove|rename|report] [options|--help]
  6. cobbler <aclsetup|buildiso|import|list|replicate|report|reposync|sync|validateks|version|signature|get-loaders|hardlink> [options|--help]
  7. # cobbler import --help # 导入镜像帮助
  8. Usage: cobbler [options]
  9. Options:
  10. -h, --help show this help message and exit
  11. --arch=ARCH OS architecture being imported
  12. --breed=BREED the breed being imported
  13. --os-version=OS_VERSION
  14. the version being imported
  15. --path=PATH local path or rsync location
  16. --name=NAME name, ex 'RHEL-5'
  17. --available-as=AVAILABLE_AS
  18. tree is here, don't mirror
  19. --kickstart=KICKSTART_FILE
  20. assign this kickstart file
  21. --rsync-flags=RSYNC_FLAGS
  22. pass additional flags to rsync
  23. #其它命令
  24. cobbler check 核对当前设置是否有问题
  25. cobbler list 列出所有的cobbler元素
  26. cobbler report 列出元素的详细信息
  27. cobbler sync 同步配置到数据目录,更改配置最好都要执行下
  28. cobbler reposync 同步yum仓库
  29. cobbler distro 查看导入的发行版系统信息
  30. cobbler system 查看添加的系统信息
  31. cobbler profile 查看配置信息