您的位置:首页 > 运维架构

自动化运维之Cobbler自动化系统部署 推荐

2016-08-28 01:00 633 查看
Cobbler简介 运维自动化包括标准化、工具化、Web化、服务化、智能化等几个不同的层级,在一家规模稍大、拥有服务器上千台甚至是几万台的生产环境中,想要真正实现运维自动化,工具化系统安装流程、统一系统安装规范,是其中最不能忽略的重要环节。普通的自动化部署可以采用pxe+dhcp+tftp配合kickstart实现,但是面对多版本、多部署需求的情况下,这种普通的部署方式可能达不到我们的要求;这时候就需要借助cobbler开源自动化部署工具来实现。 Cobbler是一款快速的网络系统部署工具,其最大的特点是集合了所有系统部署所需服务,如DHCP、DNS、TFTP,这样你在部署一台操作系统的时候不需要在各个服务之前协调切换,Cobbler都可以替你来管理,Cobbler内部集成了一个镜像版本仓库,你可以自定义相关配置文件,实现不同系统不同安装需求的选择;当然,Cobbler还提供了包括yum源管理、Web界面管理、API接口、电源管理等功能,方便你自定义开发管理。
Cobbler设计方式


发行版:表示一个操作系统。它承载了内核和 initrd 的信息,以及内核参数等其他数据
配置文件:包含一个发行版、一个 kickstart 文件以及可能的存储库,还包含更多特定的内核参数等其他数据
系统:表示要配给的机器。它包含一个配置文件或一个镜像,还包含 IP 和 MAC 地址、电源管理(地址、凭据、类型)以及更为专业的数据等信息
存储库:保存一个 yum 或 rsync 存储库的镜像信息
镜像:可替换一个包含不属于此类别的文件的发行版对象(例如,无法分为内核和 initrd 的对象)

