自动化安装系统(三)

Cobbler

简介

Cobbler是一款Linux生态的自动化运维工具,基于Python2开发,用于自动化批量部署安装操作系
统;其提供基于CLI的管理方式和WEB配置界面,其中WEB配置界面是基于Python2和Django框架开发。另外,cobbler还提供了API,方便二次开发。Cobbler属于C/S模型(客户端/服务器模型);

Cobbler主要用于快速网络安装linux操作系统,支持众多的Linux发行版如:Red Hat、Fedora、CentOS、Debian、Ubuntu和SuSE等,甚至支持windows的安装。

Cobbler实质是PXE的二次封装,将多种安装参数封装到一起,并提供统一的管理方法

Cobbler是在HTTP、TFTP、DHCP等各种服务的基础上进行相关操作的,实际安装的大体过程类似于基于PXE的网络安装:客户端(裸机)开机使用网卡引导启动,请求DHCP分配一个地址后从TFTP服务器获取启动文件,加载到客户端本地内存中运行,并显示出可安装的系统列表;在人为的选定安装的操作系统类型后,客户端会到HTTP服务器下载相应的系统安装文件并执行自动安装

工作原理

自动化安装系统(三)_第1张图片

  • client裸机配置了从网络启动后,开机后会广播包请求DHCP服务器(cobbler server)发送其分配好的一个IP
  • DHCP服务器(cobbler server)收到请求后发送responese,包括其ip地址
  • client裸机拿到ip后再向cobbler server发送请求OS引导文件的请求
  • cobbler server告诉裸机OS引导文件的名字和TFTP server的ip和port
  • client裸机通过上面告知的TFTP server地址通信,下载引导文件
  • client裸机执行执行该引导文件,确定加载信息,选择要安装的os,期间会再向cobbler server请求kickstart文件和os image
  • cobbler server发送请求的kickstart和os iamge
  • client裸机加载kickstart文件
  • client裸机接收os image,安装该os image

相关文件

配置文件

/etc/cobbler/settings  #cobbler 主配置文件 
/etc/cobbler/iso/  #iso模板配置文件
/etc/cobbler/pxe   #pxe模板文件
/etc/cobbler/power  #电源配置文件 
/etc/cobbler/user.conf   #web服务授权配置文件 
/etc/cobbler/users.digest  #web访问的用户名密码配置文件 
/etc/cobbler/dhcp.template #dhcp服务器的的配置模板
/etc/cobbler/dnsmasq.template #dns服务器的配置模板
/etc/cobbler/tftpd.template  #tftp服务的配置模板
/etc/cobbler/modules.conf #cobbler模块的配置文件

数据目录

/var/lib/cobbler/config/     #用于存放distros,system,profiles 等信息的配置文件
/var/lib/cobbler/triggers/   #用于存放用户定义的cobbler命令
/var/lib/cobbler/kickstarts/  # 默认存放kickstart文件
/var/lib/cobbler/loaders/     #存放各种引导程序

镜像目录

/var/www/cobbler/ks_mirror/    #导入的发行版系统的所有数据
/var/www/cobbler/images/       #导入发行版kernel和initrd镜像用于远程网络启动
/var/www/cobbler/repo_mirror/   #yum 仓库存储目录

日志目录

/var/log/cobbler/installing  #客户端安装日志 
/var/log/cobbler/cobbler.log #cobbler日志

相关命令

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

常见用法

#列出当前导入的linux发行版条目
cobbler distro list 
#报告当前所有的linux发行版详细信息
cobbler distro report 

#列出启动菜单条目
cobbler profile list

#导入系统源文件生成仓库。 实质是复制源镜像文件。
#--name指明目录名称,--paht指明源镜像文件路径,--arch指明架构,可选
#cobbler将系统yum源文件存放在 /var/www/cobbler/ks_mirror目录下
cobbler import --name=centos-8.0-x86_64 --path=/mnt --arch=x86_64


#新增启动菜单条目 并将linux发行版系统与其对应的ks文件建立关联。
#-- name指定新增的菜单条目,--distro指定发行版镜像,--kickstart指定自定义的ks文件
cobbler profile add --name=centos7 --distro=centos7-x86_64 --kickstart=/var/lib/cobbler/kickstarts/ks7.cfg 

#在导入发行版系统是会自动生成distro发行版列表,使用下面命令删除指定发行版条目再使用上面命令关联自定义的ks
cobbler profile remove --name=PROFILE_NAME

实验

centos7基于Cobbler实现系统自动化安装

环境

