技术原理解析:
Client向PXE Server上的DHCP发送IP地址请求消息,DHCP检测Client是否合法(主要是检测Client的网卡MAC地址),如果合法则返回Client的IP地址,同时将启动文件pxelinux.0的位置信息一并传送给Client
Client向PXE Server上的TFTP发送获取pxelinux.0请求消息,TFTP接收到消息之后再向Client发送pxelinux.0大小信息,试探Client是否满意,当TFTP收到Client发回的同意大小信息之后,正式向Client发送pxelinux.0
Client执行接收到的pxelinux.0文件
Client向TFTP Server发送针对本机的配置信息文件(在TFTP服务的pxelinux.cfg目录下,这是系统菜单文件,格式和isolinux.cfg格式一样,功能也是类似),TFTP将配置文件发回Client,继而Client根据配置文件执行后续操作。
Client向TFTP发送Linux内核请求信息,TFTP接收到消息之后将内核文件发送给Client
Client向TFTP发送根文件请求信息,TFTP接收到消息之后返回Linux根文件系统
Client启动Linux内核
Client下载安装源文件,读取自动化安装脚本
Cobbler简单介绍:
Cobbler是一个Linux服务器快速网络安装的服务,由python开发,小巧轻便(15k行python代码),可以通过PXE的方式来快速安装、重装物理服务器和虚拟机,同时还可以管理DHCP,DNS,TFTP、RSYNC以及yum仓库、构造系统ISO镜像。
Cobbler可以使用命令行方式管理,也提供了基于Web的界面管理工具(cobbler-web),还提供了API接口,可以方便二次开发使用。
Cobbler工作流程:
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
Cobbler集成的服务:
PXE服务支持
DHCP服务管理
DNS服务管理(可选bind,dnsmasq)
电源管理
Kickstart服务支持
YUM仓库管理
TFTP(PXE启动时需要)
Apache(提供kickstart的安装源,并提供定制化的kickstart配置)
Cobbler 设计方式:
发行版(distro) :表示一个操作系统,它承载了内核和initrd的信息,以及内核等其他数据
存储库 (repository):保存了一个yum或者rsync存储库的镜像信息
配置文件(profile):包含了一个发行版(distro),一个kickstart文件以及可能的存储库(repository),还包含了更多的内核参数等其他数据
系统(system):表示要配给的机器,它包含了一个配置文件或一个镜像,还包含了ip和mac地址,电源管理(地址,凭据,类型)以及更为专业的数据信息
镜像(image):可替换一个包含不属于此类别的文件的发行版对象(eg: 无法作为内核和initrd的对象)
以上各个组件中, 发行版,存储库, 配置文件为必须配置项,只有在虚拟环境中,必须要用cobbler来引导虚拟机启动时候,才会用到系统组件但事实上,在生产环境中需要大量的虚拟机实例的话,通常利用openstack等来实现虚拟机节点
Cobbler配置目录文件说明:
/etc/cobbler
/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/kickstart/ # 默认存放kickstart文件
/var/lib/cobbler/loaders/ # 存放各种引导程序 镜像目录
/var/www/cobbler/ks_mirror/ # 导入的发行版系统的所有数据
/var/www/cobbler/images/ # 导入发行版的kernel和initrd镜像用于 远程网络启动
/var/www/cobbler/repo_mirror/ # yum 仓库存储目录
Cobbler镜像目录:
/var/www/cobbler/ks_mirror/ # 导入的发行版系统的所有数据
/var/www/cobbler/images/ # 导入发行版的kernel和initrd镜像用于远程网络启动
/var/www/cobbler/repo_mirror/ # yum 仓库存储目录
Cobbler日志目录:
/var/log/cobbler/installing # 客户端安装日志
/var/log/cobbler/cobbler.log # cobbler日志
Cobbler命令介绍:
cobbler check # 核对当前设置是否有问题
cobbler list # 列出所有的cobbler元素
cobbler report # 列出元素的详细信息
cobbler sync # 同步配置到数据目录,更改配置最好都要执行下
cobbler reposync # 同步yum仓库
cobbler distro # 查看导入的发行版系统信息
cobbler system # 查看添加的系统信息
cobbler profile # 查看配置信息
/etc/cobbler/settings中重要的参数设置:
default_password_crypted: "gEc7ilpP$pg5iSOj/mlxTxEslhRvyp/"
manage_dhcp:1
manage_tftpd:1
pxe_just_once:1
next_server:< tftp服务器的 IP 地址>
server:<Cobbler服务器IP地址>
Cobble安装:
系统信息:
cat /etc/redhat-release
-
1、关闭selinux
vim /etc/selinux/config或getenforce # 必须关闭selinux
注:getenforce是暂时性关闭
-
2、关闭防火墙
systemctl status firewalld
systemctl stop firewalld
systemctl enable firewalld
加入开机自启
-
3、查看本机IP
ifconfig eth0 | awk -F "[ :]+" 'NR==2 {print $3}'
-
4、配置yum源:
yum install wget -y # 下载wget
-
5、epel 配置方法(扩展源)
1、备份(如有配置其他epel源)
mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup
mv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup
-
2、下载新repo 到/etc/yum.repos.d/
epel(RHEL 7):
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
-
6、开始安装Cobbler,下载相关服务:
yum -y install cobbler dhcp httpd xinetd tftp-server syslinux pykickstart rsync cobbler-web
-
7、启动相关服务
systemctl start httpd
systemctl enable httpd
systemctl enable cobblerd
systemctl start cobblerd
-
8、通过cobbler check 核对当前设置是否有问题
[root@cobbler ~]# 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, 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.
一般都有8到9个问题需要修复。
按照提示一个一个的解决问题:
问题1:
[root@cobbler ~]# sed -i 's/^server: 127.0.0.1/server: 10.94.2.240/' /etc/cobbler/settings # 修改server的ip地址为本机ip
问题2:
[root@cobbler ~]# sed -i 's/^next_server: 127.0.0.1/next_server: 10.94.2.240/' /etc/cobbler/settings # TFTP Server 的IP地址
问题3:
[root@cobbler ~]# 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 # 修改为no
per_source = 11
cps = 100 2
flags = IPv4
}
问题4:
[root@cobbler ~]# cobbler get-loaders # 下载缺失的文件
报错
解决办法
vim /etc/cobbler/settings 找到第384行
384 server: 10.0.0.61 ####改为自己的IP地址
问题5:
# 添加rsync到自启动并启动rsync
[root@cobbler ~]# systemctl enable rsyncd
Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.
[root@cobbler ~]# systemctl start rsyncd
问题6:
[root@cobbler ~]# yum install debmirror -y #安装debian
[root@cobbler ~]# vim /etc/debmirror.conf
28 #@dists="sid";
30 #@arches="i386";
注释掉这两行,重新check后没有报错了
问题7:
# 修改密码为123456 ,salt后面是常用的加密方式加密
[root@localhost ~]# openssl passwd -1 -salt '123456' '123456'
$1$123456$wOSEtcyiP2N/IfIl15W6Z0
[root@localhost ~]# vim /etc/cobbler/settings # 修改settings配置文件中下面位置,把新生成的密码加进去
default_password_crypted: "$1$123456$wOSEtcyiP2N/IfIl15W6Z0
问题8:
[root@cobbler ~]# yum install fence-agents -y # fence设备相关,电源管理模块
重启cobbler
systemctl restart cobblerd.service
再次执行cobbler check
[root@cobbler ~]# cobbler check
-
9、dhcp利用cobbler管理
[root@cobbler ~]# vim /etc/cobbler/settings # 修改settings中参数,由cobbler控制dhcp
manage_dhcp: 1
-
10、修改dhcp.templates配置文件(仅列出修改部分)
[root@cobbler/var/lib/cobbler/kickstarts]# vim /etc/cobbler/dhcp.template
subnet 10.0.0.0 netmask 255.255.255.0 { #网段 这个可以是10网段,也可以是172网段
option routers 10.0.0.1; #网关
option domain-name-servers 10.0.0.1; #MDS 详见:[https://blog.csdn.net/displayMessage/article/details/81133634](https://blog.csdn.net/displayMessage/article/details/81133634)
option subnet-mask 255.255.255.0; #子网掩码
range dynamic-bootp 10.0.0.210 10.0.0.220; #分配的地址段
default-lease-time 21600;
max-lease-time 43200;
next-server $next_server; #这个是cobbler配置文件里面的变量
-
11、重启服务并同步配置,改完dhcp必须要sync同步配置
[root@cobbler ~]# systemctl restart cobblerd.service
[root@cobbler ~]# cobbler sync
-
12、检查dhcp
[root@cobbler ~]# netstat -tulp | grep dhcp
-
13、现在开始一键装机
-
1、首先挂载镜像
Linux 上是需要挂载光盘,我们进行挂载
[root@localhost ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 is write-protected, mounting read-only
-
2、制作镜像,用于安装操作系统,使cobbler导入镜像
[root@m01 ~]# cobbler import --path=/mnt --name=CentOS7.6
task started: 2019-05-03_134203_import
--path=从哪里导入
--name=名称
--arch=系统位数32 or 64
task started (id=Media import, time=Fri May 3 13:42:03 2019) ######此处需要较长的一个时间
Found a candidate signature: breed=redhat, version=rhel6
Found a matching signature: breed=redhat, version=rhel6
Adding distros from path /var/www/cobbler/ks_mirror/CentOS7.6:
creating new distro: CentOS7.6-x86_64
trying symlink: /var/www/cobbler/ks_mirror/CentOS7.6 -> /var/www/cobbler/links/CentOS7.6-x86_64
creating new profile: CentOS7.6-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/CentOS7.6 for CentOS7.6-x86_64
processing repo at : /var/www/cobbler/ks_mirror/CentOS7.6
need to process repo/comps: /var/www/cobbler/ks_mirror/CentOS7.6
looking for /var/www/cobbler/ks_mirror/CentOS7.6/repodata/*comps*.xml
Keeping repodata as-is :/var/www/cobbler/ks_mirror/CentOS7.6/repodata
*** TASK COMPLETE ***
-
查看cobbler都有哪些命令
[root@localhost /]# cobbler profile
usage
=====
cobbler profile add
cobbler profile copy
cobbler profile dumpvars
cobbler profile edit
cobbler profile find
cobbler profile getks
cobbler profile list
cobbler profile remove
cobbler profile rename
cobbler profile report
-
例如:我们查看当前有几个镜像
[root@localhost /]# cobbler profile list
CentOS-6-x86_64
CentOS-7-x86_64
-
3、查看我们系统的详细信息
[root@localhost /]# cobbler profile report
Name : CentOS-7.1-x86_64
TFTP Boot Files : {}
Comment :
DHCP Tag : default
Distribution : CentOS-7.1-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
Name : CentOS-7-x86_64
TFTP Boot Files : {}
Comment :
DHCP Tag : default
Distribution : CentOS-7-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
-
4、修改ks的路径,自定义安装后,需要设置的一些东西,例如开启哪些服务,关闭哪些服务。安装什么东西等
我们先上传已经设置好的cfg文件
为了规范,我们把kickstarts文件放在/var/lib/cobbler/kickstarts
下面
[root@localhost ~]# cd /var/lib/cobbler/kickstarts/`
[root@localhost kickstarts]# rz
rz waiting to receive.
-
5、检查一下路径是否正确
[root@localhost kickstarts]# ll
total 60
-rw-r--r-- 1 root root 3704 Aug 24 2016 CentOS-6-x86_64.cfg
-rw-r--r-- 1 root root 1355 Aug 25 2016 CentOS-7-x86_64.cfg
-rw-r--r-- 1 root root 115 Jan 23 2016 default.ks
-rw-r--r-- 1 root root 22 Jan 23 2016 esxi4-ks.cfg
-rw-r--r-- 1 root root 22 Jan 23 2016 esxi5-ks.cfg
drwxr-xr-x 2 root root 54 Aug 23 09:17 install_profiles
-rw-r--r-- 1 root root 1424 Jan 23 2016 legacy.ks
-rw-r--r-- 1 root root 292 Jan 23 2016 pxerescue.ks
-rw-r--r-- 1 root root 2916 Jan 23 2016 sample_autoyast.xml
-rw-r--r-- 1 root root 1825 Jan 23 2016 sample_end.ks
-rw-r--r-- 1 root root 0 Jan 23 2016 sample_esx4.ks
-rw-r--r-- 1 root root 324 Jan 23 2016 sample_esxi4.ks
-rw-r--r-- 1 root root 386 Jan 23 2016 sample_esxi5.ks
-rw-r--r-- 1 root root 1784 Jan 23 2016 sample.ks
-rw-r--r-- 1 root root 3419 Jan 23 2016 sample_old.seed
-rw-r--r-- 1 root root 5879 Jan 23 2016 sample.seed
-
6、自定义kickstarts文件
我们使用cobbler profile report命令看到Kickstart默认在/var/lib/cobbler/kickstarts/sample_end.ks
-
修改7的kickstarts
[root@localhost /]# cobbler profile edit --name=CentOS7.6-x86_64 --kickstart=/var/lib/cobbler/kickstarts/CentOS-7-x86_64.cfg
-
因为Centos7 默认的网卡不在/etc/init.d/network 所以我们需要修改内核
[root@localhost /]# cobbler profile report
-
我们查看到这里可以定义内核参数
[root@localhost /]# cobbler profile edit --name=CentOS-7-x86_64 --kopts='net.ifnames=0 biosdevname=0'
这样我们在安装Centos7的时候就会默认给我们加上这个内核参数
-
7、执行cobbler sync会删除原来的文件,相当于从新进行加载
[root@localhost /]# cobbler sync
task started: 2016-08-24_001542_sync
task started (id=Sync, time=Wed Aug 24 00:15:42 2016)
running pre-sync triggers
cleaning trees
removing: /var/www/cobbler/images/CentOS-7-x86_64
removing: /var/www/cobbler/images/CentOS-7.1-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/images/CentOS-7.1-x86_64
removing: /var/lib/tftpboot/s390x/profile_list
copying bootloaders
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 files for distro: CentOS-7.1-x86_64
trying hardlink /var/www/cobbler/ks_mirror/CentOS-7.1-x86_64/images/pxeboot/vmlinuz -> /var/lib/tftpboot/images/CentOS-7.1-x86_64/vmlinuz
trying hardlink /var/www/cobbler/ks_mirror/CentOS-7.1-x86_64/images/pxeboot/initrd.img -> /var/lib/tftpboot/images/CentOS-7.1-x86_64/initrd.img
copying files for distro: CentOS-7-x86_64
trying hardlink /var/www/cobbler/ks_mirror/CentOS-7-x86_64/images/pxeboot/vmlinuz -> /var/lib/tftpboot/images/CentOS-7-x86_64/vmlinuz
trying hardlink /var/www/cobbler/ks_mirror/CentOS-7-x86_64/images/pxeboot/initrd.img -> /var/lib/tftpboot/images/CentOS-7-x86_64/initrd.img
copying images
generating PXE configuration files
generating PXE menu structure
copying files for distro: CentOS-7.1-x86_64
trying hardlink /var/www/cobbler/ks_mirror/CentOS-7.1-x86_64/images/pxeboot/vmlinuz -> /var/www/cobbler/images/CentOS-7.1-x86_64/vmlinuz
trying hardlink /var/www/cobbler/ks_mirror/CentOS-7.1-x86_64/images/pxeboot/initrd.img -> /var/www/cobbler/images/CentOS-7.1-x86_64/initrd.img
Writing template files for CentOS-7.1-x86_64
copying files for distro: CentOS-7-x86_64
trying hardlink /var/www/cobbler/ks_mirror/CentOS-7-x86_64/images/pxeboot/vmlinuz -> /var/www/cobbler/images/CentOS-7-x86_64/vmlinuz
trying hardlink /var/www/cobbler/ks_mirror/CentOS-7-x86_64/images/pxeboot/initrd.img -> /var/www/cobbler/images/CentOS-7-x86_64/initrd.img
Writing template files for CentOS-7-x86_64
rendering DHCP files
generating /etc/dhcp/dhcpd.conf
rendering TFTPD files
generating /etc/xinetd.d/tftp
processing boot_files for distro: CentOS-7.1-x86_64
processing boot_files for distro: CentOS-7-x86_64
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 ***
-
8、新建虚拟机进行安装
提示:我们先打开系统日志,因为dhcp默认会将日志显示在/var/log/messages
-
9、我们在新建虚拟机 开机
-
到了这一步证明你的操作应该该是没有问题了,只需要耐心等待就可以了
-
报错
-
注:这里可能是你的httpd、dhcp、nftp服务没有开,打开再试一次
-
耐心等待,如果超时2次需要重新启动,因为开启了dhcp获取到了IP地址,但是无法下载文件 肯定会超时
-
鼠标点进去选择刚才咱们配置好的系统,最后只需耐心等待,大功告成了!!!
-
10、网页管理cobbler
-
首先设置用户名跟密码
[root@cobbler/server/tools]# htdigest /etc/cobbler/users.digest "Cobbler" cobbler
-
网页输入:https://10.0.0.65/cobbler_web
-
报错:
-
解决方法
因为你的python版本只要高于1.18就会出现这个错误
所以下就是解决办法
[root@cobbler ~]# rpm -qa | grep "python2-django"
python2-django-1.11.20-1.el7.noarch
[root@cobbler ~]# rpm -e --nodeps python2-django-1.11.20-1.el7.noarch
[root@cobbler ~]# yum install python2-pip
[root@cobbler ~]# pip install --upgrade pip
[root@cobbler ~]# pip install Django==1.8.17
-
重启http
[root@cobbler/server/tools]# systemctl restart httpd
-
再次访问
-
这里的用户名是cobbler,密码就是你刚才输入的密码
-
我们可以查看profiles,点击Edit可以修改系统的一些选项
-
在kernel里面可以设置一些内核参数
-
在Kickstart Templates 可以查看我们的kickstarts文件
-
我们还可以修改kickstart文件