VMware实现PXE+kickstart无人值守安装Centos7系统

简介: VMware实现PXE+kickstart无人值守安装Centos7系统

PXE实现无人值批量部署服务器



一、PXE概述


1.1 什么是PXE


  • PEX (Pre-Boot Execution E nvironment 预启动执行环境),是一种引导方式,并不是一种安装方式。


  • 基于 Client/Server的工作模式


  • PXE在网卡的ROM 中,当计算机引导时,BIOS把PXE Client调入内存执行,PXE Client 将放置在远端的文件通过网络下载到本地运行。


1.2 什么是KickStart


  • KickStart 是一种无人值守的安装方式,KickStart 的工作原理是通过 记录典型的安装过程中记录所需要填写的各种参数(语言、时区、密码、分区、键盘等),并生成一个ks.cfg的文件。(名字可以修改,默认ks.cfg)


  • 在其后的安装过程中,当出现要求填写参数的情况时,安装程序会首先去查找KickStart 生成的文件,当找到合适的参数时,就采用找到的参数,当没有找到合适的参数,就会卡着,需要人工干预。


  • 如果KickStart 文件涵盖安装过程中所有需要填写的参数时,只需要告诉安装程序从何处取得 ks.cfg文件。安装完毕后,安装程序会根据ks.cfg中设置的重启选项重启系统,并结束安装。


1. 3 安装的必要条件


  • BIOS 支持PXE,需要在BIOS开启


  • NIC 网卡支持


二、PXE工作原理


2.1 工作原理


  • 拓扑图


image.png


  • PXE工作原理


1.首先 PXE Client 向 DHCP 服务器发起请求分配IP(网卡需要向DHCP请求地址,获取信息)
2. DHCP 除了给你分配地址外,还会给你分配 boot-loader name(引导程序的名字) 以及Tftp 服务器IP地址
3.网卡使用Tftp 的客户端,向tftp服务器发起请求,把引导程序(pxelinux.0)加载到内存中来
4.然后 BIOS会执行这个引导程序。
5.引导程序会 boot-loader会从tftp去查找它的配置文件(default)
6.根据配置文件来引导


2.2 本次实验环境


  • 实验环境


image.png


2.3 执行PXE+KiskStart安装需要准备内容


  • DHCP 服务器 用来给客户机分配IP


  • TFTP 服务器 用来存放PXE 的相关文件:系统引导文件


  • FTP|NFS|HTTP服务器 用来存放系统安装文件


  • KickStart所生成的ks.cfg配置文件


  • 带有一个 PXE支持网卡的 将安装的 主机


三、安装步骤


3.1 配置YUM源


  • YUM 源配置


