使用pxe (批量快速自动部署大量服务器操作系统)

一、pxe 介绍

PXE技术是一种允许计算机在启动时通过网络加载操作系统的技术。这种技术对于批量快速部署大量服务器操作系统、恢复系统以及在无盘环境中运行系统非常有用。PXE通过使用DHCP和TFTP协议,实现了在没有本地存储设备的情况下,从网络服务器加载启动文件和操作系统镜像。

PXE的工作原理可以概括为以下几个步骤:当计算机启动时,其BIOS或UEFI固件中的PXE客户端会向网络发送广播请求,寻找PXE服务器;DHCP服务器响应这些请求,提供IP地址和其他网络配置信息,包括启动文件的位置;使用TFTP协议,PXE客户端从服务器下载启动文件,这通常是一个小型的引导程序;下载的boot引导程序执行,它可能会加载更完整的操作系统镜像,或者启动一个轻量级的操作系统环境;在引导程序的帮助下,计算机能够加载并运行完整的操作系统。

二、部署过程

1、思路图

 在部署之前,本机服务器的网络必须要配置好,设为手动。然后需要将vmnet8的dhcp服务这样才能够使用本服务器来下发IP,然后下载完http,dhcp服务。

2、使用kickstart 来自动化安装脚本

 #如果服务器没有改软件,可以先下载

dnf install system-config-kickstart

#然后启用图形界面

system-config-kickstart

接下来就在图形界面设置。其配置步骤图如下:

 

注意:这里就是写安装源就是你iso镜像挂载的地方,并且要将它链接到/var/www/html/目录下这样才能够在网上去访问 。

mount /dev/sr0 /mnt

ln -s  /mnt/ /var/www/html

 

 

在里面添加三个分区 。

 

 这里面是可以根据你的需要来写的

 

最后就是保存,我们将其保存到根目录下

 

然后就会在根目录下出现ks.cfg。但是我们前面没有配置packages,需要我们在这个文件里面手动配置。

%packages
@base
httpd
%end

最后将这个ks.cfg文件放到/var/www/html/去在网上共享

cp /root/ks.cfg  /var/www/html/
#在这里面能够有这些文件
[root@localhost ~]# ll /var/www/html/
total 4
-rw-r--r--. 1 root root 1173 Aug  4 13:14 ks.cfg
lrwxrwxrwx. 1 root root    5 Aug  4 11:13 mnt -> /mnt/

3、配置dhcp 服务

我们下载完DHCP服务其就会出现配置文件,但是没有里面东西,我们参考其模板再去修改

\cp -f /usr/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf
[root@localhost ~]# vim /etc/dhcp/dhcpd.conf
# dhcpd.conf
#
# Sample configuration file for ISC dhcpd
#

# option definitions common to all supported networks...
#这是公司域名
option domain-name "example.org"; 
#这是对外分发的dns地址
option domain-name-servers 114.114.114.114;

default-lease-time 600;
max-lease-time 7200;

# Use this to enble / disable dynamic dns updates globally.
#ddns-update-style none;

# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
#authoritative;

# Use this to send dhcp log messages to a different log file (you also
# have to hack syslog.conf to complete the redirection).
log-facility local7;

# No service will be given on this subnet, but declaring it helps the 
# DHCP server to understand the network topology.

#subnet 10.152.187.0 netmask 255.255.255.0 {
#}

# This is a very basic subnet declaration.

subnet 172.25.254.0 netmask 255.255.255.0 {
  range 172.25.254.30 172.25.254.40;
  option routers 172.25.254.2;
  next-server 172.25.254.136;
  filename "pxelinux.0";
}

最后要重新启动dhcp服务。

4、配置tftp服务 

#下载软件包,可以先通过yum search 来查看我们有的安装包来安装
[root@localhost ~]# yum install syslinux.x86_64 -y
[root@localhost ~]# yum install tftp-server.x86_64 -y

