00:文章简介
简单的介绍了cobbler自动装机的安装及使用,可以满足中小企业自动安装系统的需求。
01:基础配置
1.1:创建虚拟机
创建1c2g100g的虚拟机,存储可以使用精简置备
1.2:安装操作系统
安装CentOS 7 系统
安装过程略
1.3:初始化配置
#关闭防火墙和selinux
[root@cobbler ~]# systemctl stop firewalld
[root@cobbler ~]# systemctl disable firewalld
[root@cobbler ~]# sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config
#设置主机名
[root@cobbler ~]# hostnamectl set-hostname cobbler
#配置网卡
[root@cobbler ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=10.10.100.252
NETMASK=255.255.255.0
GATEWAY=10.10.100.254
DNS1=223.5.5.5
#重启以让规则生效
[root@cobbler ~]# reboot
02:cobbler服务配置
2.1:初始化yum仓库
这里可以使用公网的yum仓库,例如阿里云、清华大学源等
[root@cobbler ~]# gzip /etc/yum.repos.d/C*
[root@cobbler ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.linux98.com/centos/7/CentOS-Base.repo
[root@cobbler ~]# yum clean all
[root@cobbler ~]# yum makecache
2.2:安装基础包
[root@cobbler ~]# yum -y install wget vim tree
2.3:配置pip仓库
[root@cobbler ~]# mkdir ~/.pip
[root@cobbler ~]# cat > ~/.pip/pip.conf << EOF
> [global]
> trusted-host=mirrors.aliyun.com
> index-url=https://mirrors.aliyun.com/pypi/simple/
> EOF
2.4:安装cobbler相关服务
[root@cobbler ~]# yum install -y cobbler cobbler-web dhcp tftp-server pykickstart httpd xinetd
[root@cobbler ~]# systemctl enable httpd xinetd rsyncd tftp cobblerd dhcpd
[root@cobbler ~]# systemctl start httpd xinetd rsyncd tftp cobblerd
2.5:检查cobbler配置
[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.
#未设置cobbler的server地址(cobbler-server地址)
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.
#未设置cobbler的next_server地址(ftp地址)
3 : change 'disable' to 'no' in /etc/xinetd.d/tftp
#未启动ftp
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.
#未下载cobbler get-loaders
5 : debmirror package is not installed, it will be required to manage debian deployments and repositories
#没有安装 debmirror
6 : 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
#需要设定root的初始密码
7 : 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.
#重启cobblerd服务 并且运行 cobbler sync 来应用改变
2.6:解决上面的问题
#1、未设置cobbler的server地址(cobbler-server地址)
[root@cobbler ~]# sed -i "s%^server: 127.0.0.1%server: 172.16.100.252%g" /etc/cobbler/settings
#2、未设置cobbler的next_server地址(ftp地址)
[root@cobbler ~]# sed -i "s%^next_server: 127.0.0.1%next_server: 172.16.100.252%g" /etc/cobbler/settings
#3、设置cobbler接管dhcp及防止重复安装系统
[root@cobbler ~]# vi /etc/cobbler/settings
manage_dhcp: 1 #使用cobbler管理dhcp
pxe_just_once: 1 #防止重复安装系统
#4、启动tftp
[root@cobbler ~]# sed -i '/disable\>/s/\<yes\>/no/' /etc/xinetd.d/tftp
systemctl restart xinetd
#5、下载cobbler get-loaders
[root@cobbler ~]# cobbler get-loaders
#如果运行报错,可以使用
[root@cobbler ~]# cp -a /usr/share/syslinux/{pxelinux.0,menu.c32} /var/lib/tftpboot/
#6、安装debmirror
#这是deepin系列依赖的仓库,暂时不需要
#7、设置安装后系统的root默认密码
[root@cobbler ~]# export root_pwd=$(openssl passwd -1 -salt `openssl rand 15 -base64` '123456')
[root@cobbler ~]# sed -i "s%^default_password_crypted.*%default_password_crypted: \"${root_pwd}\"%g" /etc/cobbler/settings
#8、重启服务后再次cobbler check
[root@cobbler ~]# systemctl restart cobblerd
[root@cobbler ~]# cobbler check
2.7:配置dhcp模板
[root@cobbler ~]# vim /etc/cobbler/dhcp.template
...
subnet 10.10.100.0 netmask 255.255.255.0 {
option routers 10.10.100.254;
option domain-name-servers 223.5.5.5;
option subnet-mask 255.255.255.0;
range dynamic-bootp 10.10.100.200 10.10.100.250;
default-lease-time 21600;
max-lease-time 43200;
next-server $next_server;
...
[root@cobbler ~]# cobbler sync
2.8:定制化菜单标题
[root@cobbler ~]# vim /etc/cobbler/pxe/pxedefault.template
MENU TITLE Cobbler | http://cobbler.linux98.com
03:配置镜像和ks文件
3.1:导入镜像
挂载centos7镜像并进行导入操作
[root@cobbler ~]# mount /dev/sr0 /mnt
[root@cobbler ~]# cobbler import --path=/mnt --name=CentOS7.6-x86_64 --arch=x86_64
修改centos7安装时的参数,使网卡为eth0
[root@cobbler ~]# cobbler distro edit --name=CentOS7.6-x86_64 --kopts=net.ifnames=0
3.2:修改ks文件
(先删除自动创建的,再添加)
[root@cobbler ~]# cobbler profile remove --name CentOS7.6-x86_64-mini
[root@cobbler ~]# cobbler profile add --name CentOS7.6-x86_64-mini --distro=CentOS7.6-x86_64 --kickstart=/var/lib/cobbler/kickstarts/CentOS7.6-x86_64-mini.ks
3.3:ks文件
install
text
keyboard 'us'
url --url=$tree
firstboot --enable
ignoredisk --only-use=sda
rootpw --plaintext 123456
lang en_US
auth --useshadow --passalgo=sha512
selinux --disabled
skipx
firewall --disabled
network --bootproto=dhcp --device=eth0 --onboot=on
reboot
timezone Asia/Shanghai
bootloader --append="net.ifnames=0" --location=mbr --boot-drive=sda
clearpart --all --initlabel
zerombr
autopart --type=lvm
%packages
@^minimal
@core
wget
%end
%addon com_redhat_kdump --disable --reserve-mb='auto'
%end
%post
cd /home
wget http://10.10.100.252/cobbler/pub/message.txt
wget http://10.10.100.252/cobbler/pub/auto.sh
#ks文件内放置一个初始化脚本,避免每次修改初始化脚本都要重新修改ks文件
chmod 777 auto.sh
sh auto.sh
mkdir /root/.ssh
cat >/root/.ssh/authorized_keys << EOF
ssh-rsa
....此处为ansible的免密登录使用的公钥,忽略。
EOF
chmod -R 0600 /root/.ssh
%end
3.4:auto.sh初始化脚本
[root@cobbler ~]# cat /var/www/cobbler/pub/auto.sh
#!/bin/bash
mac=`ip a |grep "ether" | awk '{print $2}'|awk 'NR==1'`
NAME=`grep $mac message.txt | cut -d',' -f2`
IP=`grep $mac message.txt | cut -d',' -f3`
cd /etc/sysconfig/network-scripts/
mv ifcfg-eth0{,.bak}
echo -ne "TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=$IP
PREFIX=24
GATEWAY=10.10.100.254
DNS1=223.5.5.5
" >> /etc/sysconfig/network-scripts/ifcfg-eth0
sed -i 's/localhost.localdomain/'$NAME'/g' /etc/hostname
sed -i 's/#UseDNS yes/UseDNS no/g' /etc/ssh/sshd_config
#alias
echo "alias vieth0='vi /etc/sysconfig/network-scripts/ifcfg-eth0'" >> /root/.bashrc
#关联的message.txt文件
[root@cobbler ~]# cat /var/www/cobbler/pub/message.txt
#mac,name,ip
00:50:56:8d:63:f1,Ib01,10.10.100.5
00:50:56:8d:a5:3e,Ib02,10.10.100.6
00:50:56:a9:75:4b,proxy01,10.10.100.11
00:50:56:a9:a1:8e,proxy02,10.10.100.12
00:50:56:a9:c5:9c,proxy03,10.10.100.13
00:50:56:a9:f5:d5,web01,10.10.100.21
00:50:56:a9:8c:11,web02,10.10.100.22
00:50:56:a9:55:61,web03,10.10.100.23
00:50:56:a9:ff:d3,nfs,10.10.100.31
00:50:56:a9:56:a6,redis01,10.10.100.41
00:50:56:a9:b2:ea,mysql01,10.10.100.51
00:50:56:a9:9b:c4,dns-master,10.10.100.91
00:50:56:a9:45:ac,dns-slave,10.10.100.92
00:50:56:a9:e5:f0,dns-son,10.10.100.93
04:测试安装
4.1:创建一个1c2g20g的虚拟机
内存必须要2g,1gpxe引导时会报错
4.2:虚拟机开机
选择下面的CentOS7.6开始安装
05:优化装机配置
使用cobbler的system模块,让虚拟机安装指定的操作系统,这样后面部署集群时只需要开机就可以了。
麻烦的地方在于创建虚拟机时需要记录虚拟机的mac地址。
5.1:批量生成cobbler system指令
[root@cobbler ~]# cat cobbler_add_system.sh
#!/bin/bash
OS="CentOS7.6-x86_64-mini"
echo "add system"
for i in `cat /var/www/cobbler/pub/message.txt | grep -v "#"`
do
NAME=`echo $i |cut -d',' -f2`
MAC=`echo $i |cut -d',' -f1`
echo "cobbler system add --name=$NAME --mac=$MAC --profile=$OS --interface=eth0"
done
echo "remove system"
for i in `cat /var/www/cobbler/pub/message.txt | grep -v "#"`
do
NAME=`echo $i |cut -d',' -f2`
MAC=`echo $i |cut -d',' -f1`
echo "cobbler system remove --name=$NAME"
done
生成效果
[root@cobbler ~]# sh cobbler_add_system.sh
add system
cobbler system add --name=client --mac=00:0c:29:26:e1:dc --profile=CentOS7.6-x86_64-mini --interface=eth0
remove system
cobbler system remove --name=client
5.2:测试虚拟机开机自动装机
文章更新于:
评论区