[root@Server~]# cd /etc/yum.repos.d/
[root@Server/etc/yum.repos.d]# ls
rivers.repo
[root@Server/etc/yum.repos.d]# mv rivers.repo rivers.repo.bak
[root@Server/etc/yum.repos.d]# vim dvd.repo
[development]
name=Centos7.6
baseurl=file:///mnt
enabled=1
gpgcheck=0
[root@Server~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 is write-protected, mounting read-only
[root@Server~]# 
[root@Server~]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: development
Other repos take up 137 M of disk space (use --verbose for details)
[root@Server~]# 


3.2 关闭防火墙、selinux


  • 关闭防火墙、selinux


[root@Server~]# systemctl disable firewalld --now
[root@Server~]#setenforce 0
# selinux 开机才生效,setenforce 0 临时关闭


3.3 安装DHCP 、tftp(tftp-server、xinetd)


3.3.1 安装dhcp、tftp-server、xinetd


  • 安装dhcp、tftp-server


[root@Server~]# yum -y install dhcp tftp-server xinetd
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package dhcp.x86_64 12:4.2.5-68.el7.centos.1 will be installed
……


3.3.2 配置DHCP服务


  • 配置DHCP文件


# 1.进入 dhcp目录
[root@Server~]# cd /etc/dhcp/
[root@Server/etc/dhcp]# ls
dhclient.d             dhcpd6.conf  scripts
dhclient-exit-hooks.d  dhcpd.conf
# 2.查看默认配置文件,是空的,但是/usr/share/doc/dhcp*/目录下有配置模板,我们可以拷贝
[root@Server/etc/dhcp]# cat dhcpd.conf 
#
# DHCP Server Configuration file.
#   see /usr/share/doc/dhcp*/dhcpd.conf.example
#   see dhcpd.conf(5) man page
#
[root@Server/etc/dhcp]# 
# 3.拷贝dhcpd 配置模板文件
[root@Server/etc/dhcp]# cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf
cp: overwrite ‘/etc/dhcp/dhcpd.conf’? y
# 4. 修改dhcp 文件,(这里可以不用拷贝配置文件,直接复制下面一段。subnet--filename)
# A slightly different configuration for an internal subnet.
subnet 10.0.0.0 netmask 255.255.255.0 {            
  range 10.0.0.120 10.0.0.200;
  option domain-name-servers 10.0.0.5, 10.0.0.6;
  option domain-name "example.com";
  option routers 10.0.0.254;
  option broadcast-address 10.0.0.255;
  default-lease-time 600;
  max-lease-time 7200;
  next-server 10.0.0.100;
  filename "pxelinux.0";
}
   subnet 10.0.0.0 netmask 255.255.255.0 #宣告网段
     range 10.0.0.120 10.0.0.200;  #分配地址范围
     option domain-name-servers:  #dns配置,正常公司会有2个DNS我这里随意配的
     option routers 10.0.0.254; # 设置网关的
     option broadcast-address 10.0.0.255; # 设置广播地址
     default-lease-time 600;  # 默认租约时间,它的单位为秒
     max-lease-time 7200;    #最大租约时间,它的单位为秒
     next-server 10.0.0.100;  # tftp-server IP地址
     filename "/pxelinux.0"; # 网络启动程序,(网络引导)
# 5. 启动dhcp 服务器,
[root@Server/etc/dhcp]# systemctl enable dhcpd
Created symlink from /etc/systemd/system/multi-user.target.wants/dhcpd.service to /usr/lib/systemd/system/dhcpd.service.
[root@Server/etc/dhcp]# systemctl start dhcpd
[root@Server/etc/dhcp]#
[root@Server/etc/dhcp]# netstat -lantup|grep :67
udp        0      0 0.0.0.0:67              0.0.0.0:*                           8503/dhcpd          
[root@Server/etc/dhcp]# 
[root@Server/etc/dhcp]# cd
[root@Server~]#
@补充:如果全局配置了,子配置没配置,那么将读取全局设置
    如果全局配置了,子的也配置了,那么将以自配置为准。


3.3.3 配置tftp服务


  • 开启tftp服务


# 1.修改tftp配置文件
[root@Server~]# vim /etc/xinetd.d/tftp 
将  disable = no 改为 yes
#重启 xinetd
[root@Server~]# systemctl restart xinetd.service
[root@Server~]# netstat -lntup|grep :69
udp        0      0 0.0.0.0:69              0.0.0.0:*                           9071/xinetd         
[root@Server~]# 


3.3.4 安装syslinux,拷贝pxelinux.0文件


  • 配置tftp-server在哪里


# 1.查找 pxelinux.0文件是那个包提供的
[root@Server~]# yum provides "*/pxelinux.0"
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
syslinux-4.05-15.el7.x86_64 : Simple kernel loader
     ...: which boots from a FAT filesystem
Repo        : development
Matched from:
Filename    : /usr/share/syslinux/pxelinux.0
syslinux-tftpboot-4.05-15.el7.noarch : SYSLINUX
     ...: modules in /var/lib/tftpboot, available for
     ...: network booting
Repo        : development
Matched from:
Filename    : /var/lib/tftpboot/pxelinux.0
# 2.安装syslinux包,然候拷贝pxelinux.0文件到 tftp-server目录
[root@Server~]# yum -y install syslinux
[root@Server~]# rpm -ql syslinux|grep pxe
/usr/share/doc/syslinux-4.05/pxelinux.txt
/usr/share/syslinux/gpxecmd.c32
/usr/share/syslinux/gpxelinux.0
/usr/share/syslinux/gpxelinuxk.0
/usr/share/syslinux/pxechain.com
/usr/share/syslinux/pxelinux.0
[root@Server~]# 
# 3. 拷贝pxelinux.0 文件到 tftp-server 目录
[root@Server~]# cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
[root@Server/var/lib/tftpboot]# ls
pxelinux.0
# 4.创建一个目录,用来放启动配置文件 default的
[root@Server/var/lib/tftpboot]# mkdir pxelinux.cfg
[root@Server/var/lib/tftpboot]# ls
pxelinux.cfg  pxelinux.0
[root@Server/var/lib/tftpboot]# cd pxelinux.cfg
[root@Server/var/lib/tftpboot/pxe.cfg]# pwd
/var/lib/tftpboot/pxelinux.cfg
# 5.将 /mnt/isolinux/目录下面的所有文件都考到 /var/lib/tftpboot下面
[root@Server/var/lib/tftpboot]# cd --
[root@Server~]# cd /mnt/isolinux/
[root@Server/mnt/isolinux]# cp -a isolinux.cfg  /var/lib/tftpboot/pxelinux.cfg/default
[root@Server/mnt/isolinux]#cp * /var/lib/tftpboot/


  • 验证


image.png


1.客户端启动系统,选择从网卡启动
2.就会从DHCP服务器(10.0.0.81)中获取IP地址,同时还获取了 tftp-server IP(10.0.0.81)地址和网络引导程序(pxelinux.0)
3.通过网卡读取到tftp-server(/var/lib/tftpboot目录)上的pxelinux.0,读取到内存中
4.在内存中执行引导程序
5.读取引导程序的配置文件(/var/lib/tftpboot/pxe.cfg/default)


3.4 编写kickstart.cfg配置文件


3.4.1安装system-config-kickstart


  • 安装 system-config-kickstart


[root@Server/etc/yum.repos.d]#  cd --
# 1. 安装system-config-kickstart
[root@Server~]# yum -y install system-config-kickstart
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
……
#2. 启动界面,配置ks.cfg
[root@Server~]# system-config-kickstart 
详解界面如下:


3.4.2 system-config-kickstart 界面配置


  • 基本配置(默认语言、键盘、时区、密码、安装后重启)


image.png


  • 安装方法(全新安装、HTTP安装方式)


image.png


  • 安装新引导装载程序


image.png


  • 分区信息


image.png


image.png


  • 网络配置


image.png


image.png


image.png


  • 防火墙配置


image.png


  • 显示配置(是否安装图形界面)


image.png


  • 软件包安装选择


image.png


image.png


  • 安装后脚本


image.png


image.png


  • 保存


image.png


3.5 配置 HTTP镜像源


3.5.1 安装http


  • 安装httpd


# 1. 安装httpd
[root@Server ~]# yum -y install httpd
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
……
# 2.设置开启自动、启动服务
[root@Server ~]# systemctl enable httpd
[root@Server ~]# systemctl start httpd
#3. 创建 目录
[root@Server ~]# cd /var/www/html
[root@Server/var/www/html]# mkdir ks.cfg 
[root@Server/var/www/html]# mkdir pub
# 4.修改yum 源,将本地改为http
[root@Server/var/www/html]# cd
[root@Server~]# cat /etc/yum.repos.d/dvd.repo
[development]
name=rhce7
baseurl=http://10.0.0.100/pub
enabled=1
gpgcheck=0
[root@Server~]# 


3.5.2 将iso 镜像文件挂在设置开机自启动


  • 设置iso开机自启动


# 1. 在末尾添加以下一条信息。
[root@Server~]# vim /etc/fstab 
/dev/cdrom       /var/www/html/pub         iso9660                 defaults,loop 0 0
[root@Server~]# tail -1 /etc/fstab 
/dev/cdrom       /var/www/html/pub         iso9660                 defaults,loop 0 0
[root@Server~]# 
# 2.挂载镜像
[root@Server~]# mount -a
# 3.用火狐浏览器访问下,如果可以访问,则说明http 镜像源没有问题
[root@Server~]# firefox http://10.0.0.100/pub &
@ 7版本上,模式可以识别loop,defaults,loop 后面的loop可以省略


3.6 配置开机菜单 default


3.6.1 将ks6.cfg移动到 /var/www/html/ks


  • 移动ks6.cfg


# 1.将我们保存在root目录中的cfg移动到 /var/www/html/ks.cfg/
[root@Server~]# mv ks6.cfg /var/www/html/ks.cfg/
[root@Server/var/www/html/ks.cfg]# ls
ks6.cfg 
[root@Server/var/www/html/ks.cfg]# 


3.6.2 配置开机菜单


  • 编写defautl文件


[root@Server~]# cd /var/lib/tftpboot/pxelinux.cfg/
[root@Server/var/lib/tftpboot/pxelinux.cfg]# ls
default
# 1.编写default文件,此时在原本的label linux 添加以下内容,
# 并删除label check里面的 menu defalut(默认启动方式,设置了,就不需要选择,默认启动选项)
[root@Server/var/lib/tftpboot/pxelinux.cfg]# vim default 
label rhce7
  menu label ^Install rhce7
  menu default
  kernel vmlinuz
  append initrd=initrd.img ks=http://10.0.0.100/ks.cfg/ks6.cfg
# 2.可以修改下默认的时间,默认是 600(单位是600秒的十分之一,就是60s)
# 这里我设置60,就是6s
timeout 60
-------------------
参数介绍
efault vesamenu.c32    # 这是必须项,或者使用menu.c32
timeout 60             # 超时等待时间,60秒内不操作将自动选择默认的菜单来加载
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 CentOS 7             # 大标题
menu vshift 8
……
label linux
  menu label ^Install CentOS 7   # 菜单文字
  kernel vmlinuz        # 内核文件路径,注意相对路径是从tftp的根路径/tftpboot开始的
  append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 quiet  
  # 内核启动选项,其中包括initrd的路径,同样要改为"ks=http://10.0.0.100/ks.cfg/ks6.cfg"
  menu default          # menu default表示开机时光标一开始默认停留在此label上
# 一般pxe环境下此路径直接指向系统安装文件的路径,具体做法见下文示例
# utilities submenu          # 子菜单项的设置方法
  menu begin ^Troubleshooting
  menu title Troubleshooting


3.6.3 将客户机 设置网络启动(raid等),开机启动即可


image.png


image.png


image.png


image.png


四、总结


所谓的无人值守,就是自动应答,当安装过程中需要人机交互提供某些选项的答案时(如如何分区),自动应答文件可以根据对应项自动提供答案。但是,无人值守并不完全是无人值守,至少设置bios从网卡启动是必须人为设置的,且安装完系统后设置不从网卡启动也是需要人为设置的。除此之外,其他的基本上都可以实现无人值守安装。


在部署时,建议使用 Kickstart+DHCP+HTTP(FTP)+TFTP,安装dhcp、tftp-server、xinetd、httpd、system-config-kickstart等软件。


在真实环境中,通常我们会发现一台服务器好几块硬盘,做完raid,整个硬盘有等10T,如果来使用kickstart自动安装并分区呢;一般服务器硬盘超过2T,如何来使用kickstart安装配置呢?这里就不能使用MBR方式来分区,需要采用GPT格式来引导并分区。需要在ks.cfg末尾添加如下命令来实现需求:


%pre
 parted -s /dev/sdb mklabel gpt
%end


相关文章
|
3天前
|
前端开发 jenkins 持续交付
新的centos7.9安装docker版本的jenkins2.436.1最新版本-前端项目发布(五)
新的centos7.9安装docker版本的jenkins2.436.1最新版本-前端项目发布(五)
20 1
|
3天前
|
jenkins 网络安全 持续交付
新的centos7.9安装docker版本的jenkins2.436.1最新版本-后端项目发布(四)
新的centos7.9安装docker版本的jenkins2.436.1最新版本-后端项目发布(四)
15 3
|
1天前
|
Linux 网络安全 数据安全/隐私保护
centos7安装gitlab-ce社区版全过程,详细到爆炸,这些面试官常问的开发面试题你都掌握好了吗
centos7安装gitlab-ce社区版全过程,详细到爆炸,这些面试官常问的开发面试题你都掌握好了吗
|
3天前
|
NoSQL Linux Redis
在CentOS上安装和配置Redis
在CentOS上安装和配置Redis
42 0
|
3天前
|
安全 Linux 测试技术
在CentOS上安装Elasticsearch和Kibana
在CentOS上安装Elasticsearch和Kibana
11 0
|
3天前
|
运维 Kubernetes 监控
本地CentOS安装轻量级容器PaaS平台KubeSphere并实现无公网IP远程访问
本地CentOS安装轻量级容器PaaS平台KubeSphere并实现无公网IP远程访问
5 0
|
3天前
|
Kubernetes Docker 容器
Docker 安装 Portainer
Portainer Community Edition是一个针对容器化应用程序的轻量级服务交付平台,可用于管理 Docker、Swarm、Kubernetes 和 ACI 环境。它的设计理念是部署和使用都简单,该应用程序允许您通过“智能”GUI 和/或广泛的 API 管理所有编排器资源。
42 3
|
3天前
|
虚拟化
vmware克隆虚拟机后没有ip地址的问题
解决vmware克隆虚拟机后没有内网ip的问题
|
3天前
|
SQL 存储 数据挖掘
【虚拟机数据恢复】VMware虚拟机文件被误删除的数据恢复案例
虚拟机数据恢复环境: 某品牌R710服务器+MD3200存储,上层是ESXI虚拟机和虚拟机文件,虚拟机中存放有SQL Server数据库。 虚拟机故障: 机房非正常断电导致虚拟机无法启动。服务器管理员检查后发现虚拟机配置文件丢失,所幸xxx-flat.vmdk磁盘文件和xxx-000001-delta.vmdk快照文件还在。服务器管理员在尝试恢复虚拟机的过程中,将原虚拟机内的xxx-flat.vmdk删除后新建了一个虚拟机,并分配了精简模式的虚拟机磁盘和快照数据盘,但原虚拟机内的数据并没有恢复。
【虚拟机数据恢复】VMware虚拟机文件被误删除的数据恢复案例
|
3天前
|
算法 虚拟化 C++
VMware虚拟机无法自适应和拖拽复制粘贴和共享目录问题
VMware虚拟机无法自适应和拖拽复制粘贴和共享目录问题
136 0