1.原理分析
从机服务器从网卡开始启动,向主机服务器的DHCP发送请求,主服务器回应ip地址和文件地址,从机再通过得到的文件地址去主服务器访问配置文件,配置文件中有URL安装源,再访问URL安装源去安装部署
2.准备事项
1.两台rhel7虚拟机
2.将VMware的虚拟网络编辑器的NAT模式更改为如下:
2.开启主机图形界面
下载图形界面:yum group install "Server with GUI"
打开图形界面:init 5
3.配置主服务器的网络可用,可以使用脚本一键配置
vim /init_ip.sh
#!/bin/bash read -p "网卡名称:" eh read -p "ip地址:" ip cat >/etc/sysconfig/network-scripts/ifcfg-$eh<<EOF DEVICE=$eh ONBOOT=yes BOOTPROTO=none IPADDR1=$ip NETMASK1=255.255.255.0 DNS1=114.114.114.114 GATEWAY1=172.25.254.2 NAME=$eh EOF nmcli connection reload nmcli connection up $eh
ip a 查看网卡名称
运行脚本:bash /init_ip.sh
网卡名称填写自己的,一般是ens33/ens160/eth0
ip地址:172.25.254.40
4.一定要关闭VMware dhcp功能
5.挂载镜像 mount /dev/sr0 /mnt
6.配置仓库方便下载
vim /etc/yum.repos.d/npm.repo
3.图形化生成kickstart自动安装脚本
3.1 下载kickstart工具
yum install system-config-kickstart
3.2 启动kickstart图形制作工具
system-config-kickstart
3.3 生成ks.cfg自动安装脚本
3.4 查看生成的ks.cfg文件
vim ks.cfg
检查ks.cfg是否有语法错误
ksvalidator ks.cfg
4.配置httpd,在浏览器中可以访问/mnt和ks.cfg目录
4.1 下载httpd
yum install httpd -y
4.2 编写/etc/httpd/conf.d/vhost.conf
vim /etc/httpd/conf.d/vhost.conf
<directory /var/www/html> allowoverride none require all granted </directory> <virtualhost 172.25.254.40:80> servername 172.25.254.40 documentroot /var/www/html </virtualhost>
4.3 将需要共享的目录文件放到/var/www/html
返回到家目录
cd
复制ks.cfg
cp ks.cfg /var/www/html
创建镜像挂载目录的软链接到/var/www/html下
ln -s /mnt /var/www/html/mnt
4.4 重启httpd和关闭防火墙
systemctl restart httpd
systemctl stop firewall
setenforce 0
4.5访问172.25.254.40/ks.cfg和172.25.254.40/mnt
5.配置DHCP服务
5.1 下载dhcp服务
yum install dhcp -y
5.2 编写dhcp配置文件
编写时可以借助模版/usr/share/doc/dhcp*/dhcpd.conf.example
直接拷贝一份模版文件
\cp -f /usr/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf
vim /etc/dhcp/dhcpd.conf
# dhcpd.conf # # Sample configuration file for ISC dhcpd # # option definitions common to all supported networks... #公司域名,自己取的 option domain-name "caikeyou.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. #IP网段和子网掩码 subnet 172.25.254.0 netmask 255.255.255.0 { #dhcp地址池,分发地址的范围 range 172.25.254.80 172.25.254.90; #网关 option routers 172.25.254.2; #正常配置dhcp服务不用写,但是我们希望将下面的目录一起发送给请求主机,实现服务器的自动化部署 next-server 172.25.254.40; filename "pxelinux.0"; }
重启dhcp服务
systemctl enable --now dhcpd
6.搭建pxe网络安装环境实现服务器自动部署
6.1 下载环境
yum install syslinux.x86_64
共享pxelinux.0数据文件的网络服务
查看下载安装包
rpm -ql tftp-server
设置开机自启动
systemctl enable --now tftp
6.2 拷贝文件目录
复制挂载镜像目录/mnt下的isolinux
cp /mnt/isolinux/* /var/lib/tftpboot/
cp /usr/share/syslinux/pxelinux.0 /var/lib/tftpboot/
cd /var/lib/tftpboot/
mkdir pxelinux.cfg
cp ioslinux.cfg pxelinux.cfg/default
6.3 编辑/var/lib/tftpboot/pxelinux.cfg/default文件
vim pxelinux.cfg/default
7.新建一台rhel7
按上下键选择项,然后按+-实现上下挪动,使顺序与下面一样