自动化运维之 Cobbler 自动化安装服务器系统

 

日常运维工作中,当大规模服务器上架时,系统安装就变成了烦心事。吵闹的IDC机房,无脑的下一步选择, 频繁的安装系统且效率低下。如果你是搞云计算的,计算节点安装肯定让你印象深刻。如果你们用的是PXE安装,初期调试肯定也容易让人崩溃。系统单一,安装效率慢,安装方式单一,为了解决这一系列由于装机引起的问题,解决工作中的痛点,我们引进了Cobbler,一个让部署系统简单方便的自动化部署工具。同时还可以制作内部Yum源,简直是方便的不能在方便,自动化安装成百上千台服务器将不是梦。

OK:不要干,就是怂。Here we go!

Cobbler简介

Cobbler是一个快速网络安装linux的服务,而且在经过调整也可以支持网络安装windows。该工具使用python开发,小巧轻便(才15k行python代码),使用简单的命令即可完成PXE网络安装环境的配置,同时还可以管理DHCP、DNS、TFTP、RSYNC以及yum仓库、构造系统ISO镜像。
Cobbler支持命令行管理,web界面管理,还提供了API接口,可以方便二次开发使用。
Cobbler客户端Koan支持虚拟机安装和操作系统重新安装,使重装系统更便捷。
Cobbler各个组件之间关系

cobbler各主要组件关系图

Cobbler功能介绍

使用 Cobbler,您无需进行人工干预即可安装机器。Cobbler 设置一个 PXE 引导环境(它还可使用 yaboot 支持 PowerPC),并控制与安装相关的所有方面,比如网络引导服务(DHCP 和 TFTP)与存储库镜像。当希望安装一台新机器时,Cobbler 可以:

  • 使用一个以前定义的模板来配置 DHCP 服务(如果启用了管理 DHCP)

  • 将一个存储库(yum 或 rsync)建立镜像或解压缩一个媒介,以注册一个新操作系统
    在 DHCP 配置文件中为需要安装的机器创建一个条目,并使用您指定的参数(IP 和 MAC 地址)

  • 在 TFTFP 服务目录下创建适当的 PXE 文件

  • 重新启动 DHCP 服务以反映更改

  • 重新启动机器以开始安装(如果电源管理已启用)

基础实验环境准备