NAT模式,关闭vmware的DHCP功能

centos7(192.168.28.141),部署cobbler、dhcp、httpd、tftp

关闭firewalld和selinux

软件包:cobbler(cobbler包位于epel源中,安装时解决依赖会自动安装httpd、tftp)、dhcp

过程

在centos7上安装软件包

[root@localhost ~]# yum -y install cobbler dhcp
[root@localhost ~]# systemctl enable --now cobblerd httpd tftp dhcpd
Created symlink from /etc/systemd/system/multi-user.target.wants/cobblerd.service to /usr/lib/systemd/system/cobblerd.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
Created symlink from /etc/systemd/system/sockets.target.wants/tftp.socket to /usr/lib/systemd/system/tftp.socket.
Created symlink from /etc/systemd/system/multi-user.target.wants/dhcpd.service to /usr/lib/systemd/system/dhcpd.service.
Job for dhcpd.service failed because the control process exited with error code. See "systemctl status dhcpd.service" and "journalctl -xe" for details.
#DHCP未配置直接启动会报错

若发现 systemctl status cobblerd 服务启动了但有报错:cannot touch ‘/usr/share/cobbler/web/cobbler.wsgi’: No such file or directory,是因为没安装cobbler-web,可选

修改cobbler配置

#cobbler需要配置的提示信息
[root@localhost ~]# 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 : change 'disable' to 'no' in /etc/xinetd.d/tftp
4 : Some network boot-loaders are missing from /var/lib/cobbler/loaders.  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.
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 : ksvalidator was not found, install pykickstart
8 : 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
9 : 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.

#修改 /etc/cobbler/settings
[root@localhost ~]# vim /etc/cobbler/settings
...
#指定cobbler服务器IP
server: 192.168.28.141
...
#指定tftp服务器IP
next_server: 192.168.28.141
...
#设置root加密后的密码,默认是cobbler,通过openssl passwd -数字 密码 生成加密后的密码
default_password_crypted: "$1$mF86/UHC$WvcIcX2t6crBz2onWxyac."
...
#默认0,cobbler不管dhcp,需要手动配置dhcp;现改为1,通过cobbler生成dhcp配置文件
manage_dhcp: 1
...

#再次运行cobbler check会发现提示信息发生变化
[root@localhost ~]# 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 : Some network boot-loaders are missing from /var/lib/cobbler/loaders.  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.
3 : enable and start rsyncd.service with systemctl
4 : debmirror package is not installed, it will be required to manage debian deployments and repositories
5 : ksvalidator was not found, install pykickstart
6 : 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
7 : 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的DHCP模版文件,修改此文件cobbler会自动生成dhcp配置文件
[root@localhost ~]# vim /etc/cobbler/dhcp.template
...
subnet 192.168.28.141 netmask 255.255.255.0 {
     option routers             192.168.28.2;
     option domain-name-servers 180.76.76.76,223.5.5.5;
     option subnet-mask         255.255.255.0;
     range dynamic-bootp        192.168.28.145 192.168.28.149;
     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";
          }
     }

}
...

#联网下载启动相关文件,会存放在 /var/lib/tftpboot/ 目录下
[root@localhost ~]# tree /var/lib/tftpboot/
/var/lib/tftpboot/
├── boot
│   └── grub
│       └── menu.lst
├── etc
├── grub
│   ├── efidefault
│   └── images -> ../images
├── images
├── images2
├── memdisk
├── menu.c32
├── ppc
├── pxelinux.0
├── pxelinux.cfg
│   └── default
└── s390x
    └── profile_list

10 directories, 7 files
[root@localhost ~]# cobbler get-loaders
No such command: get-loaders

踩坑:

执行cobbler get-loaders时,提示没有此命令。安装 yum -y install syslinux即可

#修改启动菜单标题模版
[root@localhost ~]# vim /etc/cobbler/pxe/pxedefault.template
DEFAULT menu
PROMPT 0
# |后面是自定义标题
MENU TITLE Cobbler | WenZi Cobbler Menu
TIMEOUT 200
TOTALTIMEOUT 6000
ONTIMEOUT $pxe_timeout_profile

LABEL local
        MENU LABEL (local)
        MENU DEFAULT
        LOCALBOOT -1

$pxe_menu_items

MENU end
#查看自动生成的启动菜单
[root@localhost ~]# cat /var/lib/tftpboot/pxelinux.cfg/default
DEFAULT menu
PROMPT 0
MENU TITLE Cobbler | WenZi Cobbler Menu
TIMEOUT 200
TOTALTIMEOUT 6000
ONTIMEOUT local