#启动并开机自启tftp
systemctl enable --now tftp

#将我们挂载目录下ISO镜像文件中的引导文件和相关内容复制到TFTP服务器的根目录,以便在网络启动时提供给客户端计算机。
[root@localhost ~]# cp /mnt/isolinux/* /var/lib/tftpboot/

#将PXE引导程序pxelinux.0文件从syslinux软件包的目录复制到TFTP服务器的根目录下。
[root@localhost ~]# cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/

#ISO镜像文件中的启动配置文件isolinux.cfg复制到PXE引导目录中的默认配置文件default。
[root@localhost ~]# cd /var/lib/tftpboot/
[root@localhost ~]# mkdir pxelinux.cfg
[root@localhost tftpboot]# cp isolinux.cfg /pxelinux.cfg/default

#然后写在default里面添加修改
[root@localhost tftpboot]vim pxelinux.cfg/default

#主要是修改这些内容
label linux
  menu label ^Install Red Hat Enterprise Linux 7.9 haaaaaaaaaaaaa
  menu default
  kernel vmlinuz
  append initrd=initrd.img repo=http://172.25.254.136/mnt ks=http://172.25.254.136/ks.cfg quiet

label check
  menu label Test this ^media & install Red Hat Enterprise Linux 7.9
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=RHEL-7.9\x20Server.x86_64 rd.live.check quiet

 最后也要重新启动服务。

5、测试

在其他虚拟机上选择打开电源时进入固件,然后就会进入图形界面。如下:注意要把network那一行放到最上面,可以使用+号。

然后退出 保存

最后在会在启动时出现 ,那个haaaaaaaaaa就是我们自己写的。然后就是这个会不停的启动,我们就可以在其启动完一次之后,就关机,并且在第二次可以选择硬件模式,然后就可以在完成后查看IP有没有配置,以及一些服务有没有启动。

主要配置文件代码 

1、dhcp配置文件

[root@localhost ~]# vim /etc/dhcp/dhcpd.conf
# dhcpd.conf
#
# Sample configuration file for ISC dhcpd
#

# option definitions common to all supported networks...
#这是公司域名
option domain-name "example.org"; 
#这是对外分发的dns地址
option domain-name-servers 114.114.114.114;

default-lease-time 600;
max-lease-time 7200;

# Use this to enble / disable dynamic dns updates globally.
#ddns-update-style none;

# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
#authoritative;

# Use this to send dhcp log messages to a different log file (you also
# have to hack syslog.conf to complete the redirection).
log-facility local7;

# No service will be given on this subnet, but declaring it helps the 
# DHCP server to understand the network topology.

#subnet 10.152.187.0 netmask 255.255.255.0 {
#}

# This is a very basic subnet declaration.

subnet 172.25.254.0 netmask 255.255.255.0 {
  range 172.25.254.30 172.25.254.40;
  option routers 172.25.254.2;
  next-server 172.25.254.136;
  filename "pxelinux.0";
}

 

2、ks.cfg 

[root@localhost ~]# cat ks.cfg 
#platform=x86, AMD64, or Intel EM64T
#version=DEVEL
# Install OS instead of upgrade
install
# Keyboard layouts
keyboard 'us'
# Root password
rootpw --iscrypted $1$orRsoDSl$q/reo9vzJuXNUDLlPgP79/
# System language
lang zh_CN
# System authorization information
auth  --useshadow  --passalgo=sha512
# Use text mode install
text
# SELinux configuration
selinux --disabled
# Do not configure the X Window System
skipx


# Firewall configuration
firewall --disabled
# Network information
network  --bootproto=dhcp --device=ens33
# Reboot after installation
reboot
# System timezone
timezone Asia/Shanghai
# Use network installation
url --url="http://172.25.254.136/mnt"
# System bootloader configuration
bootloader --location=mbr
# Partition clearing information
clearpart --all
# Disk partitioning information
part /boot --fstype="xfs" --size=1024
part swap --fstype="swap" --size=512
part / --fstype="xfs" --grow --size=1