[root@cobbler-node1 ~]# cat /etc/redhat-release
#系统版本CentOS Linux release 7.1.1503 (Core)
[root@cobbler-node1 ~]# uname –r
#内核版本3.10.0-229.el7.x86_64
[root@cobbler-node1 ~]# getenforce #
检测selinux是否关闭(必须关闭)Disabled
[root@cobbler-node1 ~]# systemctl stop firewalld
#关闭防火墙
[root@cobbler-node1 ~]# ifconfig eth0|awk -F '[ :]+' 'NR==2 {print $3}'
#查看IP地址10.0.0.101
[root@cobbler-node1 ~]# hostname
#查看主机名cobbler-node1
[root@cobbler-node1 ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
#cobbler安装必须使用到epel源

提示
虚拟机网卡采用NAT模式,因为我们会搭建DHCP服务器,在同一局域网多个DHCP服务会有冲突,并且导致实践失败。

安装Cobbler

[root@cobbler-node1 ~]# yum install cobbler cobbler-web pykickstart httpd tftp dhcp xinetd

RPM包详解:

cobbler:cobbler程序包
cobbler-web:cobbler的web服务包
pykickstart:cobbler检查kickstart语法错误
httpd:Apache web服务
tftp:tftp服务
dhcp:dhcp服务

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日志
/var/www/cobbler/repo_mirror  # repo源存放路径

检测Cobbler

cobbler的运行依赖于dhcp、tftp、rsync及dns服务,其中dhcp可由dhcpd(isc)提供,也可由dnsmasq提供;tftp可由tftp-server程序包提供,也可由cobbler功能提供,rsync有rsync程序包提供,dns可由bind提供,也可由dnsmasq提供。

cobbler可自行管理这些服务中的部分甚至是全部,但需要配置/etc/cobbler/settings文件中的“manange_dhcp”、“manager_tftpd”、“manager_rsync”、“manager_dns”分别来进行定义。

另外,由于各种服务都有着不同的实现方式,如若需要进行自定义,需要通过修改/etc/cobbler/modules.conf配置文件中各服务的模块参数的值来实现。

[root@cobbler-node1 ~]# systemctl start httpd     #启动apache服务
[root@cobbler-node1 ~]# systemctl start cobblerd  #启动cobbler程序
# 检查配置文件,需要在cobblerd和httpd启动的情况下检查
[root@cobbler-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

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

上述问的解决方法如下所示

1.修改/etc/cobbler/settings文件中的server参数的值为提供cobbler服务的主机相应的IP地址或主机名,如server: 10.0.0.101;
[root@cobbler-node1 ~]# sed -i 's/server: 127.0.0.1/server: 10.0.0.7/' /etc/cobbler/settings
2.修改/etc/cobbler/settings文件中的next_server参数的值为提供PXE服务的主机相应的IP地址,如next_server: 10.0.0.101;
[root@cobbler-node1 ~]# sed -i 's/next_server: 127.0.0.1/next_server: 10.0.0.7/' /etc/cobbler/settings
3.修改/etc/xinetd.d/tftp文件中的disable参数修改为disable = no
4.执行cobbler get-loaders命令即可;否则,需要安装syslinux程序包,而后复制/usr/share/syslinux/{pxelinux.0,memu.c32}等文件至/var/lib/cobbler/loaders/目录中;
5.执行systemctl enable rsyncd命令即可;
6.如果有强迫症可以选择yum –y install debmirror然后根据错误进行解决,一般错误如下。
## 注释/etc/dedmirror.conf文件中的@dists=”sid”;@arches=”i386”;
7.[root@cobbler-node1 ~]# openssl passwd -1 -salt '$(openssl rand -hex 4)' 'EC' $1$$(openss$yWzTjLjGmjQJUbnsOQnx8/
[root@cobbler-node1 ~]# grep "default_password_crypted" /etc/cobbler/settings #替换/etc/cobbler/setting内的default_password_crypted
default_password_crypted: "$1$$(openss$yWzTjLjGmjQJUbnsOQnx8/"
8.yum –y install cman fence-agents
systemctl restart cobblerd     #最后重启Cobbler

配置DHCP

[root@cobbler-node1 ~]# sed -i 's#manage_dhcp: 0#manage_dhcp: 1#g' /etc/cobbler/settings
#使用cobbler管理dhcp
[root@cobbler-node1 ~]# vim /etc/cobbler/dhcp.template
#修改cobbler的dhcp模版,因为cobbler会替换。
subnet 10.0.0.0 netmask 255.255.255.0 {
     option routers             10.0.0.2;
     option domain-name-servers 10.0.0.2;
     option subnet-mask         255.255.255.0;
     range dynamic-bootp        10.0.0.200 10.0.0.250;
     default-lease-time         21600;
     max-lease-time             43200;
     next-server                $next_server;

同步Cobbler

[root@cobbler-node1 ~]# systemctl restart xinetd#重启xinetd
[root@cobbler-node1 ~]# systemctl restart cobblerd#重启cobbler
[root@cobbler-node1 ~]# cobbler sync#同步最新cobbler配置,可以看具体做了哪些操作
task started: 2016-07-29_234117_sync
task started (id=Sync, time=Fri 29 10:34:21 2016)
running pre-sync triggers
cleaning trees
removing: /var/lib/tftpboot/pxelinux.cfg/defaultremoving: /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
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 ***

管理Cobbler

管理distro

cobbler变得可用的第一步为定义distro,其可以通过为其指定外部的安装引导内核及ramdisk文件的方式实现。
如果已经有完成的安装树(如os的安装镜像)则推荐使用improt之间导入的方式进行。

[root@cobbler-node1 ~]# mount /dev/cdrom /mnt/#挂在ISO光盘至服务器
mount: /dev/sr0 is write-protected, mounting read-only
[root@cobbler-node1 ~]# cobbler import --path=/mnt/ --name=CentOS-7.1-x86_64-distro --arch=x86_64# --path 镜像路径# --name 为安装源定义一个名字
# --arch 指定安装源是32位、64位、ia64, 目前支持的选项有: x86│x86_64│ia64# 安装源的唯一标示就是根据name参数来定义,本例导入成功后,安装源的唯一标示就是:CentOS-7.1-distro-x86_64。
# 镜像存放目录,cobbler会将镜像中的所有安装文件拷贝到本地一份,放在/var/www/cobbler/ks_mirror下的CentOS-7.1-x86_64-distro-x86_64目录下。因此/var/www/cobbler目录必须具有足够容纳安装文件的空间。
task started: 2016-07-30_010122_import
task started (id=Media import, time=Sat 30 11:05:02 2016)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.1-x86_64-distro-x86_64:
creating new distro: CentOS-7.1-distro-x86_64
trying symlink: /var/www/cobbler/ks_mirror/CentOS-7.1-x86_64-distro-x86_64 -> /var/www/cobbler/links/CentOS-7.1-distro-x86_64
creating new profile: CentOS-7.1-distro-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.1-x86_64-distro-x86_64 for CentOS-7.1-distro-x86_64
processing repo at : /var/www/cobbler/ks_mirror/CentOS-7.1-x86_64-distro-x86_64
need to process repo/comps: /var/www/cobbler/ks_mirror/CentOS-7.1-x86_64-distro-x86_64
looking for /var/www/cobbler/ks_mirror/CentOS-7.1-x86_64-distro-x86_64/repodata/*comps*.xml
Keeping repodata as-is :/var/www/cobbler/ks_mirror/CentOS-7.1-x86_64-distro-x86_64/repodata
*** TASK COMPLETE ***
[root@cobbler-node1 ~]#  cobbler distro list  #列出所有的distro
   CentOS-7.1-distro-x86_64
[root@cobbler-node1 ~]# cobbler profile list  #导入distro会自动生成profile
   CentOS-7.1-distro-x86_64

如果有kickstart文件,也可以使用—kickstart=/path/to/kickstart_file进行导入,因此import会自动为导入的distro生成一个profile

管理profile

cobbler使用profile来为特定的需求类别提供锁需要安装的配置,即在distro的基础上通过提供kiskstart文件来生成一个特定的系统安装配置。distro的profile可以出现在pxe的引导菜单中作为安装的选择之一。

Cobbler-CentOS-7.1-x86_64.cfg
默认是有kickstart文件的,所以edit,如果没有kickstart文件可以add。

[root@cobbler-node1 ~]# cobbler profile edit --name=Centos7.1-distro-x86_64 --kickstart=/var/lib/cobbler/kickstarts/Cobbler-CentOS-7.1-x86_64.cfg
#指定ks路径#CentOS7系统网卡名变成eno...这种,为了运维标准化,我们需要修改为我们常用的eth0,使用下面的参数。但要注意是CentOS7才需要下面的步骤,CentOS6不需要。
[root@cobbler-node1 ~]# cobbler profile edit --name=Centos7.1-distro-x86_64 --kopts='net.ifnames=0 biosdevname=0'
#修改centos7内核#新部署机器安装yum源,并同步。建议使用内网yum源,在这里使用阿里云yum源
[root@cobbler-node1 ~]# cobbler repo add --name=centos-7-base --mirror=http://mirrors.aliyun.com/centos/7/os/x86_64/Packages/ --arch=x86_64 --breed=yum
#添加yum源
[root@cobbler-node1 ~]# cobbler reposync
#同步yum源
[root@cobbler-node1 ~]# cobbler sync
#每次修改profile都需要同步

以上添加和同步Yum源,就是在制作内部Yum源,Cobbler会将你添加的源地址下载到本地 /var/www/cobbler/repo_mirror目录下。下载完毕直接通过http访问就可以了,和用互联网源没有什么区别。

部署操作系统

新建一台虚拟机,通过网络启动即可。(我这里虚拟机使用的是NAT模式,并且关闭DHCP功能)

用户:root
密码:ec123456
openssl生成时指定

开始部署:

进入Cobbler选择对应操作系统安装

安装完毕:

安装完毕登陆CentOS7系统

查看版本:

检查系统版本内核IP地址

定制化安装

由于kickstart指定某台服务器使用某个ks文件比较复杂,所以引用Cobbler就很简单。通过物理MAC地址来区分。

自定义安装

system主要目的配置网络接口,通过system来固定机器的IP、掩码、网关、DNS、主机名、等等实现基础环境标准化,根据机器的MAC地址,自动绑定IP,网关,dns等.

查看设备Mac地址

开始安装

[root@cobbler-node1 ~]#cobbler system add --name=xuliangwei-pc --mac=00:0C:29:6E:41:CB --profile=Centos7.1-profile-x86_64 \\\\
--ip-address=10.0.0.110 --subnet=255.255.255.0 --gateway=10.0.0.2 --interface=eth0 \\\\
--static=1 --hostname=EC.com --name-servers="114.114.114.114 8.8.8.8"
[root@cobbler-node1 ~]# cobbler sync
[root@linux-node1 ~]#  cobbler system list

不进行询问,自动安装

登陆系统检查相关信息

自定义登陆界面

企业声明,可以通过这种方式

[root@cobbler-node1 ~]# grep "EC" /etc/cobbler/pxe/pxedefault.template MENU TITLE EC | http://EC.com
#自定义装机页面
[root@cobbler-node1 ~]# cobbler sync
#同步

web管理Cobbler

新版cobbler的web界面使用的是https,登录https://10.0.0.7/cobbler_web
cobbler_web支持多种认证方式,如authn_configfil、authn_ldap或authn_pam等,默认为authn_denyall,即拒绝所有用户登陆。

用户认证

下面说明三种能认证用户登录cobbler_web的方式

  • 使用authn_pam模块认证cobbler_web用户
    首先修改modules中的[authentication]段中的module参数的值为authn_pam
    接着创建系统用户,并为用户设定密码
    而后将设定的系统用户添加至cobbler_web的admin组中,修改/etc/cobbler/users.conf 文件,将设定的用户添加为admin参数的值即可

  • 使用authn_configfile模块认证cobbler_web用户
    首先修改modules中的[authentication]段中的module参数的值为authn_configfile
    添加第一用户时,需要为htdigest命令使用“-c”etc/cobbler/users.digest,后续添加其他用户则不能再使用,同步cobbler重启httpd以及cobbler

  • 使用cobbler默认的web账号密码认证
    user:cobbler
    pass:cobbler

访问https://IP/cobbler_web

cobbler_web界面

查看当前distro

查看当前profile

查看当前system

查看当前repos

猜你喜欢

转载自blog.csdn.net/qq_24058965/article/details/103295238