目录
一、简介
作为运维,在数据机房的建立中批量部署系统,在日后运维中重装系统,最常用的方法是使用PXE。Cobbler是PXE的高级封装,以命令行的方式进行管理(也有web管理),是一个系统启动服务,同时也是依赖基础的DHCP、TFTP等基础服务。
二、Cobbler服务端部署
更新yum源为国内
内网环境手动将需要的包上传的服务器
#从aliyun下载repo文件
curl -o /etc/yum.repos.d/CentOS-aliyun.repo https://mirrors.aliyun.com/repo/Centos-7.repo
yum clean all #清除yum仓缓存
yum makecache #重建缓存
关闭selinux、防火墙
sed -i 's/^SELINUX=enforcing/SELINUX=disable/g' /etc/selinux/config
#配置需要重启才能生效,或者setenforce 0临时生效(不建议)
getenforce #查看selinux状态,为Permissive则表示已关闭
systemctl stop firewalld.service
systemctl disable firewalld.service
安装cobbler及依赖软件
yum -y install epel-release
yum -y install cobbler cobbler-web httpd dhcp tftp python-ctypes xinetd pykickstart net-tool cman fence-agents
#cobbler-web可不安装,如果安装必须保存与cobbler版本一致
#安装过程中会自动安装许多依赖包,若是内网需要将依赖包也全部下载
修改配置settings
#修改cobbler服务器ip为本机ip
sed -i 's/^server: 127.0.0.1/server: 192.168.222.128/g' /etc/cobbler/settings
#修改next-server(tftp)为本机ip
sed -i 's/^next_server: 127.0.0.1/next_server: 192.168.222.128/g' /etc/cobbler/settings
#配置DHCP控制,修改如下则让cobbler控制DHCP,/etc/cobbler/dhcp.template文件内容会自动更新到dhcp.conf中
#sed -i 's/^manage_dhcp: 0/manage_dhcp: 1/g' /etc/cobbler/settings
#sed -i 's/^manage_tftpd: 0/manage_tftpd: 1/g' /etc/cobbler/settings
#sed -i 's/^manage_rsync: 0/manage_rsync: 1/g' /etc/cobbler/settings
启动TFTP
vim /etc/xinetd.d/tftp
#将如下内容中disable后的值改为no
service tftp
{
socket_type = dgram
protocol = udp
wait = yes
user = root
server = /usr/sbin/in.tftpd
server_args = -s /var/lib/tftpboot
disable = no
per_source = 11
cps = 100 2
flags = IPv4
}
#或直接sed -i '/disable/s/yes/no/g' /etc/xinetd.d/tftp
设置密码
[root@localhost etc]# openssl passwd -1 -salt "$RANDOM"
#-1代码使用MD5基于BSD密钥算法
#出现Password时输入你的密码,该密码即为你安装机器的初始密码
Password:
$1$19437$egldqNTHKax3EGPRIz3kC.
#在配置文件中将上述密文填入
vim /etc/cobbler/settings
# openssl passwd -1
# and put the output between the "" below.
default_password_crypted: "$1$19437$egldqNTHKax3EGPRIz3kC."
配置DHCP
#我这是不让cobbler控制dhcp
#让cobbler控制需要更改:sed -i 's/^manage_dhcp: 0/manage_dhcp: 1/g' /etc/cobbler/settings
# set to 1 to enable Cobbler's DHCP management features.
vim /etc/dhcp/dhcpd.conf
subnet 192.168.222.0 netmask 255.255.255.0 {
range 192.168.222.100 192.168.222.240;
option routers 192.168.222.128;
}
next-server 192.168.222.128;
filename="pxelinux.0";
配置启动文件
#因为cobbler后面取消了get-loaders命令,所以需要手动添加启动文件
yum install -y syslinux
cp /usr/share/syslinux/pxelinux.0 /var/lib/cobbler/loaders/
cp /usr/share/syslinux/menu.c32 /var/lib/cobbler/loaders/
systemctl restart cobblerd
开启服务
systemctl enable --now httpd
systemctl enable --now cobblerd
systemctl enable --now xinetd
systemctl enable --now dhcpd
systemctl enable --now tftp
systemctl enable --now rsyncd
##
cobbler sync
cobbler check
#若出现一下错误,表示正常
1 : SELinux is enabled. Please review the following wiki page for details on ensuring cobbler works correctly in your SELinux environment:
https://github.com/cobbler/cobbler/wiki/Selinux
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 : debmirror package is not installed, it will be required to manage debian deployments and repositories
Restart cobblerd and then run 'cobbler sync' to apply changes.
镜像导入
#若是虚拟机,请提前连接好CD设备
mount -t iso9660 /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
cobbler import --name="centos7-x86_64" --path=/mnt/
#使用cobbler可以查看导入的镜像及profile文件
[root@localhost kickstarts]# cobbler list
distros:
centos7-x86_64
profiles:
centos7-x86_64
systems:
repos:
images:
mgmtclasses:
packages:
files:
######################
cobbler的默认系统ks.cfg文件为/var/lib/cobbler/kickstarts/sample.ks
自己可以手写自定义的文件,需要将默认的移除然后添加即可
cobbler profile remove --name=centos7-x86_64
cobbler profile add --name=centos7 --distro=centos7-x86_64 --kickstart=你的ks.cfg文件路径
在新建的服务器打开后出现如下即为部署成功(虚拟机请选择稍后安装系统):
选择相应的系统(cobbler可同时配置多个系统镜像以及对应的安装引导文件),然后默默等等安装成功即可。
搞定!!!
三、一键化脚本
注:请根据自己服务器实际情况修改
#!/bin/bash
ipaddr=`ifconfig |grep inet |awk '{print $2}'| head -n1`
ipaddr1=`ifconfig |grep inet |awk '{print $2}'| head -n1|grep -oP '\d+.\d+.\d+.'`
echo -e "\033[32m 当前IP为:$ipaddr \033[0m"
sleep 1
echo -e "\033[32m 1、部署国内镜像 \033[0m"
curl -o /etc/yum.repos.d/CentOS-aliyun.repo https://mirrors.aliyun.com/repo/Centos-7.repo
yum clean all
yum makecache
sleep 1
echo -e "\033[32m 2、关闭防火墙、SELINUX \033[0m"
systemctl stop firewalld
systemctl disable firewalld
sed -i 's/^SELINUX=enforcing/SELINUX=disable/g' /etc/selinux/config
setenforce 0
echo -e "\033[32m SELINUX: \033[0m"
echo `getenforce`
echo -e "\033[32m 3、安装cobbler及依赖包 \033[0m"
yum -y install epel-release
sleep 1
yum -y install cobbler cobbler-web httpd dhcp tftp python-ctypes xinetd pykickstart net-tool cman fence-agents
sleep 1
echo -e "\033[32m 4、配置setting \033[0m"
echo -e "\033[32m cobbler初始密码设为123456 \033[0m"
passwd=`openssl passwd -1 -salt "$RANDOM" "123456"`
echo $passwd
sed -i "s/^default_password_crypted.*/default_password_crypted: \""${passwd}"\"/g" /etc/cobbler/settings
sed -i "s/^server: 127.0.0.1/server: $ipaddr/g" /etc/cobbler/settings
sed -i "s/^next_server: 127.0.0.1/next_server: $ipaddr/g" /etc/cobbler/settings
sleep 1
echo -e "\033[32m 4、配置dhcp、xinetd \033[0m"
sed -i '/disable/s/yes/no/g' /etc/xinetd.d/tftp
cat << EOF >/etc/dhcp/dhcpd.conf
subnet ${ipaddr1}0 netmask 255.255.255.0 {
range ${ipaddr1}100 ${ipaddr1}240;
option routers $ipaddr;
}
next-server $ipaddr;
filename="pxelinux.0";
EOF
echo -e "\033[32m 5、配置启动文件 \033[0m"
yum install -y syslinux
cp /usr/share/syslinux/pxelinux.0 /var/lib/cobbler/loaders/
cp /usr/share/syslinux/menu.c32 /var/lib/cobbler/loaders/
sleep 1
echo -e "\033[32m 6、启动服务 \033[0m"
systemctl enable --now httpd
systemctl enable --now cobblerd
systemctl enable --now xinetd
systemctl enable --now dhcpd
systemctl enable --now tftp
systemctl enable --now rsyncd
sleep 1
cobbler sync
echo -e "\033[33m cobbler sync \033[0m"
cobbler check
echo -e "\033[33m cobbler check \033[0m"
sleep 1
echo -e "\033[32m 7、加载镜像(已提前挂好ISO) \033[0m"
mount -t iso9660 /dev/cdrom /mnt/
cobbler import --name="centos7-x86_64" --path=/mnt/
cobbler list
echo -e "\033[32m 全部执行完毕!请检查! \033[0m"