第1章 Cobbler 自动化部署系统
1.1 环境准备
1.1.1 查看系统版本
[root@Cobbler ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
1.1.2 内核版本
[root@Cobbler ~]# uname -a
Linux Cobbler 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
1.1.3 确保关闭selinux
[root@Cobbler ~]# getenforce
Disabled
1.1.4 关闭防火墙
[root@Cobbler ~]# iptables -L -n
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
1.1.5 主机名
[root@Cobbler ~]# hostname
Cobbler
1.1.6 hosts本地解析
[root@Cobbler ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.60 Cobbler
172.16.1.60 Cobbler
1.1.7 安装yum源
[root@Cobbler ~]# rpm -ivh http://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
第2章 安装cobbler
2.1 安装cobbler
[root@Cobbler ~]# yum install cobbler cobbler-web pykickstart httpd dhcp tftp xinetd -y
安装包说明:
cobbler #Cobbler程序包
cobbler-web #Cobbler的web服务包
pykickstart #Cobbler检查kickstart语法错误
httpd #Apache web服务
dhcp #Dhcp服务
tftp #Tftp服务
2.2 重要配置文件注释
[root@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日志
2.3 启动服务
cobbler的运行依赖于dhcp、tftp、rsync及dns服务
[root@Cobbler ~]# systemctl start httpd
[root@Cobbler ~]# systemctl start cobblerd
[root@Cobbler ~]# ss -tlunp
2.4 检查Cobbler的配置,如果看不到下面的结果,再次执行systemctl start cobblerd
[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 kckstarting features will not work. This should be a resolvable hostname or IP for the boot server s 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 someting 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-laders' to download them, or, if you only want to handle x86/x86_64 netbooting, you may ensure that ou have installed a *recent* version of the syslinux package installed and can ignore this message ntirely. Files in this directory, should you want to support all architectures, should include pxeinux.0, menu.c32, elilo.efi, and yaboot. The 'cobbler get-loaders' command is the easiest way to reolve 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 reposiories
7 : The default password used by the sample templates for newly installed machines (default_passwor_crypted in /etc/cobbler/settings) is still set to 'cobbler' and should be changed, try: "openssl psswd -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.
2.4.1 针对上方问题的逐一解决:
1.修改/etc/cobbler/settings文件中的server参数的值为提供cobbler服务的主机相应的IP地址或主机名"如server: 10.0.0.60";
此命令可以更改两个参数解决 1,2问题。
sed -i 's/server: 127.0.0.1/server: 172.16.1.60/' /etc/cobbler/settings
检查:
[root@linux-node1 ~]# grep 172.16.1.60 /etc/cobbler/settings
next_server: 172.16.1.60
server: 172.16.1.60
2. 见上条命令结果检查;
3. 修改/etc/xinetd.d/tftp中的disable的参数修改为"disable = no";
4. 按照提示执行"cobbler get-loaders"下载loaders;
查看下载的内容
[root@Cobbler ~]# ls /var/lib/cobbler/loaders/
COPYING.elilo COPYING.yaboot grub-x86_64.efi menu.c32 README
COPYING.syslinux elilo-ia64.efi grub-x86.efi pxelinux.0 yaboot
5. 安装提示执行 "systemctl enable rsyn
[root@Cobbler ~]# systemctl enable rsyncd
6. 待定;
7. 创建默认系统用户及密码:
[root@Cobbler ~]# openssl passwd -1 -salt 'root' '123456' (此为系统root的密码)
$1$root$j0bp.KLPyr.u9kgQ428D10
将生成的密码添加到配置文件
[root@Cobbler ~]# grep default_password /etc/cobbler/settings
default_password_crypted: "$1$root$j0bp.KLPyr.u9kgQ428D10"
8. 待定;
2.4.2 重启cobblerd后再做检查:
还差两项(6,8),一个为debian系统相关,一个为电源管理设备相关,此处暂不做调整;
[root@Cobbler ~]# systemctl restart cobblerd
[root@Cobbler ~]# cobbler check
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 reposiories
2 : fencing tools were not found, and are required to use the (optional) power management features.install cman or fence-agents to use them
2.5 配置DHCP
[root@Cobbler ~]# sed -i 's#manage_dhcp: 0#manage_dhcp: 1#g' /etc/cobbler/settings
[root@Cobbler ~]# vim /etc/cobbler/dhcp.template
……
subnet 172.16.1.0 netmask 255.255.255.0 {
#option routers 192.168.1.5;
#option domain-name-servers 192.168.1.1;
option subnet-mask 255.255.255.0;
range dynamic-bootp 172.16.1.100 172.16.1.254;
default-lease-time 21600;
max-lease-time 43200;
next-server $next_server;
……
2.6 同步Cobbler配置
同步最新cobbler配置,它会根据配置自动修改dhcp等服务
[root@Cobbler ~]# systemctl restart xinetd
[root@Cobbler ~]# systemctl restart cobblerd
[root@Cobbler ~]# cobbler rync
No such command: rync
[root@Cobbler ~]# cobbler sync
task started: 2017-12-21_125119_sync
task started (id=Sync, time=Thu Dec 21 12:51:19 2017)
running pre-sync triggers
cleaning trees
removing: /var/lib/tftpboot/grub/images
copying bootloaders
trying hardlink /var/lib/cobbler/loaders/pxelinux.0 -> /var/lib/tftpboot/pxelinux.0
trying hardlink /var/lib/cobbler/loaders/menu.c32 -> /var/lib/tftpboot/menu.c32
trying hardlink /var/lib/cobbler/loaders/yaboot -> /var/lib/tftpboot/yaboot
trying hardlink /usr/share/syslinux/memdisk -> /var/lib/tftpboot/memdisk
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 ***
#查看dhcp配置文件的标注开头证明自己由Cobbler管理
[root@Cobbler ~]# less /etc/dhcp/dhcpd.conf
# ******************************************************************
# Cobbler managed dhcpd.conf file
# generated from cobbler dhcp.conf template (Thu Dec 21 04:51:20 2017)
# Do NOT make changes to /etc/dhcpd.conf. Instead, make your changes
# in /etc/cobbler/dhcp.template, as /etc/dhcpd.conf will be
# overwritten.
# ******************************************************************
2.7 导入镜像参数
[root@Cobbler ~]# cobbler import --help
Usage: cobbler import [options]
Options:
-h, --help show this help message and exit
--arch=ARCH OS architecture being imported
--breed=BREED the breed being imported
--os-version=OS_VERSION
the version being imported
--path=PATH local path or rsync location
--name=NAME name, ex 'RHEL-5'
--available-as=AVAILABLE_AS
tree is here, don't mirror
--kickstart=KICKSTART_FILE
assign this kickstart file
--rsync-flags=RSYNC_FLAGS
pass additional flags to rsync
2.8 常见参数注释:
cobbler check 核对当前设置是否有问题
cobbler list 列出所有的cobbler元素
cobbler report 列出元素的详细信息
cobbler sync 同步配置到数据目录,更改配置最好都要执行下
cobbler reposync 同步yum仓库
cobbler distro 查看导入的发行版系统信息
cobbler system 查看添加的系统信息
cobbler profile 查看配置信息
2.9 可单个执行查看帮助信息
[root@Cobbler ~]# cobbler distro
usage
cobbler distro add
cobbler distro copy
cobbler distro edit
cobbler distro find
cobbler distro list
cobbler distro remove
cobbler distro rename
cobbler distro report
第3章 网页登陆配置cobbler
浏览器地址栏输入:https://10.0.0.60/cobbler_web即可打开cobbler网页
默认用户:cobbler
默认密码:cobbler
3.1.1 修改cobbler网页默认登陆用户和密码
/etc/cobbler/users.conf # Web服务授权配置文件
/etc/cobbler/users.digest # 用于web访问的用户名密码配置文件
[root@nfs01 ~]# cat /etc/cobbler/users.digest
cobbler:Cobbler:a2d6bae81669d707b72c0bd9806e01f3
# 设置Cobbler web用户登陆密码
# 在Cobbler组添加cobbler用户,提示输入2遍密码确认
htdigest /etc/cobbler/users.digest "Cobbler" cobbler
Changing password for user cobbler in realm Cobbler
New password: 123456
Re-type new password:123456
cobbler sync 同步配置文件
3.2 web界面管理
3.2.1 挂载光盘
3.2.1.1 检查挂在详情
mount /dev/cdrom /mnt/ 将光盘挂载到/mnt目录下面
df –h 通过df –h命令查看挂载结果
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 6.6G 1.6G 4.8G 25% /
tmpfs 364M 0 364M 0% /dev/shm
/dev/sda1 190M 35M 146M 19% /boot
/dev/sr0 3.7G 3.7G 0 100% /mnt
3.2.1.2 光盘挂载完之后在web页面进行以下操作
3.2.1.3 在命令行通过以下命令查看同步结果
[root@nfs01 ~]# ps -ef |grep rsync
root 29425 28711 23 16:56 ? 00:00:04 rsync -a /mnt/ /var/www/cobbler/ks_mirror/CentOS6.9-x86_64 --progress
root 29426 29425 0 16:56 ? 00:00:00 rsync -a /mnt/ /var/www/cobbler/ks_mirror/CentOS6.9-x86_64 --progress
root 29427 29426 20 16:56 ? 00:00:03 rsync -a /mnt/ /var/www/cobbler/ks_mirror/CentOS6.9-x86_64 –progress
看到上面三个进程说明正在同步到/var/www/cobbler/ks_mirror/CentOS6.9-x86_64目录中
3.2.1.4 查看同步结果
[root@nfs01 CentOS6.9-x86_64]# ls /var/www/cobbler/ks_mirror/CentOS6.9-x86_64/
CentOS_BuildTag GPL Packages RPM-GPG-KEY-CentOS-6 RPM-GPG-KEY-CentOS-Testing-6
EFI images RELEASE-NOTES-en-US.html RPM-GPG-KEY-CentOS-Debug-6 TRANS.TBL
EULA isolinux repodata RPM-GPG-KEY-CentOS-Security-6
[root@nfs01 CentOS6.9-x86_64]# du -sh
3.8G .
已经将光盘中的内容同步到cobbler服务器中
3.2.2 查看镜像信息
3.2.2.1 以下信息都是默认的,不需要填写
3.2.3 属性信息 ks.cfg配置文件信息
3.2.4 添加Kickstart Templates
3.2.4.1 ks文件内容如下(CentOS6.x)
cat /var/lib/cobbler/kickstarts/CentOS-6.9-x86_64.cfg
# Cobbler for Kickstart Configurator for CentOS 6.9 by jx Liu
install
url --url=$tree
text
lang en_US.UTF-8
keyboard us
zerombr
bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"
$SNIPPET('network_config')
timezone --utc Asia/Shanghai
authconfig --enableshadow --passalgo=sha512
rootpw --iscrypted $default_password_crypted
clearpart --all --initlabel
part /boot --fstype=ext4 --asprimary --size=200
part swap --size=1024
part / --fstype=ext4 --grow --asprimary --size=200
firstboot --disable
selinux --disabled
firewall --disabled
logging --level=info
reboot
%pre
$SNIPPET('log_ks_pre')
$SNIPPET('kickstart_start')
$SNIPPET('pre_install_network_config')
# Enable installation monitoring
$SNIPPET('pre_anamon')
%end
%packages
@base
@compat-libraries
@debugging
@development
tree
nmap
sysstat
lrzsz
dos2unix
telnet
%end
%post --nochroot
$SNIPPET('log_ks_post_nochroot')
%end
%post
$SNIPPET('log_ks_post')
# Start yum configuration
$yum_config_stanza
# End yum configuration
$SNIPPET('post_install_kernel_options')
$SNIPPET('post_install_network_config')
$SNIPPET('func_register_if_enabled')
$SNIPPET('download_config_files')
$SNIPPET('koan_environment')
$SNIPPET('redhat_register')
$SNIPPET('cobbler_register')
# Enable post-install boot notification
$SNIPPET('post_anamon')
# Start final steps
$SNIPPET('kickstart_done')
# End final steps
#wget -O /tmp/optimization.sh http://10.0.0.31/ks_config/optimization.sh &>/dev/null
#/bin/sh /tmp/optimization.sh 此脚本需要自己定义
%end
3.2.4.2 ks配置文件(CentOS7.x)
cat /var/lib/cobbler/kickstarts/CentOS-7.2-x86_64.cfg
# Cobbler for Kickstart Configurator for CentOS 7.2 by jx Liu
install
url --url=$tree
text
lang en_US.UTF-8
keyboard us
zerombr
bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet"
$SNIPPET('network_config')
timezone --utc Asia/Shanghai
authconfig --enableshadow --passalgo=sha512
rootpw --iscrypted $default_password_crypted
clearpart --all --initlabel
part /boot --fstype xfs --size 1024
part swap --size 1024
part / --fstype xfs --size 1 --grow
firstboot --disable
selinux --disabled
firewall --disabled
logging --level=info
reboot
%pre
$SNIPPET('log_ks_pre')
$SNIPPET('kickstart_start')
$SNIPPET('pre_install_network_config')
# Enable installation monitoring
$SNIPPET('pre_anamon')
%end
%packages
@base
@compat-libraries
@debugging
@development
tree
nmap
sysstat
lrzsz
dos2unix
telnet
%end
%post --nochroot
$SNIPPET('log_ks_post_nochroot')
%end
%post
$SNIPPET('log_ks_post')
# Start yum configuration
$yum_config_stanza
# End yum configuration
$SNIPPET('post_install_kernel_options')
$SNIPPET('post_install_network_config')
$SNIPPET('func_register_if_enabled')
$SNIPPET('download_config_files')
$SNIPPET('koan_environment')
$SNIPPET('redhat_register')
$SNIPPET('cobbler_register')
# Enable post-install boot notification
$SNIPPET('post_anamon')
# Start final steps
$SNIPPET('kickstart_done')
# End final steps
wget -O /tmp/optimization.sh http://10.0.0.31/ks_config/optimization.sh &>/dev/null
/bin/sh /tmp/optimization.sh
%end
3.2.4.3 脚本文件存放路径
[root@nfs01 ks_config]# ls /var/www/html/ks_config/ #只要在网页能显示出来就能执行
CentOS-Base.repo epel.repo hosts optimization.sh sshd_config sysctl.conf
3.2.4.4 涉及到修改ip的文件
/var/lib/cobbler/kickstarts/CentOS-6.9-x86_64.cfg 需要将里面涉及到的ip地址改为当前服务器的即可
/var/www/html/ks_config/optimization.sh 需要将里面涉及到的ip地址改为当前服务器的即可
3.2.4.5 脚本/var/www/html/ks_config/optimization.sh内容如下:
#!/bin/bash
. /etc/init.d/functions
Ip=172.16.1.61
Port=80
ConfigDir=ks_config
# Judge Http server is ok?
PortNum=`nmap $Ip -p $Port 2>/dev/null|grep open|wc -l`
[ $PortNum -lt 1 ] && {
echo "Http server is bad!"
exit 1
}
# Defined result function
function Msg(){
if [ $? -eq 0 ];then
action "$1" /bin/true
else
action "$1" /bin/false
fi
}
# Defined IP function
function ConfigIP(){
Suffix=`ifconfig eth1|awk -F "[ .]+" 'NR==2 {print $6}'`
cat >/etc/sysconfig/network-scripts/ifcfg-eth0 <<-END
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
IPADDR=10.0.0.$Suffix
PREFIX=24
GATEWAY=10.0.0.254
DNS1=10.0.0.254
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
NAME="System eth0"
END
Msg "config eth0"
cat >/etc/sysconfig/network-scripts/ifcfg-eth1 <<-END
DEVICE=eth1
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
IPADDR=172.16.1.$Suffix
PREFIX=24
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
NAME="System eth1"
END
Msg "config eth1"
}
# Defined Yum source Functions
function yum(){
YumDir=/etc/yum.repos.d
[ -f "$YumDir/CentOS-Base.repo" ] && cp $YumDir/CentOS-Base.repo{,.ori}
wget -O $YumDir/CentOS-Base.repo http://$Ip:$Port/$ConfigDir/CentOS-Base.repo &>/dev/null &&\
wget -O $YumDir/epel.repo http://$Ip:$Port/$ConfigDir/epel.repo &>/dev/null &&\
Msg "YUM source"
}
# Defined add Ordinary users Functions
function AddUser(){
useradd oldboy &>/dev/null &&\
echo "123456"|passwd --stdin oldboy &>/dev/null &&\
sed -i '98a oldboy ALL=(ALL) NOPASSWD:ALL' /etc/sudoers &&\
visudo -c &>/dev/null
Msg "AddUser oldboy"
}
# Defined Hide the system version number Functions
function HideVersion(){
[ -f "/etc/issue" ] && >/etc/issue
Msg "Hide issue"
[ -f "/etc/issue.net" ] && > /etc/issue.net
Msg "Hide issue.net"
}
# Defined SSHD config Functions
function sshd(){
SshdDir=/etc/ssh
[ -f "$SshdDir/sshd_config" ] && /bin/mv $SshdDir/sshd_config{,.ori}
wget -O $SshdDir/sshd_config http://$Ip:$Port/$ConfigDir/sshd_config &>/dev/null &&\
chmod 600 $SshdDir/sshd_config
Msg "sshd config"
}
# Defined OPEN FILES Functions
function openfiles(){
[ -f "/etc/security/limits.conf" ] && {
echo '* - nofile 65535' >> /etc/security/limits.conf
Msg "open files"
}
}
# Defined Kernel parameters Functions
function kernel(){
KernelDir=/etc
[ -f "$KernelDir/sysctl.conf" ] && /bin/mv $KernelDir/sysctl.conf{,.ori}
wget -O $KernelDir/sysctl.conf http://$Ip:$Port/$ConfigDir/sysctl.conf &>/dev/null
Msg "Kernel config"
}
# Defined hosts file Functions
function hosts(){
HostsDir=/etc
[ -f "$HostsDir/hosts" ] && /bin/mv $HostsDir/hosts{,.ori}
wget -O $HostsDir/hosts http://$Ip:$Port/$ConfigDir/hosts &>/dev/null
Msg "Hosts config"
}
# Defined System Startup Services Functions
function boot(){
for oldboy in `chkconfig --list|grep "3:on"|awk '{print $1}'|grep -vE "crond|network|rsyslog|sshd"`
do
chkconfig $oldboy off
done
Msg "BOOT config"
}
# Defined Time Synchronization Functions
function Time(){
echo "#time sync by zhangyao at $(date +%F)" >>/var/spool/cron/root
echo '*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com &>/dev/null' >>/var/spool/cron/root
Msg "Time Synchronization"
}
# Defined main Functions
function main(){
ConfigIP
yum
#AddUser
HideVersion
#sshd
openfiles
kernel
hosts
boot
Time
}
main
3.2.5 配置systems
3.2.5.1 systems选项用来指定客户端的ip地址,主机名等信息
3.2.5.2 CentOS7.x需要在Kernel Options加入如下内容
修改CentOS7的内核-修改网卡的名字
net.ifnames=0 biosdevname=0