%packages
@base
httpd
%end

%post
mkdir -p /rhel7

echo mount /dev/cdrom  /rhel7 >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local


cat> /etc/yum.repos.d/rhel7.repo << EOF
[rhel7]
name=rhel7
baseurl=file:///rhel7
gpgcheck=0
EOF

yum install gcc -y
%end
 

3、 pxelinux.cfg/default

[root@localhost tftpboot]# cat pxelinux.cfg/default
default vesamenu.c32
timeout 600

display boot.msg

# Clear the screen when exiting the menu, instead of leaving the menu displayed.
# For vesamenu, this means the graphical background is still displayed without
# the menu itself for as long as the screen remains in graphics mode.
menu clear
menu background splash.png
menu title Red Hat Enterprise Linux 7.9
menu vshift 8
menu rows 18
menu margin 8
#menu hidden
menu helpmsgrow 15
menu tabmsgrow 13

# Border Area
menu color border * #00000000 #00000000 none

# Selected item
menu color sel 0 #ffffffff #00000000 none

# Title bar
menu color title 0 #ff7ba3d0 #00000000 none

# Press [Tab] message
menu color tabmsg 0 #ff3a6496 #00000000 none

# Unselected menu item
menu color unsel 0 #84b8ffff #00000000 none

# Selected hotkey
menu color hotsel 0 #84b8ffff #00000000 none

# Unselected hotkey
menu color hotkey 0 #ffffffff #00000000 none

# Help text
menu color help 0 #ffffffff #00000000 none

# A scrollbar of some type? Not sure.
menu color scrollbar 0 #ffffffff #ff355594 none

# Timeout msg
menu color timeout 0 #ffffffff #00000000 none
menu color timeout_msg 0 #ffffffff #00000000 none

# Command prompt text
menu color cmdmark 0 #84b8ffff #00000000 none
menu color cmdline 0 #ffffffff #00000000 none

# Do not display the actual menu unless the user presses a key. All that is displayed is a timeout message.

menu tabmsg Press Tab for full configuration options on menu items.

menu separator # insert an empty line
menu separator # insert an empty line

label linux
  menu label ^Install Red Hat Enterprise Linux 7.9 haaaaaaaaaaaaa
  menu default
  kernel vmlinuz
  append initrd=initrd.img repo=http://172.25.254.136/mnt ks=http://172.25.254.136/ks.cfg quiet

label check
  menu label Test this ^media & install Red Hat Enterprise Linux 7.9
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=RHEL-7.9\x20Server.x86_64 rd.live.check quiet

menu separator # insert an empty line

# utilities submenu
menu begin ^Troubleshooting
  menu title Troubleshooting

label vesa
  menu indent count 5
  menu label Install Red Hat Enterprise Linux 7.9 in ^basic graphics mode
  text help
    Try this option out if you're having trouble installing
    Red Hat Enterprise Linux 7.9.
  endtext
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=RHEL-7.9\x20Server.x86_64 xdriver=vesa nomodeset quiet

label rescue
  menu indent count 5
  menu label ^Rescue a Red Hat Enterprise Linux system
  text help
    If the system will not boot, this lets you access files
    and edit config files to try to get it booting again.
  endtext
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=RHEL-7.9\x20Server.x86_64 rescue quiet

label memtest
  menu label Run a ^memory test
  text help
    If your system is having issues, a problem with your
    system's memory may be the cause. Use this utility to
    see if the memory is working correctly.
  endtext
  kernel memtest

menu separator # insert an empty line

label local
  menu label Boot from ^local drive
  localboot 0xffff

menu separator # insert an empty line
menu separator # insert an empty line

label returntomain
  menu label Return to ^main menu
  menu exit

menu end

  • 12
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

左水水%

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值