LABEL local
        MENU LABEL (local)
        MENU DEFAULT
        LOCALBOOT -1



MENU end


#导入系统镜像资源  虚拟机新加centos6/8 的镜像
[root@localhost ~]# mkdir /mnt/centos{6,7,8}
[root@localhost ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   19G  0 part
  ├─centos-root 253:0    0   17G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sr0              11:0    1  3.7G  0 rom
sr1              11:1    1  7.7G  0 rom
sr2              11:2    1  4.4G  0 rom
[root@localhost ~]# mount /dev/sr0 /mnt/centos6
[root@localhost ~]# mount /dev/sr2 /mnt/centos7
[root@localhost ~]# mount /dev/sr1 /mnt/centos8
#cobbler将系统yum源文件存放在 /var/www/cobbler/ks_mirror目录下
[root@localhost ~]# cobbler import --name=centos-8.2-x86_64 --path=/mnt/centos8 --arch=x86_64
[root@localhost ~]# cobbler import --name=centos-7.0-x86_64 --path=/mnt/centos7 --arch=x86_64
[root@localhost ~]# cobbler import --name=centos-6.8-x86_64 --path=/mnt/centos6 --arch=x86_64
#导入后重启、同步
[root@localhost ~]# systemctl restart cobblerd
[root@localhost ~]# cobbler sync
[root@localhost ~]# tree -d /var/www/cobbler/ks_mirror/
/var/www/cobbler/ks_mirror/
├── centos-6.8-x86_64
│   ├── EFI
│   │   └── BOOT
│   ├── images
│   │   └── pxeboot
│   ├── isolinux
│   ├── Packages
│   └── repodata
├── centos-7.0-x86_64
│   ├── EFI
│   │   └── BOOT
│   │       └── fonts
│   ├── images
│   │   └── pxeboot
│   ├── isolinux
│   ├── LiveOS
│   ├── Packages
│   └── repodata
├── centos-8.2-x86_64
│   ├── AppStream
│   │   ├── Packages
│   │   └── repodata
│   ├── BaseOS
│   │   ├── Packages
│   │   └── repodata
│   ├── EFI
│   │   └── BOOT
│   │       └── fonts
│   ├── images
│   │   └── pxeboot
│   └── isolinux
└── config

32 directories
#查看以导入的发行版信息
[root@localhost ~]# cobbler distro list
   centos-6.8-x86_64
   centos-7.0-x86_64
   centos-8.2-x86_64
#此时已经自动生成启动菜单文件,已关联ks文件
[root@localhost ~]# vim /var/lib/tftpboot/pxelinux.cfg/default
DEFAULT menu
PROMPT 0
MENU TITLE Cobbler | WenZi Cobbler Menu
TIMEOUT 200
TOTALTIMEOUT 6000
ONTIMEOUT local

LABEL local
        MENU LABEL (local)
        MENU DEFAULT
        LOCALBOOT -1

LABEL centos-6.8-x86_64
        kernel /images/centos-6.8-x86_64/vmlinuz
        MENU LABEL centos-6.8-x86_64
        append initrd=/images/centos-6.8-x86_64/initrd.img ksdevice=bootif lang=  kssendmac text  ks=http://192.168.28.141/cblr/svc/op/ks/profile/centos-6.8-x86_64
        ipappend 2

LABEL centos-7.0-x86_64
        kernel /images/centos-7.0-x86_64/vmlinuz
        MENU LABEL centos-7.0-x86_64
        append initrd=/images/centos-7.0-x86_64/initrd.img ksdevice=bootif lang=  kssendmac text  ks=http://192.168.28.141/cblr/svc/op/ks/profile/centos-7.0-x86_64
        ipappend 2

LABEL centos-8.2-x86_64
        kernel /images/centos-8.2-x86_64/vmlinuz
        MENU LABEL centos-8.2-x86_64
        append initrd=/images/centos-8.2-x86_64/initrd.img ksdevice=bootif lang=  kssendmac text  ks=http://192.168.28.141/cblr/svc/op/ks/profile/centos-8.2-x86_64
        ipappend 2



MENU end




#若使用自定义ks应答文件,则如下
#将写好的ks应答文件复制到 /var/lib/cobbler/kickstarts目录下
[root@localhost ~]# ls
anaconda-ks.cfg  centos6_ks.cfg  centos7_ks.cfg  centos8_ks.cfg
[root@localhost ~]# cp centos* /var/lib/cobbler/kickstarts/
#将ks文件中 url  改为 url --url=$tree
[root@localhost ~]# vim /var/lib/cobbler/kickstarts/centos6_ks.cfg
[root@localhost ~]# vim /var/lib/cobbler/kickstarts/centos7_ks.cfg
[root@localhost ~]# vim /var/lib/cobbler/kickstarts/centos8_ks.cfg
#删除导入镜像时自动生成的发行版条目(菜单选项)
[root@localhost ~]# cobbler profile remove --name=centos-8.2-x86_64
[root@localhost ~]# cobbler profile remove --name=centos-7.0-x86_64
[root@localhost ~]# cobbler profile remove --name=centos-6.8-x86_64
[root@localhost ~]# cobbler profile list
#新增6/7/8的启动菜单并将各自镜像与各自ks文件对应
[root@localhost ~]# cobbler profile add --name=centos6_wenzi --distro=centos-6.8-x86_64 --kickstart=/var/lib/cobbler/kickstarts/centos6_ks.cfg
[root@localhost ~]# cobbler profile add --name=centos7_wenzi --distro=centos-7.0-x86_64 --kickstart=/var/lib/cobbler/kickstarts/centos7_ks.cfg
[root@localhost ~]# cobbler profile add --name=centos8_wenzi --distro=centos-8.2-x86_64 --kickstart=/var/lib/cobbler/kickstarts/centos8_ks.cfg
#启动菜单列表
[root@localhost ~]# cobbler profile list
   centos6_wenzi
   centos7_wenzi
   centos8_wenzi
#镜像列表
[root@localhost ~]# cobbler distro list
   centos-6.8-x86_64
   centos-7.0-x86_64
   centos-8.2-x86_64
[root@localhost ~]# systemctl restart cobblerd
[root@localhost ~]# cobbler sync
#查看菜单配置
[root@localhost ~]# cat /var/lib/tftpboot/pxelinux.cfg/default
DEFAULT menu
PROMPT 0
MENU TITLE Cobbler | WenZi Cobbler Menu
TIMEOUT 200
TOTALTIMEOUT 6000
ONTIMEOUT local

LABEL local
        MENU LABEL (local)
        MENU DEFAULT
        LOCALBOOT -1

LABEL centos6_wenzi
        kernel /images/centos-6.8-x86_64/vmlinuz
        MENU LABEL centos6_wenzi
        append initrd=/images/centos-6.8-x86_64/initrd.img ksdevice=bootif lang=  kssendmac text  ks=http://192.168.28.141/cblr/svc/op/ks/profile/centos6_wenzi
        ipappend 2

LABEL centos7_wenzi
        kernel /images/centos-7.0-x86_64/vmlinuz
        MENU LABEL centos7_wenzi
        append initrd=/images/centos-7.0-x86_64/initrd.img ksdevice=bootif lang=  kssendmac text  ks=http://192.168.28.141/cblr/svc/op/ks/profile/centos7_wenzi
        ipappend 2

LABEL centos8_wenzi
        kernel /images/centos-8.2-x86_64/vmlinuz
        MENU LABEL centos8_wenzi
        append initrd=/images/centos-8.2-x86_64/initrd.img ksdevice=bootif lang=  kssendmac text  ks=http://192.168.28.141/cblr/svc/op/ks/profile/centos8_wenzi
        ipappend 2



MENU end
#查看详细信息
[root@localhost ~]# cobbler report

测试

自动化安装系统(三)_第2张图片

踩坑:

自动安装centos8时出现问题,在下载文件页面停留两三分钟

自动化安装系统(三)_第3张图片

然后出现报错,感觉是没找到kickstart文件

dracut- initqueue:Warning:dracut- initqueue timeout - starting timeout scripts

自动化安装系统(三)_第4张图片

查看 /var/lib/tftpboot/pxelinux.cfg/default 中centos8的ks文件地址 ks=http://192.168.28.141/cblr/svc/op/ks/profile/centos8_wenzi ,通过浏览器访问发现无法正常查看ks文件内容,网页提示

# This kickstart had errors that prevented it from being rendered correctly.
# The cobbler.log should have information relating to this failure.

查看日志 /var/log/cobbler/cobbler.log ,与语言字符有关。

自动化安装系统(三)_第5张图片

在安装centos8时,使用的是自动化安装系统(二)_笔落_惊风雨的博客-CSDN博客中的ks文件,包含中文注释,把中文部分删除,systemctl restart cobbler,重新生成配置 cobbler sync,就成功了。

你可能感兴趣的:(运维工具,#,系统部署,运维,linux)