自动化运维实战之——构建PXE无人值守自动安装服务器
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自动化运维实战之——构建PXE无人值守自动安装服务器相关的知识,希望对你有一定的参考价值。
【企业案例】
某公司新增一个机房,需要大规模部署操作系统环境,由于手动部署操作系统效率极为低下,现需搭建一台PXE服务器来自动化部署操作系统。
具体要求如下:
为节省硬件资源,PXE、DHCP、TFTP、FTP全部建立在一台服务器上;
实现多个操作系统版本的自动部署;
构建自动应答文件ks.cfg,并对系统做初始化设置
l 实现ssh免密码登录配置
l 关闭防火墙和SElinux
l 配置yum仓库
l ???(可根据实际环境做添加)
【实验环境】
操作系统及内核版本
[[email protected] ~]# cat /etc/redhat-release CentOS Linux release 7.3.1611 (Core) [[email protected] ~]# uname -r 3.10.0-514.el7.x86_64
网络地址
服务器 | 网络地址 |
DHCP | 192.168.10.254/24 |
PXE | 192.168.10.254/24 |
TFTP | 192.168.10.254/24 |
FTP | 192.168.10.254/24 |
配置DHCP服务器
安装DHCP服务器
[[email protected] ~]# yum -y install dhcp
配置DHCP服务器
[[email protected] ~]# cat >> /etc/dhcp/dhcpd.conf <<EOF subnet 192.168.10.0 netmask 255.255.255.0 { range 192.168.10.1 192.168.10.200; next-server 192.168.10.254; filename "pxelinux.0"; } EOF [[email protected] ~]# systemctl start dhcpd
配置TFTP服务器
安装TFTP服务和syslinux软件包
[[email protected] ~]# yum -y install tftp-server syslinux
配置TFTP服务
[[email protected] ~]# cp -v /usr/share/syslinux/{menu.c32,pxelinux.0} /var/lib/tftpboot/ ‘/usr/share/syslinux/menu.c32’ -> ‘/var/lib/tftpboot/menu.c32’ ‘/usr/share/syslinux/pxelinux.0’ -> ‘/var/lib/tftpboot/pxelinux.0’ [[email protected] ~]# mkdir -v /var/lib/tftpboot/{pxelinux.cfg,centos7.3,centos6.9} mkdir: created directory ‘/var/lib/tftpboot/pxelinux.cfg’ mkdir: created directory ‘/var/lib/tftpboot/centos7.3’ mkdir: created directory ‘/var/lib/tftpboot/centos6.9’
[[email protected] ~]# cat > /var/lib/tftpboot/pxelinux.cfg/default <<EOF default menu.c32 timeout 300 menu title ########## PXE Boot Menu ########## label centos6.9 menu label ^Kickstart Inatall CentOS 6.9 menu default kernel centos6.9/vmlinuz append ks=ftp://192.168.10.254/pub/ks/ks6.cfg initrd=centos6.9/initrd.img label centos7.3 menu label ^Kickstart Inatall CentOS 7.3 menu default kernel centos7.3/vmlinuz append ks=ftp://192.168.10.254/pub/ks/ks7.cfg initrd=centos7.3/initrd.img EOF
配置FTP服务器
安装ftp服务器
[[email protected] ~]# yum -y install vsftpd
配置ftp服务器
[[email protected] ~]# mkdir -pv /var/ftp/pub/centos/{6,7} #光驱插入centos7镜像 [[email protected] ~]# cp -Rv /misc/cd/* /var/ftp/pub/centos/7/ #光驱插入centos6镜像 [[email protected] ~]# cp -Rv /misc/cd/* /var/ftp/pub/centos/6/ [[email protected] ~]# mkdir -v /var/ftp/pub//ks mkdir: created directory ‘/var/ftp/pub/ks’
复制内核和虚拟磁盘文件到tftp目录
[[email protected] ~]# cp -v /var/ftp/pub/centos/6/isolinux/{vmlinuz,initrd.img} /var/lib/tftpboot/centos6.9/ [[email protected] ~]# cp -v /var/ftp/pub/centos/7/isolinux/{vmlinuz,initrd.img} /var/lib/tftpboot/centos7.3/
设置自动应答文件
CentOS7.3 自动应答文件 #version=DEVEL # System authorization information auth --enableshadow --passalgo=sha512 # Use CDROM installation media url --url=ftp://192.168.10.254/pub/centos/7 # 指明安装源的路径 # Use graphical install text # 将cdrom修改为text,基于字符界面安装 selinux --disabled # Run the Setup Agent on first boot firstboot --enable ignoredisk --only-use=sda # Keyboard layouts keyboard --vckeymap=us --xlayouts=‘us‘ # System language lang en_US.UTF-8 # Network information network --bootproto=dhcp --device=ens33 --onboot=on --ipv6=auto --activate # --bootproto必须是dhcp获取,--onboot=on network --hostname=centos7.magedu.com # Root password = zhyang rootpw --iscrypted $6$wdMKFHGt773BoqCx$0TS.jHNyWO/T37yl9SdkFql0zC2TjeJQXil0/rI6CH6YbOc2tf3CtSEROtJFeHpGT0DXzEbrrqKhCAglVblwo1 # System services services --disabled="chronyd" # System timezone timezone Asia/Shanghai --isUtc --nontp user --name=wang --password=$6$v.VphW/puRblcrFB$uaSrdEhGAwMXap27WIKTn5lyOOfoFyB/SNxyyL3og6s9/VQoAKoL2KQjKmeYFmoYTuYkSNL7BBxgbJzeryKr9. --iscrypted --gecos="wang" # X Window System configuration information xconfig --startxonboot # System bootloader configuration bootloader --append=" crashkernel=auto" --location=mbr --boot-drive=sda # Partition clearing information zerombr # 添加zerombr ,表示清除旧磁盘上原有的mbr,新磁盘安装可不写 clearpart --all # 清除所有分区信息,新磁盘安装可不写 # Disk partitioning information part swap --fstype="swap" --ondisk=sda --size=2048 # 分区表信息,如果你想添加分区,可按照该格式添加 part /app --fstype="xfs" --ondisk=sda --size=51200 part / --fstype="xfs" --ondisk=sda --size=51200 part /boot --fstype="xfs" --ondisk=sda --size=1024 reboot # 安装完成之后重启 %packages # 安装软件包 @^minimal @core @base @development tools autofs # 安装autofs服务包 vim %end %addon com_redhat_kdump --enable --reserve-mb=‘auto‘ %end %anaconda pwpolicy root --minlen=6 --minquality=50 --notstrict --nochanges --notempty pwpolicy user --minlen=6 --minquality=50 --notstrict --nochanges --notempty pwpolicy luks --minlen=6 --minquality=50 --notstrict --nochanges --notempty %end %post # 安装后脚本,我配置了yum源和ssh密钥登录设置(复制用于管理的服务器公钥) systemctl enable autofs systemctl disable firewalld.service sed -i ‘s/linux16 \/vmlinuz-3.*/& net.ifnames=0/‘ /boot/grub2/grub.cfg mv /etc/sysconfig/network-scripts/{ifcfg-ens33,ifcfg-eth0} sed -i ‘s/DEVICE.*/DEVICE=eth0/‘ /etc/sysconfig/network-scripts/ifcfg-eth0 rm -rf /etc/yum.repos.d/* cat > /etc/yum.repos.d/base.repo <<eof [base] name=base baseurl=ftp://192.168.10.254/pub/centos/7/ gpgcheck=0 eof mkdir /root/.ssh cat >> /root/.ssh/authorized_keys << EOF ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCjog2/Glq8VVAe5fchc4UO7OhO609+f/NEO9TcYtzzxZx450bK/nHpfIg6l9ZLF8CVEfW5S+fV2uW2pxHY0XwDPGPVIH54tacw8ILeI6vZn00WGpEkDVgcW764svH8qDlgKcTBTaPZFJJDohUHh7XRWgnub8lvhk6L0+GRXzEUhW5hEyh7ht8LsxSTVXW3s2pqOEFNFzL4bC0JXzgJa720/J6izrxYhIGFm6fBpu9ljC+DmoJZQxbxcb8EfPKOo4BqZxdfzFI2Zwai1IxbutGcbe4gRZXSFuYzkDclwLcRV2CSbm60d9l/qcvsrq2vMEtQ04KqvPFfNO91vcze7f67 [email protected] EOF %end CentOS6.9自动应答文件 #version=DEVEL install text # 基于字符界面安装 lang en_US.UTF-8 keyboard us network --onboot yes --device eth0 --bootproto dhcp --noipv6 # Root password = zhyang rootpw --iscrypted $6$wdMKFHGt773BoqCx$0TS.jHNyWO/T37yl9SdkFql0zC2TjeJQXil0/rI6CH6YbOc2tf3CtSEROtJFeHpGT0DXzEbrrqKhCAglVblwo1 firewall --service=ssh authconfig --enableshadow --passalgo=sha512 url --url=ftp://192.168.10.254/pub/centos/6 # yum源的路径 selinux --disabled # 关闭selinux timezone Asia/Shanghai bootloader --location=mbr --driveorder=sda --append="crashkernel=auto rhgb quiet" # The following is the partition information you requested # Note that any partitions you deleted are not expressed # here so unless you clear all partitions first, this is # not guaranteed to work zerombr # 清除mbr clearpart --all # 清除分区信息 reboot # 安装完成后重启 part /boot --fstype=ext4 --size=1000 # 分区信息 part / --fstype=ext4 --size=50000 part /app --fstype=ext4 --size=40000 part swap --size=2048 #repo --name="CentOS" --baseurl=cdrom:sr0 --cost=100 # 注释掉此行 %packages @base @core @development tools vim autofs %end %post # 安装后脚本,我配置了yum源和ssh密钥登录设置(复制用于管理的服务器公钥) chkconfig autofs on chkconfig iptables off rm -rf /etc/yum.repos.d/* cat > /etc/yum.repos.d/base.repo <<eof [base] name=base baseurl=ftp://192.168.10.254/pub/centos/6/ gpgcheck=0 eof mkdir /root/.ssh cat >> /root/.ssh/authorized_keys << EOF ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCjog2/Glq8VVAe5fchc4UO7OhO609+f/NEO9TcYtzzxZx450bK/nHpfIg6l9ZLF8CVEfW5S+fV2uW2pxHY0XwDPGPVIH54tacw8ILeI6vZn00WGpEkDVgcW764svH8qDlgKcTBTaPZFJJDohUHh7XRWgnub8lvhk6L0+GRXzEUhW5hEyh7ht8LsxSTVXW3s2pqOEFNFzL4bC0JXzgJa720/J6izrxYhIGFm6fBpu9ljC+DmoJZQxbxcb8EfPKOo4BqZxdfzFI2Zwai1IxbutGcbe4gRZXSFuYzkDclwLcRV2CSbm60d9l/qcvsrq2vMEtQ04KqvPFfNO91vcze7f67 [email protected] EOF %end
复制应答文件到ftp共享目录
[[email protected] ~]# cp -v ks7.cfg /var/ftp/pub/centos/ks/ ‘ks7.cfg’ -> ‘/var/ftp/pub/centos/ks/ks7.cfg’ [[email protected] ~]# cp -v ks6.cfg /var/ftp/pub/centos/ks/ ‘ks6.cfg’ -> ‘/var/ftp/pub/centos/ks/ks6.cfg’
设置相关服务启动
[[email protected] ~]# systemctl enable dhcpd.service [[email protected] ~]# systemctl start dhcpd.service [[email protected] ~]# systemctl enable tftp.socket [[email protected] ~]# systemctl start tftp.socket [[email protected] ~]# systemctl enable vsftpd.service [[email protected] ~]# systemctl start vsftpd.service 关闭防火墙和SElinux [[email protected] ~]# systemctl disable firewalld.service [[email protected] ~]# systemctl stop firewalld.service [[email protected] ~]# sed -i ‘s/SELINUX=enforcing/SELINUX=disabled/‘ /etc/selinux/config [[email protected] ~]# setenforce 0
本文出自 “Keep simple keep stupid” 博客,请务必保留此出处http://yangzhiheng.blog.51cto.com/11586378/1974519
以上是关于自动化运维实战之——构建PXE无人值守自动安装服务器的主要内容,如果未能解决你的问题,请参考以下文章
自动化运维之CentOS7下PXE+Kickstart+DHCP+TFTP+HTTP无人值守安装系统