Cobbler部署实验环境(网段172.16.2.0/24)
[root@linux-node1 ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[root@linux-node1 ~]# uname -r
3.10.0-327.el7.x86_64
1)安装epel源
[root@linux-node1 ~]# rpm -ivh http://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm[/code]2)安装相关服务组件 
[root@linux-node1 ~]# yum install -y httpd dhcp tftp cobbler cobbler-web pykickstart xinetd
#httpd dhcp tftp cobbler程序包 cobbler-web服务包 pykickstart KS文件检查 xinetd 守护进程
[root@linux-node1 ~]# systemctl start cobblerd
[root@linux-node1 ~]# systemctl start httpd
[root@linux-node1 ~]# systemctl start xinetd
安装完成后启动服务,我们先来看看cobbler的一些重要文件目录
/etc/cobbler             # 配置文件目录
/etc/cobbler/settings       # cobbler主配置文件
/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日志
/etc/cobbler/settings中几个比较重要的参数设置
manage_dhcp:1
manage_dns:1
manage_tftpd:1
restart_dhcp:1
restart_dns:1
pxe_just_once:1
next_server:<服务器的 IP 地址>
server:<服务器的 IP 地址>
#选项 next_server 用在 DHCP 配置文件中,向机器告知提供引导文件的服务器地址。选项 server 在机器安装期间用于引用 Cobbler 服务器地址。最后,选项 pxe_just_once 预防将机器中的安装循环配置为始终从网络引导。激活此选项时,机器告诉 Cobbler 安装已完成。Cobbler 将系统对象的 netboot 标志更改为 false,这会强制机器从本地磁盘引导
3)初始化cobbler
[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.
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
上面是告诉我们要对cobbler做的一些初始化设置,接下来一步步设置即可①修改/etc/cobbler/settings配置文件
272 next_server: 172.16.2.150 #如果配置过kickstart知道,这里是告诉启动文件服务器下载地址
②修改/etc/cobbler/settings cobbler主机地址
384 server: 172.16.2.150 #cobbler主机服务器地址
③开启TFTP服务
[root@linux-node1 ~]# vim /etc/xinetd.d/tftp
disable                 = no
④运行cobbler get-loaders下载一些网络引导文件
[root@linux-node1 ~]# cobbler get-loaders
⑤开启rsyncd服务
[root@linux-node1 ~]# systemctl start rsyncd
⑥debian库服务,这里我们可以忽略⑦创建系统初始化后的root密码
[root@linux-node1 ~]# openssl passwd -1 -salt 'cobler' 'cobler'
$1$cobler$XJnisBweZJlhL651HxAM00
[root@linux-node1 ~]# vim /etc/cobbler/settings
101 default_password_crypted: "$1$cobler$XJnisBweZJlhL651HxAM00"
⑧电源模块支持,我们暂时不需要,忽略再次执行检查结果
[root@linux-node1 ~]# cobbler check
The following are potential configuration items that you may want to fix:

1 : enable and start rsyncd.service with systemctl
2 : debmirror package is not installed, it will be required to manage debian deployments and repositories
3 : 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)让cobbler管理DHCP如果我们需要让cobbler替我们管理DHCP服务器,需要修改以下配置
[root@linux-node1 ~]# vim /etc/cobbler/settings
242 manage_dhcp: 1 #改为数字1
[root@linux-node1 ~]# vim /etc/cobbler/dhcp.template #重启cobbler后,cobbler会生成一个模板文件
subnet 172.16.2.0 netmask 255.255.255.0 {
option routers             172.16.2.150; #路由地址
option domain-name-servers 172.16.2.150; #DHCP服务器地址
option subnet-mask         255.255.255.0; #掩码
range dynamic-bootp        172.168.2.200 172.12.2.250; #DHCP地址池
default-lease-time         21600;
max-lease-time             43200;
next-server                $next_server;
5)重启cobbler,并sync生成各种新的配置文件
[root@linux-node1 ~]# systemctl restart cobblerd
[root@linux-node1 ~]# cobbler sync
task started: 2016-08-25_114015_sync
task started (id=Sync, time=Thu Aug 25 11:40:15 2016)
running pre-sync triggers
cleaning trees
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/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 images
generating PXE configuration files #启动PXE程序
generating PXE menu structure
rendering DHCP files #配置DHCP服务
generating /etc/dhcp/dhcpd.conf
rendering TFTPD files #配置TFTP服务
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 #重启DHCP
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 *** #完成
6)接下来,我们就可以在cobbler中加入一个完成的系统安装镜像
[root@linux-node1 ~]# mount /dev/cdrom /mnt #在光驱中加载一个完成的centos7 ISO文件
mount: /dev/sr0 is write-protected, mounting read-only
[root@linux-node1 ~]# cobbler import --path=/mnt/ --name=/CentOS-7-x86_64 --arch=x86_64 #使用import导入
[root@linux-node1 ~]# cobbler import --path=/mnt/ --name=/CentOS-6-x86_64 --arch=x86_64
#同样方式导入一个centos6镜像
[root@linux-node1 ~]# cobbler profile list #查看镜像目录
CentOS-6-x86_64
CentOS-7-x86_64
[root@linux-node1 ~]# cobbler profile report #查看cobbler系统仓库中的版本信息
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文件
7)上传相关的kickstart文件,修改cobbler默认KS配置
[root@linux-node1 ~]# cd /var/lib/cobbler/kickstarts/
[root@linux-node1 kickstarts]# ll
total 60
-rw-r--r-- 1 root root 3701 Aug 25 21:55 CentOS-6-x86_64.cfg
-rw-r--r-- 1 root root 1467 Aug 26 02:07 CentOS-7-x86_64.cfg
[root@linux-node1 kickstarts]# cobbler profile edit --name=CentOS-7-x86_64 --kickstart=/var/lib/cobbler/kickstarts/CentOS-7-x86_64.cfg #上传完成后修改成我们自己的ks文件
[root@linux-node1 kickstarts]# cobbler profile edit --name=CentOS-6-x86_64 --kickstart=/var/lib/cobbler/kickstarts/CentOS-6-x86_64.cfg
[root@linux-node1 ~]# cobbler profile edit --name=CentOS-7-x86_64 --kopts='net.ifnames=0 biosdevname=0' #这里我们顺便修改一下内核文件参数,让centos7初始化网卡名为“eth”
8)修改配置文件记得重启cobbler并执行sync
[root@linux-node1 ~]# cobbler sync
9)网络启动一台新主机,测试cobbler




Cobbler自定义yum源
[root@linux-node1 ~]# cobbler repo add --name=openstack-mitaka --mirror=http://mirrors.aliyun.com/centos/7.2.1511/cloud/x86_64/openstack-mitaka/ --arch=x86_64 --breed=yum #在cobbler repo中增加openstack yum源
[root@linux-node1 ~]# cobbler reposync #同步网络yum源仓库数据
[root@linux-node1 ~]# cobbler profile edit --name=CentOS-7-x86_64 --repos="openstack-mitaka"
#加入到相应name的版本仓库中
[root@linux-node1 ~]# vim /etc/cobbler/settings #修改cobbler配置允许cobbler帮我们自定义yum
yum_post_install_mirror: 1
[root@linux-node1 ~]# vim /var/lib/cobbler/kickstarts/CentOS-7-x86_64.cfg
%post
systemctl disable postfix.service

$yum_config_stanza #在kickstart文件加入新yum
%end

[root@linux-node1 ~]# echo "1 3 * * * /usr/bin/cobbler reposync --tries=3 --no-fail" >> /var/spool/cron/root #定时任务同步yum数据
[root@linux-node1 ~]# cat /etc/yum.repos.d/cobbler-config.repo #安装成功,能在被部署的机器上看见yum源


cobbler指定MC地址定制安装

[root@linux-node1 yum.repos.d]# cobbler system add --name=linux-node2.lichengbing.cn --mac=00:0C:29:8E:A9:4F --profile=CentOS-7-x86_64 \
> --ip-address=172.16.2.151 --subnet=255.255.255.0 --gateway=172.16.2.151 --interface=eth0 \
> --static=1 --hostname=linux-node2.lichengbing.cn --name-servers="172.16.2.150" \
> --kickstart=/var/lib/cobbler/kickstarts/CentOS-7-x86_64.cfg
#指定MAC地址的机器安装,配置IP地址、主机名等
[root@linux-node1 yum.repos.d]# cobbler system list #查看自定义主机列表
linux-node2.lichengbing.cn



Web管理Cobbler
[root@linux-node1 cobbler]# cat users.conf
#配置web界面用户文件,生成密码
[root@linux-node1 cobbler]# htdigest /etc/cobbler/users.digest "Cobbler" cobbler
Changing password for user cobbler in realm Cobbler
New password:
Re-type new password:

[root@linux-node1 ~]# cat /etc/cobbler/pxe/pxedefault.template #自定义cobbler安装界面
DEFAULT menu
PROMPT 0
MENU TITLE Cobbler | http://cobbler.github.io TIMEOUT 200
TOTALTIMEOUT 6000
ONTIMEOUT $pxe_timeout_profile

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

$pxe_menu_items

MENU end

[root@linux-node1 ~]# cobbler sync




查看Distros信息

Cobbler自定义重装
[root@localhost ~]# rpm -ivh http://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm [root@localhost ~]# yum install koan -y #安装koan服务
[root@localhost ~]# koan --replace-self --server=172.16.2.150 --profile=CentOS-6-x86_64
#指定这台主机需要被重装,指定cobbler服务器地址和profile文件
[root@localhost ~]# reboot #重启后就会自动重新安装
cobbler同样有API接口,有Python基础的可以试试...
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息